Quantcast

How to change sampling rate?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to change sampling rate?

Elio Blanca-2
How can I use sox in order to just change the sampling frequency of a file?
I mean, I do not want to convert the file, just slow down (or accelerate) the same samples, varying
the sampling frequency. Can this be done with sox?
I see the rate filter does resample the source audio, and so does the speed filter, which in turn
calls the rate filter. What am I missing?
Any help is appreciated.
Elio

------------------------------------------------------------------------------
BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the
BlackBerry® mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry® DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1 
_______________________________________________
Sox-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/sox-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to change sampling rate?

Jan Stary
On Sep 13 13:17:58, Elio Blanca wrote:
> How can I use sox in order to just change the sampling frequency of a file?
> I mean, I do not want to convert the file, just slow down (or accelerate) the same samples, varying
> the sampling frequency. Can this be done with sox?

First of all, why do you want to do this?  Are you fixing broken
files (that specify a wrong srate in their headers)?

If it's a raw (headerless) file, there is nothing to be done,
just play/process it with '-r WHATEVER'.

If the file has a header that specifies the sampling rate,
and you only want to rewrite the srate information in the header
without actually resampling to this new sampling rate
(did I understand it right?), I don't think SoX will let
you do this directly.

You can open the file in a hex editor and just rewrite the sample
rate information in the header, without doing anything else.

Or you can make the file headerless first, and then specify any
rate you want when writing the final input, like this:

$ soxi in.wav                                                      

Input File     : 'in.wav'
Channels       : 2
Sample Rate    : 48000
Precision      : 16-bit
Duration       : 00:00:10.00 = 480000 samples ~ 750 CDDA sectors
File Size      : 1.92M
Bit Rate       : 1.54M
Sample Encoding: 16-bit Signed Integer PCM

$ sox in.wav -t raw - | sox -t raw -e signed -b 16 -c 2 -r 44100 - out.wav

$ soxi out.wav                                                    

Input File     : 'out.wav'
Channels       : 2
Sample Rate    : 44100
Precision      : 16-bit
Duration       : 00:00:10.88 = 480000 samples = 816.327 CDDA sectors
File Size      : 1.92M
Bit Rate       : 1.41M
Sample Encoding: 16-bit Signed Integer PCM

Now the out.wav file contains exactly the same samples as in.wav,
but its header says the sample rate is 44100 instead of 48000,
and the byte_rate is appropriately different:

$ diff -a in.wav out.wav            
1c1
< RIFF$LWAVEfmt ťîdataLujŤŠTVž¨,Ó,čý'î'î˙Ž&;幌Ż
}                                               4/eŚúŰń$q<&ő˙úĂR
---
> RIFF$LWAVEfmt DŹądataLujŤŠTVž¨,Ó,čý'î'î˙Ž&;幌Ż
}                                                4/eŚúŰń$q<&ő˙úĂR

$ hexdump in.wav | head -2 | tee /tmp/in
0000000    4952    4646    4c24    001d    4157    4556    6d66    2074
0000010    0010    0000    0001    0002    bb80    0000    ee00    0002

$ hexdump out.wav  | head -2 | tee /tmp/out
0000000    4952    4646    4c24    001d    4157    4556    6d66    2074
0000010    0010    0000    0001    0002    ac44    0000    b110    0002

(The exact positions in the WAV header
is left as an excerscise to the reader.)


        Jan


------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the
BlackBerry&reg; mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry&reg; DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1 
_______________________________________________
Sox-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/sox-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to change sampling rate?

Jan Stary
On Sep 13 17:29:24, Jan Stary wrote:

> On Sep 13 13:17:58, Elio Blanca wrote:
> > How can I use sox in order to just change the sampling frequency of a file?
> > I mean, I do not want to convert the file, just slow down (or accelerate) the same samples, varying
> > the sampling frequency. Can this be done with sox?
>
> First of all, why do you want to do this?  Are you fixing broken
> files (that specify a wrong srate in their headers)?
>
> If it's a raw (headerless) file, there is nothing to be done,
> just play/process it with '-r WHATEVER'.
>
> If the file has a header that specifies the sampling rate,
> and you only want to rewrite the srate information in the header
> without actually resampling to this new sampling rate
> (did I understand it right?), I don't think SoX will let
> you do this directly.
>
> You can open the file in a hex editor and just rewrite the sample
> rate information in the header, without doing anything else.
>
> Or you can make the file headerless first, and then specify any
> rate you want when writing the final input, like this:

final _output_ of course.

