Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FLAC file created with the wrapper not playing #2

Open
designscripting opened this issue Dec 8, 2012 · 0 comments
Open

FLAC file created with the wrapper not playing #2

designscripting opened this issue Dec 8, 2012 · 0 comments

Comments

@designscripting
Copy link

Hi Luke,

I have created a sample file to try using flac wrapper in Flash. It is working fine for me but the converted file is not playing in any player.

Below is the sample code

import flash.media.Sound;
import flash.events.SampleDataEvent;
import flash.events.MouseEvent;

import cmodule.flac.CLibInit;
import flash.utils.ByteArray;

const FLOAT_MAX_VALUE:Number = 1.0;
const SHORT_MAX_VALUE:int = 0x7fff;

var flacData:ByteArray;
var raw:ByteArray;
var bool:Boolean = true;
var flacCodec:Object;
flacCodec = (new cmodule.flac.CLibInit).init();

var sound:Sound = new Sound();
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
sound.play();
var byteData:ByteArray = new ByteArray();
var mono:Boolean = true;
stage.addEventListener(MouseEvent.CLICK, onClick);
function onClick(event:MouseEvent):void
{
mono = !mono;
flacData = new ByteArray();
flacData.endian = Endian.LITTLE_ENDIAN;
flacCodec.encode(encodingCompleteHandler,encodingProgressHandler, raw, flacData, raw.length, 10);

}
function encodingCompleteHandler(event:*):void {
flacData.position = 0;

(new FileReference()).save(flacData, ".flac");
}

function encodingProgressHandler(progress:int):void {
}

function onSampleData(event:SampleDataEvent):void
{
for(var i:int = 0; i < 2048; i++)
{
var sampleA:Number = Math.random() * 2.0 - 1.0; // -1 to 1
var sampleB:Number = Math.random() * 2.0 - 1.0; // -1 to 1
event.data.writeFloat(sampleA); // left
if(mono)
{
event.data.writeFloat(sampleA); // left again
}
else
{
event.data.writeFloat(sampleB); // right
}
}
if(bool)
{
bool = false;
raw = ByteArray(event.data)
raw.position = 0;
raw = convert32to16(raw)
}
}

/**

  • Converts an (raw) audio stream from 32-bit (signed, floating point)

  • to 16-bit (signed integer).

  • @param source The audio stream to convert.
    */
    function convert32to16(source:ByteArray):ByteArray
    {
    trace("BitrateConvertor.convert32to16(source)", source.length);

          var result:ByteArray = new ByteArray();
          result.endian = Endian.LITTLE_ENDIAN;
    
          while( source.bytesAvailable ) {
              var sample:Number = source.readFloat() * SHORT_MAX_VALUE;
    
              // Make sure we don't overflow.
              if (sample < -SHORT_MAX_VALUE) sample = -SHORT_MAX_VALUE;
              else if (sample > SHORT_MAX_VALUE) sample = SHORT_MAX_VALUE;
    
              result.writeShort(sample);
          }
    
          trace(" - result.length:", result.length);
          result.position = 0;
          return result;
    

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant