How do I get an audio file sample rate using sox? - audio

I would like to get the sample-rate of a given audio file using sox. Couldn't find the commandline to do that.

just use:
soxi <filename>
or
sox --i <filename>
to produce output such as:
Input File : 'final.flac'
Channels : 4
Sample Rate : 44100
Precision : 16-bit
Duration : 00:00:11.48 = 506179 samples = 860.849 CDDA sectors
File Size : 2.44M
Bit Rate : 1.70M
Sample Encoding: 16-bit FLAC
Comment : 'Comment=Processed by SoX'
The latter one is in case you're using the win32 version that doesn't include soxi, by default. To grab the sample rate only, just use:
soxi -r <filename>
or
sox --i -r <filename>
which will return the sample rate alone.

Related

FFMPEG Bit Rate as 16Bit for Audio Conversion

I am using FFMPEG Audio Converter to convert the file format. At Present it bit rate is 176.4 kbit/s, so it file size to big.
I want to convert it as possible at low bit rate but unable to find any solution.
I use below command line to convert the file
ffmpeg.exe -i "Audio Input FilePath" "Audio Output FilePath"
You can define with -b:a the target bitrate so try for example:
ffmpeg.exe -i "Audio Input FilePath" -b:a 64k "Audio Output FilePath"

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.

Capture/Record Audio in Linux for Milliseconds

arecord -d 10 sample.wav
Here, this command will record sample.wav as a 10 second wave file.
From,
http://linuxcommand.org/man_pages/arecord1.html
http://linux.die.net/man/1/arecord
Here, in arecord, for duration (parameter -d) only second can be used.
But for my project, I need to record for 600 milliseconds or 2700 milliseconds. Here, is there any way to use millisecond or microsecond?
Do I need to modify ALSA code to achieve this?
You need to change arecord's source code (aplay.c in the alsa-utils package) to change the type and the parsing of the timelimit variable.
You can use the code i made. It's a C program to use alsa simply.
https://github.com/Waxo/ALSA_encapsulation
With ffmpeg, here to record a 5ms wav sample:
ffmpeg -y -loglevel panic -f alsa -ac 1 -ar 44100 -i hw:2 -t 0.05 volt.wav
The sound card list for the -i parameter:
arecord -l
Bonus! To analyse the sample peaks:
sox -S volt.wav -n stats
You can compile and use the ALSACaptureSplitter application, specifying the duration as a floating point number - which will allow you to specify any accuracy you want. For example :
ALSACaptureSplitter -t 0.6 /tmp/test wav
This command will save audio 600 ms of audio to individual files in the /tmp directory.
You can build the application for your system using this email as a guide.
Here is the help output from the ALSACaptureSplitter command :
ALSACaptureSplitter -h
ALSACaptureSplitter : An application to capture input and save to independent files.
Usage:
ALSACaptureSplitter [options] outFileNamePrefix ext
e.g. ALSACaptureSplitter [options] /tmp/out wav
-D : The name of the device : (-D hw:0)
-c : The number of channels to open, if the available number is less, then it is reduced to the available : (-c 2)
-t : The duration to sample for : (-t 2.1)
-r : The sample rate to use in Hz : (-r 48000)
AUDIO FILE FORMATS:The known output file extensions (output file formats) are the following :
8svx aif aifc aiff aiffc al amb amr-nb amr-wb anb au avr awb caf cdda cdr cvs cvsd cvu dat dvms f32 f4 f64 f8 fap flac fssd gsm gsrt hcom htk ima ircam la lpc lpc10 lu mat mat4 mat5 maud mp2 mp3 nist ogg paf prc pvf raw s1 s16 s2 s24 s3 s32 s4 s8 sb sd2 sds sf sl sln smp snd sndfile sndr sndt sou sox sph sw txw u1 u16 u2 u24 u3 u32 u4 u8 ub ul uw vms voc vorbis vox w64 wav wavpcm wv wve xa xi

Recording each channel using ALSA API

I'm using a playstation eye, plugged into a raspberry pi. I have the raspberry pi recognising the built in microphone array of the PSEye and I can sample input levels using
arecord -vv /dev/null -r 16000 -f S16_LE -c 4 -D iec958:CARD=CameraB409241,DEV=0 /dev/null < /dev/null
Now obviously this shows the levels for all 4 channels being summed together. What I wish to do is record each channel separately. Is this possible using the ALSA API?
I've looked through this http://www.alsa-project.org/alsa-doc/alsa-lib/pcm.html
And took a look at this http://www.linuxjournal.com/article/6735?page=0,2
But neither seemed to what I needed. This is also similar to sampling both channels of a stereo mic over ALSA as well I suppose. Eventually I want to be able to sample the dB from each microphone at a specific point in time.
To read into a separate buffer for each channel, replace SND_PCM_ACCESS_RW_INTERLEAVED with SND_PCM_ACCESS_RW_NONINTERLEAVED, and replace snd_pcm_readi with snd_pcm_readn.
If you want to record each channel to independent audio files, then you can use this application :
https://github.com/flatmax/gtkiostream/blob/master/applications/ALSACaptureSplitter.C
It needs to be compiled, which is described here :
https://lists.audioinjector.net/pipermail/people/2020-March/000028.html
To use the application specify the base file name and the extension, for example :
ALSACaptureSplitter /tmp/test wav
It can record to many different audio file formats.
You can use the options to change the device, specify the recording time, channel count, etc. Here is the help printed out by the application :
./applications/ALSACaptureSplitter -h
ALSACaptureSplitter : An application to capture input and save to
independent files.
Usage:
ALSACaptureSplitter [options] outFileNamePrefix ext
e.g. ALSACaptureSplitter [options] /tmp/out wav
-D : The name of the device : (-D hw:0)
-c : The number of channels to open, if the available number is less, then it is reduced to the available : (-c 2)
-t : The duration to sample for : (-t 2.1)
-r : The sample rate to use in Hz : (-r 48000)
AUDIO FILE FORMATS:The known output file extensions (output file formats) are the following :
8svx aif aifc aiff aiffc al amb amr-nb amr-wb anb au avr awb caf cdda cdr cvs cvsd cvu dat dvms f32 f4 f64 f8 fap flac fssd gsm gsrt hcom htk ima ircam la lpc lpc10 lu mat mat4 mat5 maud mp2 mp3 nist ogg paf prc pvf raw s1 s16 s2 s24 s3 s32 s4 s8 sb sd2 sds sf sl sln smp snd sndfile sndr sndt sou sox sph sw txw u1 u16 u2 u24 u3 u32 u4 u8 ub ul uw vms voc vorbis vox w64 wav wavpcm wv wve xa xi

Sox conversion from .au to .wav

I have a file, sound.au, which file describes as Sun/NeXT audo data: 8-bit ISDN mu-law, mono, 8000 Hz. I'd like to convert this to a WAV that file would describe as RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 8000 Hz. However, I cannot get the right set of arguments to make this conversion and see what it sounds like.
Has anyone performed this conversion or similar before? sox -t auto -w -s -r 8000 -c sound.au sound.wav gets me close, but it's G711 mu-law, not 16 bit PCM.
Thanks.
I don't have an .au file to try, but I suspect sox sound.au -e signed-integer sound.wav would work. You are only trying to change the encoding from u-law to PCM, right? sox should pick up all the necessary input info from the .au header. If it doesn't, maybe you need sox -t auto sound.au -e signed-integer sound.wav.

Resources