Flash sound
Play sound from flash memory in IC
Flash memory
IC/Board |
Flash memory(Program Memory) |
UNO(328P) |
32 KB |
ATtiny412 |
4 KB |
ATtiny1614 |
16 KB |
ATtiny3216 |
32 KB |
Download mp3 sample
ref. Orenge free sounds
dog.mp3
Convert mp3 to hex text file
ref. WAVまたはMP3ファイルを再生する
Useage
| % ffmpeg -y -i input.mp3 -acodec pcm_u8 -f u8 -ac 1 -ar 8000 output.pcm
% xxd -i output.pcm > output.h
|
ffmpeg parameters
| -y :Overwrite output files without asking
-i :input/file/path
-acodec :Set the audio codec (pcm_u8 = PCM unsigned 8-bit)
-f :Format (u8 = PCM unsigned 8-bit)
-ac :Number of audio channels (1 = monaural)
-ar :Audio sampling frequency (8000 [Hz])
|
xxd (hexdamp) parameters
| -i : C include file style output
> :Redirection of output
|
Convert mp3 to hexdamp text file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | % ls
dog.mp3 //293KB
// Shorten large mp3 to 1 sec(seek start position 0[s] to duration 1[s])
% ffmpeg -ss 0 -i dog.mp3 -t 1 dog_1sec.mp3
// Convert to .pcm with 4000Hz
% ffmpeg -y -i dog_1sec.mp3 -acodec pcm_u8 -f u8 -ac 1 -ar 4000 dog.pcm
// Write to hexdamp file as .h for arduino
% xxd -i dog.pcm > dog.h
% ls
dog.mp3 // 293KB
dog_1sec.mp3 // 17KB
dog.pcm // 4KB
dog.h // 25KB
|
Arduino Sketch
ref Arduinoで鉢植えをしゃべらせる
dog.h
Open dog.h
and add const
and PROGMEM
in 1st line
Before: unsigned char dog_pcm[] = {
| unsigned char dog_pcm[] = {
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
....
0x80, 0x80, 0x80, 0x80
};
unsigned int dog_pcm_len = 4000;
|
After: const unsigned char dog_pcm[] PROGMEM = {
| const unsigned char dog_pcm[] PROGMEM= {
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
....
0x80, 0x80, 0x80, 0x80
};
unsigned int dog_pcm_len = 4000;
|
flash_sound.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | #include "dog.h"
void setup() {
pinMode(3, OUTPUT); //speaker pin
TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS20);
play();
}
void play() {
for (int i = 0; i < dog_pcm_len; i++) {
OCR2B = pgm_read_byte_near(&dog_pcm[i]);
delayMicroseconds(250); //1000000uSec/4000Hz
}
}
void loop() {
}
|
Save both in one folder
download flash_sound.ino|dog.h
Connection
Speaker (+) to Pin3
Speaker (-) to GND
Hint
To louder, use NPN bipolar transistor such as 2SC1815.
Connection:
- Base to Pin3
- 5V to Speaker (+)
- Emitter to Speaker (-)
- Collector to GND
Video
video link
Last update: August 19, 2021