/// <summary> /// Saves a byte array of audio samples to a properly formatted WAV file. /// </summary> /// <param name="audiodata"></param> private void WriteAudioDataToRiffWAVFile(byte[] audiodata, string filename) { try { string filePath = Path.Combine(Application.temporaryCachePath, filename); Debug.Log($"Opening new WAV file for recording: {filePath}"); FileStream fs = new FileStream(filePath, FileMode.Create); BinaryWriter wr = new BinaryWriter(fs); // Writing WAV header Debug.Log($"Writing WAV header to file with a count of {recordingSamples} samples."); var header = recoServiceClient.BuildRiffWAVHeader(recordingSamples, samplingResolution, numChannels, samplingRate); wr.Write(header, 0, header.Length); // Write the audio data to the main file body Debug.Log($"Writing {audiodata.Length} WAV data samples to file."); wr.Write(audiodata, 0, audiodata.Length); wr.Dispose(); fs.Dispose(); Debug.Log($"Completed writing {audiodata.Length} WAV data samples to file."); } catch (Exception ex) { string msg = String.Format("Error: Something went wrong when saving the audio data to a WAV file. See error details below:{0}{1}{2}{3}", Environment.NewLine, ex.ToString(), Environment.NewLine, ex.Message); Debug.LogError(msg); UpdateUICanvasLabel(msg, FontStyle.Normal); } }
/// <summary> /// Saves a byte array of audio samples to a properly formatted WAV file. /// </summary> /// <param name="audiodata"></param> private void WriteAudioDataToRiffWAVFile(byte[] audiodata, string filename) { string filePath = Path.Combine(Application.temporaryCachePath, filename); Debug.Log($"Opening new WAV file for recording: {filePath}"); FileStream fs = new FileStream(filePath, FileMode.Create); BinaryWriter wr = new BinaryWriter(fs); // Writing WAV header Debug.Log($"Writing WAV header to file with a count of {recordingSamples} samples."); var header = recoServiceClient.BuildRiffWAVHeader(recordingSamples, samplingResolution, numChannels, samplingRate); wr.Write(header, 0, header.Length); // Write the audio data to the main file body Debug.Log($"Writing {audiodata.Length} WAV data samples to file."); wr.Write(audiodata, 0, audiodata.Length); wr.Close(); fs.Close(); Debug.Log($"Completed writing {audiodata.Length} WAV data samples to file."); }