direwolf/gen_tone.c

335 lines
7.6 KiB
C
Raw Permalink Normal View History

Version 1.0 - Initial commit Changes to be committed: new file: .gitattributes new file: .gitignore new file: APRStt-Implementation-Notes.pdf new file: CHANGES.txt new file: LICENSE-dire-wolf.txt new file: LICENSE-other.txt new file: Makefile.linux new file: Makefile.win new file: Quick-Start-Guide-Windows.pdf new file: Raspberry-Pi-APRS.pdf new file: User-Guide.pdf new file: aclients.c new file: aprs_tt.c new file: aprs_tt.h new file: atest.c new file: audio.c new file: audio.h new file: audio_win.c new file: ax25_pad.c new file: ax25_pad.h new file: beacon.c new file: beacon.h new file: config.c new file: config.h new file: decode_aprs.c new file: decode_aprs.h new file: dedupe.c new file: dedupe.h new file: demod.c new file: demod.h new file: demod_9600.c new file: demod_9600.h new file: demod_afsk.c new file: demod_afsk.h new file: digipeater.c new file: digipeater.h new file: direwolf.c new file: direwolf.conf new file: direwolf.desktop new file: direwolf.h new file: dsp.c new file: dsp.h new file: dtmf.c new file: dtmf.h new file: dw-icon.ico new file: dw-icon.png new file: dw-icon.rc new file: dw-start.sh new file: dwgps.c new file: dwgps.h new file: encode_aprs.c new file: encode_aprs.h new file: fcs_calc.c new file: fcs_calc.h new file: fsk_demod_agc.h new file: fsk_demod_state.h new file: fsk_filters.h new file: fsk_gen_filter.h new file: gen_packets.c new file: gen_tone.c new file: gen_tone.h new file: hdlc_rec.c new file: hdlc_rec.h new file: hdlc_rec2.c new file: hdlc_rec2.h new file: hdlc_send.c new file: hdlc_send.h new file: igate.c new file: igate.h new file: kiss.c new file: kiss.h new file: kiss_frame.c new file: kiss_frame.h new file: kissnet.c new file: kissnet.h new file: latlong.c new file: latlong.h new file: ll2utm.c new file: misc/README-dire-wolf.txt new file: misc/strcasestr.c new file: misc/strsep.c new file: misc/strtok_r.c new file: morse.c new file: multi_modem.c new file: multi_modem.h new file: ptt.c new file: ptt.h new file: pttest.c new file: rdq.c new file: rdq.h new file: redecode.c new file: redecode.h new file: regex/COPYING new file: regex/INSTALL new file: regex/LICENSES new file: regex/NEWS new file: regex/README new file: regex/README-dire-wolf.txt new file: regex/re_comp.h new file: regex/regcomp.c new file: regex/regex.c new file: regex/regex.h new file: regex/regex_internal.c new file: regex/regex_internal.h new file: regex/regexec.c new file: rrbb.c new file: rrbb.h new file: server.c new file: server.h new file: symbols-new.txt new file: symbols.c new file: symbols.h new file: symbolsX.txt new file: textcolor.c new file: textcolor.h new file: tocalls.txt new file: tq.c new file: tq.h new file: tt_text.c new file: tt_text.h new file: tt_user.c new file: tt_user.h new file: tune.h new file: udp_test.c new file: utm/LatLong-UTMconversion.c new file: utm/LatLong-UTMconversion.h new file: utm/README.txt new file: utm/SwissGrid.cpp new file: utm/UTMConversions.cpp new file: utm/constants.h new file: utm2ll.c new file: version.h new file: xmit.c new file: xmit.h
2015-07-27 00:35:07 +00:00
//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2011 John Langner, WB2OSZ
//
// 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, see <http://www.gnu.org/licenses/>.
//
/*------------------------------------------------------------------
*
* Module: gen_tone.c
*
* Purpose: Convert bits to AFSK for writing to .WAV sound file
* or a sound device.
*
*
*---------------------------------------------------------------*/
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "direwolf.h"
#include "audio.h"
#include "gen_tone.h"
#include "textcolor.h"
// Properties of the digitized sound stream & modem.
static struct audio_s modem;
/*
* 8 bit samples are unsigned bytes in range of 0 .. 255.
*
* 16 bit samples are signed short in range of -32768 .. +32767.
*/
/* Constants after initialization. */
#define TICKS_PER_CYCLE ( 256.0 * 256.0 * 256.0 * 256.0 )
static int ticks_per_sample; /* same for all channels. */
static int ticks_per_bit[MAX_CHANS];
static int f1_change_per_sample[MAX_CHANS];
static int f2_change_per_sample[MAX_CHANS];
static short sine_table[256];
/* Accumulators. */
static unsigned int tone_phase[MAX_CHANS]; // Phase accumulator for tone generation.
// Upper bits are used as index into sine table.
static int bit_len_acc[MAX_CHANS]; // To accumulate fractional samples per bit.
static int lfsr[MAX_CHANS]; // Shift register for scrambler.
/*------------------------------------------------------------------
*
* Name: gen_tone_init
*
* Purpose: Initialize for AFSK tone generation which might
* be used for RTTY or amateur packet radio.
*
* Inputs: pp - Pointer to modem parameter structure, modem_s.
*
* The fields we care about are:
*
* samples_per_sec
* baud
* mark_freq
* space_freq
* samples_per_sec
*
* amp - Signal amplitude on scale of 0 .. 100.
*
* Returns: 0 for success.
* -1 for failure.
*
* Description: Calculate various constants for use by the direct digital synthesis
* audio tone generation.
*
*----------------------------------------------------------------*/
static int amp16bit; /* for 9600 baud */
int gen_tone_init (struct audio_s *pp, int amp)
{
int j;
int chan = 0;
/*
* Save away modem parameters for later use.
*/
memcpy (&modem, pp, sizeof(struct audio_s));
amp16bit = (32767 * amp) / 100;
ticks_per_sample = (int) ((TICKS_PER_CYCLE / (double)modem.samples_per_sec ) + 0.5);
for (chan = 0; chan < modem.num_channels; chan++) {
ticks_per_bit[chan] = (int) ((TICKS_PER_CYCLE / (double)modem.baud[chan] ) + 0.5);
f1_change_per_sample[chan] = (int) (((double)modem.mark_freq[chan] * TICKS_PER_CYCLE / (double)modem.samples_per_sec ) + 0.5);
f2_change_per_sample[chan] = (int) (((double)modem.space_freq[chan] * TICKS_PER_CYCLE / (double)modem.samples_per_sec ) + 0.5);
tone_phase[chan] = 0;
bit_len_acc[chan] = 0;
lfsr[chan] = 0;
}
for (j=0; j<256; j++) {
double a;
int s;
a = ((double)(j) / 256.0) * (2 * M_PI);
s = (int) (sin(a) * 32767 * amp / 100.0);
/* 16 bit sound sample is in range of -32768 .. +32767. */
assert (s >= -32768 && s <= 32767);
sine_table[j] = s;
}
return (0);
} /* end gen_tone_init */
/*-------------------------------------------------------------------
*
* Name: gen_tone_put_bit
*
* Purpose: Generate tone of proper duration for one data bit.
*
* Inputs: chan - Audio channel, 0 = first.
*
* dat - 0 for f1, 1 for f2.
*
* -1 inserts half bit to test data
* recovery PLL.
*
* Assumption: fp is open to a file for write.
*
*--------------------------------------------------------------------*/
void tone_gen_put_bit (int chan, int dat)
{
int cps = dat ? f2_change_per_sample[chan] : f1_change_per_sample[chan];
unsigned short sam = 0;
int x;
if (dat < 0) {
/* Hack to test receive PLL recovery. */
bit_len_acc[chan] -= ticks_per_bit[chan];
dat = 0;
}
if (modem.modem_type[chan] == SCRAMBLE) {
x = (dat ^ (lfsr[chan] >> 16) ^ (lfsr[chan] >> 11)) & 1;
lfsr[chan] = (lfsr[chan] << 1) | (x & 1);
dat = x;
}
do {
if (modem.modem_type[chan] == AFSK) {
tone_phase[chan] += cps;
sam = sine_table[(tone_phase[chan] >> 24) & 0xff];
}
else {
// TODO: Might want to low pass filter this.
sam = dat ? amp16bit : (-amp16bit);
}
/* Ship out an audio sample. */
assert (modem.num_channels == 1 || modem.num_channels == 2);
/* Generalize to allow 8 bits someday? */
assert (modem.bits_per_sample == 16);
if (modem.num_channels == 1)
{
audio_put (sam & 0xff);
audio_put ((sam >> 8) & 0xff);
}
else if (modem.num_channels == 2)
{
if (chan == 1)
{
audio_put (0); // silent left
audio_put (0);
}
audio_put (sam & 0xff);
audio_put ((sam >> 8) & 0xff);
//byte_count += 2;
if (chan == 0)
{
audio_put (0); // silent right
audio_put (0);
}
}
/* Enough for the bit time? */
bit_len_acc[chan] += ticks_per_sample;
} while (bit_len_acc[chan] < ticks_per_bit[chan]);
bit_len_acc[chan] -= ticks_per_bit[chan];
}
/*-------------------------------------------------------------------
*
* Name: main
*
* Purpose: Quick test program for above.
*
* Description: Compile like this for unit test:
*
* gcc -Wall -DMAIN -o gen_tone_test gen_tone.c audio.c textcolor.c
*
* gcc -Wall -DMAIN -o gen_tone_test.exe gen_tone.c audio_win.c textcolor.c -lwinmm
*
*--------------------------------------------------------------------*/
#if MAIN
int main ()
{
int n;
int chan1 = 0;
int chan2 = 1;
int r;
struct audio_s audio_param;
/* to sound card */
/* one channel. 2 times: one second of each tone. */
memset (&audio_param, 0, sizeof(audio_param));
strcpy (audio_param.adevice_in, DEFAULT_ADEVICE);
strcpy (audio_param.adevice_out, DEFAULT_ADEVICE);
audio_open (&audio_param);
gen_tone_init (&audio_param, 100);
for (r=0; r<2; r++) {
for (n=0; n<audio_param.baud[0] * 2 ; n++) {
tone_gen_put_bit ( chan1, 1 );
}
for (n=0; n<audio_param.baud[0] * 2 ; n++) {
tone_gen_put_bit ( chan1, 0 );
}
}
audio_close();
/* Now try stereo. */
memset (&audio_param, 0, sizeof(audio_param));
strcpy (audio_param.adevice_in, DEFAULT_ADEVICE);
strcpy (audio_param.adevice_out, DEFAULT_ADEVICE);
audio_param.num_channels = 2;
audio_open (&audio_param);
gen_tone_init (&audio_param, 100);
for (r=0; r<4; r++) {
for (n=0; n<audio_param.baud[0] * 2 ; n++) {
tone_gen_put_bit ( chan1, 1 );
}
for (n=0; n<audio_param.baud[0] * 2 ; n++) {
tone_gen_put_bit ( chan1, 0 );
}
for (n=0; n<audio_param.baud[0] * 2 ; n++) {
tone_gen_put_bit ( chan2, 1 );
}
for (n=0; n<audio_param.baud[0] * 2 ; n++) {
tone_gen_put_bit ( chan2, 0 );
}
}
audio_close();
return(0);
}
#endif
/* end gen_tone.c */