Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2013
1,385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. using System;
  6. using System.Runtime.InteropServices;
  7.  
  8.  
  9. public class MP3Import : MonoBehaviour
  10. {
  11.    
  12.     public IntPtr handle_mpg;
  13.     public IntPtr errPtr;
  14.     public IntPtr rate;
  15.     public IntPtr channels;
  16.     public IntPtr encoding;
  17.     public IntPtr id3v1;
  18.     public IntPtr id3v2;
  19.     public IntPtr done;
  20.    
  21.     public string mPath;
  22.     public int x;
  23.     public int intRate;
  24.     public int intChannels;
  25.     public int intEncoding;
  26.     public int FrameSize;
  27.     public int lengthSamples;
  28.     public AudioClip myClip;
  29.     public AudioSource audioSource;
  30.    
  31.      #region Consts: Standard values used in almost all conversions.
  32.     private const float const_1_div_128_ = 1.0f / 128.0f;  // 8 bit multiplier
  33.     private const float const_1_div_32768_ = 1.0f / 32768.0f; // 16 bit multiplier
  34.     private const double const_1_div_2147483648_ = 1.0 / 2147483648.0; // 32 bit
  35.      #endregion
  36.    
  37.     public void StartImport()
  38.     {
  39.         mPath = EditorUtility.OpenFilePanel ("Open MP3", "", "mp3");   
  40.                
  41.         audioSource = (AudioSource)gameObject.GetComponent(typeof(AudioSource));
  42.         if(audioSource==null)audioSource=(AudioSource)gameObject.AddComponent("AudioSource");
  43.        
  44.         MPGImport.mpg123_init ();
  45.         handle_mpg = MPGImport.mpg123_new (null, errPtr);
  46.         x = MPGImport.mpg123_open (handle_mpg, mPath);     
  47.         MPGImport.mpg123_getformat (handle_mpg, out rate, out channels, out encoding);
  48.         intRate = rate.ToInt32 ();
  49.         intChannels = channels.ToInt32 ();
  50.         intEncoding = encoding.ToInt32 ();
  51.        
  52.         MPGImport.mpg123_id3 (handle_mpg, out id3v1, out id3v2);       
  53.         MPGImport.mpg123_format_none (handle_mpg);
  54.         MPGImport.mpg123_format (handle_mpg, intRate, intChannels, 208);
  55.        
  56.         FrameSize = MPGImport.mpg123_outblock (handle_mpg);    
  57.         byte[] Buffer = new byte[FrameSize];       
  58.         lengthSamples = MPGImport.mpg123_length (handle_mpg);
  59.                
  60.         myClip = AudioClip.Create ("myClip", lengthSamples, intChannels, intRate, false, false);
  61.        
  62.         int importIndex = 0;
  63.        
  64.         while (0 == MPGImport.mpg123_read(handle_mpg, Buffer, FrameSize, out done)) {
  65.                
  66.            
  67.             float[] fArray;
  68.             fArray = ByteToFloat (Buffer);
  69.                                
  70.             myClip.SetData (fArray, (importIndex*fArray.Length)/2);
  71.            
  72.             importIndex++;                
  73.         }          
  74.        
  75.         MPGImport.mpg123_close (handle_mpg);
  76.                        
  77.         audioSource.clip = myClip;
  78.         audioSource.loop = true;
  79.         audioSource.Play ();
  80.     }
  81.        
  82.     void Start ()
  83.     {
  84.         StartImport();
  85.     }
  86.  
  87.     public float[] IntToFloat (Int16[] from)
  88.     {
  89.         float[] to = new float[from.Length];
  90.            
  91.         for (int i = 0; i < from.Length; i++)
  92.             to [i] = (float)(from [i] * const_1_div_32768_);
  93.  
  94.         return to;
  95.     }
  96.  
  97.     public Int16[] ByteToInt16 (byte[] buffer)
  98.     {
  99.         Int16[] result = new Int16[1];
  100.         int size = buffer.Length;
  101.         if ((size % 2) != 0) {
  102.             /* Error here */
  103.             Console.WriteLine ("error");
  104.             return result;
  105.         } else {
  106.             result = new Int16[size / 2];
  107.             IntPtr ptr_src = Marshal.AllocHGlobal (size);
  108.             Marshal.Copy (buffer, 0, ptr_src, size);
  109.             Marshal.Copy (ptr_src, result, 0, result.Length);
  110.             Marshal.FreeHGlobal (ptr_src);
  111.             return result;
  112.         }
  113.     }
  114.    
  115.     public float[] ByteToFloat (byte[] bArray)
  116.     {
  117.         Int16[] iArray;    
  118.            
  119.         iArray = ByteToInt16 (bArray);
  120.        
  121.         return IntToFloat (iArray);
  122.     }
  123.    
  124.    
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement