Guest
Public paste!

Mauftcom

By: a guest | Mar 29th, 2009 | Syntax: ActionScript | Size: 4.09 KB | Hits: 522 | Expires: Never
Copy text to clipboard
  1. package {
  2.         import cmodule.oggvorbis.CLibInit;
  3.        
  4.         import flash.display.Sprite;
  5.         import flash.events.SampleDataEvent;
  6.         import flash.media.Sound;
  7.         import flash.media.SoundChannel;
  8.         import flash.utils.ByteArray;
  9.         import flash.utils.Endian;
  10.        
  11.         public class Test extends Sprite{
  12.                 [Embed(source="C:/Test.ogg",mimeType="application/octet-stream")] //Path to the imported OGG file, which is stored as byte array
  13.                 private var MUSIC_DATA:Class;                           //Class of the imported file
  14.                 private var _decoder:*                                          //OGG Decoder
  15.                 private var _audio_clip:Sound                           //The dynamically generated Sound of the imported file
  16.                 private var _audio_id:Number                            //ID of the decoded file, for Decoder use
  17.                 private var _audio_bytearray:ByteArray          //Bytearray of the imported OGG file
  18.                 private var _audio_info:Object=new Object       //Informations about the imported OGG file
  19.                 private var temp_array:ByteArray                        //Temporary variable, stores audio data on fly
  20.                 private var data_read_size:Number                       //Temporary variable used for converting
  21.                 private var sample_read_size:Number             //Temporary variable used for converting
  22.                 private const num_of_samples:Number=4096        //Constant
  23.                 public function Test(){
  24.                         var clib:CLibInit=new CLibInit();               //Instantation of the CLib
  25.                         _decoder=clib.init();                                   //Initialize the CLib and store the OGG Decoder
  26.  
  27.                         _audio_clip=new Sound;                                  //Create new Sound to hold the audio data
  28.                         _audio_clip.addEventListener("sampleData", onSamplesRequest);   //MOST CRUCIAL LINE - adds listener to catch event when the music is to be converted
  29.                        
  30.                         _audio_bytearray=(new MUSIC_DATA) as ByteArray //Create the bytearray of the import OGG
  31.                        
  32.                         _audio_id=_decoder.setupDecoder(_audio_bytearray, _audio_bytearray.length);     //Prepare the decoder to decoding the OGG File
  33.                        
  34.                         var infoObj:Object = _decoder.getHeader(_audio_id);     //Get some information about the OGG file
  35.                         _audio_info.format = "PCM";
  36.                         _audio_info.channels = infoObj.channels;
  37.                         _audio_info.sampleRate = infoObj.sampleRate;
  38.                         _audio_info.sampleMultiplier = 44100 / infoObj.sampleRate;
  39.                         _audio_info.bitRateUpper = infoObj.bitRateUpper;
  40.                         _audio_info.bitRateLower = infoObj.bitRateLower;
  41.                         _audio_info.bitRateNominal = infoObj.bitRateNominal;
  42.                         _audio_info.bitsPerSample = 16;
  43.                         _audio_info.blockAlign = _audio_info.channels * 2;
  44.                        
  45.                         _audio_clip.play()                                              //Play the Sound
  46.                 }
  47.                 private function onSamplesRequest(event:SampleDataEvent):void {
  48.                         temp_array=new ByteArray                                       
  49.                         temp_array.endian = Endian.LITTLE_ENDIAN       
  50.  
  51.                         //This part generally fills temo_array with decoded audio, and sets data_read_size to the amount of data processed
  52.                         data_read_size=_decoder.getSampleData(_audio_id, num_of_samples*_audio_info.blockAlign/_audio_info.sampleMultiplier, temp_array)
  53.                         temp_array.position=0
  54.                        
  55.                         sample_read_size = data_read_size/_audio_info.blockAlign;
  56.                        
  57.                         if (_audio_info.channels==1){                   //Convert the decoder's output to Flash friendly audio output
  58.                                 convertMono(event)                     
  59.                         } else {
  60.                                 convertStereo(event)
  61.                         }
  62.                        
  63.                         var i:uint;
  64.                         var j:uint;
  65.                        
  66.                         //Fill the empty space with zeroes if needed.
  67.                         for (i=sample_read_size * _audio_info.sampleMultiplier; i<num_of_samples; ++i) {
  68.                                 event.data.writeFloat(0);
  69.                                 event.data.writeFloat(0);
  70.                         }
  71.  
  72.                 }
  73.                 private function convertMono(event:SampleDataEvent):void{
  74.                         var i:uint;
  75.                         var j:uint;
  76.                         var sample:Number;
  77.                         for (i=0; i<sample_read_size; ++i) {
  78.                                 if (temp_array.position==temp_array.length){break}
  79.                                 sample = temp_array.readShort() / 32767;
  80.                                 for (j=0; j<_audio_info.sampleMultiplier; ++j) {
  81.                                         event.data.writeFloat(sample);
  82.                                         event.data.writeFloat(sample);
  83.                                 }
  84.                         }
  85.                        
  86.                 }
  87.                 private function convertStereo(event:*):void{
  88.                         var i:uint;
  89.                         var j:uint;
  90.                         var sample:Number;
  91.                        
  92.                         for (i=0; i<sample_read_size; ++i) {
  93.                                 sample = temp_array.readShort() / 32767;
  94.                                 for (j=0; j< _audio_info.sampleMultiplier; ++j) {
  95.                                         event.data.writeFloat(sample);
  96.                                 }
  97.                                
  98.                                 sample = temp_array.readShort() / 32767;
  99.                                 for (j=0; j< _audio_info.sampleMultiplier; ++j) {
  100.                                         event.data.writeFloat(sample);
  101.                                 }
  102.                         }
  103.                 }
  104.         }
  105. }