CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2004
    Posts
    138

    from 8 bit to 16

    I have a voice sample. It's coded with 8 bits per sample (char).

    What's the correct algorithm to convert the VALUE (not just the type) into 16 bits (short)?

    Thanks

  2. #2
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    Just square the value of the 8bit variable. The 8bit value 256, will then end up being 65536 (the max for a 16bit variable), but 0 will still be 0. Is that what you wanted?
    Code:
    unsigned char some_value = 128;
    unsigned short resulting_value = static_cast<unsigned short>(some_value) * static_cast<unsigned short>(some_value);
    The resulting data will of course not be any more accurate than the original 8bit data though.

  3. #3
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Hm.. now that Assmaster has suggested.. I don't know , first off, what the requirement is .

    But if it is a voice waveform that you have , does squaring the each value be the right thing to do ?

    I mean, if I consider the 8-bit value as 2 points in a waveform, then , according to this , the ratio between 2 amplitudes at two instants will not be the same in 8 bit and 16-bit.

    Are you trying to scale the values ? In that case , won't a simple multiplier help ?

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    You must be extremely careful, many audio formats ESPECIALLY 8-bit ones are non linear encoding (i.e. 128 is NOT "halfway" between 0 and 255).

    Some of these are even delta calculations.

    These operations are done to preserve both dynamic range and resolution within the key frequency domain [the human ear is also non-linear!]

    Without knowing the current coding format, and the desiret target format [most 16 bit formats ARE linear, but not all], it is impossible to answer.

    I have the conversion functions for most formats if you can provide the needed information. [ One of the perks of having been the lead software designed for Otari's first 100% digital Autio Console ]
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Yeah, probably multiplying by 256 is what you want. The distribution of the values in the domain will remain the same. If you square the values, then smaller values will get suppressed.

    Ok, well, bested by that guy above
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    Yves is correct IF both of the encodings are linear...you might just get lucky! You will know right off the bat if you are wrong [it will sound like nothing you have ever heard!]
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured