/* cwpcm.c - text to International Morse Code converter Copyright (C) 2001-2003 Randall S. Bohn 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include "morse.h" #include "pcm.h" /* tbase (dit time in samples) is 1200 msec * 8 samples per msec */ #define tbase 9600 /* 900 Hz */ #define PITCH 900 /* volume on 0..100 percent */ #define VOLUME 70 /* rates (character, word) for various speeds (slow|med|fast|extra) */ #define CSLOW 15 #define WSLOW 5 #define CMED 18 #define WMED 12 #define CFAST 18 #define WFAST 18 #define CEXTRA 20 #define WEXTRA 20 /* program settings */ int pcm = 1; int hvox = 0; /* character time, space time (in samples) */ int cTime, sTime; /* get the dit-time for characters */ int getCharacterTime(int c_rate) { return tbase/c_rate; } /* get the dit-time for space between characters */ int getSpaceTime(int c_rate, int w_rate) { /* NONB helped with this section. Thanks Nate! */ int t_total; int t_chars; int t_space; if (w_rate < 5) w_rate = 5; if (w_rate >= c_rate) return getCharacterTime(c_rate); /* spaces take longer but how much longer? */ t_total = getCharacterTime(w_rate) * 50; t_chars = getCharacterTime(c_rate) * 36; t_space = t_total - t_chars; return t_space / 14; } /* morse-speak: */ void dit(FILE *out) { mark(hvox, cTime, out); space(hvox, cTime, out); } void dah(FILE *out) { mark(hvox, cTime * 3, out); space(hvox, cTime, out); } void err(FILE *out) { } void cspace(FILE *out) { space(hvox, sTime*2, out); } void wspace(FILE *out) { space(hvox, sTime*4, out); fflush(out); } void setupVoice(int hz, int amp) { hvox = voiceFactory(hz, amp, 128, 8000); } int getInt(char *s, int low, int high) { int ival; char *temp = malloc(6); strncpy(temp, s, 5); if (strlen(s) > 5) *(temp+5) = '\0'; ival = atoi(temp); free(temp); if (ival < low) return low; if (ival > high) return high; return ival; } int main(int argc, char **argv) { int ch, x, pitch, volume; int cwpm; int wwpm; int verbose = 0; FILE *out = stdout; pitch = PITCH; volume = VOLUME; cwpm = 0; wwpm = 0; /* decode startup options */ for (x = 0; x < argc; x++) { if (ARG_IS("-ss")) { cwpm = CSLOW; wwpm = WSLOW; } if (ARG_IS("-sm")) { cwpm = CMED; wwpm = WMED; } if (ARG_IS("-sf")) { cwpm = CFAST; wwpm = WFAST; } if (ARG_IS("-sx")) { cwpm = CEXTRA; wwpm = WEXTRA; } if (ARG_IS("-f:")) { pitch = getInt(argv[x]+3, 40, 3200); } else if (ARG_IS("-f")) { if (++x < argc) { pitch = getInt(argv[x], 40, 3200); } continue; } if (ARG_IS("-vol:")) { volume = getInt(argv[x]+5, 0, 100); } else if (ARG_IS("-v")) { if (++x < argc) { volume = getInt(argv[x], 0, 100); } continue; } if (ARG_IS("-w")) { if (++x < argc) { wwpm = getInt(argv[x], 5, 100); if (cwpm == 0) cwpm = wwpm; } continue; } if (ARG_IS("-F")) { if (++x < argc) cwpm = getInt(argv[x], 5, 100); continue; } if (ARG_IS("-d")) { verbose=1; } } /* speed not set? */ if (cwpm == 0) cwpm = CFAST; if (wwpm == 0) wwpm = CFAST; cTime = getCharacterTime(cwpm); sTime = getSpaceTime(cwpm, wwpm); setupVoice(pitch, volume*127/100); if (verbose) { fprintf(stderr, "Pitch: %d Volume: %d%%\n", pitch, volume); fprintf(stderr, "WPM: %d Farnsworth: %d\n", wwpm, cwpm); } while ((ch = fgetc(stdin)) != EOF) { if (isspace(ch)) wspace(out); else genMorse(tolower(ch), out); } return 0; }