Main Content

Shift Pitch of Audio File Stored in SD Card of Arduino Hardware

This example shows you how to shift the pitch of an audio file that is stored in an SD card of the Arduino® hardware using Simulink® Support Package for Arduino Hardware.

In this example, you will learn how to:

  • Read audio from the SD card using the SD Card File Read block

  • Shift the pitch of the audio

  • Play the processed audio through the headphones connected to the hardware

Pitch shifting is the ability to modify the pitch of an audio signal to either increase or decrease the pitch. For example, when a fast car passes you in the street, the pitch of the sound from the car increases as the vehicle approaches you and decreases as the vehicle moves away from you. As the source of the audio moves closer or away from the receiver, the successive signals reach the receiver at either smaller or larger intervals of time, respectively. This time difference causes a change in the frequency of the audio being heard by the receiver. You can perform a similar pitch-shifting exercise by splitting an audio signal into smaller frames, time stretching these audio frames, and then resampling the frames to generate an approximation of the original audio sequence. This process simulates the effect of either increasing or decreasing the pitch depending on how the signals are processed.

Required Hardware

  • Arduino MKR Zero or any other supported Arduino hardware

  • Headphones with 3.5mm audio jack (recommended)

  • USB cable

Prerequisite

Configure the Arduino network using Install Support for Arduino Hardware.

Simulink Model Description

Open the arduino_pitch Simulink model.

The model is divided into three areas based on the functionality of the blocks in each area:

  • Audio Source

  • Pitch Algorithm

  • Pitch Audio

Audio Source: This subsystem reads the audio from the sampleAudio_8kHz_8bit.wav file stored on the SD card by using the SD Card File Read block. The audio file is set to repeat once the audio has finished playing.

Pitch Algorithm: This subsystem implements the algorithm for shifting the pitch of the audio by following the frame-based approach. This approach alters the pitch by expanding or compressing the audio frames on the time-axis using these steps:

1. Split the audio signal into smaller frames of fixed lengths.

2. Double each frame.

3. Insert a copy of each frame in the stream to increase the time duration of the audio. But this does not change the pitch.

4. Down sample the time-stretched frame to generate an approximation of the original sequence. Down sampling retains the length of the original signal while the pitch is raised.

Pitch Audio: The Analog Output block accepts the pitch-shifted audio and then plays it through the headphones connected to the DAC0 pin of the hardware.

Step 1: Connect Arduino Hardware for Echo Effect

Before you start this example, we recommend you complete the Get Started with Arduino Hardware example.

1. Connect the micro end of the USB cable to the Arduino MKR Zero board and the regular end of the USB cable to the computer. Wait until the PWR LED on the hardware starts blinking.

2. Connect the headphone to the TRRS breakout shield as shown.

a. The Left and Right channels are on the TIP and RING1 of TRRS breakout. These channels are connected to the DAC0 pin of the hardware.

b. The Ground is on the RING2 of TRRS breakout. The Ground is connected to the GND pin of the hardware.

Step 2: Copy Audio File into SD Card

1. Insert the SD card in your computer.

2. Format the SD card to file allocation table 32 (FAT32) format. This step is required so that the example generates the desired output.

3. In the MATLAB® Command Window, execute the which command. This command displays the location of the sampleAudio_8kHz_8bit.wav file in your computer. Copy the sampleAudio_8kHz_8bit.wav file from its location and paste it to the root directory of the SD card.

which sampleAudio_8kHz_8bit.wav

4. Insert the SD card in your Arduino MKR Zero board. If you are using a board that does not have an inbuilt slot for the card, use an SD card shield.

Step 3: Configure Arduino Pitch Shift Simulink Model

1. Open the arduino_pitch Simulink model.

2. In the Audio Source subsystem, the SD Card File Read block is configured to read audio from the sampleAudio_8kHz_8bit.wav file. If you want to use any other file that is already saved on your SD card, specify the name of that file in the File Name parameter and specify the properties of your audio file in the block parameters dialog box.

3. On the Modeling tab, select Model Settings.

4. Select the Hardware Implementation pane. From the Hardware board list, select the type of Arduino board that you are using.

5. From the SPI properties under Target hardware resources, set SD Card SPI CS pin to the Chip Select (CS) pin that your SD card shield uses for SPI communication with the connected SD card. Do not make any modifications if you are using Arduino MKR Zero board. As the MKR Zero board has an inbuilt SD card slot, the support package auto-populates the SD Card SPI SS pin parameter.

6. Click Apply. Click OK to close the dialog box.

Step 4: Deploy Simulink Model on Arduino Hardware

On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start. This action builds, downloads, and runs the model on the Arduino hardware. You can hear the pitch-shifted audio through the headphones connected to the analog pin on the hardware. The audio continues to run even if the hardware is disconnected from the computer.

See Also

Other Things to Try

Open the arduino_audio_effects Simulink model.

Deploy the Simulink model on the Arduino hardware. This model implements the algorithm for adding echo and reverb, and pitch shifting the audio using MATLAB Function blocks. This model does not require SD card to process the audio.