12-bit and 8-bit Audio Samplers


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!).

connectionLabels


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.

Code

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.

8-bit Sampler

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?




Published by Michael, on July 5th, 2013 at 5:11 am. Filed under: Audio. | 2 Comments |





2 Responses to “12-bit and 8-bit Audio Samplers”

  1. Hi there.
    I’m working on making my own audio hacker circuit following your instruction. However, there’s not a lot in the code that will compile. Any recommendations? Some things I can assume I myself change (like DEFAULT_RECORDING..) but what is UINT16_MAX – (F_CPU/passthroughSampleRate)? Please elaborate.

    Comment by gtr_stitch on January 22, 2014 at 12:02 AM



  2. Apologies. Human error. I forget how to unzip folders at times :S

    Comment by gtr_stitch on January 22, 2014 at 12:06 AM



Leave a Reply

*