This project is an introduction to the new nootropic design Audio Hacker shield. The first sketch you should use with the Audio Hacker is the 12-bit Sampler. Install the Audio Hacker Arduino library in your Arduino sketchbook, then choose the menu item File->Examples->Audio Hacker->Sampler_12bit.
Connect an audio source to the input jack of the Audio Hacker. For example, an MP3 player or computer with the volume at a high level. Make sure the bypass switch is in the left position (no bypass) and that the preamp gain potentiometer is set to the lowest level (counterclockwise). Connect the Audio Hacker output jack to amplified computer speakers, an input to a stereo receiver, or use headphones (but turn the Audio Hacker volume down first!).
The audio input signal is sampled and reproduced on the output at 44.1 kHz. Everything you are hearing has been processed by the Arduino code. To compare it to the original signal, move the bypass switch to the right position. Now you are hearing the original signal (but after it has been summed to a mono signal). Can you hear a difference between the original and the audio sampled and reproduced by Arduino code?
With the bypass off (left position), press S1 to record the audio to memory (up to 8.6s) with a sample rate of 22 kHz. Press S2 to play it back.
Sampling and playing audio requires precise timing. The Arduino timer1 is used to run an interrupt service routine (ISR) at a very specific frequency. The sample rate determines how often the interrupt service routine (ISR) is invoked. In order to efficiently store 12-bit audio samples, two samples are “packed” into 3 bytes before they are written to memory. To make this process efficient, read and write memory operations alternate. That is, we only write to memory on “odd” numbered ISRs and read from memory on “even” numbered ISRs.
The 8-bit sampler File->Examples->Audio Hacker->Sampler_8bit is simpler code and can record longer samples. If you want to understand the Audio Hacker library API, this sketch will help you. Since this approach is simpler, we don’t trade off read and write operations on alternating ISRs. You will likely hear a sound quality difference, though. If you listen to samples with headphones you may hear background noise (like a hiss) in the samples compared to the 12-bit version.
Sample rate adjustments
You can make your own adjustments to the recording sample rate by changing the value of the variable recordingSampleRate. For example, set the value to 18000 for a sample length of 10.4 seconds. The lower the rate, the longer the sample you can record, but quality starts to suffer. See this table for information on sample lengths.
Adjustable playback rate
You can use a potentiometer connected to A0 to adust the playback rate of the recorded sample.
To enable this feature, make a minor code change to set adjustablePlaybackSpeed = true;. Arduino will prompt you to save your changed sketch to another location. Now connect a potentiometer to A0. If you are unsure how to wire it, see analog input tutorial. Then you can play back a sample with a different speed. Set the pot to midway point when you record, then adjust. Fun, huh?