If your audio is in a different format, you can convert it with a tool like Audacity (free), Adobe Audition ($$$) or you may already have a utility on your computer that can produce something. Even iTunes can do this, if you tweak the import settings:
The Winbond flash chip we’re using has a capacity of 1,048,576 bytes (1 megabyte, often called “8 megabit” because marketing). Six bytes are used to store data about the length and sampling rate of the audio, leaving 1,048,570 bytes for the audio data itself. Each byte is one audio sample.
To estimate the maximum duration of audio you can store on the chip:
Max. duration (in seconds) = 1,048,570 ÷ sampling rate
e.g. with 16,000 Hz (16 KHz) music:
1,048,570 ÷ 16,000 = ~65.5 seconds
If your source audio file is too big for the available space, the end will be truncated to fit.
Chip Loading Circuit
Because the Trinket can’t support traditional serial I/O, we’ll make temporary use of a regular Arduino board to help transfer data to the Winbond chip. Later we move it to a playback circuit.
We suggest using an Arduino Uno, because it has easy access to the SPI pins used to communicate with the flash chip. This can be done with a Leonardo, Mega or other board, but you’ll need to adapt the wiring to use the 6-pin ICSP header (MISO, MOSI and SCK pins, specifically).
The serial flash chip is a 3.3 Volt device, whereas the Arduino is 5 Volts. To avoid unleashing the Blue Smoke Monster, it’s necessary to power the chip from the Arduino’s 3.3V (not 5V) pin, and then use level-shifting circuitry to drive the control lines. There are chips that do this; use ’em if you got ’em, or a simple approximation can be made using resistors to create a voltage divider: output signal pins from the Arduino each connect to a 470 Ohm resistor, the other end connected both to an input pin on the flash chip and a 1K resistor to ground (see diagrams below). If you don’t have exactly these values of resistors on-hand, that’s okay, it’s not rocket surgery…you can substitute other values with approximately a 1:2 ratio, for example 1K and 2.2K (or use two 1K resistors in series for the latter). 1K/2.2K is about the upper limit on values; don’t go higher than this. Won’t harm anything, just won’t work very reliably. Also, note that the data output line back into the Arduino (pin 12, blue wire) connects directly, there are no resistors on this line.
An LED and 220 Ohm resistor can be added between pin A0 and GND to provide a simple status display. The LED will blink to indicate an error, or flickers during the data transfer. This is not essential and can be left out of the circuit if you don’t have the parts.
Finally, there’s a 0.1 μF capacitor between the flash chip’s VCC supply and GND. In a pinch you can get by without this, but it’s good form to have it there, keeps electrical gremlins away.
File→Sketchbook→Libraries→Adafruit_TinyFlash→AudioLoader
Select the board type and serial port from the Tools menu, and upload this code to the board. If you don’t have the flash memory chip installed (or if it's positioned incorrectly, or turned around), the status LED should blink. If it’s working properly, you won’t see anything from the LED, but you can check the serial monitor (at 57600 baud). It should display:
HELLO 1048576
Transferring Audio
Close the Arduino serial monitor if you still have it open; the other code won’t work if it’s there.Launch Processing and load the AudioXfer sketch. It’s inside the Adafruit_TinyFlash folder that you downloaded earlier, in a sub-folder called “Processing.” Sorry to make you hunt through the Arduino folder for this, but it was less troublesome that requiring a separate download.
portname = "/dev/tty.usbmodem1a1331"; // bypass scan
AudioXfer is a pretty barebones program…other than the file selection, there is no fancy GUI, it just prints text to the console. You’ll see a long line of dots (and the LED will flicker) as data is transferred to the Arduino and written to the chip. It can take several minutes to load a 1 megabyte chip to capacity. So you may want to test with just a short sound at first.
If all goes well, the software reports “done!” If an error was encountered, you’ll instead see a message with some indication of the problem.
Once a sound is successfully loaded, disconnect the Arduino from USB, remove the flash memory chip from the breadboard or socket, and then we’ll move it over to the playback circuit…