/// <summary> /// Creates a new instance of <see cref="IRecorder"/> writing to <see cref="IAudioFileWriter"/>. /// </summary> /// <param name="AudioWriter">The <see cref="IAudioFileWriter"/> to write to.</param> /// <param name="AudioProvider">The audio source.</param> public Recorder(IAudioFileWriter AudioWriter, IAudioProvider AudioProvider) { _audioWriter = AudioWriter ?? throw new ArgumentNullException(nameof(AudioWriter)); _audioProvider = AudioProvider ?? throw new ArgumentNullException(nameof(AudioProvider)); _audioProvider.DataAvailable += (s, e) => _audioWriter.Write(e.Buffer, 0, e.Length); }
/// <summary> /// Creates a new instance of <see cref="AudioRecorder"/>. /// </summary> /// <param name="Provider">The Audio Source.</param> /// <param name="Writer">The <see cref="IAudioFileWriter"/> to write audio to.</param> /// <exception cref="ArgumentNullException"><paramref name="Provider"/> or <paramref name="Writer"/> is null.</exception> public AudioRecorder(IAudioProvider Provider, IAudioFileWriter Writer) { if (Provider == null) { throw new ArgumentNullException(nameof(Provider)); } if (Writer == null) { throw new ArgumentNullException(nameof(Writer)); } _audioProvider = Provider; _writer = Writer; _audioProvider.DataAvailable += (s, e) => _writer.Write(e.Buffer, 0, e.Length); _audioProvider.RecordingStopped += (s, e) => RaiseRecordingStopped(e.Error); }
void Loop() { bool CanContinue() { try { return(_continueEvent.WaitOne() && !_stopEvent.WaitOne(0)); } catch (ObjectDisposedException) { return(false); } } while (CanContinue()) { var read = _audioProvider.Read(_buffer, 0, _buffer.Length); _audioWriter.Write(_buffer, 0, read); Thread.Sleep(ReadInterval); } }
private void AudioProvider_DataAvailable(object sender, DataAvailableEventArgs dataAvailableEventArgs) { if (_videoWriter == null) { _audioWriter.Write(dataAvailableEventArgs.Buffer, 0, dataAvailableEventArgs.Length); return; } try { lock (_syncLock) { if (_disposed) { return; } } _videoWriter.WriteAudio(dataAvailableEventArgs.Buffer, dataAvailableEventArgs.Length); } catch (Exception e) { if (_imageProvider == null) { lock (_syncLock) { if (!_disposed) { ErrorOccurred?.Invoke(e); Dispose(true); } } } } }
/// <summary> /// Writes all the Data in the decoder to a file /// </summary> /// <param name="Writer">Audio File Writer to write to</param> /// <param name="Offset">+ve for forward, -ve for backward</param> public void Write(IAudioFileWriter Writer, int Offset = 0) { long InitialPosition = Position; Position += Offset; int BlockLength = (int)Seconds2Bytes(2); byte[] Buffer = new byte[BlockLength]; var gch = GCHandle.Alloc(Buffer, GCHandleType.Pinned); while (HasData) { Bass.ChannelGetData(Handle, gch.AddrOfPinnedObject(), BlockLength); Writer.Write(Buffer, BlockLength); } gch.Free(); Writer.Dispose(); Position = InitialPosition; }