diff --git a/src/audio/audio_interfaces/AlsaInterface.cpp b/src/audio/audio_interfaces/AlsaInterface.cpp index fc8fc67..8c05f4d 100644 --- a/src/audio/audio_interfaces/AlsaInterface.cpp +++ b/src/audio/audio_interfaces/AlsaInterface.cpp @@ -2,9 +2,9 @@ #include "FileLogger.h" AlsaInterface::AlsaInterface() - :mHandle(), - mHardwareParams(), - mPeriodSize(8192) + :mHandle(), + mHardwareParams(), + mPeriodSize(8192) { } @@ -16,24 +16,24 @@ AlsaInterface::~AlsaInterface() std::shared_ptr AlsaInterface::Create() { - return std::make_shared(); + return std::make_shared(); } void AlsaInterface::OpenDevice(AudioDevicePtr device) { - MLOG_INFO("Opening Device"); + MLOG_INFO("Opening Device"); snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK; if (snd_pcm_open(&mHandle, device->GetName().c_str(), stream, 0) < 0) { - MLOG_ERROR("Error opening PCM device: " + device->GetName()); - return; + MLOG_ERROR("Error opening PCM device: " + device->GetName()); + return; } snd_pcm_hw_params_alloca(&mHardwareParams); if (snd_pcm_hw_params_any(mHandle, mHardwareParams) < 0) { - MLOG_ERROR("Can not configure this PCM device."); - return; + MLOG_ERROR("Can not configure this PCM device."); + return; } SetAccessType(device); @@ -46,16 +46,16 @@ void AlsaInterface::OpenDevice(AudioDevicePtr device) /* Apply HW parameter settings to */ /* PCM device and prepare device */ if (snd_pcm_hw_params(mHandle, mHardwareParams) < 0) { - MLOG_ERROR("Error setting HW params."); - return; + MLOG_ERROR("Error setting HW params."); + return; } } void AlsaInterface::SetAccessType(AudioDevicePtr device) { if (snd_pcm_hw_params_set_access(mHandle, mHardwareParams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) { - MLOG_ERROR("Error setting device access."); - return; + MLOG_ERROR("Error setting device access."); + return; } } @@ -63,22 +63,22 @@ void AlsaInterface::SetSampleFormat(AudioDevicePtr device) { /* Set sample format */ if (snd_pcm_hw_params_set_format(mHandle, mHardwareParams, SND_PCM_FORMAT_S16_LE) < 0) { - MLOG_ERROR("Error setting format. "); - return; + MLOG_ERROR("Error setting format. "); + return; } } void AlsaInterface::SetSampleRate(AudioDevicePtr device) { - unsigned rate = device->GetSampleRate(); + unsigned rate = device->GetSampleRate(); unsigned exact_rate = rate; if (snd_pcm_hw_params_set_rate_near(mHandle, mHardwareParams, &exact_rate, 0) < 0) { - MLOG_ERROR("Error setting rate. "); - return; + MLOG_ERROR("Error setting rate. "); + return; } if (rate != exact_rate) { - MLOG_ERROR("The rate is not supported by your hardware."); + MLOG_ERROR("The rate is not supported by your hardware."); } } @@ -87,22 +87,21 @@ void AlsaInterface::SetPeriod(AudioDevicePtr device) /* Set number of periods. Periods used to be called fragments. */ if (snd_pcm_hw_params_set_periods(mHandle, mHardwareParams, device->GetPeriod(), 0) < 0) { - MLOG_ERROR("Error setting periods. "); - return; + MLOG_ERROR("Error setting periods. "); + return; } } void AlsaInterface::SetBufferSize(AudioDevicePtr device) { - snd_pcm_uframes_t periodsize = 8192; /* Periodsize (bytes) */ - int periods = static_cast(device->GetPeriod()); + int periods = static_cast(device->GetPeriod()); /* Set buffer size (in frames). The resulting latency is given by */ /* latency = periodsize * periods / (rate * bytes_per_frame) */ if (snd_pcm_hw_params_set_buffer_size(mHandle, mHardwareParams, (mPeriodSize * periods)>>2) < 0) { - MLOG_ERROR("Error setting buffersize. "); - return; + MLOG_ERROR("Error setting buffersize. "); + return; } } @@ -111,32 +110,31 @@ void AlsaInterface::SetChannelNumber(AudioDevicePtr device) /* Set number of channels */ if (snd_pcm_hw_params_set_channels(mHandle, mHardwareParams, device->GetNumChannels()) < 0) { - MLOG_ERROR("Error setting channels"); - return; + MLOG_ERROR("Error setting channels"); + return; } } void AlsaInterface::Play(AudioDevicePtr device) { - MLOG_INFO("Playing audio"); - int num_frames = 10; + MLOG_INFO("Playing audio"); unsigned char *data = (unsigned char *)malloc(mPeriodSize); int frames = mPeriodSize >> 2; - for(int l1 = 0; l1 < 100; l1++) + for(int count = 0; count < 100; count++) { - for(int l2 = 0; l2 < num_frames; l2++) - { - short s1 = (l2 % 128) * 100 - 5000; - short s2 = (l2 % 256) * 100 - 5000; - data[4*l2] = (unsigned char)s1; - data[4*l2+1] = s1 >> 8; - data[4*l2+2] = (unsigned char)s2; - data[4*l2+3] = s2 >> 8; - } - while ((snd_pcm_writei(mHandle, data, frames)) < 0) - { - snd_pcm_prepare(mHandle); - MLOG_ERROR("<<<<<<<<<<<<<<< Buffer Underrun >>>>>>>>>>>>>>>"); - } + for(int l2 = 0; l2 < frames; l2++) + { + short s1 = (l2 % (512-count)) * 100 - 5000; + short s2 = (l2 % (256-count)) * 100 - 5000; + data[4*l2] = (unsigned char)s1; + data[4*l2+1] = s1 >> 8; + data[4*l2+2] = (unsigned char)s2; + data[4*l2+3] = s2 >> 8; + } + while ((snd_pcm_writei(mHandle, data, frames)) < 0) + { + snd_pcm_prepare(mHandle); + MLOG_ERROR("<<<<<<<<<<<<<<< Buffer Underrun >>>>>>>>>>>>>>>"); + } } } diff --git a/src/console/MainApplication.cpp b/src/console/MainApplication.cpp index 0fb4776..dc9a92b 100644 --- a/src/console/MainApplication.cpp +++ b/src/console/MainApplication.cpp @@ -34,11 +34,11 @@ void MainApplication::RunServer() void MainApplication::PlayAudio() { - MidiReader reader; - reader.Read("/home/james/sample.mid"); -// auto device = AudioDevice::Create(); -// mAudioManager->GetAudioInterface()->OpenDevice(device); -// mAudioManager->GetAudioInterface()->Play(device); + //MidiReader reader; + //reader.Read("/home/james/sample.mid"); + auto device = AudioDevice::Create(); + mAudioManager->GetAudioInterface()->OpenDevice(device); + mAudioManager->GetAudioInterface()->Play(device); } void MainApplication::ShutDown()