Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 828585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:44:50+00:00 2026-05-15T03:44:50+00:00

Please see the class I have created at http://textsnip.com/see/WAVinAS3 for parsing a WAVE file

  • 0

Please see the class I have created at http://textsnip.com/see/WAVinAS3 for parsing a WAVE file in ActionScript 3.0.

This class is correctly pulling apart info from the file header & fmt chunks, isolating the data chunk, and creating a new ByteArray to store the data chunk. It takes in an uncompressed WAVE file with a format tag of 1. The WAVE file is embedded into my SWF with the following Flex embed tag:

[Embed(source="some_sound.wav", mimeType="application/octet-stream")]
public var sound_class:Class;
public var wave:WaveFile = new WaveFile(new sound_class());

After the data chunk is separated, the class attempts to make a Sound object that can stream the samples from the data chunk. I’m having issues with the streaming process, probably because I’m not good at math and don’t really know what’s happening with the bits/bytes, etc.

Here are the two documents I’m using as a reference for the WAVE file format:
http://www.lightlink.com/tjweber/StripWav/Canon.html
https://ccrma.stanford.edu/courses/422/projects/WaveFormat/

Right now, the file IS playing back! In real time, even! But…the sound is really distorted. What’s going on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:44:51+00:00Added an answer on May 15, 2026 at 3:44 am

    The problem is in the onSampleData handler.

    In your wav file, the amplitudes are stored as signed shorts, that is 16 bit integers. You are reading them as 32 bit signed floats. Integers and floats are represented differently in binary, so that will never work right.

    Now, the player expects floats. Why did they use floats? Don’t know for sure, but one good reason is that it allows the player to accept a normalized value for each sample. That way you don’t have to care or know what bitdept the player is using: the max value is 1, and the min value is -1, and that’s it.

    So, your problem is you have to convert your signed short to a normalized signed float. A short takes 16 bits, so it can store 2 ^ 16 (or 65,536) different values. Since it’s signed and the sign takes up one bit, the max value will be 2 ^ 15. So, you know your input is the range -32,768 … 32,767.

    The sample value is normalized and must be in the range -1 … 1, on the other hand.

    So, you have to normalize your input. It’s quite easy. Just take the read value and divide it by the max value, and you have your input amplitude converted to the range -1 … 1.

    Something like this:

        private function onSampleData(evt:SampleDataEvent):void 
        { 
            var amplitude:int = 0;
            var maxAmplitude:int = 1 << (bitsPerSample - 1); // or Math.pow(2, bitsPerSample - 1);
            var sample:Number = 0; 
            var actualSamples:int = 8192;
            var samplesPerChannel:int = actualSamples / channels;
    
            for ( var c:int = 0; c < samplesPerChannel ; c++ ) { 
                var i:int = 0;
                while(i < channels && data.bytesAvailable >= 2) {
                    amplitude = data.readShort();
                    sample = amplitude / maxAmplitude;
                    evt.data.writeFloat(sample); 
                    i++;
                }
            } 
        }  
    

    A couple of things to note:

    1. maxAmplitude could (and probably
      should) be calculated when you read
      the bitdepth. I’m doing it in the
      method just so you can see it in the
      pasted code.

    2. Although maxAmplitude is calculated
      based on the read bitdepth and thus
      will be correct for any bitdepth,
      I’m reading shorts in the loop, so
      if your wav file happens to use a
      different bitdepth, this function
      will not work correctly. You could
      add a switch and read the necessary
      ammount of data (i.e., readInt if
      bitdepth is 32). However, 16 bits is
      such a widely used standard, that I
      doubt this is practically needed.

    3. This function will work for
      stereo wavs. If you want it to work
      for mono, re write it to write the
      same sample twice. That is, for each
      read, you do two writes (your input
      is mono, but the player expects 2
      samples).

    4. I removed the EOF catch, as you can
      know if you have enough data to read
      from your buffer checking
      bytesAvailable. Reaching the end of
      stream is not exceptional in any
      way, IMO, so I’d rather control that
      case without an exception handler,
      but this is just a personal
      preference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
Important : Please see this very much related question: Return multiple values in C++
I have an Adorner which adornes a Border (please see screenshot below). The MouseDown
I have sql server procedure, please see below. ALTER PROCEDURE [dbo].[uspInsertDelegate] ( @CourseID int,
Please See Correct Answer for solution to the requested question. Hi, Recently I have
I have created an Inventory Class but I don't know how to retrieve information
I have a problem with this very simple block of code. please give me
Please see code below. The destructors are never called. Anyone know why and how
Please see here for a related question . However, char goes to 0xffff (or
Please see following methods. public static ProductsCollection GetDummyData(int? customerId, int? supplierId) { try {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.