24 bit signed integer binary conversion help needed

Discussion in 'Python' started by Robert Somerville, Jan 8, 2010.

  1. hi;
    I am trying to read 24bit signed WAV format (little endian) data from a
    WAV file and convert it to 32 bit little endian integer format ... can
    anybody please tell me how to do the conversion from 24 bit to 32 bit
    with a snippet of Python code ???

    Thanks so much
    Robert Somerville
     
    Robert Somerville, Jan 8, 2010
    #1
    1. Advertising

  2. On 2010-01-08, Robert Somerville <> wrote:

    > I am trying to read 24bit signed WAV format (little endian) data from a
    > WAV file and convert it to 32 bit little endian integer format ... can
    > anybody please tell me how to do the conversion from 24 bit to 32 bit
    > with a snippet of Python code ???


    def sext24(d):
    if ord(d[2]) & 0x80:
    return d+'\xff'
    else:
    return d+'\x00'

    --
    Grant Edwards grante Yow! Am I having fun yet?
    at
    visi.com
     
    Grant Edwards, Jan 8, 2010
    #2
    1. Advertising

  3. On 2010-01-08, Grant Edwards <> wrote:
    > On 2010-01-08, Robert Somerville <> wrote:
    >
    >> I am trying to read 24bit signed WAV format (little endian) data from a
    >> WAV file and convert it to 32 bit little endian integer format ... can
    >> anybody please tell me how to do the conversion from 24 bit to 32 bit
    >> with a snippet of Python code ???

    >
    > def sext24(d):
    > if ord(d[2]) & 0x80:
    > return d+'\xff'
    > else:
    > return d+'\x00'



    I guess I assumed you knew how to read 3 bytes of data from a
    file:

    f = open('datafile','rb')
    d = f.read(3)


    --
    Grant Edwards grante Yow! I need to discuss
    at BUY-BACK PROVISIONS
    visi.com with at least six studio
    SLEAZEBALLS!!
     
    Grant Edwards, Jan 8, 2010
    #3
  4. On 8-1-2010 22:12, Robert Somerville wrote:
    > hi;
    > I am trying to read 24bit signed WAV format (little endian) data from a
    > WAV file and convert it to 32 bit little endian integer format ... can
    > anybody please tell me how to do the conversion from 24 bit to 32 bit
    > with a snippet of Python code ???
    >
    > Thanks so much
    > Robert Somerville


    Are you using the standard wave module?
    I guess that will produce a string of 3-byte audio frames with readframes().

    Won't it work to chop this up in individual 3-byte frames,
    then appending a '\0' char to every frame,
    and then writing the frames to the target wave ?

    -irmen
     
    Irmen de Jong, Jan 8, 2010
    #4
  5. On 2010-01-08, Irmen de Jong <> wrote:
    > On 8-1-2010 22:12, Robert Somerville wrote:
    >> hi;
    >> I am trying to read 24bit signed WAV format (little endian) data from a
    >> WAV file and convert it to 32 bit little endian integer format ... can
    >> anybody please tell me how to do the conversion from 24 bit to 32 bit
    >> with a snippet of Python code ???

    >
    > Are you using the standard wave module? I guess that will
    > produce a string of 3-byte audio frames with readframes().
    >
    > Won't it work to chop this up in individual 3-byte frames,
    > then appending a '\0' char to every frame, and then writing
    > the frames to the target wave ?


    Not if the 3-byte values are signed two's compliment values.

    --
    Grant Edwards grante Yow! I feel like I am
    at sharing a ``CORN-DOG''
    visi.com with NIKITA KHRUSCHEV ...
     
    Grant Edwards, Jan 8, 2010
    #5
  6. On 8-1-2010 22:37, Grant Edwards wrote:
    > On 2010-01-08, Irmen de Jong<> wrote:
    >> Are you using the standard wave module? I guess that will
    >> produce a string of 3-byte audio frames with readframes().
    >>
    >> Won't it work to chop this up in individual 3-byte frames,
    >> then appending a '\0' char to every frame, and then writing
    >> the frames to the target wave ?

    >
    > Not if the 3-byte values are signed two's compliment values.
    >


    Meh. The wave module is pretty useless for format conversion then until
    it grows some utility methods.

    -irmen
     
    Irmen de Jong, Jan 8, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fangs
    Replies:
    3
    Views:
    9,801
    darshana
    Oct 26, 2008
  2. Replies:
    14
    Views:
    2,118
    CBFalconer
    Jun 18, 2005
  3. Replies:
    12
    Views:
    581
    Richard Heathfield
    Apr 8, 2007
  4. Robert Somerville
    Replies:
    0
    Views:
    430
    Robert Somerville
    Jan 8, 2010
  5. Rob1bureau
    Replies:
    1
    Views:
    814
    joris
    Feb 27, 2010
Loading...

Share This Page