>
> $ soxi in.wav                                                      
>
> Input File     : 'in.wav'
> Channels       : 2
> Sample Rate    : 48000
> Precision      : 16-bit
> Duration       : 00:00:10.00 = 480000 samples ~ 750 CDDA sectors
> File Size      : 1.92M
> Bit Rate       : 1.54M
> Sample Encoding: 16-bit Signed Integer PCM
>
> $ sox in.wav -t raw - | sox -t raw -e signed -b 16 -c 2 -r 44100 - out.wav
>
> $ soxi out.wav                                                    
>
> Input File     : 'out.wav'
> Channels       : 2
> Sample Rate    : 44100
> Precision      : 16-bit
> Duration       : 00:00:10.88 = 480000 samples = 816.327 CDDA sectors
> File Size      : 1.92M
> Bit Rate       : 1.41M
> Sample Encoding: 16-bit Signed Integer PCM
>
> Now the out.wav file contains exactly the same samples as in.wav,
> but its header says the sample rate is 44100 instead of 48000,
> and the byte_rate is appropriately different:
>
> $ diff -a in.wav out.wav            
> 1c1
> < RIFF$LWAVEfmt ťîdataLujŤŠTVž¨,Ó,čý'î'î˙Ž&;幌Ż
> }                                               4/eŚúŰń$q<&ő˙úĂR
> ---
> > RIFF$LWAVEfmt DŹądataLujŤŠTVž¨,Ó,čý'î'î˙Ž&;幌Ż
> }                                                4/eŚúŰń$q<&ő˙úĂR
>
> $ hexdump in.wav | head -2 | tee /tmp/in
> 0000000    4952    4646    4c24    001d    4157    4556    6d66    2074
> 0000010    0010    0000    0001    0002    bb80    0000    ee00    0002
>
> $ hexdump out.wav  | head -2 | tee /tmp/out
> 0000000    4952    4646    4c24    001d    4157    4556    6d66    2074
> 0000010    0010    0000    0001    0002    ac44    0000    b110    0002
>
> (The exact positions in the WAV header
> is left as an excerscise to the reader.)
>
>
> Jan
>
>
> ------------------------------------------------------------------------------
> BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
> Learn about the latest advances in developing for the
> BlackBerry&reg; mobile platform with sessions, labs & more.
> See new tools and technologies. Register for BlackBerry&reg; DevCon today!
> http://p.sf.net/sfu/rim-devcon-copy1 
> _______________________________________________
> Sox-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/sox-users

------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the
BlackBerry&reg; mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry&reg; DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1 
_______________________________________________
Sox-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/sox-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to change sampling rate?

Gregorio Garcia Karman
In reply to this post by Elio Blanca-2
Hi Elio,

Not sure if this would help you, but I once had to fix the header of bunch of wave files with the wrong sample rate written in the header (the header said 48 kHz but it should have been 44.1 kHz). I asked sox to reinterpret the input as a raw file and rewrite the right header in the following way:

sox -r 44.1k -e float -e signed -c 2  input.wav output.wav

Cheers

Gregorio Garcia Karman
[hidden email]



On 13.09.2011, at 13:17, Elio Blanca wrote:

> How can I use sox in order to just change the sampling frequency of a file?
> I mean, I do not want to convert the file, just slow down (or accelerate) the same samples, varying
> the sampling frequency. Can this be done with sox?
> I see the rate filter does resample the source audio, and so does the speed filter, which in turn
> calls the rate filter. What am I missing?
> Any help is appreciated.
> Elio
>
> ------------------------------------------------------------------------------
> BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
> Learn about the latest advances in developing for the
> BlackBerry&reg; mobile platform with sessions, labs & more.
> See new tools and technologies. Register for BlackBerry&reg; DevCon today!
> http://p.sf.net/sfu/rim-devcon-copy1 
> _______________________________________________
> Sox-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/sox-users


------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the
BlackBerry&reg; mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry&reg; DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1 
_______________________________________________
Sox-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/sox-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to change sampling rate?

Pascal Giard
On Tue, Sep 13, 2011 at 11:53 AM, Gregorio Garcia Karman
<[hidden email]> wrote:
> Hi Elio,
>
> Not sure if this would help you, but I once had to fix the header of bunch of wave files with the wrong sample rate written in the header (the header said 48 kHz but it should have been 44.1 kHz). I asked sox to reinterpret the input as a raw file and rewrite the right header in the following way:
>
> sox -r 44.1k -e float -e signed -c 2  input.wav output.wav

FWIW, it's either float or signed not both.
IIRC, sox will take the one specified last, in this case, signed.

Cheers,

-Pascal
--
Homepage (http://organact.mine.nu)
Debian GNU/Linux (http://www.debian.org)
COMunité/LACIME: École de technologie supérieure (http://www.comunite.ca)
Integrated Microsystems Laboratory: McGill (http://www.iml.ece.mcgill.ca)

------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the
BlackBerry&reg; mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry&reg; DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1 
_______________________________________________
Sox-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/sox-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to change sampling rate?

Gregorio Garcia Karman
thanks Pascal, they say man is the only animal that trips twice...


FWIW, it's either float or signed not both.
IIRC, sox will take the one specified last, in this case, signed.

Cheers,

-Pascal
--
Homepage (http://organact.mine.nu)
Debian GNU/Linux (http://www.debian.org)
COMunité/LACIME: École de technologie supérieure (http://www.comunite.ca)
Integrated Microsystems Laboratory: McGill (http://www.iml.ece.mcgill.ca)

------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the
BlackBerry&reg; mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry&reg; DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1
_______________________________________________
Sox-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/sox-users


------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the
BlackBerry&reg; mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry&reg; DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1 
_______________________________________________
Sox-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/sox-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to change sampling rate?

Elio Blanca-2
In reply to this post by Pascal Giard
Ok guys, thank you for you suggestions!
Well, you got it right, I just want to (slightly) modify srate into wav headers and now I know two
different ways for doing this :-)
The simplest way:
> sox -r 44.1k -e signed -c 2 -b 16 input.wav output.wav
as suggested by Gregorio, and the *FAST!* way (useful when managing several hours audio) of using
the hex editor, as suggested by Jan:
> $ hexdump in.wav | head -2 | tee /tmp/in
> 0000000    4952    4646    4c24    001d    4157    4556    6d66    2074
> 0000010    0010    0000    0001    0002    bb80    0000    ee00    0002
>
> $ hexdump out.wav  | head -2 | tee /tmp/out
> 0000000    4952    4646    4c24    001d    4157    4556    6d66    2074
> 0000010    0010    0000    0001    0002    ac44    0000    b110    0002
[Well, be careful with this, since in the wav header the current byte rate is also included (it's
the field just after the sampling frequency) which has to be fixed as well.]
Thank you all!

------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the
BlackBerry&reg; mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry&reg; DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1 
_______________________________________________
Sox-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/sox-users
Loading...