public void Start() { _firstAfterStart = true; if (_soundPcm != null) { _soundPcm.Dispose(); } _soundPcm = new SoundPcm("default", snd_pcm_stream_t.SND_PCM_STREAM_CAPTURE, 0); using (var hardwareParams = new SoundPcmHardwareParams(_soundPcm)) { hardwareParams.Any(); hardwareParams.Access = snd_pcm_access_t.SND_PCM_ACCESS_RW_INTERLEAVED; hardwareParams.Format = snd_pcm_format_t.SND_PCM_FORMAT_S16_LE; uint rate = 8000; int dir = 0; hardwareParams.SetRateNear(ref rate, ref dir); if (rate != 8000) { throw new Exception($"Could not get required sample rate of {8000} instead got {rate}"); } hardwareParams.Channels = 1; uint periodSize = 160; hardwareParams.SetPeriodSizeNear(ref periodSize, ref dir); if (periodSize != 160) { Console.WriteLine($"Requested Alsa PeriodSize 160 but got {periodSize}"); } uint periods = 2; hardwareParams.SetPeriodsNear(ref periods, ref dir); if (periods != 2) { Console.WriteLine($"Requested Alsa Periods 2 but got {periods}"); } _soundPcm.HardwareParams = hardwareParams; _soundPcm.Prepare(); //this starts filling the buffer so need top Drop _soundPcm.Start(); } }
public AlsaAudioPlayer(bool prefillBuffer) { _prefillBuffer = prefillBuffer; _soundPcm = new SoundPcm("default", snd_pcm_stream_t.SND_PCM_STREAM_PLAYBACK, 0); using (var hardwareParams = new SoundPcmHardwareParams(_soundPcm)) { hardwareParams.Any(); hardwareParams.Access = snd_pcm_access_t.SND_PCM_ACCESS_RW_INTERLEAVED; hardwareParams.Format = snd_pcm_format_t.SND_PCM_FORMAT_S16_LE; uint rate = 8000; int dir = 0; hardwareParams.SetRateNear(ref rate, ref dir); if (rate != 8000) { throw new Exception($"AlsaAudioPlayer: Could not get required sample rate of {8000} instead got {rate}"); } hardwareParams.Channels = 1; uint periodSize = 160; hardwareParams.SetPeriodSizeNear(ref periodSize, ref dir); if (periodSize != 160) { Console.WriteLine($"Requested Alsa PeriodSize 160 but got {periodSize}"); } uint periods = 2; hardwareParams.SetPeriodsNear(ref periods, ref dir); if (periods != 2) { Console.WriteLine($"AlsaAudioPlayer: Could not get required periods {2} instead got {periods}"); } _periods = periods; uint bufferSize = 320; hardwareParams.SetBufferNear(ref bufferSize); if (bufferSize != 320) { Console.WriteLine($"AlsaAudioPlayer: Could not get requested buffer size instead got {bufferSize}"); } _soundPcm.HardwareParams = hardwareParams; } }