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 52bf2ff

Browse files
committedJan 14, 2017
build under linux and start ALSA implementation
1 parent aa5176e commit 52bf2ff

File tree

4 files changed

+160
-20
lines changed

4 files changed

+160
-20
lines changed
 

‎Makefile.linux

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ direwolf : direwolf.o config.o recv.o demod.o dsp.o demod_afsk.o demod_9600.o hd
232232
hdlc_rec2.o multi_modem.o redecode.o rdq.o rrbb.o dlq.o \
233233
fcs_calc.o ax25_pad.o \
234234
decode_aprs.o symbols.o server.o kiss.o kissnet.o kiss_frame.o hdlc_send.o fcs_calc.o \
235-
gen_tone.o audio.o audio_stats.o digipeater.o pfilter.o dedupe.o tq.o xmit.o morse.o \
235+
gen_tone.o audio.o audio_ptt.o audio_stats.o digipeater.o pfilter.o dedupe.o tq.o xmit.o morse.o \
236236
ptt.o beacon.o encode_aprs.o latlong.o encode_aprs.o latlong.o textcolor.o \
237237
dtmf.o aprs_tt.o tt_user.o tt_text.o igate.o nmea.o serial_port.o log.o telemetry.o \
238238
dwgps.o dwgpsnmea.o dwgpsd.o dtime_now.o \

‎audio_ptt.c

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// This file is part of Dire Wolf, an amateur radio packet TNC.
33
//
44
// Copyright (C) 2011, 2012, 2013, 2014, 2015 John Langner, WB2OSZ
5+
// Copyright (C) 2017 Andrew Walker, VA7YAA
56
//
67
// This program is free software: you can redistribute it and/or modify
78
// it under the terms of the GNU General Public License as published by
@@ -18,3 +19,149 @@
1819
//
1920

2021

22+
/*------------------------------------------------------------------
23+
*
24+
* Module: audio_ptt.c
25+
*
26+
* Purpose: Interface to audio device commonly called a "sound card" for
27+
* historical reasons.
28+
*
29+
* This version uses the native Windows sound interface.
30+
*
31+
*---------------------------------------------------------------*/
32+
33+
#if __WIN32__
34+
#else
35+
#include <limits.h>
36+
#include <math.h>
37+
#include <pthread.h>
38+
39+
#if USE_ALSA
40+
#include <alsa/asoundlib.h>
41+
#else
42+
#include <errno.h>
43+
#ifdef __OpenBSD__
44+
#include <soundcard.h>
45+
#else
46+
#include <sys/soundcard.h>
47+
#endif
48+
#endif
49+
50+
#include "direwolf.h"
51+
#include "audio.h"
52+
#include "audio_stats.h"
53+
#include "textcolor.h"
54+
#include "ptt.h"
55+
#include "audio_ptt.h"
56+
57+
#if USE_ALSA
58+
static int set_alsa_params (int a, snd_pcm_t *handle, struct audio_s *pa, char *name, char *dir);
59+
//static void alsa_select_device (char *pick_dev, int direction, char *result);
60+
#else
61+
static int set_oss_params (int a, int fd, struct audio_s *pa);
62+
#endif
63+
64+
static struct audio_s *save_audio_config_p;
65+
66+
static void * ptt_thread (void *arg);
67+
68+
int start_ptt_thread (struct audio_s *pa, int ch)
69+
{
70+
pthread_t tid = 0;
71+
int e;
72+
73+
save_audio_config_p = pa;
74+
75+
e = pthread_create (&tid, NULL, ptt_thread, (void*)(long)ch);
76+
77+
return tid;
78+
}
79+
80+
static void * ptt_thread (void *arg)
81+
{
82+
int ch = (int)(long)arg; // channel number.
83+
int channel = save_audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_channel;
84+
int freq = save_audio_config_p->achan[channel].octrl[OCTYPE_PTT].ptt_frequency;
85+
int a = ACHAN2ADEV( channel );
86+
87+
if( save_audio_config_p->adev[a].defined ) {
88+
#if USE_ALSA
89+
snd_pcm_t *handle;
90+
int err;
91+
92+
err = snd_pcm_open(&handle, save_audio_config_p->adev[a].adevice_out, SND_PCM_STREAM_PLAYBACK, 0);
93+
if (err == 0) {
94+
snd_pcm_sframes_t frames;
95+
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
96+
97+
err = snd_pcm_set_params(handle, format, SND_PCM_ACCESS_RW_INTERLEAVED,
98+
save_audio_config_p->adev[a].num_channels,
99+
save_audio_config_p->adev[a].samples_per_sec, 1, 500000);
100+
if (err == 0) {
101+
short* pnData;
102+
short sample;
103+
int nSamples = save_audio_config_p->adev[a].samples_per_sec / 10;
104+
int nBufferLength = save_audio_config_p->adev[a].num_channels * nSamples * sizeof(short);
105+
int i;
106+
107+
pnData = (short*)malloc (nBufferLength);
108+
109+
if (save_audio_config_p->adev[a].num_channels == 1) {
110+
for (i = 0; i < nSamples; i++) {
111+
sample = (short)( (double)SHRT_MAX * sin( ( (double)i * freq / (double)save_audio_config_p->adev[a].samples_per_sec ) * 2.0 * M_PI ) );
112+
pnData[i] = sample;
113+
}
114+
}
115+
else {
116+
for (i = 0; i < nSamples; i++) {
117+
sample = (short)( (double)SHRT_MAX * sin( ( (double)i * freq / (double)save_audio_config_p->adev[a].samples_per_sec ) * 2.0 * M_PI ) );
118+
if (channel == ADEVFIRSTCHAN( a )) {
119+
120+
// Stereo, left channel.
121+
122+
pnData[i*2 + 0] = sample;
123+
pnData[i*2 + 1] = 0;
124+
}
125+
else {
126+
127+
// Stereo, right channel.
128+
129+
pnData[i*2 + 0] = 0;
130+
pnData[i*2 + 1] = sample;
131+
}
132+
}
133+
}
134+
135+
//
136+
// ptt_set on
137+
//
138+
139+
for (i=0; i<50; i++) {
140+
frames = snd_pcm_writei(handle, pnData, nSamples);
141+
}
142+
143+
//
144+
// ptt_set off
145+
//
146+
147+
//
148+
// close
149+
//
150+
151+
free (pnData);
152+
}
153+
154+
snd_pcm_close(handle);
155+
}
156+
#else
157+
int oss_audio_device_fd;
158+
159+
oss_audio_device_fd = open (save_audio_config_p->adev[a].adevice_out, O_WRONLY);
160+
if (oss_audio_device_fd != -1) {
161+
162+
}
163+
#endif
164+
}
165+
}
166+
167+
#endif

