What's the best way to eliminate audio clipping with sox? - audio

I am trying to convert some audio files to a raw format for machine learning and I need to ensure that clipping does not occur because I do not want it to contaminate my dataset.
This question has been asked before, but unfortunately the solutions did not work for me.
Several options have been proposed to help mitigate this problem.
The --norm option does not appear to work very well ...
$ for name in *.au; do sox --norm ${name} -c 1 -r 44100 --bits 8 ${name}.mono-sr41000-ss8.raw; done
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
The -v 0.99 option does not appear to work very well:
$ for name in *.au; do sox -v 0.99 ${name} -c 1 -r 44100 --bits 8 ${name}.mono-sr41000-ss8.raw; done
sox WARN rate: rate clipped 11 samples; decrease volume?
sox WARN dither: dither clipped 8 samples; decrease volume?
sox WARN rate: rate clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN rate: rate clipped 12 samples; decrease volume?
sox WARN dither: dither clipped 9 samples; decrease volume?
sox WARN rate: rate clipped 6 samples; decrease volume?
sox WARN dither: dither clipped 5 samples; decrease volume?
sox WARN rate: rate clipped 10 samples; decrease volume?
sox WARN dither: dither clipped 11 samples; decrease volume?
sox WARN rate: rate clipped 3 samples; decrease volume?
sox WARN dither: dither clipped 3 samples; decrease volume?
sox WARN rate: rate clipped 2 samples; decrease volume?
sox WARN dither: dither clipped 2 samples; decrease volume?
ubuntu#ip-10-217-154-40:~/data/genre/genres/blues$ for name in *.au;
The -G option seems to work best. However, even with this option some clipping occurs.
$ for name in *.au; do sox -G ${name} -c 1 -r 41000 --bits 8 ${name}.mono-sr41000-ss8.raw; done
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 2 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
Combining -G and --norm did worse than using either individually ...
$ for name in *.au; do sox -G --norm ${name} -c 1 -r 44100 --bits 8 ${name}.mono-sr41000-ss8.raw; done
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 2 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
UPDATE: Further reducing the volume seems to eliminate the clipping.
Is this the best solution?
$ for name in *.au; do sox -G -v 0.95 ${name} -c 1 -r 44100 --bits 8 ${name}.mono-sr41000-ss8.raw; done
sox WARN dither: dither clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?
$ for name in *.au; do sox -G -v 0.9 ${name} -c 1 -r 44100 --bits 8 ${name}.mono-sr41000-ss8.raw; done

Related

Can I force arecord to record with a certain sampling rate if my sound card allows it?

I am using arecord to record audio using a USB microphone that allows recording up to 384k sampling rate. With Audacity I can easily record with other lower sampling rates (48k, 44.1k ..) but when I use:
arecord -vD hw:4,0 -f S16_LE -d 120 -r 48000 -c 1 sample.wav
It displays a warning message:
Warning: rate is not accurate (requested = 48000Hz, got = 384000Hz)
And records with 384000Hz.
I can't get to fix this.

arecord: set_params:1405: Unable to install hw params:

I'm trying to record audio from a microphone but I got below error. I tried plughw:0,4 to get automatic sample format conversion and also given manually as arecord -D hw:0,4 -d 5 -f S32_LE -r 48000 test.wav, but I get same error.
When I run alsamixer I get this error as ALSA lib ../../alsa-plugins-1.2.1/pulse/pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused cannot open mixer: Connection refused. When I manually start pulseaudio --start alsamixer runs, but when I do some microphone related operation alsamixer repeats same error again.
arecord:
Available formats:
- S16_LE
- S24_LE
- S32_LE
Recording WAVE 'test.wav' : Signed 32 bit Little Endian, Rate 48000 Hz, Mono
arecord: set_params:1405: Unable to install hw params:
ACCESS: RW_INTERLEAVED
FORMAT: S32_LE
SUBFORMAT: STD
SAMPLE_BITS: 32
FRAME_BITS: 32
CHANNELS: 1
RATE: 48000
PERIOD_TIME: 125000
PERIOD_SIZE: 6000
PERIOD_BYTES: 24000
PERIODS: 4
BUFFER_TIME: 500000
BUFFER_SIZE: 4000
BUFFER_BYTES: 96000
TICK_TIME: 0

SoX clipping audio even when -G flag is used

I'm doing batch conversion of bit depth and sample rate with SoX using the following command:
sox -G $infile -b 16 $outfile rate 44100 dither
I'm using the -G flag to prevent clipping in the converted files, but for some files I still get the warning:
sox.exe WARN dither: dither clipped 1 samples; decrease volume?
Why might this still be happening?

Prevent SoX from doing the clipping?

I have a heavily "overflowed" WAV file (samples written in float 32-bit format): instead of [-1.0,+1.0], the samples range goes as far as [-5.0,+5.0].
Using SoX to get raw PCM audio samples from WAV file:
sox --bits 32 --channels 1 --encoding floating-point --rate 48000 input.wav output.raw
I get warnings:
sox WARN sox: `input.wav' input clipped 1163400 samples
sox WARN sox: `output.raw' output clipped 605664 samples; decrease volume?
When I look into the output, I see the samples were clipped, and the range is [-1.0,+1.0] now. However, that is not what I want.
I would like to have exactly same output as input, just in different format (RAW instead of WAV). I need to use command line tool for the task. Is there a way to prevent SoX from doing the clipping?
I got the answer from the SoX mailing list.
The required behaviour isn't possible, however, in this specific case there is a workaround. Overriding input sample encoding as 32-bit signed integer instead of float, the values will be copied untouched; since the output is headerless, it doesn't matter what SoX thinks the samples are.
So, this works:
sox --bits 32 --channels 1 --encoding signed-integer --rate 48000 input.wav output.raw

Why does sox corrupt my wav file when I try and change the sample rate?

When I try to use sox to change the sample rate of a wav file I get a file of noise and garbage and the 2 following errors:
sox WARN rate: rate clipped 579 samples; decrease volume?
sox WARN dither: dither clipped 518 samples; decrease volume?
Although there are question marks there, I never get to answer them. They come up more as a status.
What I am starting with is a PCM file of my own making. The specs are:
16 bit signed
big endian
1 channel
44100 sample rate
I can import the file (0.wav) into Audacity and it's fine.
Then I convert the file into a wav file with the following line:
sox -r 44100 -e signed-integer -b 16 -c 1 0.raw 1.wav
I can import that file into Audacity and it also is fine.
But then when I try to change the sampling rate with:
sox 1.wav -r 16000 2.wav
If anyone knows what I'm doing wrong, or missing, I would appreciate it. Also I put up the original raw here: http://warptv.com/raw.html
This appears to be a problem of endian-ness. Your raw file is stored in big endian byte order, while WAV is, at least most of the time, little-endian.
This being the case you have to explicitly inform SoX about the endian-ness of your raw file, so this can be taken into account when converting. The appropriate switch for this is -B, changing your command into something like:
sox -r 44100 -e signed-integer -B -b 16 -c 1 0.raw 1.wav
With the raw file properly converted into a wav file the subsequent rate conversion as you attempted it should work as expected. At least it did for me.
Playing around a bit with Audacity it appears that it has some method of detecting (or guessing) the endian-ness of raw files that SoX lacks, and that this is the source of the discrepancy you found.

Resources