private void LoadMusic() { blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(musicPath))); waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()); waveOut.Init(blockAlignedStream); }
/// <summary> /// Creates a default WaveOut device /// Will use window callbacks if called from a GUI thread, otherwise function /// callbacks /// </summary> public WaveOut() : this(SynchronizationContext.Current == null ? WaveCallbackInfo.FunctionCallback() : WaveCallbackInfo.NewWindow()) { }
/// <summary> /// Creates a WaveOut device using the specified window handle for callbacks /// </summary> /// <param name="windowHandle">A valid window handle</param> public WaveOut(IntPtr windowHandle) : this(WaveCallbackInfo.ExistingWindow(windowHandle)) { }
public void StartEncoding(int mic, MMDevice speakers, string guid, InputDeviceManager inputManager, IPAddress ipAddress, int port, MMDevice micOutput) { _stop = false; try { _micInputQueue.Clear(); InitMixers(); InitAudioBuffers(); //Audio manager should start / stop and cleanup based on connection successfull and disconnect //Should use listeners to synchronise all the state _waveOut = new WasapiOut(speakers, AudioClientShareMode.Shared, true, 40); //add final volume boost to all mixed audio _volumeSampleProvider = new VolumeSampleProviderWithPeak(_clientAudioMixer, (peak => SpeakerMax = (float)VolumeConversionHelper.ConvertFloatToDB(peak))); _volumeSampleProvider.Volume = SpeakerBoost; if (speakers.AudioClient.MixFormat.Channels == 1) { if (_volumeSampleProvider.WaveFormat.Channels == 2) { _waveOut.Init(_volumeSampleProvider.ToMono()); } else { //already mono _waveOut.Init(_volumeSampleProvider); } } else { if (_volumeSampleProvider.WaveFormat.Channels == 1) { _waveOut.Init(_volumeSampleProvider.ToStereo()); } else { //already stereo _waveOut.Init(_volumeSampleProvider); } } _waveOut.Play(); //opus _encoder = OpusEncoder.Create(INPUT_SAMPLE_RATE, 1, Application.Voip); _encoder.ForwardErrorCorrection = false; _decoder = OpusDecoder.Create(INPUT_SAMPLE_RATE, 1); _decoder.ForwardErrorCorrection = false; //speex _speex = new Preprocessor(AudioManager.SEGMENT_FRAMES, AudioManager.INPUT_SAMPLE_RATE); } catch (Exception ex) { Logger.Error(ex, "Error starting audio Output - Quitting! " + ex.Message); ShowOutputError("Problem Initialising Audio Output!"); Environment.Exit(1); } if (micOutput != null) // && micOutput !=speakers { //TODO handle case when they're the same? try { _micWaveOut = new WasapiOut(micOutput, AudioClientShareMode.Shared, true, 40); _micWaveOutBuffer = new BufferedWaveProvider(new WaveFormat(AudioManager.INPUT_SAMPLE_RATE, 16, 1)); _micWaveOutBuffer.ReadFully = true; _micWaveOutBuffer.DiscardOnBufferOverflow = true; var sampleProvider = _micWaveOutBuffer.ToSampleProvider(); if (micOutput.AudioClient.MixFormat.Channels == 1) { if (sampleProvider.WaveFormat.Channels == 2) { _micWaveOut.Init(sampleProvider.ToMono()); } else { //already mono _micWaveOut.Init(sampleProvider); } } else { if (sampleProvider.WaveFormat.Channels == 1) { _micWaveOut.Init(sampleProvider.ToStereo()); } else { //already stereo _micWaveOut.Init(sampleProvider); } } _micWaveOut.Play(); } catch (Exception ex) { Logger.Error(ex, "Error starting mic audio Output - Quitting! " + ex.Message); ShowOutputError("Problem Initialising Mic Audio Output!"); Environment.Exit(1); } } if (mic != -1) { try { _waveIn = new WaveIn(WaveCallbackInfo.FunctionCallback()) { BufferMilliseconds = INPUT_AUDIO_LENGTH_MS, DeviceNumber = mic, }; _waveIn.NumberOfBuffers = 2; _waveIn.DataAvailable += _waveIn_DataAvailable; _waveIn.WaveFormat = new WaveFormat(INPUT_SAMPLE_RATE, 16, 1); _tcpVoiceHandler = new TCPVoiceHandler(_clientsList, guid, ipAddress, port, _decoder, this, inputManager); var voiceSenderThread = new Thread(_tcpVoiceHandler.Listen); voiceSenderThread.Start(); _waveIn.StartRecording(); MessageHub.Instance.Subscribe <SRClient>(RemoveClientBuffer); } catch (Exception ex) { Logger.Error(ex, "Error starting audio Input - Quitting! " + ex.Message); ShowInputError("Problem initialising Audio Input!"); Environment.Exit(1); } } }