How to separate audio channels of a recording

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to separate audio channels of a recording

How to separate audio channels of a recording

Hello, on this post I will explain how to record separated audio channels using an 8MIC-RPI-MX8 Board.

As background about how to setup the board to record and play audio using i.MX boards, I suggest you take a look on the next post:

How to configure, record and play audio using an 8MIC-RPI-MX8 Board.

Requirements:

Waveform Audio Format

WAV, known for WAVE (Waveform Audio File Format), is a subset of Microsoft’s Resource Interchange File Format (RIFF) specification for storing digital audio files. This format does not apply compression to the information and stores the audio with different sampling rates and bitrates.

WAV files are larger in size compared to other formats such as MP3 which uses compression to reduce the file size while maintaining a good audio quality but, there is always some lose on quality since audio information is too random to be compressed with conventional methods, the main advantage of this format is provide an audio file without losses that is also widely used on studio.

This files starts with a file header with data chunks. A WAV file consists of two sub-chunks:

  • fmt chunk: data format.
  • data chunk: sample data.

So, is structured by a metadata that is called WAV file header and the actual audio information.

The header of a WAV (RIFF) file is 44 bytes long and has the following format:

JorgeCas_0-1716227653935.png

JorgeCas_1-1716227760727.png

How to separate the channels?

To separate each audio channel from the recording we need to use the next command that will record raw data of each channel.

arecord -D plughw:<audio device> -c<number of chanels> -f <format> -r <sample rate> -d <duration of the recording> --separate-channels <output file name>.wav
arecord -D plughw:2,0 -c8 -f s16_le -r 48000 -d 10 --separate-channels sample.wav

This command will output raw data of recorded channels as is showed below.

JorgeCas_2-1716227828320.png

This raw data cannot be used as a “normal” .wav file because the header information is missing. It is possible to confirm it if import raw data to a DAW and play recorded samples:

JorgeCas_3-1716227842722.png

So, to use this information we need to create the header for each file using WAVE library on python. Here the script that I used:

import wave
import os

name = input("Enter the name of the audio file: ")

os.system("arecord -D plughw:2,0 -c8 -f s16_le -r 48000 -d 10 --separate-channels " + name + ".wav")

for i in range (0,8):
    with open(name + ".wav." + str(i), "rb") as in_file:
        data = in_file.read()
        with wave.open(name + "_channel_" + str(i) +".wav", "wb") as out_file: 
            out_file.setnchannels(1) 
            out_file.setsampwidth(2)
            out_file.setframerate(48000)
            out_file.writeframesraw(data)

os.system("mkdir output_files")
os.system("mv " + name + "_channel_" + "* " + "output_files")
os.system("rm " + name + ".wav.*")

If we run the script, will generate a directory with the eight audio channels in .wav format.

JorgeCas_4-1716227909413.png

Now, we will be able to play each channel individually using an audio player.

References

No ratings
Version history
Last update:
‎06-05-2024 10:38 AM
Updated by: