/***************************************************************************
* Copyright (C) 2006-2021 by Ilya Kotov *
* forkotov02@ya.ru *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <QSettings>
#include <QSocketNotifier>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <qmmp/buffer.h>
#include <qmmp/visual.h>
#include <qmmp/statehandler.h>
#include "outputalsa.h"
OutputALSA::OutputALSA()
{
QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
QString dev_name = settings.value("ALSA/device","default").toString();
m_use_mmap = settings.value("ALSA/use_mmap", false).toBool();
pcm_name = strdup(dev_name.toLatin1().data());
m_alsa_channels = {
{ SND_CHMAP_NA, Qmmp::CHAN_NULL },
{ SND_CHMAP_MONO, Qmmp::CHAN_FRONT_CENTER },
{ SND_CHMAP_FL, Qmmp::CHAN_FRONT_LEFT },
{ SND_CHMAP_FR, Qmmp::CHAN_FRONT_RIGHT },
{ SND_CHMAP_RL, Qmmp::CHAN_REAR_LEFT },
{ SND_CHMAP_RR, Qmmp::CHAN_REAR_RIGHT },
{ SND_CHMAP_FC, Qmmp::CHAN_FRONT_CENTER },
{ SND_CHMAP_LFE, Qmmp::CHAN_LFE },
{ SND_CHMAP_SL, Qmmp::CHAN_SIDE_LEFT },
{ SND_CHMAP_SR, Qmmp::CHAN_SIDE_RIGHT },
{ SND_CHMAP_RC, Qmmp::CHAN_REAR_CENTER }
};
}
OutputALSA::~OutputALSA()
{
uninitialize();
free (pcm_name);
}
bool OutputALSA::initialize(quint32 freq, ChannelMap map, Qmmp::AudioFormat format)
{
m_inited = false;
if (pcm_handle)
return false;
if (snd_pcm_open(&pcm_handle, pcm_name, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0)
{
qWarning ("OutputALSA: Error opening PCM device %s", pcm_name);
return false;
}
// we need to configure
uint rate = freq; /* Sample rate */
uint exact_rate = freq; /* Sample rate returned by */
/* load settings from config */
QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
settings.beginGroup("ALSA");
uint buffer_time = settings.value("buffer_time",500).toUInt()*1000;
uint period_time = settings.value("period_time",100).toUInt()*1000;
bool use_pause = settings.value("use_snd_pcm_pause", false).toBool();
settings.endGroup();
snd_pcm_hw_params_t *hwparams = nullptr;
snd_pcm_sw_params_t *swparams = nullptr;
int err; //alsa error code
//hw params
snd_pcm_hw_params_alloca(&hwparams);
if ((err = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0)
{
qWarning("OutputALSA: Can not read configuration for PCM device: %s", snd_strerror(err));
return false;
}
if (m_use_mmap)
{
if ((err = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0)
{
qWarning("OutputALSA: Error setting mmap access: %s", snd_strerror(err));
m_use_mmap = false;
}
}
if (!m_use_mmap)
{
if ((err = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
{
qWarning("OutputALSA: Error setting access: %s", snd_strerror(err));
return false;
}
}
snd_pcm_format_t alsa_format = SND_PCM_FORMAT_UNKNOWN;
switch (format)
{
case Qmmp::PCM_S8:
alsa_format = SND_PCM_FORMAT_S8;
break;
case Qmmp::PCM_S16LE:
alsa_format = SND_PCM_FORMAT_S16_LE;
break;
case Qmmp::PCM_S24LE:
alsa_format = SND_PCM_FORMAT_S24_LE;
break;
case Qmmp::PCM_S32LE:
alsa_format = SND_PCM_FORMAT_S32_LE;
break;
case Qmmp::PCM_FLOAT:
alsa_format = SND_PCM_FORMAT_FLOAT;
break;
default:
qWarning("OutputALSA: unsupported format detected");
return false;
}
if ((err = snd_pcm_hw_params_set_format(pcm_handle, hwparams, alsa_format)) < 0)
{
qDebug("OutputALSA: Error setting format: %s", snd_strerror(err));
return false;
}
exact_rate = rate;
if ((err = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &exact_rate, nullptr)) < 0)
{
qWarning("OutputALSA: Error setting rate: %s", snd_strerror(err));
return false;
}
if (rate != exact_rate)
{
qWarning("OutputALSA: The rate %d Hz is not supported by your hardware.\n==> Using %d Hz instead.", rate, exact_rate);
rate = exact_rate;
}
uint c = map.count();
if ((err = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0)
{
qWarning("OutputALSA: Error setting channels: %s", snd_strerror(err));
return false;
}
if (c != (uint)map.count())
{
qWarning("OutputALSA: The channel number %d is not supported by your hardware", map.count());
qWarning("==> Using %d instead.", c);
}
if ((err = snd_pcm_hw_params_set_period_time_near(pcm_handle, hwparams, &period_time, nullptr)) < 0)
{
qWarning("OutputALSA: Error setting period time: %s", snd_strerror(err));
return false;
}
if ((err = snd_pcm_hw_params_set_buffer_time_near(pcm_handle, hwparams, &buffer_time, nullptr)) < 0)
{
qWarning("OutputALSA: Error setting buffer time: %s", snd_strerror(err));
return false;
}
if ((err = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
{
qWarning("OutputALSA: Error setting HW params: %s", snd_strerror(err));
return false;
}
//read some alsa parameters
snd_pcm_uframes_t buffer_size = 0;
snd_pcm_uframes_t period_size = 0;
if ((err = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size)) < 0)
{
qWarning("OutputALSA: Error reading buffer size: %s", snd_strerror(err));
return false;