public static byte[] ConvertToWav(byte[] amrBuffer) { int status = 0; int wavSize = 0; IntPtr amrSrcPtr = Marshal.AllocHGlobal(amrBuffer.Length); Marshal.Copy(amrBuffer, 0, amrSrcPtr, amrBuffer.Length); IntPtr wavPtr = AudioConvert.convert_amrwb_to_wav(amrSrcPtr, amrBuffer.Length, ref status, ref wavSize); Marshal.FreeHGlobal(amrSrcPtr); if (wavPtr != null && status == 0) { byte[] wavBuffer = new byte[wavSize]; Marshal.Copy(wavPtr, wavBuffer, 0, wavSize); AudioConvert.free_memory(wavPtr); return(wavBuffer); } if (wavPtr != null) { AudioConvert.free_memory(wavPtr); } return(null); }
private void ParseAudioData() { byte[] wavBuffer = AudioConvert.ConvertToWav(audio); int channelCount = wavBuffer[22]; int pos = 12; while (!(wavBuffer[pos] == 100 && wavBuffer[pos + 1] == 97 && wavBuffer[pos + 2] == 116 && wavBuffer[pos + 3] == 97)) { pos += 4; int chunkSize = wavBuffer[pos] + wavBuffer[pos + 1] * 256 + wavBuffer[pos + 2] * 65536 + wavBuffer[pos + 3] * 16777216; pos += 4 + chunkSize; } pos += 8; lengthSamples = (wavBuffer.Length - pos) / 2; pcmData = new float[lengthSamples]; int idx = 0; while (pos < wavBuffer.Length) { pcmData[idx] = BytesToFloat(wavBuffer[pos], wavBuffer[pos + 1]); pos += 2; idx++; } }
private void GetAudioData(AudioClip clip) { var soundData = new float[clip.samples * clip.channels]; clip.GetData(soundData, 0); var newData = new float[position * clip.channels]; for (int i = 0; i < newData.Length; i++) { newData[i] = soundData[i]; } var newClip = AudioClip.Create(clip.name, position, clip.channels, clip.frequency, false); newClip.SetData(newData, 0); long duration = (long)(newClip.length * 1000); MemoryStream amrStream = new MemoryStream(); ConvertAndWriteWav(amrStream, newClip); WriteWavHeader(amrStream, newClip); int lengthSamples = newClip.samples; ClientEngine.RunTask(() => { micPhone.OnRecord(new RTMAudioData(AudioConvert.ConvertToAmrwb(amrStream.ToArray()), newData, RTMAudioData.DefaultCodec, lang, duration, lengthSamples, RECORD_SAMPLE_RATE)); }); }
public void Play(RTMAudioData data) { #if UNITY_ANDROID if (AudioRecord != null) { AudioRecord.Call("broadAudio", AudioConvert.ConvertToWav(data.Audio)); } #elif (UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN) byte[] wavBuffer = AudioConvert.ConvertToWav(data.Audio); startPlay(wavBuffer, wavBuffer.Length, PlayFinishCallback); #else startPlay(data.Audio, data.Audio.Length, PlayFinishCallback); #endif }
private byte[] GetRtmAudioData(RTMAudioHeader rtmAudioHeader, AudioClip clip, long duration) { MemoryStream amrStream = new MemoryStream(); ConvertAndWriteWav(amrStream, clip); WriteWavHeader(amrStream, clip); byte[] amrArray = AudioConvert.ConvertToAmrwb(amrStream.ToArray()); MemoryStream audioStream = new MemoryStream(); byte[] rtmHeader = rtmAudioHeader.HeaderArray; audioStream.Write(rtmHeader, 0, rtmHeader.Length); audioStream.Write(amrArray, 0, amrArray.Length); return(audioStream.ToArray()); }
private static void StopRecordCallback(IntPtr data, int length, long time) { recording = false; if (audioRecorderListener != null) { audioRecorderListener.RecordEnd(); if (cancelRecord) { cancelRecord = false; return; } byte[] payload = new byte[length]; Marshal.Copy(data, payload, 0, length); #if (UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN) RTMAudioData audioData = new RTMAudioData(AudioConvert.ConvertToAmrwb(payload), language, time); #else RTMAudioData audioData = new RTMAudioData(payload, language, time); #endif audioRecorderListener.OnRecord(audioData); } }
private void ParseAudioData(byte[] audio) { rtmAudioHeader = new RTMAudioHeader(); int offset = 0; if (audio.Length < 4) { return; } rtmAudioHeader.version = audio[0]; rtmAudioHeader.containerType = (RTMAudioHeader.ContainerType)audio[1]; rtmAudioHeader.codecType = (RTMAudioHeader.CodecType)audio[2]; byte infoDataCount = audio[3]; offset += 4; for (byte i = 0; i < infoDataCount; i++) { int sectionLength = BitConverter.ToInt32(audio, offset); offset += 4; Dictionary <Object, Object> infoData = MsgUnpacker.Unpack(audio, offset, sectionLength); object value; if (infoData.TryGetValue("lang", out value)) { rtmAudioHeader.lang = (string)value; } if (infoData.TryGetValue("dur", out value)) { rtmAudioHeader.duration = Convert.ToInt64(value); duration = rtmAudioHeader.duration; } if (infoData.TryGetValue("srate", out value)) { rtmAudioHeader.sampleRate = Convert.ToInt32(value); frequency = rtmAudioHeader.sampleRate; } offset += sectionLength; } if (offset >= audio.Length) { return; } byte[] amrBuffer = new byte[audio.Length - offset]; Array.Copy(audio, offset, amrBuffer, 0, audio.Length - offset); byte[] wavBuffer = AudioConvert.ConvertToWav(amrBuffer); int channelCount = wavBuffer[22]; int pos = 12; while (!(wavBuffer[pos] == 100 && wavBuffer[pos + 1] == 97 && wavBuffer[pos + 2] == 116 && wavBuffer[pos + 3] == 97)) { pos += 4; int chunkSize = wavBuffer[pos] + wavBuffer[pos + 1] * 256 + wavBuffer[pos + 2] * 65536 + wavBuffer[pos + 3] * 16777216; pos += 4 + chunkSize; } pos += 8; samples = (wavBuffer.Length - pos) / 2; pcmData = new float[samples]; int idx = 0; while (pos < wavBuffer.Length) { pcmData[idx] = BytesToFloat(wavBuffer[pos], wavBuffer[pos + 1]); pos += 2; idx++; } }