build under ALSA

This commit is contained in:
Andrew 2017-01-15 13:42:52 -08:00
parent 52bf2ff5bd
commit 99e7095f17
5 changed files with 169 additions and 167 deletions

10
audio.h
View File

@ -35,6 +35,12 @@ enum ptt_method_e {
typedef enum ptt_method_e ptt_method_t; typedef enum ptt_method_e ptt_method_t;
enum ptt_audio_state_e {
PTT_AUDIO_STATE_STOP,
PTT_AUDIO_STATE_START,
PTT_AUDIO_STATE_CLOSE };
typedef enum ptt_audio_state_e ptt_audio_state_t;
enum ptt_line_e { PTT_LINE_NONE = 0, PTT_LINE_RTS = 1, PTT_LINE_DTR = 2 }; // Important: 0 for neither. enum ptt_line_e { PTT_LINE_NONE = 0, PTT_LINE_RTS = 1, PTT_LINE_DTR = 2 }; // Important: 0 for neither.
typedef enum ptt_line_e ptt_line_t; typedef enum ptt_line_e ptt_line_t;
@ -203,7 +209,9 @@ struct audio_s {
HANDLE ptt_stop; /* Handle for event that stops ptt tone. */ HANDLE ptt_stop; /* Handle for event that stops ptt tone. */
HANDLE ptt_close; /* Handle for event that closes ptt. */ HANDLE ptt_close; /* Handle for event that closes ptt. */
#else #else
pthread_mutex_t ptt_mutex; /* Mutex for controlling ptt tone state. */
pthread_cond_t ptt_condition; /* Condition for controlling ptt tone state. */
ptt_audio_state_t ptt_state; /* State of ptt tone. */
#endif #endif
#ifdef USE_HAMLIB #ifdef USE_HAMLIB

View File

@ -91,7 +91,6 @@ static void * ptt_thread (void *arg)
err = snd_pcm_open(&handle, save_audio_config_p->adev[a].adevice_out, SND_PCM_STREAM_PLAYBACK, 0); err = snd_pcm_open(&handle, save_audio_config_p->adev[a].adevice_out, SND_PCM_STREAM_PLAYBACK, 0);
if (err == 0) { if (err == 0) {
snd_pcm_sframes_t frames;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE; snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
err = snd_pcm_set_params(handle, format, SND_PCM_ACCESS_RW_INTERLEAVED, err = snd_pcm_set_params(handle, format, SND_PCM_ACCESS_RW_INTERLEAVED,
@ -100,58 +99,56 @@ static void * ptt_thread (void *arg)
if (err == 0) { if (err == 0) {
short* pnData; short* pnData;
short sample; short sample;
int nSamples = save_audio_config_p->adev[a].samples_per_sec / 10; int nSamples = save_audio_config_p->adev[a].samples_per_sec / 5;
int nBufferLength = save_audio_config_p->adev[a].num_channels * nSamples * sizeof(short); int nBufferLength = save_audio_config_p->adev[a].num_channels * nSamples * sizeof(short);
int i; int i;
int j;
pnData = (short*)malloc (nBufferLength); pnData = (short*)malloc (nBufferLength);
if (save_audio_config_p->adev[a].num_channels == 1) {
for (i = 0; i < nSamples; i++) { for (i = 0; i < nSamples; i++) {
sample = (short)( (double)SHRT_MAX * sin( ( (double)i * freq / (double)save_audio_config_p->adev[a].samples_per_sec ) * 2.0 * M_PI ) ); sample = (short)( (double)SHRT_MAX * sin( ( (double)i * freq / (double)save_audio_config_p->adev[a].samples_per_sec ) * 2.0 * M_PI ) );
pnData[i] = sample;
}
}
else {
for (i = 0; i < nSamples; i++) {
sample = (short)( (double)SHRT_MAX * sin( ( (double)i * freq / (double)save_audio_config_p->adev[a].samples_per_sec ) * 2.0 * M_PI ) );
if (channel == ADEVFIRSTCHAN( a )) {
// Stereo, left channel. for (j = 0; j < save_audio_config_p->adev[a].num_channels; j++) {
if (channel == ADEVFIRSTCHAN( a ) + j) {
pnData[i*2 + 0] = sample; pnData[i*save_audio_config_p->adev[a].num_channels + j] = sample;
pnData[i*2 + 1] = 0; } else {
} pnData[i*save_audio_config_p->adev[a].num_channels + j] = 0;
else {
// Stereo, right channel.
pnData[i*2 + 0] = 0;
pnData[i*2 + 1] = sample;
} }
} }
} }
// while (1) {
// ptt_set on pthread_mutex_lock (&save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_mutex);
// ptt_audio_state_t ptt_state = save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_state;
pthread_mutex_unlock (&save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_mutex);
for (i=0; i<50; i++) { if (ptt_state == PTT_AUDIO_STATE_STOP) {
frames = snd_pcm_writei(handle, pnData, nSamples); snd_pcm_drop (handle);
pthread_mutex_lock (&save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_mutex);
pthread_cond_wait (&save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_condition, &save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_mutex);
ptt_state = save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_state;
pthread_mutex_unlock (&save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_mutex);
if (ptt_state == PTT_AUDIO_STATE_START) {
snd_pcm_prepare (handle);
}
} }
// if (ptt_state == PTT_AUDIO_STATE_START) {
// ptt_set off snd_pcm_writei (handle, pnData, nSamples);
// }
else if (ptt_state == PTT_AUDIO_STATE_CLOSE) {
snd_pcm_drop (handle);
// break;
// close }
// }
free (pnData); free (pnData);
} }
snd_pcm_close (handle);
snd_pcm_close(handle);
} }
#else #else
int oss_audio_device_fd; int oss_audio_device_fd;
@ -165,3 +162,4 @@ static void * ptt_thread (void *arg)
} }
#endif #endif

View File

@ -79,7 +79,7 @@ unsigned __stdcall ptt_thread (void *arg)
int a = ACHAN2ADEV( channel ); int a = ACHAN2ADEV( channel );
int err; int err;
if( save_audio_config_p->adev[a].defined ) { if (save_audio_config_p->adev[a].defined) {
wf.wFormatTag = WAVE_FORMAT_PCM; wf.wFormatTag = WAVE_FORMAT_PCM;
wf.nChannels = save_audio_config_p->adev[a].num_channels; wf.nChannels = save_audio_config_p->adev[a].num_channels;
wf.nSamplesPerSec = save_audio_config_p->adev[a].samples_per_sec; wf.nSamplesPerSec = save_audio_config_p->adev[a].samples_per_sec;
@ -94,12 +94,13 @@ unsigned __stdcall ptt_thread (void *arg)
*/ */
err = waveOutOpen ( &hWaveOut, atoi( save_audio_config_p->adev[a].adevice_out ), &wf, (DWORD_PTR)NULL, 0, CALLBACK_NULL ); err = waveOutOpen ( &hWaveOut, atoi( save_audio_config_p->adev[a].adevice_out ), &wf, (DWORD_PTR)NULL, 0, CALLBACK_NULL );
if( err == MMSYSERR_NOERROR ) { if (err == MMSYSERR_NOERROR) {
WAVEHDR waveHeader; WAVEHDR waveHeader;
short* pnData; short* pnData;
short sample; short sample;
int nSamples = save_audio_config_p->adev[a].samples_per_sec / freq; int nSamples = save_audio_config_p->adev[a].samples_per_sec / freq;
int i; int i;
int j;
waveHeader.dwBufferLength = save_audio_config_p->adev[a].num_channels * nSamples * sizeof( short ); waveHeader.dwBufferLength = save_audio_config_p->adev[a].num_channels * nSamples * sizeof( short );
waveHeader.lpData = malloc( waveHeader.dwBufferLength ); waveHeader.lpData = malloc( waveHeader.dwBufferLength );
@ -109,34 +110,20 @@ unsigned __stdcall ptt_thread (void *arg)
pnData = (short*)waveHeader.lpData; pnData = (short*)waveHeader.lpData;
if( save_audio_config_p->adev[a].num_channels == 1 ) { for (i = 0; i < nSamples; i++) {
for( i = 0; i < nSamples; i++ ) {
sample = (short)( (double)SHRT_MAX * sin( ( (double)i / (double)nSamples ) * 2.0 * M_PI ) ); sample = (short)( (double)SHRT_MAX * sin( ( (double)i / (double)nSamples ) * 2.0 * M_PI ) );
pnData[i] = sample;
}
}
else {
for( i = 0; i < nSamples; i++ ) {
sample = (short)( (double)SHRT_MAX * sin( ( (double)i / (double)nSamples ) * 2.0 * M_PI ) );
if( channel == ADEVFIRSTCHAN( a ) ) {
// Stereo, left channel. for (j = 0; j < save_audio_config_p->adev[a].num_channels; j++) {
if (channel == ADEVFIRSTCHAN( a ) + j) {
pnData[i*2 + 0] = sample; pnData[i*save_audio_config_p->adev[a].num_channels + j] = sample;
pnData[i*2 + 1] = 0; } else {
} pnData[i*save_audio_config_p->adev[a].num_channels + j] = 0;
else {
// Stereo, right channel.
pnData[i*2 + 0] = 0;
pnData[i*2 + 1] = sample;
} }
} }
} }
err = waveOutPrepareHeader ( hWaveOut, &waveHeader, sizeof( WAVEHDR ) ); err = waveOutPrepareHeader (hWaveOut, &waveHeader, sizeof( WAVEHDR ));
if( err == MMSYSERR_NOERROR ) { if (err == MMSYSERR_NOERROR) {
HANDLE handles[3]; HANDLE handles[3];
DWORD dwWait; DWORD dwWait;
@ -144,28 +131,25 @@ unsigned __stdcall ptt_thread (void *arg)
handles[1] = save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_stop; handles[1] = save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_stop;
handles[2] = save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_close; handles[2] = save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_close;
while( 1 ) while (1)
{ {
dwWait = WaitForMultipleObjects ( 3, handles, FALSE, INFINITE ); dwWait = WaitForMultipleObjects (3, handles, FALSE, INFINITE);
if( dwWait == WAIT_OBJECT_0 + 0 ) if (dwWait == WAIT_OBJECT_0 + 0) {
{
// //
// ptt_set on // ptt_set on
// //
waveOutWrite ( hWaveOut, &waveHeader, sizeof( WAVEHDR ) ); waveOutWrite ( hWaveOut, &waveHeader, sizeof( WAVEHDR ) );
} }
else if( dwWait == WAIT_OBJECT_0 + 1 ) else if (dwWait == WAIT_OBJECT_0 + 1) {
{
// //
// ptt_set off // ptt_set off
// //
waveOutReset ( hWaveOut ); waveOutReset ( hWaveOut );
} }
else if( dwWait == WAIT_OBJECT_0 + 2 ) else if( dwWait == WAIT_OBJECT_0 + 2 ) {
{
// //
// close // close
// //

View File

@ -673,6 +673,7 @@ void config_init (char *fname, struct audio_s *p_audio_config,
p_audio_config->achan[channel].octrl[ot].ptt_stop = NULL; p_audio_config->achan[channel].octrl[ot].ptt_stop = NULL;
p_audio_config->achan[channel].octrl[ot].ptt_close = NULL; p_audio_config->achan[channel].octrl[ot].ptt_close = NULL;
#else #else
p_audio_config->achan[channel].octrl[ot].ptt_state = PTT_AUDIO_STATE_STOP;
#endif #endif
} }
@ -1635,6 +1636,9 @@ void config_init (char *fname, struct audio_s *p_audio_config,
p_audio_config->achan[channel].octrl[ot].ptt_stop = CreateEvent (NULL, FALSE, FALSE, NULL); p_audio_config->achan[channel].octrl[ot].ptt_stop = CreateEvent (NULL, FALSE, FALSE, NULL);
p_audio_config->achan[channel].octrl[ot].ptt_close = CreateEvent (NULL, FALSE, FALSE, NULL); p_audio_config->achan[channel].octrl[ot].ptt_close = CreateEvent (NULL, FALSE, FALSE, NULL);
#else #else
p_audio_config->achan[channel].octrl[ot].ptt_state = PTT_AUDIO_STATE_STOP;
pthread_mutex_init(&p_audio_config->achan[channel].octrl[ot].ptt_mutex, 0);
pthread_cond_init(&p_audio_config->achan[channel].octrl[ot].ptt_condition, 0);
#endif #endif
} }
else { else {

22
ptt.c
View File

@ -1010,20 +1010,25 @@ void ptt_set (int ot, int chan, int ptt_signal)
* Using audio channel? * Using audio channel?
*/ */
if( save_audio_config_p->achan[chan].octrl[ot].ptt_method == PTT_METHOD_AUDIO ) { if (save_audio_config_p->achan[chan].octrl[ot].ptt_method == PTT_METHOD_AUDIO) {
if( ptt_signal ) { if (ptt_signal) {
#ifdef __WIN32__ #ifdef __WIN32__
SetEvent( save_audio_config_p->achan[chan].octrl[ot].ptt_start ); SetEvent (save_audio_config_p->achan[chan].octrl[ot].ptt_start);
#else #else
pthread_mutex_lock (&save_audio_config_p->achan[chan].octrl[ot].ptt_mutex);
save_audio_config_p->achan[chan].octrl[ot].ptt_state = PTT_AUDIO_STATE_START;
pthread_cond_signal (&save_audio_config_p->achan[chan].octrl[ot].ptt_condition);
pthread_mutex_unlock (&save_audio_config_p->achan[chan].octrl[ot].ptt_mutex);
#endif #endif
} }
else else
{ {
#ifdef __WIN32__ #ifdef __WIN32__
SetEvent( save_audio_config_p->achan[chan].octrl[ot].ptt_stop ); SetEvent (save_audio_config_p->achan[chan].octrl[ot].ptt_stop);
#else #else
pthread_mutex_lock (&save_audio_config_p->achan[chan].octrl[ot].ptt_mutex);
save_audio_config_p->achan[chan].octrl[ot].ptt_state = PTT_AUDIO_STATE_STOP;
pthread_mutex_unlock (&save_audio_config_p->achan[chan].octrl[ot].ptt_mutex);
#endif #endif
} }
} }
@ -1137,7 +1142,10 @@ void ptt_term (void)
#ifdef __WIN32__ #ifdef __WIN32__
SetEvent (save_audio_config_p->achan[n].octrl[OCTYPE_PTT].ptt_close) ; SetEvent (save_audio_config_p->achan[n].octrl[OCTYPE_PTT].ptt_close) ;
#else #else
pthread_mutex_lock (&save_audio_config_p->achan[n].octrl[OCTYPE_PTT].ptt_mutex);
save_audio_config_p->achan[n].octrl[OCTYPE_PTT].ptt_state = PTT_AUDIO_STATE_CLOSE;
pthread_cond_signal (&save_audio_config_p->achan[n].octrl[OCTYPE_PTT].ptt_condition);
pthread_mutex_unlock (&save_audio_config_p->achan[n].octrl[OCTYPE_PTT].ptt_mutex);
#endif #endif
} }
} }