|
2 | 2 | // This file is part of Dire Wolf, an amateur radio packet TNC.
|
3 | 3 | //
|
4 | 4 | // Copyright (C) 2011, 2012, 2013, 2014, 2015 John Langner, WB2OSZ
|
| 5 | +// Copyright (C) 2017 Andrew Walker, VA7YAA |
5 | 6 | //
|
6 | 7 | // This program is free software: you can redistribute it and/or modify
|
7 | 8 | // it under the terms of the GNU General Public License as published by
|
|
18 | 19 | //
|
19 | 20 |
|
20 | 21 |
|
| 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 |
0 commit comments