Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7463853

Browse files
committedJan 16, 2017
build under OSS
1 parent 99e7095 commit 7463853

File tree

2 files changed

+99
-10
lines changed

2 files changed

+99
-10
lines changed
 

‎audio_ptt.c

Lines changed: 96 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
#include <limits.h>
3636
#include <math.h>
3737
#include <pthread.h>
38+
#include <stdio.h>
39+
#include <unistd.h>
40+
#include <stdlib.h>
41+
#include <sys/types.h>
42+
#include <sys/stat.h>
43+
#include <sys/ioctl.h>
44+
#include <fcntl.h>
3845

3946
#if USE_ALSA
4047
#include <alsa/asoundlib.h>
@@ -89,11 +96,11 @@ static void * ptt_thread (void *arg)
8996
snd_pcm_t *handle;
9097
int err;
9198

92-
err = snd_pcm_open(&handle, save_audio_config_p->adev[a].adevice_out, SND_PCM_STREAM_PLAYBACK, 0);
99+
err = snd_pcm_open (&handle, save_audio_config_p->adev[a].adevice_out, SND_PCM_STREAM_PLAYBACK, 0);
93100
if (err == 0) {
94101
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
95102

96-
err = snd_pcm_set_params(handle, format, SND_PCM_ACCESS_RW_INTERLEAVED,
103+
err = snd_pcm_set_params (handle, format, SND_PCM_ACCESS_RW_INTERLEAVED,
97104
save_audio_config_p->adev[a].num_channels,
98105
save_audio_config_p->adev[a].samples_per_sec, 1, 500000);
99106
if (err == 0) {
@@ -136,8 +143,8 @@ static void * ptt_thread (void *arg)
136143
}
137144
}
138145

139-
if (ptt_state == PTT_AUDIO_STATE_START) {
140-
snd_pcm_writei (handle, pnData, nSamples);
146+
if (ptt_state == PTT_AUDIO_STATE_START) {
147+
snd_pcm_writei (handle, pnData, nSamples);
141148
}
142149
else if (ptt_state == PTT_AUDIO_STATE_CLOSE) {
143150
snd_pcm_drop (handle);
@@ -147,15 +154,97 @@ static void * ptt_thread (void *arg)
147154
}
148155

149156
free (pnData);
150-
}
157+
} else {
158+
dw_printf("Failed to configure ALSA device. PTT tone will not be enabled.\n");
159+
}
151160
snd_pcm_close (handle);
152-
}
161+
} else {
162+
dw_printf("Failed to open ALSA device. PTT tone will not be enabled.\n");
163+
}
153164
#else
154165
int oss_audio_device_fd;
155166

156167
oss_audio_device_fd = open (save_audio_config_p->adev[a].adevice_out, O_WRONLY);
157-
if (oss_audio_device_fd != -1) {
168+
if (oss_audio_device_fd >= 0) {
169+
int devcaps;
170+
int num_channels;
171+
int samples_per_sec;
172+
int bits_per_sample;
173+
int err;
174+
175+
num_channels = save_audio_config_p->adev[a].num_channels;
176+
err = ioctl (oss_audio_device_fd, SNDCTL_DSP_CHANNELS, &num_channels);
177+
178+
if (err != -1) {
179+
samples_per_sec = save_audio_config_p->adev[a].samples_per_sec;
180+
err = ioctl (oss_audio_device_fd, SNDCTL_DSP_SPEED, &samples_per_sec);
181+
}
182+
183+
if (err != -1) {
184+
bits_per_sample = save_audio_config_p->adev[a].bits_per_sample;
185+
err = ioctl (oss_audio_device_fd, SNDCTL_DSP_SETFMT, &bits_per_sample);
186+
}
187+
188+
if (err != -1) {
189+
err = ioctl (oss_audio_device_fd, SNDCTL_DSP_GETCAPS, &devcaps);
190+
}
191+
192+
if (err != -1) {
193+
short* pnData;
194+
short sample;
195+
int nBufferLength;
196+
int nSamples;
197+
int written;
198+
int i;
199+
int j;
200+
201+
nSamples = samples_per_sec / 5;
202+
nBufferLength = num_channels * nSamples * sizeof(short);
203+
pnData = (short*)malloc (nBufferLength);
204+
205+
for (i = 0; i < nSamples; i++) {
206+
sample = (short)( (double)SHRT_MAX * sin( ( (double)i * freq / (double)samples_per_sec ) * 2.0 * M_PI ) );
207+
208+
for (j = 0; j < num_channels; j++) {
209+
if (channel == ADEVFIRSTCHAN( a ) + j) {
210+
pnData[i*num_channels + j] = sample;
211+
} else {
212+
pnData[i*num_channels + j] = 0;
213+
}
214+
}
215+
}
216+
217+
while (1) {
218+
pthread_mutex_lock (&save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_mutex);
219+
ptt_audio_state_t ptt_state = save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_state;
220+
pthread_mutex_unlock (&save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_mutex);
221+
222+
if (ptt_state == PTT_AUDIO_STATE_STOP) {
223+
ioctl (oss_audio_device_fd, SNDCTL_DSP_RESET, NULL);
224+
225+
pthread_mutex_lock (&save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_mutex);
226+
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);
227+
ptt_state = save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_state;
228+
pthread_mutex_unlock (&save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_mutex);
229+
}
230+
231+
if (ptt_state == PTT_AUDIO_STATE_START) {
232+
written = write (oss_audio_device_fd, pnData, nBufferLength);
233+
}
234+
else if (ptt_state == PTT_AUDIO_STATE_CLOSE) {
235+
ioctl (oss_audio_device_fd, SNDCTL_DSP_RESET, NULL);
236+
237+
break;
238+
}
239+
}
158240

241+
free (pnData);
242+
} else {
243+
dw_printf("Failed to configure OSS device. PTT tone will not be enabled.\n");
244+
}
245+
close (oss_audio_device_fd);
246+
} else {
247+
dw_printf("Failed to open OSS device. PTT tone will not be enabled.\n");
159248
}
160249
#endif
161250
}

‎audio_ptt_win.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ unsigned __stdcall ptt_thread (void *arg)
149149

150150
waveOutReset ( hWaveOut );
151151
}
152-
else if( dwWait == WAIT_OBJECT_0 + 2 ) {
152+
else if (dwWait == WAIT_OBJECT_0 + 2) {
153153
//
154154
// close
155155
//
156156

157-
waveOutReset ( hWaveOut );
158-
waveOutUnprepareHeader ( hWaveOut, &waveHeader, sizeof( WAVEHDR ) );
157+
waveOutReset (hWaveOut);
158+
waveOutUnprepareHeader (hWaveOut, &waveHeader, sizeof(WAVEHDR));
159159

160160
break;
161161
}

0 commit comments

Comments
 (0)
Please sign in to comment.