Uploading audio samples - masonandrewmann/MEAP GitHub Wiki

The easiest way to upload audio samples to M.E.A.P. is to convert them to a form that can be directly included in the code uploaded by the Arduino IDE. We could manually write out the data for each sample in an array in our .ino file, but a practical way to accomplish the same thing is to use a python script included in the Mozzi library which will do this for us and store it in a ".h" (header) file which can then be linked to our main .ino file in much the same way we include a library.

Converting your sample

  • First find a sample you want to upload to your M.E.A.P. board. Note that the board has limited memory and can only store short samples. If you try to upload too large of a sample, the Arduino IDE will give you an error when you try to upload your program.

  • Use Audacity to convert your sample to the correct format

    • Open sample in Audacity
    • If your file is stereo, convert to mono
      • Tracks > Mix > Mix Stereo Down to Mono
    • Change your project sample rate to 16384Hz. In version 3.3.3 go to Audacity > Preferences and click on the Audio Settings sub-menu. Click on the Project Sample Rate menu and select Other... In the text box to the right, type in 16384.
    • Export your file in raw 8-bit PCM format. Click File > Export > Export Audio. Under file type menu, select "Other uncompressed files". Under header, choose "RAW (header-less)". And under Encoding, choose "Signed 8-bit PCM"
    • Name your file with ".raw" extension. For example: "Cmaj7.raw"
  • locate "char2mozzi.py" script within Mozzi library

    • navigate to your arduino libraries folder (where you originally installed your Mozzi library)
    • navigate to Mozzi-master/extras/python and you should see the "char2mozzi.py" script
    • copy your raw audio file into the same directory as the "char2mozzi.py" script
  • run the "char2mozzi.py" script

    • to run this script you will need to have python installed on your computer. Most computers come with it but if not you can download it here
    • open up a command line interface program (Terminal on Mac, Command Prompt on Windows)
    • navigate to the directory with "char2mozzi.py". On my Mac, the line that does it looks like this but your path may look a little different.
cd ~/Documents/Arduino/libraries/Mozzi-master/extras/python
  • from this directory, run the python script with the following options
	Usage: 
	>>>char2mozzi.py <infile outfile tablename samplerate>
	
	@param infile		The file to convert, RAW(headerless) Signed 8 bit PCM.
	@param outfile	The file to save as output, a .h file containing a table for Mozzi.
	@param tablename	The name to give the table of converted data in the new file.
	@param samplerate	The samplerate the sound was recorded at.  Choose what make sense for you, if it's not a normal recorded sample.

The following example will read a file called "Cmaj7.raw" with a sample rate of 16384Hz, and export it as a file called "cmaj7.h". The file will contain an array called "myChord_DATA[]" containing the data of your sample.

python char2mozzi.py Cmaj7.raw cmaj7.h myChord 16384

Using your sample

  • Copy your newly created .h file into the project folder of whatever program you want to use it in (ie. in the same directory as your .ino file)

  • Add a line to the top of your .ino file including your .h file. The following line is how I would do it for the example above, just replace "cmaj7" with the name of your file.

#include "cmaj7.h"
  • The Mozzi class called Sample is how we use these samples in a program
    • First, define your object in global scope. The following line will create an object called mySample that contains the data created above. Again, you will have to change this to match the sample name you created. For myChord_NUM_CELLS, replace "myChord" with whatever you chose as your tablename when you ran the python script. Do the same with myChord_DATA. mySample is the variable name which you will use to refer to this sample later in your code, choose whatever name you wish.
Sample <myChord_NUM_CELLS, AUDIO_RATE> mySample(myChord_DATA);
  • In setup() you will want to set the playback speed of your sample. You can change this later if you want to pitch it. The following line will play the sample at the speed it was recorded.
mySample.setFreq((float) myChord_SAMPLERATE / (float) myChord_NUM_CELLS); 
  • In updateAudio() you will need to fetch the next sample just as you do with oscillators. The following line will grab the next sample and send it to the DAC.
  return MonoOutput::from8Bit(mySample.next());
  • Refer to the MEAP_BasicSample example for full code for playing back a single sample.