public void Start(string inputDevice) { if (Started) { throw new Exception("Input already started"); } //waveIn = new WaveIn(WaveCallbackInfo.FunctionCallback()); waveIn = new WaveInEvent(); waveIn.BufferMilliseconds = 20; //Console.WriteLine("Input device: " + WaveIn.GetCapabilities(0).ProductName); waveIn.DeviceNumber = ClientAudioUtilities.MapInputDevice(inputDevice); waveIn.DataAvailable += _waveIn_DataAvailable; waveIn.WaveFormat = new WaveFormat(sampleRate, 16, 1); inputVolumeStreamArgs = new InputVolumeStreamEventArgs() { DeviceNumber = waveIn.DeviceNumber, PeakRaw = 0, PeakDB = float.NegativeInfinity, PeakVU = 0 }; opusDataAvailableArgs = new OpusDataAvailableEventArgs(); //inputControls = ClientAudioUtilities.GetWaveInMixerControls(waveIn.DeviceNumber); waveIn.StartRecording(); Started = true; }
public void Start(string outputDevice, ISampleProvider sampleProvider) { if (Started) { throw new Exception("Output already started"); } MMDevice device = ClientAudioUtilities.MapWasapiOutputDevice(outputDevice); waveOut = new WasapiOut(device, AudioClientShareMode.Shared, true, 20); waveOut.Init(sampleProvider); waveOut.Play(); Started = true; }
private void audioTimer_Elapsed(object sender, ElapsedEventArgs e) { List <TxTransceiverDto> transceivers = new List <TxTransceiverDto> { new TxTransceiverDto() { ID = 0 } }; //Play a sample every 10 seconds. //audioPublishInputQueue.Add(trimmedBuff); var reader = new WaveFileReader(file); var bytes = (int)reader.SampleCount * 2; bytes = bytes - (bytes % reader.BlockAlign); byte[] buffer = new byte[bytes]; reader.Read(buffer, 0, bytes); var waveBuffer = ClientAudioUtilities.ConvertBytesTo16BitPCM(buffer); int segmentCount = (int)System.Math.Floor((decimal)waveBuffer.Length / frameSize); int bufferOffset = 0; for (int i = 0; i < segmentCount; i++) { int len = encoder.Encode(waveBuffer, bufferOffset, frameSize, encodedDataBuffer, 0, encodedDataBuffer.Length); bufferOffset += frameSize; byte[] trimmedBuff = new byte[len]; Buffer.BlockCopy(encodedDataBuffer, 0, trimmedBuff, 0, len); if (Connection.IsConnected) { Connection.VoiceServerTransmitQueue.Add(new AudioTxDto() { Callsign = Callsign, SequenceCounter = audioSequenceCounter++, Audio = trimmedBuff, LastPacket = false, Transceivers = transceivers.ToArray() }); } Thread.Sleep(17); } Array.Clear(lastPacketBuffer, 0, frameSize); int remainderSamples = waveBuffer.Length - (segmentCount * frameSize); Buffer.BlockCopy(waveBuffer, bufferOffset, lastPacketBuffer, 0, remainderSamples); int lenRemainder = encoder.Encode(lastPacketBuffer, 0, frameSize, encodedDataBuffer, 0, encodedDataBuffer.Length); byte[] trimmedBuffRemainder = new byte[lenRemainder]; Buffer.BlockCopy(encodedDataBuffer, 0, trimmedBuffRemainder, 0, lenRemainder); Connection.VoiceServerTransmitQueue.Add(new AudioTxDto() { Callsign = Callsign, SequenceCounter = audioSequenceCounter++, Audio = trimmedBuffRemainder, LastPacket = true, Transceivers = transceivers.ToArray() }); }
private void _waveIn_DataAvailable(object sender, WaveInEventArgs e) { var samples = ClientAudioUtilities.ConvertBytesTo16BitPCM(e.Buffer); if (samples.Length != frameSize) { throw new Exception("Incorrect number of samples."); } float value = 0; for (int i = 0; i < samples.Length; i++) { value = samples[i] * Volume; if (value > short.MaxValue) { value = short.MaxValue; } if (value < short.MinValue) { value = short.MinValue; } samples[i] = (short)value; } encodedDataLength = encoder.Encode(samples, 0, frameSize, encodedDataBuffer, 0, encodedDataBuffer.Length); OpusBytesEncoded += encodedDataLength; //Calculate max and raise event if needed if (InputVolumeStream != null) { for (int i = 0; i < samples.Length; i++) { sampleInput = samples[i]; sampleInput = System.Math.Abs(sampleInput); if (sampleInput > maxSampleInput) { maxSampleInput = sampleInput; } } sampleCount += samples.Length; if (sampleCount >= sampleCountPerEvent) { inputVolumeStreamArgs.PeakRaw = maxSampleInput / short.MaxValue; inputVolumeStreamArgs.PeakDB = (float)(20 * Math.Log10(inputVolumeStreamArgs.PeakRaw)); float db = inputVolumeStreamArgs.PeakDB; if (db < minDb) { db = minDb; } if (db > maxDb) { db = maxDb; } float ratio = (db - minDb) / (maxDb - minDb); if (ratio < 0.30) { ratio = 0; } if (ratio > 1.0) { ratio = 1; } inputVolumeStreamArgs.PeakVU = ratio; InputVolumeStream(this, inputVolumeStreamArgs); sampleCount = 0; maxSampleInput = 0; } } if (OpusDataAvailable != null) { byte[] trimmedBuff = new byte[encodedDataLength]; Buffer.BlockCopy(encodedDataBuffer, 0, trimmedBuff, 0, encodedDataLength); opusDataAvailableArgs.Audio = trimmedBuff; opusDataAvailableArgs.SequenceCounter = audioSequenceCounter++; OpusDataAvailable(this, opusDataAvailableArgs); } }