Prep before md work.

This commit is contained in:
jmsgrogan 2020-05-09 09:35:09 +01:00
parent 4d5ca4d654
commit 875cdc84ff
2 changed files with 47 additions and 49 deletions

View file

@ -2,9 +2,9 @@
#include "FileLogger.h" #include "FileLogger.h"
AlsaInterface::AlsaInterface() AlsaInterface::AlsaInterface()
:mHandle(), :mHandle(),
mHardwareParams(), mHardwareParams(),
mPeriodSize(8192) mPeriodSize(8192)
{ {
} }
@ -16,24 +16,24 @@ AlsaInterface::~AlsaInterface()
std::shared_ptr<AlsaInterface> AlsaInterface::Create() std::shared_ptr<AlsaInterface> AlsaInterface::Create()
{ {
return std::make_shared<AlsaInterface>(); return std::make_shared<AlsaInterface>();
} }
void AlsaInterface::OpenDevice(AudioDevicePtr device) void AlsaInterface::OpenDevice(AudioDevicePtr device)
{ {
MLOG_INFO("Opening Device"); MLOG_INFO("Opening Device");
snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK; snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
if (snd_pcm_open(&mHandle, device->GetName().c_str(), stream, 0) < 0) if (snd_pcm_open(&mHandle, device->GetName().c_str(), stream, 0) < 0)
{ {
MLOG_ERROR("Error opening PCM device: " + device->GetName()); MLOG_ERROR("Error opening PCM device: " + device->GetName());
return; return;
} }
snd_pcm_hw_params_alloca(&mHardwareParams); snd_pcm_hw_params_alloca(&mHardwareParams);
if (snd_pcm_hw_params_any(mHandle, mHardwareParams) < 0) if (snd_pcm_hw_params_any(mHandle, mHardwareParams) < 0)
{ {
MLOG_ERROR("Can not configure this PCM device."); MLOG_ERROR("Can not configure this PCM device.");
return; return;
} }
SetAccessType(device); SetAccessType(device);
@ -46,16 +46,16 @@ void AlsaInterface::OpenDevice(AudioDevicePtr device)
/* Apply HW parameter settings to */ /* Apply HW parameter settings to */
/* PCM device and prepare device */ /* PCM device and prepare device */
if (snd_pcm_hw_params(mHandle, mHardwareParams) < 0) { if (snd_pcm_hw_params(mHandle, mHardwareParams) < 0) {
MLOG_ERROR("Error setting HW params."); MLOG_ERROR("Error setting HW params.");
return; return;
} }
} }
void AlsaInterface::SetAccessType(AudioDevicePtr device) void AlsaInterface::SetAccessType(AudioDevicePtr device)
{ {
if (snd_pcm_hw_params_set_access(mHandle, mHardwareParams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) { if (snd_pcm_hw_params_set_access(mHandle, mHardwareParams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
MLOG_ERROR("Error setting device access."); MLOG_ERROR("Error setting device access.");
return; return;
} }
} }
@ -63,22 +63,22 @@ void AlsaInterface::SetSampleFormat(AudioDevicePtr device)
{ {
/* Set sample format */ /* Set sample format */
if (snd_pcm_hw_params_set_format(mHandle, mHardwareParams, SND_PCM_FORMAT_S16_LE) < 0) { if (snd_pcm_hw_params_set_format(mHandle, mHardwareParams, SND_PCM_FORMAT_S16_LE) < 0) {
MLOG_ERROR("Error setting format. "); MLOG_ERROR("Error setting format. ");
return; return;
} }
} }
void AlsaInterface::SetSampleRate(AudioDevicePtr device) void AlsaInterface::SetSampleRate(AudioDevicePtr device)
{ {
unsigned rate = device->GetSampleRate(); unsigned rate = device->GetSampleRate();
unsigned exact_rate = rate; unsigned exact_rate = rate;
if (snd_pcm_hw_params_set_rate_near(mHandle, mHardwareParams, &exact_rate, 0) < 0) if (snd_pcm_hw_params_set_rate_near(mHandle, mHardwareParams, &exact_rate, 0) < 0)
{ {
MLOG_ERROR("Error setting rate. "); MLOG_ERROR("Error setting rate. ");
return; return;
} }
if (rate != exact_rate) { 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. */ /* Set number of periods. Periods used to be called fragments. */
if (snd_pcm_hw_params_set_periods(mHandle, mHardwareParams, device->GetPeriod(), 0) < 0) if (snd_pcm_hw_params_set_periods(mHandle, mHardwareParams, device->GetPeriod(), 0) < 0)
{ {
MLOG_ERROR("Error setting periods. "); MLOG_ERROR("Error setting periods. ");
return; return;
} }
} }
void AlsaInterface::SetBufferSize(AudioDevicePtr device) void AlsaInterface::SetBufferSize(AudioDevicePtr device)
{ {
snd_pcm_uframes_t periodsize = 8192; /* Periodsize (bytes) */ int periods = static_cast<int>(device->GetPeriod());
int periods = static_cast<int>(device->GetPeriod());
/* Set buffer size (in frames). The resulting latency is given by */ /* Set buffer size (in frames). The resulting latency is given by */
/* latency = periodsize * periods / (rate * bytes_per_frame) */ /* latency = periodsize * periods / (rate * bytes_per_frame) */
if (snd_pcm_hw_params_set_buffer_size(mHandle, mHardwareParams, (mPeriodSize * periods)>>2) < 0) if (snd_pcm_hw_params_set_buffer_size(mHandle, mHardwareParams, (mPeriodSize * periods)>>2) < 0)
{ {
MLOG_ERROR("Error setting buffersize. "); MLOG_ERROR("Error setting buffersize. ");
return; return;
} }
} }
@ -111,32 +110,31 @@ void AlsaInterface::SetChannelNumber(AudioDevicePtr device)
/* Set number of channels */ /* Set number of channels */
if (snd_pcm_hw_params_set_channels(mHandle, mHardwareParams, device->GetNumChannels()) < 0) if (snd_pcm_hw_params_set_channels(mHandle, mHardwareParams, device->GetNumChannels()) < 0)
{ {
MLOG_ERROR("Error setting channels"); MLOG_ERROR("Error setting channels");
return; return;
} }
} }
void AlsaInterface::Play(AudioDevicePtr device) void AlsaInterface::Play(AudioDevicePtr device)
{ {
MLOG_INFO("Playing audio"); MLOG_INFO("Playing audio");
int num_frames = 10;
unsigned char *data = (unsigned char *)malloc(mPeriodSize); unsigned char *data = (unsigned char *)malloc(mPeriodSize);
int frames = mPeriodSize >> 2; 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++) for(int l2 = 0; l2 < frames; l2++)
{ {
short s1 = (l2 % 128) * 100 - 5000; short s1 = (l2 % (512-count)) * 100 - 5000;
short s2 = (l2 % 256) * 100 - 5000; short s2 = (l2 % (256-count)) * 100 - 5000;
data[4*l2] = (unsigned char)s1; data[4*l2] = (unsigned char)s1;
data[4*l2+1] = s1 >> 8; data[4*l2+1] = s1 >> 8;
data[4*l2+2] = (unsigned char)s2; data[4*l2+2] = (unsigned char)s2;
data[4*l2+3] = s2 >> 8; data[4*l2+3] = s2 >> 8;
} }
while ((snd_pcm_writei(mHandle, data, frames)) < 0) while ((snd_pcm_writei(mHandle, data, frames)) < 0)
{ {
snd_pcm_prepare(mHandle); snd_pcm_prepare(mHandle);
MLOG_ERROR("<<<<<<<<<<<<<<< Buffer Underrun >>>>>>>>>>>>>>>"); MLOG_ERROR("<<<<<<<<<<<<<<< Buffer Underrun >>>>>>>>>>>>>>>");
} }
} }
} }

View file

@ -34,11 +34,11 @@ void MainApplication::RunServer()
void MainApplication::PlayAudio() void MainApplication::PlayAudio()
{ {
MidiReader reader; //MidiReader reader;
reader.Read("/home/james/sample.mid"); //reader.Read("/home/james/sample.mid");
// auto device = AudioDevice::Create(); auto device = AudioDevice::Create();
// mAudioManager->GetAudioInterface()->OpenDevice(device); mAudioManager->GetAudioInterface()->OpenDevice(device);
// mAudioManager->GetAudioInterface()->Play(device); mAudioManager->GetAudioInterface()->Play(device);
} }
void MainApplication::ShutDown() void MainApplication::ShutDown()