2 byte integer / .wav data -  sample value  conversion

Posted on 2003-03-10
Medium Priority
Last Modified: 2009-07-29
Hello ,

I'm trying to simply display a wave file graphically ...
I'm stuck in reading the  bytes and convert it to a sample value :
ie. for 16bit PCM , I have to convert 2 bytes to a value ranging from -32768 to 32767

so , I have the 2 byte values , ranging from 0 to 255 which represent the 65536-step value , but I dont know how to convert them ,
I guess i could just multiply them, so I have 256 * 256 (=65536) possible values ,
but that must be not correct , cause I have values like (0 and 9) or (0 and 200) which is multiplied =>> 0 * 9 = 0 ???

My guesss is that I have to read as a byte ranging from 0x7FFF(=32767) to 0x8000(-32768),
a 2 byte integer ... but
how do you do that ??
how do you read 2 byte integers ??
Question by:DotNetSlet
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 44

Accepted Solution

Arthur_Wood earned 225 total points
ID: 8104136
what is normally done is to treat these as a "HIGH ORDER" and "LOW ORDER" pair, and then MULIPLY the High-Order value by 256, and Add the Low-Order value, ending up with a value formatted as HHHHLLLL.


Expert Comment

ID: 8104271
Maybe I don't understand well what you need. But if you
need to read a file as array of short values here is a solution:

System.IO.FileStream a = System.IO.File.OpenRead(@"C:\Program Files\NetMeeting\Blip.wav");

short[] c = new short[50];
byte[] bp = new byte[100];
a.Read(bp, 0, 100);

fixed(byte* pSrc = bp) {
  fixed(short* pDst = c) {
    byte* ps = pSrc;
    short* pd = pDst;
    for (int n = 0; n < 50; n++) {
      *((short*)pd) = *((short*)ps);
      pd ++;
      ps += 2;

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question