private void Afr_AudioAvailable(object sender, Sources.Audio.DataAvailableEventArgs e) { UISync.Execute(() => { MessageBox.Show(this, "Connected!"); }); afr.Close(); }
public void AudioDeviceDataAvailable(object sender, DataAvailableEventArgs e) { if (Levels == null || IsReconnect) return; try { lock (_lockobject) { Helper.FrameAction fa; if (!Recording) { var dt = Helper.Now.AddSeconds(0 - Micobject.settings.buffer); while (Buffer.Count > 0) { if (Buffer.TryPeek(out fa)) { if (fa.TimeStamp < dt) { if (Buffer.TryDequeue(out fa)) fa.Nullify(); } else { break; } } } } fa = new Helper.FrameAction(e.RawData, e.BytesRecorded, Levels.Max(), Helper.Now); Buffer.Enqueue(fa); } if (Micobject.settings.needsupdate) { Micobject.settings.samples = AudioSource.RecordingFormat.SampleRate; Micobject.settings.channels = AudioSource.RecordingFormat.Channels; Micobject.settings.needsupdate = false; } OutSockets.RemoveAll(p => p.TcpClient.Client.Connected == false); if (OutSockets.Count>0) { if (_mp3Writer == null) { _audioStreamFormat = new WaveFormat(22050, 16, Micobject.settings.channels); var wf = new WaveFormat(_audioStreamFormat.SampleRate, _audioStreamFormat.BitsPerSample, _audioStreamFormat.Channels); _mp3Writer = new LameMP3FileWriter(_outStream, wf, LAMEPreset.STANDARD); } byte[] bSrc = e.RawData; int totBytes = bSrc.Length; var ws = new TalkHelperStream(bSrc, totBytes, AudioSource.RecordingFormat); var helpStm = new WaveFormatConversionStream(_audioStreamFormat, ws); totBytes = helpStm.Read(_bResampled, 0, 22050); ws.Close(); ws.Dispose(); helpStm.Close(); helpStm.Dispose(); _mp3Writer.Write(_bResampled, 0, totBytes); var bterm = Encoding.ASCII.GetBytes("\r\n"); if (_outStream.Length > 0) { var bout = new byte[(int) _outStream.Length]; _outStream.Seek(0, SeekOrigin.Begin); _outStream.Read(bout, 0, (int) _outStream.Length); _outStream.SetLength(0); _outStream.Seek(0, SeekOrigin.Begin); foreach (var s in OutSockets) { var b = Encoding.ASCII.GetBytes(bout.Length.ToString("X") + "\r\n"); try { s.Stream.Write(b, 0, b.Length); s.Stream.Write(bout, 0, bout.Length); s.Stream.Write(bterm, 0, bterm.Length); } catch { OutSockets.Remove(s); break; } } } } else { if (_mp3Writer != null) { _mp3Writer.Close(); _mp3Writer = null; } } DataAvailable?.Invoke(this, new NewDataAvailableArgs((byte[])e.RawData.Clone())); if (_reconnectTime != DateTime.MinValue) { Micobject.settings.active = true; _errorTime = _reconnectTime = DateTime.MinValue; DoAlert("reconnect"); } _errorTime = DateTime.MinValue; } catch (Exception ex) { ErrorHandler?.Invoke(ex.Message); } }