|
| 1 | +/** |
| 2 | + * $File: NAudioPlayer.cs $ |
| 3 | + * $Date: 2021-04-13 21:50:50 $ |
| 4 | + * $Revision: $ |
| 5 | + * $Creator: Jen-Chieh Shen $ |
| 6 | + * $Notice: See LICENSE.txt for modification and distribution information |
| 7 | + * Copyright © 2021 by Shen, Jen-Chieh $ |
| 8 | + */ |
| 9 | +using System; |
| 10 | +using System.IO; |
| 11 | +using UnityEngine; |
| 12 | +using NAudio.Wave; |
| 13 | + |
| 14 | +namespace UWL |
| 15 | +{ |
| 16 | + public static class NAudioPlayer |
| 17 | + { |
| 18 | + public static AudioClip FromMp3Data(string name, byte[] data) |
| 19 | + { |
| 20 | + // Load the data into a stream |
| 21 | + MemoryStream mp3stream = new MemoryStream(data); |
| 22 | + // Convert the data in the stream to WAV format |
| 23 | + Mp3FileReader mp3audio = new Mp3FileReader(mp3stream); |
| 24 | + WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(mp3audio); |
| 25 | + // Convert to WAV data |
| 26 | + WAV wav = new WAV(AudioMemStream(waveStream).ToArray()); |
| 27 | + AudioClip audioClip = AudioClip.Create(name, wav.SampleCount, 1, wav.Frequency, false); |
| 28 | + audioClip.SetData(wav.LeftChannel, 0); |
| 29 | + // Return the clip |
| 30 | + return audioClip; |
| 31 | + } |
| 32 | + |
| 33 | + private static MemoryStream AudioMemStream(WaveStream waveStream) |
| 34 | + { |
| 35 | + MemoryStream outputStream = new MemoryStream(); |
| 36 | + using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputStream, waveStream.WaveFormat)) |
| 37 | + { |
| 38 | + byte[] bytes = new byte[waveStream.Length]; |
| 39 | + waveStream.Position = 0; |
| 40 | + waveStream.Read(bytes, 0, Convert.ToInt32(waveStream.Length)); |
| 41 | + waveFileWriter.Write(bytes, 0, bytes.Length); |
| 42 | + waveFileWriter.Flush(); |
| 43 | + } |
| 44 | + return outputStream; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /* From http://answers.unity3d.com/questions/737002/wav-byte-to-audioclip.html */ |
| 49 | + public class WAV |
| 50 | + { |
| 51 | + |
| 52 | + // convert two bytes to one float in the range -1 to 1 |
| 53 | + static float bytesToFloat(byte firstByte, byte secondByte) |
| 54 | + { |
| 55 | + // convert two bytes to one short (little endian) |
| 56 | + short s = (short)((secondByte << 8) | firstByte); |
| 57 | + // convert to range from -1 to (just below) 1 |
| 58 | + return s / 32768.0F; |
| 59 | + } |
| 60 | + |
| 61 | + static int bytesToInt(byte[] bytes, int offset = 0) |
| 62 | + { |
| 63 | + int value = 0; |
| 64 | + for (int i = 0; i < 4; i++) |
| 65 | + { |
| 66 | + value |= ((int)bytes[offset + i]) << (i * 8); |
| 67 | + } |
| 68 | + return value; |
| 69 | + } |
| 70 | + // properties |
| 71 | + public float[] LeftChannel { get; internal set; } |
| 72 | + public float[] RightChannel { get; internal set; } |
| 73 | + public int ChannelCount { get; internal set; } |
| 74 | + public int SampleCount { get; internal set; } |
| 75 | + public int Frequency { get; internal set; } |
| 76 | + |
| 77 | + public WAV(byte[] wav) |
| 78 | + { |
| 79 | + |
| 80 | + // Determine if mono or stereo |
| 81 | + ChannelCount = wav[22]; // Forget byte 23 as 99.999% of WAVs are 1 or 2 channels |
| 82 | + |
| 83 | + // Get the frequency |
| 84 | + Frequency = bytesToInt(wav, 24); |
| 85 | + |
| 86 | + // Get past all the other sub chunks to get to the data subchunk: |
| 87 | + int pos = 12; // First Subchunk ID from 12 to 16 |
| 88 | + |
| 89 | + // Keep iterating until we find the data chunk (i.e. 64 61 74 61 ...... (i.e. 100 97 116 97 in decimal)) |
| 90 | + while (!(wav[pos] == 100 && wav[pos + 1] == 97 && wav[pos + 2] == 116 && wav[pos + 3] == 97)) |
| 91 | + { |
| 92 | + pos += 4; |
| 93 | + int chunkSize = wav[pos] + wav[pos + 1] * 256 + wav[pos + 2] * 65536 + wav[pos + 3] * 16777216; |
| 94 | + pos += 4 + chunkSize; |
| 95 | + } |
| 96 | + pos += 8; |
| 97 | + |
| 98 | + // Pos is now positioned to start of actual sound data. |
| 99 | + SampleCount = (wav.Length - pos) / 2; // 2 bytes per sample (16 bit sound mono) |
| 100 | + if (ChannelCount == 2) SampleCount /= 2; // 4 bytes per sample (16 bit stereo) |
| 101 | + |
| 102 | + // Allocate memory (right will be null if only mono sound) |
| 103 | + LeftChannel = new float[SampleCount]; |
| 104 | + if (ChannelCount == 2) RightChannel = new float[SampleCount]; |
| 105 | + else RightChannel = null; |
| 106 | + |
| 107 | + // Write to double array/s: |
| 108 | + int i = 0; |
| 109 | + while (pos < wav.Length) |
| 110 | + { |
| 111 | + LeftChannel[i] = bytesToFloat(wav[pos], wav[pos + 1]); |
| 112 | + pos += 2; |
| 113 | + if (ChannelCount == 2) |
| 114 | + { |
| 115 | + RightChannel[i] = bytesToFloat(wav[pos], wav[pos + 1]); |
| 116 | + pos += 2; |
| 117 | + } |
| 118 | + i++; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public override string ToString() |
| 123 | + { |
| 124 | + return string.Format("[WAV: LeftChannel={0}, RightChannel={1}, ChannelCount={2}, SampleCount={3}, Frequency={4}]", LeftChannel, RightChannel, ChannelCount, SampleCount, Frequency); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments