Quantcast
Channel: Questions in topic: "external-libraries"
Viewing all articles
Browse latest Browse all 86

Audioclip error using NAudio to open MP3 file on Windows

$
0
0
I'm using the [NAudio libraries][1] to open an MP3 file from the HardDrive, convert the byte array returned to a float array and then create an audioclip to set it to an AudioSource because i created a visualizer and i need the audio coming from inside Unity not from the SoundCard, well the problem is that i only hear strange sounds maybe because of the structure of the final audioclip but i doesn't find where the problem is or even how to identify it because i don't know too much how the sound files work, so please someone help me to fix this issue. Also, sorry if i comitted too much language errors but my mother language is spanish using UnityEngine; using System.Collections; using NAudio; using NAudio.Wave; using System.IO; using NAudio.MediaFoundation; public class AbrirMP3 : MonoBehaviour { private IWavePlayer mWaveOutDevice; private WaveStream mMainOutputStream; private WaveChannel32 mVolumeStream; public GameObject Spectrum; public int position = 0; public int samplerate = 44100; public float frequency = 440; public AudioSource source1; public int SamplesArrayFloat; // Use this for initialization void Start () { } private bool LoadAudioFromData(byte[] data) { try { MemoryStream tmpStr = new MemoryStream(data); mMainOutputStream = new Mp3FileReader(tmpStr); mVolumeStream = new WaveChannel32(mMainOutputStream); mWaveOutDevice = new WaveOut(); mWaveOutDevice.Init(mVolumeStream); return true; } catch (System.Exception ex) { Debug.LogWarning("Error! " + ex.Message); } return false; } public static float[] ConvertByteToFloat(byte[] array) { float[] floatArr = new float[array.Length / 4]; for (int i = 0; i < floatArr.Length; i++) { if (System.BitConverter.IsLittleEndian) { System.Array.Reverse(array, i * 4, 4); } floatArr[i] = System.BitConverter.ToSingle(array, i * 4); } return floatArr; } private void LoadAudio() { System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog(); ofd.Title = "Open audio file"; ofd.Filter = "MP3 audio (*.mp3) | *.mp3"; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { WWW www = new WWW("file://" + ofd.FileName); Debug.Log("path = " + ofd.FileName); while (!www.isDone) { }; if (!string.IsNullOrEmpty(www.error)) { System.Windows.Forms.MessageBox.Show("Error! Cannot open file: " + ofd.FileName + "; " + www.error); return; } byte[] imageData = www.bytes; if (!LoadAudioFromData(imageData)) { System.Windows.Forms.MessageBox.Show("Cannot open mp3 file!"); return; } source1.Stop(); float[] f = ConvertByteToFloat(imageData); SamplesArrayFloat = f.Length; AudioClip audioClip = AudioClip.Create("testSound", f.Length, 1, 44100, false, OnAudioRead, OnAudioSetPosition); audioClip.SetData(f, 0); source1.clip = audioClip; source1.Play(); Resources.UnloadUnusedAssets(); } } void OnAudioRead(float[] data) { int count = 0; while (count < data.Length) { data[count] = Mathf.Sign(Mathf.Sin(2 * Mathf.PI * frequency * position / samplerate)); position++; count++; } } void OnAudioSetPosition(int newPosition) { position = newPosition; } private void UnloadAudio() { if (mWaveOutDevice != null) { mWaveOutDevice.Stop(); } if (mMainOutputStream != null) { // this one really closes the file and ACM conversion mVolumeStream.Close(); mVolumeStream = null; // this one does the metering stream mMainOutputStream.Close(); mMainOutputStream = null; } if (mWaveOutDevice != null) { mWaveOutDevice.Dispose(); mWaveOutDevice = null; } } // Update is called once per frame void Update () { } } [1]: https://naudio.codeplex.com/

Viewing all articles
Browse latest Browse all 86

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>