async Task ReadAudioAsync() { using (var fileStream = new FileStream(filePath, System.IO.FileMode.Create, System.IO.FileAccess.Write)) { while (true) { if (endRecording) { endRecording = false; break; } try { // Keep reading the buffer while there is audio input. int numBytes = await audioRecord.ReadAsync(audioBuffer, 0, audioBuffer.Length); await fileStream.WriteAsync(audioBuffer, 0, numBytes); // Do something with the audio input. } catch (Exception ex) { Console.Out.WriteLine(ex.Message); break; } } fileStream.Close(); } audioRecord.Stop(); audioRecord.Release(); isRecording = false; RaiseRecordingStateChangedEvent(); }
private void recordLoop() { Android.OS.Process.SetThreadPriority(Android.OS.ThreadPriority.UrgentAudio); while (running) { int num_bytes = 0; try { if (audioRecorder != null) { if (audioEncoder is OpusEncoder) { num_bytes = audioRecorder.ReadAsync(shortsBuffer, 0, shortsBuffer.Length).Result; encode(num_bytes, true); } else { num_bytes = audioRecorder.Read(buffer, 0, buffer.Length); encode(num_bytes, false); } } else { stop(); } } catch (Exception e) { Logging.error("Exception occured while recording audio stream: " + e); } Thread.Sleep(1); } recordThread = null; }
private async Task HandleBufferAsync() { while (true) { if (endRecording) { endRecording = false; break; } try { // Keep reading the buffer while there is audio input. int numBytes = await audioRecorder.ReadAsync(audioBuffer, 0, audioBuffer.Length); //TODO: does this reset the audioRecorder? await WAVStream.WriteAsync(audioBuffer, 0, numBytes); } catch (Exception ex) { Console.Out.WriteLine(ex.Message); //break; } } }
private async Task RecordAudioAsync() { wavPath = Path.Combine(audioDir, Guid.NewGuid().ToString() + "_audio.wav"); byte[] audioBuffer = new byte[8000]; audioRecord = new AudioRecord( AudioSource.Mic, // Hardware source of recording. sampleRate, // Frequency channelIn, // Mono or stereo encoding, // Audio encoding audioBuffer.Length // Length of the audio clip. ); var id = audioRecord.AudioSessionId; audioRecord.StartRecording(); int totalAudioLen = 0; isRecording = true; using (System.IO.Stream outputStream = System.IO.File.Open(wavPath, FileMode.Create)) using (BinaryWriter bWriter = new BinaryWriter(outputStream)) { //init a header with no length - it will be added later WriteWaveFileHeader(bWriter, maxAudioFreamesLength); /// Keep reading the buffer while there is audio input. while (isRecording && totalAudioLen <= maxAudioFreamesLength) { totalAudioLen += await audioRecord.ReadAsync(audioBuffer, 0, audioBuffer.Length); bWriter.Write(audioBuffer); //analysis var intbuffer = ByteArrayTo16Bit(audioBuffer); var min = intbuffer.Min(); var max = intbuffer.Max(); var avg = intbuffer.Average(x => (double)x); var sos = intbuffer.Select(x => (long)x) .Aggregate((prev, next) => prev + next * next); var rms = Math.Sqrt((double)1 / intbuffer.Length * sos); var fft = FFT(intbuffer); } isRecording = false; //write lenght to header outputStream.Close(); bWriter.Close(); } audioRecord.Stop(); audioRecord.Dispose(); //this file is now fully written and can be sent to server for analysis OnAudioReadyForUpload(new AudioUploadEventArgs(DateTime.Now.ToUniversalTime(), wavPath)); }
/// <summary> /// Record from the microphone and broadcast the buffer. /// </summary> /// <returns>Task.</returns> private async Task Record() { var buffer = new byte[_bufferSize]; var readCount = await _audioSource.ReadAsync(buffer, 0, _bufferSize); OnBroadcast.Invoke <byte[]>(this, buffer); }
async Task Record() { var buffer = new byte[_bufferSize]; var readCount = await _audioSource.ReadAsync(buffer, 0, _bufferSize); DataBufferReached?.Invoke(this, new DataBufferEventArgs(buffer)); }
private async Task RecordAudio(List <byte[]> audioTracks, MemoryStream memoryStream) { if (memoryStream == null) { memoryStream = new MemoryStream(); } while (true) { if (endRecording) { endRecording = false; break; } try { // Keep reading the buffer while there is audio input. await audioRecorder.ReadAsync(audioBuffer, 0, audioBuffer.Length); // Write out the audio file. await memoryStream.WriteAsync(audioBuffer, 0, audioBuffer.Length); await Console.Out.WriteLineAsync("RECORDING SOUND. Memory stream size:" + memoryStream.Length); } catch (Exception ex) { Console.Out.WriteLine(ex.Message); break; } } await Console.Out.WriteLineAsync("We successfully stopped recording."); audioRecorder.Stop(); audioRecorder.Release(); var mainTextView = activity.FindViewById <TextView>(mainTextViewId); if (memoryStream.Length > 0) { audioTracks.Add(memoryStream.ToArray()); mainTextView.Text = "Numbers of Tracks:" + audioTracks.Count; } isRecording = false; RaiseRecordingStateChangedEvent(); }
private async Task ProcessAudioData() { var data = _data[0]; var filename = TempFileName; using (var tempStream = new FileStream(filename, FileMode.Create)) { // ==================================== main recording loop ======================================== while (_isRecording) { // ====================================== read data ============================================ //uncomment for PcmFloat mode: ============================ //await _recorder.ReadAsync(data, 0, _sizeInFloats, 0); //instead of Pcm16bit: await _recorder.ReadAsync(_bytes, 0, _bufferSize); ByteConverter.ToFloats16Bit(_bytes, _data); // ======================================================== // ===================================== process data ========================================== _pitchExtractor.ProcessFrame(data, _pitch); _pitchArgs.PitchZcr = Pitch.FromZeroCrossingsSchmitt(data, _samplingRate); _pitchArgs.PitchAutoCorr = _pitch[0]; PitchEstimated(this, _pitchArgs); // event _robotizer.Process(data, data); // ==================== write data to output stream (if necessary) ============================= //uncomment for PcmFloat mode: ========================= //Buffer.BlockCopy(data, 0, _bytes, 0, _bufferSize); // faster than writing float-after-float //await tempStream.WriteAsync(_bytes, 0, _bufferSize); //instead of Pcm16bit: Buffer.BlockCopy(data, 0, _temp, 0, _temp.Length); await tempStream.WriteAsync(_temp, 0, _temp.Length); // ===================================================== } } SaveToFile(); }
private static async Task ReadAudioAsync(string filename) { if (!Directory.Exists(ConfigService.BaseDirectory)) { Directory.CreateDirectory(ConfigService.BaseDirectory); } using (var fileStream = new FileStream(_audioFileService.GetFullPathToNewRecording(filename), FileMode.Create, FileAccess.Write)) { while (true) { if (_endRecording) { _endRecording = false; fileStream.Close(); fileStream.Dispose(); break; } try { int numBytes = await _audioRecord.ReadAsync(_audioBuffer, 0, _audioBuffer.Length); await fileStream.WriteAsync(_audioBuffer, 0, numBytes); } catch (Exception ex) { Console.Out.WriteLine(ex.Message); break; } } fileStream.Close(); fileStream.Dispose(); } _audioRecord.Stop(); _audioRecord.Release(); }
private async void ReadAudioBufferAsync() { try { while (_recorder != null) { // Ensure we are on the UI thread. var read = await _recorder.ReadAsync(_audioBuffer, 0, _audioBuffer.Length); if (read <= 0 || _audioCaptureStream == null) { break; } var offset = TrimAudioZeros(read); if (read > offset) { _audioCaptureStream.Write(_audioBuffer, offset, read - offset); } } } catch { } }
/// <summary> /// Save AudioRecord in file /// </summary> /// <returns></returns> async Task SaveRecordAsync() { WvlLogger.Log(LogType.TraceAll, "SaveRecordAsync()"); var filePath = GetTempFilename(); using (var fileStream = new FileStream(filePath, System.IO.FileMode.Create, System.IO.FileAccess.Write)) { while (true) { WvlLogger.Log(LogType.TraceAll, "SaveRecordAsync() - while true"); if (endRecording) { endRecording = false; break; } try { // default /* * // Keep reading the buffer while there is audio input. * int numBytes = await audioRecord.ReadAsync(audioBuffer, 0, audioBuffer.Length); * //await fileStream.WriteAsync(audioBuffer, 0, numBytes); * fileStream.Write(audioBuffer, 0, numBytes); * // Do something with the audio input. */ // custom //short[] audioBuffer = new short[2048]; // Keep reading the buffer while there is audio input. int numBytes = await audioRecord.ReadAsync(audioBuffer, 0, audioBuffer.Length); WvlLogger.Log(LogType.TraceValues, "SaveRecordAsync() - audioRecord.ReadAsync() - audioBuffer.Length : " + audioBuffer.Length.ToString() + " - numBytes : " + numBytes.ToString()); await fileStream.WriteAsync(audioBuffer, 0, numBytes); byte[] fileAudioBuffer = new byte[audioBuffer.Length * sizeof(short)]; WvlLogger.Log(LogType.TraceValues, "SaveRecordAsync() - audioRecord.ReadAsync() - fileAudioBuffer.Length : " + fileAudioBuffer.Length.ToString()); // Do something with the audio input. // OnNext //OnNext(); /* * WvlLogger.Log(LogType.TraceAll,"EmbeddedOnNext()"); * int[] result = new int[audioBuffer.Length]; * for (int i = 0; i < audioBuffer.Length; i++) * { * result[i] = (int)audioBuffer[i]; * } * samplesUpdated(this, new SamplesUpdatedEventArgs(result)); */ } catch (Exception ex) { WvlLogger.Log(LogType.TraceExceptions, "SaveRecordAsync() - Exception : " + ex.ToString()); Console.Out.WriteLine(ex.Message); break; } } fileStream.Close(); } audioRecord.Stop(); audioRecord.Release(); isRecording = false; //RaiseRecordingStateChangedEvent(); }
void StartRecording() { _cts = new CancellationTokenSource(); _task = Task.Run(async() => { var minBufferSize = AudioTrack.GetMinBufferSize(16000, ChannelOut.Mono, Encoding.Pcm16bit); var recorder = new AudioRecord(AudioSource.Mic, 16000, ChannelIn.Mono, Encoding.Pcm16bit, minBufferSize); recorder.StartRecording(); var audioBuffer = new byte[minBufferSize]; await using var fileStream = new FileStream(GetFileNameForRecording(this, Ext), FileMode.Create, FileAccess.Write); while (!_cts.IsCancellationRequested) { var bytesRead = await recorder.ReadAsync(audioBuffer, 0, minBufferSize); await fileStream.WriteAsync(audioBuffer, 0, bytesRead, CancellationToken.None); } await using var writer = new BinaryWriter(fileStream, System.Text.Encoding.UTF8); writer.Seek(0, SeekOrigin.Begin); // ChunkID writer.Write('R'); writer.Write('I'); writer.Write('F'); writer.Write('F'); // ChunkSize writer.Write(BitConverter.GetBytes(fileStream.Length + 36), 0, 4); // Format writer.Write('W'); writer.Write('A'); writer.Write('V'); writer.Write('E'); //SubChunk writer.Write('f'); writer.Write('m'); writer.Write('t'); writer.Write(' '); // SubChunk1Size - 16 for PCM writer.Write(BitConverter.GetBytes(16), 0, 4); // AudioFormat - PCM=1 writer.Write(BitConverter.GetBytes((short)1), 0, 2); // Channels: Mono=1, Stereo=2 writer.Write(BitConverter.GetBytes(1), 0, 2); // SampleRate writer.Write(16000); // ByteRate var byteRate = 16000 * 1 * 16 / 8; writer.Write(BitConverter.GetBytes(byteRate), 0, 4); // BlockAlign var blockAlign = 1 * 16 / 8; writer.Write(BitConverter.GetBytes((short)blockAlign), 0, 2); // BitsPerSample writer.Write(BitConverter.GetBytes(16), 0, 2); // SubChunk2ID writer.Write('d'); writer.Write('a'); writer.Write('t'); writer.Write('a'); // Subchunk2Size writer.Write(BitConverter.GetBytes(fileStream.Length), 0, 4); fileStream.Close(); recorder.Stop(); recorder.Release(); }, CancellationToken.None); }
/// <summary> /// Starts the recorder. /// </summary> /// <returns></returns> private async Task StartRecorder() { try { //RaiseRecordingStateChangedEvent(); // Buffer for 20 milliseconds of data, e.g. 160 samples at 8kHz. var buffer20ms = new short[Constants.SampleRate / 50]; var audioBuffer = new byte[buffer20ms.Length]; var bufferSize = AudioRecord.GetMinBufferSize(Constants.SampleRate, ChannelIn.Mono, Encoding.Pcm16bit); // TODO: check if I need that //if (bufferSize < Constants.SampleRate) //{ // bufferSize = Constants.SampleRate; //} var audioRecord = new AudioRecord( // Hardware source of recording. AudioSource.Mic, // Frequency Constants.SampleRate, // Channel (Mono or Stereo) ChannelIn.Mono, // Audio encoding Encoding.Pcm16bit, // Length of the audio clip. bufferSize); if (audioRecord.State != State.Initialized) { // return error } _isRecording = true; audioRecord.StartRecording(); while (_isRecording) { var samplesNumber = await audioRecord.ReadAsync(audioBuffer, 0, audioBuffer.Length); var buffer20ms2 = new short[audioBuffer.Length / 2]; var pressure = ProcessAudio(buffer20ms2); if (pressure > Constants.RecordAudioAtSoundPressure || _isRecording2) { // start recording if (!_isRecording2) { _fs = new FileStream(_filePath, FileMode.Create, FileAccess.ReadWrite); _isRecording2 = true; } await _fs.WriteAsync(audioBuffer, 0, samplesNumber); } } _isRecording2 = false; _fs.Close(); audioRecord.Stop(); audioRecord.Release(); } catch (System.Exception ex) { Log.Verbose(TAG, "Error reading audio", ex); } }
private async Task RecordAudioContinuously() { byte[] audioBuffer = new byte[8000]; byte[] preAudioBuffer = new byte[8000]; audioRecord = new AudioRecord( AudioSource.Mic, // Hardware source of recording. sampleRate, // Frequency channelIn, // Mono or stereo encoding, // Audio encoding audioBuffer.Length // Length of the audio clip. ); _forceStop = false; audioRecord.StartRecording(); using (MemoryStream memory = new MemoryStream()) using (BufferedStream stream = new BufferedStream(memory)) { while (!_forceStop) { //start listening await audioRecord.ReadAsync(audioBuffer, 0, audioBuffer.Length); //analysis var intbuffer = ByteArrayTo16Bit(audioBuffer); var audioData = new AudioData(intbuffer, isRecording); if (audioData.IsAllZeros) { //not sure if it is neccesary isRecording = false; memory.Flush(); memory.Clear(); // this one is though continue; } ; //this should be smarter ;) containsVoice = audioData.IdentifyVoice(); //send info to MVVM to display OnRecordStatusChanged(new AudioDataEventArgs(audioData)); //if voice has been detected, start writing if (containsVoice && !isRecording) { isRecording = true; stream.Write(preAudioBuffer, 0, preAudioBuffer.Length); stream.Write(audioBuffer, 0, audioBuffer.Length); } //if sound is still detected keep on recording else if (containsVoice && isRecording) { //write to buffer stream.Write(audioBuffer, 0, audioBuffer.Length); } //if sound is no longer detected, and is still recording else if (!containsVoice && isRecording) { //save to file wavPath = Path.Combine(audioDir, Guid.NewGuid().ToString() + "_audio.wav"); //how much audio do we have if ((int)memory.Length <= 2 * audioBuffer.Length) { //this is probably a false positive, at least no valid sound because to short isRecording = false; continue; } else { //Get one more segment of sound await audioRecord.ReadAsync(audioBuffer, 0, audioBuffer.Length); stream.Write(audioBuffer, 0, audioBuffer.Length); using (System.IO.Stream outputStream = System.IO.File.Open(wavPath, FileMode.Create)) using (BinaryWriter bWriter = new BinaryWriter(outputStream)) { //write header WriteWaveFileHeader(bWriter, (int)memory.Length); memory.WriteTo(outputStream); //close file outputStream.Close(); bWriter.Close(); isRecording = false; } OnAudioReadyForUpload(new AudioUploadEventArgs(DateTime.Now.ToUniversalTime(), wavPath)); } //not sure if it is neccesary memory.Flush(); memory.Clear(); // this one is though } //no voice else { ; } preAudioBuffer = (byte[])audioBuffer.Clone(); } //break out of continously loop //TODO: handle break - does not care if we were recording audioRecord.Stop(); audioRecord.Dispose(); } }