‎audio_ptt_win.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,33 +96,28 @@ unsigned __stdcall ptt_thread (void *arg)
9696
err = waveOutOpen ( &hWaveOut, atoi( save_audio_config_p->adev[a].adevice_out ), &wf, (DWORD_PTR)NULL, 0, CALLBACK_NULL );
9797
if( err == MMSYSERR_NOERROR ) {
9898
WAVEHDR waveHeader;
99-
SHORT* pnData;
100-
SHORT sample;
101-
int nsamples = save_audio_config_p->adev[a].samples_per_sec / freq;
99+
short* pnData;
100+
short sample;
101+
int nSamples = save_audio_config_p->adev[a].samples_per_sec / freq;
102102
int i;
103103

104-
if( save_audio_config_p->adev[a].num_channels == 1 ) {
105-
waveHeader.dwBufferLength = 1 * nsamples * sizeof( SHORT );
106-
}
107-
else {
108-
waveHeader.dwBufferLength = 2 * nsamples * sizeof( SHORT );
109-
}
104+
waveHeader.dwBufferLength = save_audio_config_p->adev[a].num_channels * nSamples * sizeof( short );
110105
waveHeader.lpData = malloc( waveHeader.dwBufferLength );
111106
waveHeader.dwUser = 0;
112107
waveHeader.dwFlags = WHDR_BEGINLOOP | WHDR_ENDLOOP;
113108
waveHeader.dwLoops = 0xFFFF;
114109

115-
pnData = (SHORT*)waveHeader.lpData;
110+
pnData = (short*)waveHeader.lpData;
116111

117112
if( save_audio_config_p->adev[a].num_channels == 1 ) {
118-
for( i = 0; i < nsamples; i++ ) {
119-
sample = (SHORT)( (double)SHRT_MAX * sin( ( (double)i / (double)nsamples ) * 2.0 * M_PI ) );
113+
for( i = 0; i < nSamples; i++ ) {
114+
sample = (short)( (double)SHRT_MAX * sin( ( (double)i / (double)nSamples ) * 2.0 * M_PI ) );
120115
pnData[i] = sample;
121116
}
122117
}
123118
else {
124-
for( i = 0; i < nsamples; i++ ) {
125-
sample = (SHORT)( (double)SHRT_MAX * sin( ( (double)i / (double)nsamples ) * 2.0 * M_PI ) );
119+
for( i = 0; i < nSamples; i++ ) {
120+
sample = (short)( (double)SHRT_MAX * sin( ( (double)i / (double)nSamples ) * 2.0 * M_PI ) );
126121
if( channel == ADEVFIRSTCHAN( a ) ) {
127122

128123
// Stereo, left channel.
@@ -191,4 +186,4 @@ unsigned __stdcall ptt_thread (void *arg)
191186

192187
return 0;
193188
}
194-
#endif
189+
#endif

‎ptt.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -774,10 +774,8 @@ void ptt_init (struct audio_s *audio_config_p)
774774
return;
775775
}
776776
#else
777-
int e;
778-
779-
e = pthread_create (&(ptt_tid[j]), NULL, ptt_thread, (void *)(long)ch);
780-
if (e != 0) {
777+
audio_ptt_tid[ch] = start_ptt_thread (audio_config_p, ch);
778+
if (audio_ptt_tid[ch] == 0) {
781779
text_color_set(DW_COLOR_ERROR);
782780
dw_printf ("Could not create audio_ptt thread on channel %d for PTT of channel %d.\n",
783781
audio_config_p->achan[ch].octrl[OCTYPE_PTT].ptt_channel, ch );

0 commit comments

Comments
 (0)
Please sign in to comment.