static void SenderMain(string IPAddress, int tcpPort, int udpPort) { UdpSender udpSend; soundIn = new WasapiLoopbackCapture(30, new WaveFormat(44100, 16, 2)); { //important: always initialize the soundIn instance before creating the //SoundInSource. The SoundInSource needs the WaveFormat of the soundIn, //which gets determined by the soundIn.Initialize method. soundIn.Initialize(); //wrap a sound source around the soundIn instance //in order to prevent playback interruptions, set FillWithZeros to true //otherwise, if the SoundIn does not provide enough data, the playback stops IWaveSource source = new SoundInSource(soundIn) { FillWithZeros = true }; udpSend = new UdpSender(soundIn, IPAddress, tcpPort, udpPort); soundIn.DataAvailable += udpSend.AudioCaptureEvent; //start capturing data soundIn.Start(); Console.WriteLine("Sending audio... Press any key to exit the program."); Console.ReadKey(); } }
static void ReceiverMain(int tcpPort, int udpPort) { //UdpReceiver soundIn; soundIn = new UdpReceiver(tcpPort, udpPort); soundIn.Initialize(); IWaveSource source = new SoundInSource(soundIn) { FillWithZeros = true }; soundIn.Start(); //create a soundOut instance to play the data soundOut = new WasapiOut(); //initialize the soundOut with the echoSource //the echoSource provides data from the "source" and applies the echo effect //the "source" provides data from the "soundIn" instance soundOut.Initialize(source); //play soundOut.Play(); /*System.Timers.Timer aTimer = new System.Timers.Timer(); * aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); * aTimer.Interval = 2000; * aTimer.Enabled = true;*/ Console.WriteLine("Receiving audio... Press any key to exit the program."); Console.ReadKey(); }
public virtual void OnTestInitialize() { _soundIn = CreateSoundIn(); if (_soundIn == null) throw new Exception("No valid soundin."); Debug.WriteLine(_soundIn.GetType().FullName); }
public virtual void OnTestInitialize() { _soundIn = CreateSoundIn(); if (_soundIn == null) { throw new Exception("No valid soundin."); } Debug.WriteLine(_soundIn.GetType().FullName); }
private static ISoundIn ThrowIfSoundInNotInitialized(ISoundIn soundIn) { if (soundIn == null || soundIn.WaveFormat == null) { throw new ArgumentException("The SoundIn has to be initialized. Make sure that the passed SoundIn is not null and provides the format of the recorded data.", "soundIn"); } return(soundIn); }
/// <summary> /// Initializes a new instance of the <see cref="SoundInSource"/> class. /// </summary> /// <param name="soundIn">The soundIn which provides recorded data.</param> /// <param name="bufferSize">Size of the internal buffer in bytes.</param> /// <remarks> /// Note that soundIn has to be already initialized. /// Note that old data ("old" gets specified by the bufferSize) gets overridden. /// For example, if the bufferSize is about 5 seconds big, data which got recorded 6 seconds ago, won't be available anymore. /// </remarks> public SoundInSource(ISoundIn soundIn, int bufferSize) { if (soundIn == null) throw new ArgumentNullException("soundIn"); ThrowIfSoundInNotInitialized(soundIn); _buffer = new WriteableBufferingSource(soundIn.WaveFormat, bufferSize) {FillWithZeros = false}; _soundIn = soundIn; _soundIn.DataAvailable += OnDataAvailable; }
public UdpSender(ISoundIn audioIf, string ipAddress, int tcpPort, int udpPort) { oldFormat = audioIf.WaveFormat; _ipAddress = IPAddress.Parse(ipAddress); _tcpPort = tcpPort; convertWaveFormat(oldFormat); sendWaveFormat(); udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); udpSocket.Connect(_ipAddress, udpPort); }
/// <summary> /// Creates an new instance of SoundInSource with a default bufferSize of 5 seconds. /// </summary> /// <param name="soundIn">The soundIn which provides recorded data.</param> /// <param name="bufferSize">Size of the buffer in bytes.</param> /// <remarks> /// Note that soundIn has to be already initialized. /// Note that old data ("old" gets specified by the bufferSize) gets overridden. /// For example, if the bufferSize is about 5 seconds big, data which got recorded 6 seconds ago, won't be available anymore. /// </remarks> public SoundInSource(ISoundIn soundIn, int bufferSize) { if (soundIn == null) throw new ArgumentNullException("soundIn"); //bufferSize gets validated by WriteableBufferingSource _buffer = new WriteableBufferingSource(soundIn.WaveFormat, bufferSize); _buffer.FillWithZeros = false; _soundIn = soundIn; _soundIn.DataAvailable += OnDataAvailable; }
/// <summary> /// Creates an new instance of SoundInSource with a default bufferSize of 5 seconds. /// </summary> /// <param name="soundIn">The soundIn which provides recorded data.</param> /// <param name="bufferSize">Size of the buffer in bytes.</param> /// <remarks> /// Note that soundIn has to be already initialized. /// Note that old data ("old" gets specified by the bufferSize) gets overridden. /// For example, if the bufferSize is about 5 seconds big, data which got recorded 6 seconds ago, won't be available anymore. /// </remarks> public SoundInSource(ISoundIn soundIn, int bufferSize) { if (soundIn == null) { throw new ArgumentNullException("soundIn"); } //bufferSize gets validated by WriteableBufferingSource _buffer = new WriteableBufferingSource(soundIn.WaveFormat, bufferSize); _buffer.FillWithZeros = false; _soundIn = soundIn; _soundIn.DataAvailable += OnDataAvailable; }
/// <summary> /// Initializes a new instance of the <see cref="SoundInSource"/> class. /// </summary> /// <param name="soundIn">The soundIn which provides recorded data.</param> /// <param name="bufferSize">Size of the internal buffer in bytes.</param> /// <remarks> /// Note that soundIn has to be already initialized. /// Note that old data ("old" gets specified by the bufferSize) gets overridden. /// For example, if the bufferSize is about 5 seconds big, data which got recorded 6 seconds ago, won't be available anymore. /// </remarks> public SoundInSource(ISoundIn soundIn, int bufferSize) { if (soundIn == null) { throw new ArgumentNullException("soundIn"); } ThrowIfSoundInNotInitialized(soundIn); _buffer = new WriteableBufferingSource(soundIn.WaveFormat, bufferSize) { FillWithZeros = false }; _soundIn = soundIn; _soundIn.DataAvailable += OnDataAvailable; }
protected virtual void Dispose(bool disposing) { if (!_disposed) { if (SoundIn != null) { SoundIn.DataAvailable -= OnDataAvailable; _soundIn = null; } if (_buffer != null) { _buffer.Dispose(); _buffer = null; } } _disposed = true; }
private void Stop() { Running = false; if (_soundIn != null) { _soundIn.Stop(); _soundIn.Dispose(); _soundIn = null; } if (_source != null) { _source.Dispose(); _source = null; } _disableTimer.Stop(); _volumeTimer.Stop(); _mayStop = false; }
public MP3Recorder(string filename) { if (File.Exists(filename)) { File.Delete(filename); } wasapiCapture_ = new WasapiCapture(); wasapiCapture_.Initialize(); var wasapiCaptureSource = new SoundInSource(wasapiCapture_); stereoSource_ = wasapiCaptureSource.ToStereo(); writer_ = MediaFoundationEncoder.CreateMP3Encoder(stereoSource_.WaveFormat, filename); byte [] buffer = new byte[stereoSource_.WaveFormat.BytesPerSecond]; wasapiCaptureSource.DataAvailable += (s, e) => { int read = stereoSource_.Read(buffer, 0, buffer.Length); writer_.Write(buffer, 0, read); }; wasapiCapture_.Start(); }
/// <summary> /// Initializes a new instance of the <see cref="SoundInSource"/> class with a default bufferSize of 5 seconds. /// </summary> /// <param name="soundIn">The soundIn which provides recorded data.</param> /// <remarks> /// Note that soundIn has to be already initialized. /// Note that old data ("old" gets specified by the bufferSize) gets overridden. /// For example, if the bufferSize is about 5 seconds big, data which got recorded 6 seconds ago, won't be available anymore. /// </remarks> public RealTimeSoundInSource(ISoundIn soundIn) : this(ThrowIfSoundInNotInitialized(soundIn), 8196) { }
private void Start() { if (_starting) { return; } Logger.Debug("Starting audio capture for device: {0}", Device?.FriendlyName ?? "default"); _starting = true; try { Stop(); if (Type == MmDeviceType.Input) { _soundIn = Device != null ? new WasapiCapture { Device = Device } : new WasapiCapture(); } else { _soundIn = Device != null ? new WasapiLoopbackCapture { Device = Device } : new WasapiLoopbackCapture(); } _soundIn.Initialize(); var soundInSource = new SoundInSource(_soundIn); _source = soundInSource.ToSampleSource().AppendSource(x => new GainSource(x), out _volume); // create a spectrum provider which provides fft data based on some input _spectrumProvider = new BasicSpectrumProvider(_source.WaveFormat.Channels, _source.WaveFormat.SampleRate, FftSize); // the SingleBlockNotificationStream is used to intercept the played samples var notificationSource = new SingleBlockNotificationStream(_source); // pass the intercepted samples as input data to the spectrumprovider (which will calculate a fft based on them) notificationSource.SingleBlockRead += (s, a) => _spectrumProvider.Add(a.Left, a.Right); var waveSource = notificationSource.ToWaveSource(16); // We need to read from our source otherwise SingleBlockRead is never called and our spectrum provider is not populated var buffer = new byte[waveSource.WaveFormat.BytesPerSecond / 2]; soundInSource.DataAvailable += (s, aEvent) => { while (waveSource.Read(buffer, 0, buffer.Length) > 0) { } }; _lineSpectrum = null; _singleSpectrum = new SingleSpectrum(FftSize, _spectrumProvider); _mayStop = false; _disableTimer.Start(); _volumeTimer.Start(); _soundIn.Start(); Running = true; } catch (Exception e) { Logger.Warn(e, "Failed to start WASAPI audio capture"); } _starting = false; }
/// <summary> /// Initializes a new instance of the <see cref="SoundInSource"/> class with a default bufferSize of 5 seconds. /// </summary> /// <param name="soundIn">The soundIn which provides recorded data.</param> /// <remarks> /// Note that soundIn has to be already initialized. /// Note that old data ("old" gets specified by the bufferSize) gets overridden. /// For example, if the bufferSize is about 5 seconds big, data which got recorded 6 seconds ago, won't be available anymore. /// </remarks> public SoundInSource(ISoundIn soundIn) : this(ThrowIfSoundInNotInitialized(soundIn), soundIn.WaveFormat.BytesPerSecond * 5) { }
/// <summary> /// Creates an new instance of SoundInSource with a default bufferSize of 5 seconds. /// </summary> /// <param name="soundIn">The soundIn which provides recorded data.</param> /// <remarks> /// Note that soundIn has to be already initialized. /// Note that old data ("old" gets specified by the bufferSize) gets overridden. /// For example, if the bufferSize is about 5 seconds big, data which got recorded 6 seconds ago, won't be available anymore. /// </remarks> public SoundInSource(ISoundIn soundIn) : this(soundIn, soundIn.WaveFormat.BytesPerSecond * 5) { }
private static ISoundIn ThrowIfSoundInNotInitialized(ISoundIn soundIn) { if(soundIn == null || soundIn.WaveFormat == null) throw new ArgumentException("The SoundIn has to be initialized. Make sure that the passed SoundIn is not null and provides the format of the recorded data.", "soundIn"); return soundIn; }
public virtual void OnTestCleanup() { _soundIn.Dispose(); _soundIn = null; }