miero
Senior Member
- Joined
- Aug 1, 2018
- Messages
- 319
- Likes
- 353
SoX tool might be a very good reason to start learning how to use a command line. It allows to generate simple signals such as sine or square, combine them in various ways and apply many filters in a row using just several text keywords and numbers.
Initially SoX was created for Linux operating system but it is available also on other operating systems nowadays.
For example Windows 10 supports installing seamlessly integrated Ubuntu Linux distribution that will enable to use this guide: https://www.microsoft.com/en-us/p/ubuntu/9nblggh4msv6
A command line can be accessed from a console if there is no GUI on a machine or using a terminal application. On a Windows OS there are SSH or telnet clients (for example Putty) that allow a remote connection to a Linux machine (for example RaspberryPi).
For example, in a Debian based Linux distributions the SoX can be installed using following command:
This command will install also additional sox libraries (e.g. mp3 support) and a precision calculator 'bc' that will be used in examples bellow.
SoX provides 3 tools:
- sox, tool that can generate or convert audio files
- play, tool that can play files or generated signals to a sound card
- rec, tool that can record audio data to a file
Let's start with a simple command that generates 30 second sine tone of frequency 1kHz and volume -10dBFS with 48kHz sample rate and writes it to sin1k.wav file:
Briefly to used arguments:
Notice: The SoX applies dither effect automatically if outputs data to 16 bit precision.
It might be useful to inspect parameters of generated file, so let's check it using stats effect:
Let's play that file!
The command terminates itself after playing the whole file (30 seconds), but this command and all other commands running in a console can be interrupted earlier by pressing Ctrl+C key combination.
Notice: the play command does not need output arguments; it choose default sound card and plays audio into it.
I don't want to play on a default audio ouput (pulseaudio in my case), but I want to play directly to a hardware to get a bit-perfect output. So I can execute following command that sets a shell variable that will reroute output of play command to 3rd ALSA sound card (Linux only, cards are numbered from 0).
In the last example of this introductory post let's generate continuous sine signal on the fly and play it directly to a sound card:
You can see the output of play command that I aborted with Ctrl+C before 3rd second.
Initially SoX was created for Linux operating system but it is available also on other operating systems nowadays.
For example Windows 10 supports installing seamlessly integrated Ubuntu Linux distribution that will enable to use this guide: https://www.microsoft.com/en-us/p/ubuntu/9nblggh4msv6
A command line can be accessed from a console if there is no GUI on a machine or using a terminal application. On a Windows OS there are SSH or telnet clients (for example Putty) that allow a remote connection to a Linux machine (for example RaspberryPi).
For example, in a Debian based Linux distributions the SoX can be installed using following command:
Code:
sudo apt-get install sox libsox-fmt-all bc
SoX provides 3 tools:
- sox, tool that can generate or convert audio files
- play, tool that can play files or generated signals to a sound card
- rec, tool that can record audio data to a file
Let's start with a simple command that generates 30 second sine tone of frequency 1kHz and volume -10dBFS with 48kHz sample rate and writes it to sin1k.wav file:
Rich (BB code):
sox -V -r 48000 -n -b 16 -c 2 sin1k.wav synth 30 sin 1000 vol -10dB
-V ... this argument can be omitted, but it enables verbose output, so we can check input/output parameters and effect that are used
-r 48000 -n ... (input specification) sets sample rate 48k for this session and specifies that there is no input file
-b 16 -c 2 sin1k.wav ... (output specification) 16 bit output file with two channels named sin1k.wav
synth 30 sin 1000 ... (1st effect) synthesizes 30 seconds of sine signal at frequency 1000Hz with max. amplitude
vol -10dB ... (2nd effect) applies 10dB attenuation to a generated signal
Let's see an example output of that command:
Rich (BB code):
$ sox -V -r 48000 -n -b 16 -c 2 sin1k.wav synth 30 sin 1000 vol -10dB
sox: SoX v14.4.2
Input File : '' (null)
Channels : 1
Sample Rate : 48000
Precision : 32-bit
Output File : 'sin1k.wav'
Channels : 2
Sample Rate : 48000
Precision : 16-bit
Sample Encoding: 16-bit Signed Integer PCM
Endian Type : little
Reverse Nibbles: no
Reverse Bits : no
Comment : 'Processed by SoX'
sox INFO sox: effects chain: input 48000Hz 1 channels
sox INFO sox: effects chain: synth 48000Hz 1 channels
sox INFO sox: effects chain: vol 48000Hz 1 channels
sox INFO sox: effects chain: channels 48000Hz 2 channels
sox INFO sox: effects chain: dither 48000Hz 2 channels
sox INFO sox: effects chain: output 48000Hz 2 channels
It might be useful to inspect parameters of generated file, so let's check it using stats effect:
Rich (BB code):
$ sox -V sin1k.wav -n stats
sox: SoX v14.4.2
sox INFO formats: detected file format type `wav'
Input File : 'sin1k.wav'
Channels : 2
Sample Rate : 48000
Precision : 16-bit
Duration : 00:00:30.00 = 1440000 samples ~ 2250 CDDA sectors
File Size : 5.76M
Bit Rate : 1.54M
Sample Encoding: 16-bit Signed Integer PCM
Endian Type : little
Reverse Nibbles: no
Reverse Bits : no
Output File : '' (null)
Channels : 2
Sample Rate : 48000
Precision : 16-bit
Duration : 00:00:30.00 = 1440000 samples ~ 2250 CDDA sectors
sox INFO sox: effects chain: input 48000Hz 2 channels
sox INFO sox: effects chain: stats 48000Hz 2 channels
sox INFO sox: effects chain: output 48000Hz 2 channels
Overall Left Right
DC offset 0.000000 0.000000 0.000000
Min level -0.316254 -0.316254 -0.316254
Max level 0.316254 0.316254 0.316254
Pk lev dB -10.00 -10.00 -10.00
RMS lev dB -13.01 -13.01 -13.01
RMS Pk dB -13.00 -13.00 -13.00
RMS Tr dB -13.05 -13.05 -13.05
Crest factor - 1.41 1.41
Flat factor 0.00 0.00 0.00
Pk count 12.7k 12.8k 12.7k
Bit-depth 15/16 15/16 15/16
Num samples 1.44M
Length s 30.000
Scale max 1.000000
Window s 0.050
Let's play that file!
Rich (BB code):
$ play -V sin1k.wav
play: SoX v14.4.2
play INFO formats: detected file format type `wav'
Input File : 'sin1k.wav'
Channels : 2
Sample Rate : 48000
Precision : 16-bit
Duration : 00:00:30.00 = 1440000 samples ~ 2250 CDDA sectors
File Size : 5.76M
Bit Rate : 1.54M
Sample Encoding: 16-bit Signed Integer PCM
Endian Type : little
Reverse Nibbles: no
Reverse Bits : no
Output File : 'default' (pulseaudio)
Channels : 2
Sample Rate : 48000
Precision : 32-bit
Duration : 00:00:30.00 = 1440000 samples ~ 2250 CDDA sectors
Sample Encoding: 32-bit Signed Integer PCM
Endian Type : little
Reverse Nibbles: no
Reverse Bits : no
play INFO sox: effects chain: input 48000Hz 2 channels
play INFO sox: effects chain: output 48000Hz 2 channels
In:100% 00:00:30.00 [00:00:00.00] Out:1.44M [ | ] Clip:0
Done.
Notice: the play command does not need output arguments; it choose default sound card and plays audio into it.
I don't want to play on a default audio ouput (pulseaudio in my case), but I want to play directly to a hardware to get a bit-perfect output. So I can execute following command that sets a shell variable that will reroute output of play command to 3rd ALSA sound card (Linux only, cards are numbered from 0).
Rich (BB code):
$ AUDIODEV=hw:2,0 play -V sin1k.wav
play WARN alsa: can't encode 0-bit Unknown or not applicable
play: SoX v14.4.2
play INFO formats: detected file format type `wav'
Input File : 'sin1k.wav'
Channels : 2
Sample Rate : 48000
Precision : 16-bit
Duration : 00:00:30.00 = 1440000 samples ~ 2250 CDDA sectors
File Size : 5.76M
Bit Rate : 1.54M
Sample Encoding: 16-bit Signed Integer PCM
Endian Type : little
Reverse Nibbles: no
Reverse Bits : no
Output File : 'hw:2,0' (alsa)
Channels : 2
Sample Rate : 48000
Precision : 16-bit
Duration : 00:00:30.00 = 1440000 samples ~ 2250 CDDA sectors
Sample Encoding: 16-bit Signed Integer PCM
Endian Type : little
Reverse Nibbles: no
Reverse Bits : no
play INFO sox: effects chain: input 48000Hz 2 channels
play INFO sox: effects chain: output 48000Hz 2 channels
In:100% 00:00:30.00 [00:00:00.00] Out:1.44M [ | ] Clip:0
Done.
In the last example of this introductory post let's generate continuous sine signal on the fly and play it directly to a sound card:
Rich (BB code):
AUDIODEV=hw:2,0 play -V -r 48000 -n -b 16 -c 2 synth sin 1000 vol -10dB
play: SoX v14.4.2
Input File : '' (null)
Channels : 1
Sample Rate : 48000
Precision : 32-bit
Output File : 'hw:2,0' (alsa)
Channels : 2
Sample Rate : 48000
Precision : 16-bit
Sample Encoding: 16-bit Signed Integer PCM
Endian Type : little
Reverse Nibbles: no
Reverse Bits : no
play INFO sox: effects chain: input 48000Hz 1 channels
play INFO sox: effects chain: synth 48000Hz 1 channels
play INFO sox: effects chain: vol 48000Hz 1 channels
play INFO sox: effects chain: channels 48000Hz 2 channels
play INFO sox: effects chain: dither 48000Hz 2 channels
play INFO sox: effects chain: output 48000Hz 2 channels
In:0.00% 00:00:02.90 [00:00:00.00] Out:135k [ ====|==== ] Clip:0
Aborted.
Last edited: