In this example we will show you how to use a digital signal to generate analog sound effects using the PSoC 4 device. The example projects will encode a sound as a string of 1s and 0s which, when smoothed by an RC filter, creates an approximation of the original sound.
Please refer to the following web site that explains the concept and algorithm in detail. This example was adapted from this web page.
We have included an example of the 1-bit audio output at the bottom of this post.
Forum Post Attachments:
At the bottom of this post we are including the following items:
- Example Project Zip File
- Zip File of Images
- Project Schematic
- Component Configurations
- Audio Recording
The user can download the example project at the bottom of this post. The project uses the following list of Creator Components:
The components are configured by right clicking on the component in your Top Design schematic view and selecting Configure. Please enable the following selections in the Configuration windows for the listed components above.
The main.c firmware is included in the example project. Please review the commented sections for more details.
At a high level the following actions were taken with the example project:
- Convert a sound effect .wav file to 1-bit sound format (encoding the data)
- Convert the 1-bit format to an array of bytes (formatting the data)
- Shift out the array to a pin connected to a speaker (playing the data)
Encoding the Data:
The sound effect .wav file was converted to 1-bit format byte array using the free BTc Sound Encoder 3.0 software developed by Roman Black.
There are some important features to this software:
- Being able to resample the data at a different rate
- Simulating what the 1-bit sound would sound like
- Seeing how the 1-bit sound wave matches the actual sound wave
- Exporting the 1-bit format data to a ‘PIC Mikro C file’.
Formatting the Data:
The generated code from the above application is not directly readable by the PSoC. It needs to be converted to an array of bytes to allow for the array to be initialized when declared. This step was done using a Python script (attached to this post).
Playing the Data:
Once the sound is ready to be initialized as an array in C, playing it out with the PSoC is simple. In the example we use the SPI component to output the values to the RC circuit. The array of data is shifted out MSB first using the SPIM Component.
- Since the data was sampled at 11.025 kHz, the SPIM is clocked at 22.050 kHz to achieve a bit-rate of 11.025 kHz.
- Playing around with the (external) passives did not have much of an effect on playback quality. Playback quality is primarily determined by the bit-rate and is a tradeoff with the flash footprint.
- The flash footprint of the projectwith 5 sound effects (bark, bell, boing, quack, crowing) is ~9k.
In this example the user will need to connect an external RC circuit to an external speaker. The SPIM component is then connected to Pin 0.
Test Your Project:
Program the Pioneer board and enjoy the sound effects.
I hope this example can help you in your design.