2009-02-06 19:52:59 +00:00
|
|
|
/* ZeroSPU2
|
2010-01-16 16:08:17 +00:00
|
|
|
* Copyright (C) 2006-2010 zerofrog
|
2009-02-06 19:52:59 +00:00
|
|
|
*
|
|
|
|
* 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
|
2010-07-04 22:49:00 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
2009-02-06 19:52:59 +00:00
|
|
|
*/
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-06 19:52:59 +00:00
|
|
|
// Modified by arcum42@gmail.com
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-06 19:52:59 +00:00
|
|
|
// ALSA
|
2010-01-16 16:08:17 +00:00
|
|
|
#include "Alsa.h"
|
2009-02-06 19:52:59 +00:00
|
|
|
#include "Linux.h"
|
|
|
|
|
|
|
|
static snd_pcm_t *handle = NULL;
|
|
|
|
static snd_pcm_uframes_t buffer_size;
|
|
|
|
|
|
|
|
int AlsaSetupSound()
|
|
|
|
{
|
|
|
|
snd_pcm_hw_params_t *hwparams;
|
|
|
|
snd_pcm_sw_params_t *swparams;
|
|
|
|
snd_pcm_status_t *status;
|
|
|
|
unsigned int pspeed = SAMPLE_RATE;
|
|
|
|
int pchannels = 2;
|
|
|
|
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
|
|
|
|
unsigned int buffer_time = SOUNDSIZE;
|
|
|
|
unsigned int period_time= buffer_time / 4;
|
|
|
|
int err;
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-06 19:52:59 +00:00
|
|
|
err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
|
2010-04-25 00:31:27 +00:00
|
|
|
if(err < 0)
|
2009-02-20 03:22:19 +00:00
|
|
|
{
|
|
|
|
ERROR_LOG("Audio open error: %s\n", snd_strerror(err));
|
2009-02-06 19:52:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-06 19:52:59 +00:00
|
|
|
err = snd_pcm_nonblock(handle, 0);
|
2010-04-25 00:31:27 +00:00
|
|
|
if(err < 0)
|
2009-02-20 03:22:19 +00:00
|
|
|
{
|
|
|
|
ERROR_LOG("Can't set blocking mode: %s\n", snd_strerror(err));
|
2009-02-06 19:52:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-06 19:52:59 +00:00
|
|
|
snd_pcm_hw_params_alloca(&hwparams);
|
|
|
|
snd_pcm_sw_params_alloca(&swparams);
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-06 19:52:59 +00:00
|
|
|
err = snd_pcm_hw_params_any(handle, hwparams);
|
2010-04-25 00:31:27 +00:00
|
|
|
if (err < 0)
|
2009-02-20 03:22:19 +00:00
|
|
|
{
|
|
|
|
ERROR_LOG("Broken configuration for this PCM: %s\n", snd_strerror(err));
|
2009-02-06 19:52:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-06 19:52:59 +00:00
|
|
|
err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
|
2010-04-25 00:31:27 +00:00
|
|
|
if (err < 0)
|
2009-02-20 03:22:19 +00:00
|
|
|
{
|
|
|
|
ERROR_LOG("Access type not available: %s\n", snd_strerror(err));
|
2009-02-06 19:52:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_pcm_hw_params_set_format(handle, hwparams, format);
|
2010-04-25 00:31:27 +00:00
|
|
|
if (err < 0)
|
2009-02-20 03:22:19 +00:00
|
|
|
{
|
|
|
|
ERROR_LOG("Sample format not available: %s\n", snd_strerror(err));
|
2009-02-06 19:52:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-06 19:52:59 +00:00
|
|
|
err = snd_pcm_hw_params_set_channels(handle, hwparams, pchannels);
|
2010-04-25 00:31:27 +00:00
|
|
|
if (err < 0)
|
2009-02-20 03:22:19 +00:00
|
|
|
{
|
|
|
|
ERROR_LOG("Channels count not available: %s\n", snd_strerror(err));
|
2009-02-06 19:52:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &pspeed, 0);
|
2010-04-25 00:31:27 +00:00
|
|
|
if (err < 0)
|
2009-02-20 03:22:19 +00:00
|
|
|
{
|
|
|
|
ERROR_LOG("Rate not available: %s\n", snd_strerror(err));
|
2009-02-06 19:52:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-06 19:52:59 +00:00
|
|
|
err = snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, 0);
|
|
|
|
if(err < 0) {
|
2009-02-20 03:22:19 +00:00
|
|
|
ERROR_LOG("Buffer time error: %s\n", snd_strerror(err));
|
2009-02-06 19:52:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-06 19:52:59 +00:00
|
|
|
err = snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, 0);
|
2009-02-20 03:22:19 +00:00
|
|
|
if (err < 0)
|
|
|
|
{
|
|
|
|
ERROR_LOG("Period time error: %s\n", snd_strerror(err));
|
2009-02-06 19:52:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-06 19:52:59 +00:00
|
|
|
err = snd_pcm_hw_params(handle, hwparams);
|
2010-04-25 00:31:27 +00:00
|
|
|
if (err < 0)
|
2009-02-20 03:22:19 +00:00
|
|
|
{
|
|
|
|
ERROR_LOG("Unable to install hw params: %s\n", snd_strerror(err));
|
2009-02-06 19:52:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-06 19:52:59 +00:00
|
|
|
snd_pcm_status_alloca(&status);
|
|
|
|
err = snd_pcm_status(handle, status);
|
2010-04-25 00:31:27 +00:00
|
|
|
if(err < 0)
|
2009-02-20 03:22:19 +00:00
|
|
|
{
|
|
|
|
ERROR_LOG("Unable to get status: %s\n", snd_strerror(err));
|
2009-02-06 19:52:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-06 19:52:59 +00:00
|
|
|
buffer_size=snd_pcm_status_get_avail(status);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlsaRemoveSound()
|
|
|
|
{
|
|
|
|
if(handle != NULL) {
|
|
|
|
snd_pcm_drop(handle);
|
|
|
|
snd_pcm_close(handle);
|
|
|
|
handle = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int AlsaSoundGetBytesBuffered()
|
|
|
|
{
|
2010-04-25 00:31:27 +00:00
|
|
|
unsigned long l = snd_pcm_avail_update(handle);
|
|
|
|
if (MaxBuffer == 0) MaxBuffer = l;
|
|
|
|
return MaxBuffer - l;
|
2010-01-16 16:08:17 +00:00
|
|
|
/*int l;
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-06 19:52:59 +00:00
|
|
|
// failed to open?
|
2010-04-25 00:31:27 +00:00
|
|
|
if(handle == NULL) return SOUNDSIZE;
|
|
|
|
|
|
|
|
l = snd_pcm_avail_update(handle);
|
|
|
|
|
|
|
|
if (l<0)
|
2009-02-06 19:52:59 +00:00
|
|
|
return 0;
|
|
|
|
else if (l<buffer_size/2) // can we write in at least the half of fragments?
|
|
|
|
l=SOUNDSIZE; // -> no? wait
|
2010-04-25 00:31:27 +00:00
|
|
|
else
|
2009-02-06 19:52:59 +00:00
|
|
|
l=0; // -> else go on
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-01-16 16:08:17 +00:00
|
|
|
return l;*/
|
2009-02-06 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlsaSoundFeedVoiceData(unsigned char* pSound,long lBytes)
|
|
|
|
{
|
|
|
|
if (handle == NULL) return;
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-06 19:52:59 +00:00
|
|
|
if (snd_pcm_state(handle) == SND_PCM_STATE_XRUN)
|
|
|
|
snd_pcm_prepare(handle);
|
|
|
|
snd_pcm_writei(handle,pSound, lBytes/4);
|
|
|
|
}
|