dtmfencode.cpp
来自「DTMF Encode to Wav f」· C++ 代码 · 共 172 行
CPP
172 行
/* dtmf_encode.c - DTMF Encoder Functions This file is part of DTMFkit Copyright (c) 2004 Joseph Battaglia <sephail@atnum.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *///#include "stdafx.h"#include <stdio.h>#include <ctype.h>#include <math.h>//#include <unistd.h>#include "dtmfencode.h"/* Buffer size */#define BUF_SIZE 4096/* The following #defines are for code readibility when using dtmf_encode_tones[][] */#define DIGIT 0#define FREQ1 1#define FREQ2 2/* DTMF digit frequency pairs */int dtmf_encode_tones[][3] = { /* digit, freq1, freq2 */ {'1', 697, 1209}, {'2', 697, 1336}, {'3', 697, 1477}, {'A', 697, 1633}, {'4', 770, 1209}, {'5', 770, 1336}, {'6', 770, 1477}, {'B', 770, 1633}, {'7', 852, 1209}, {'8', 852, 1336}, {'9', 852, 1477}, {'C', 852, 1633}, {'*', 941, 1209}, {'0', 941, 1336}, {'#', 941, 1477}, {'D', 941, 1633}, { -1, -1, -1}};/* Generate a DTMF tone or ',' for pause of length msec with specified sample rate, channels, and bits */ int dtmf_encode(int fd, int digit, int msec, int sample_rate, int bits, int channels) { unsigned char buffer[BUF_SIZE]; double add1, add2, c1, c2; int bufpos = 0, freq1, freq2, i, length, val, val1, val2; /* Query lookup table for DTMF digit frequencies */ for(i = 0; dtmf_encode_tones[i][DIGIT] != -1; i++) { if((dtmf_encode_tones[i][DIGIT]) == toupper(digit)) { freq1 = dtmf_encode_tones[i][FREQ1]; freq2 = dtmf_encode_tones[i][FREQ2]; } } /* Otherwise generate a pause */ if(digit == ',') { freq1 = 0; freq2 = 0; } /* If the tone could not be found, return */ if(digit != ',' && freq1 == 0) { return(ERR_INVALID_DIGIT); } /* Calculate values for waveform generation */ add1 = (double)freq1 / sample_rate; add2 = (double)freq2 / sample_rate; length = (msec * sample_rate) / 1000; while(--length >= 0) { /* Generate waveform */ val1 = (int)(sin(c1 * 2 * M_PI) * 16383); val2 = (int)(sin(c2 * 2 * M_PI) * 16383); val = val1 + val2; /* Loop for each channel */ for(i = 0; i < channels; i++) { if(bits == 8) { /* Convert to 8-bit unsigned */ buffer[bufpos++] = 128 + (val >> 8); } if(bits == 16) { /* Convert to 16-bit signed (LE) */ buffer[bufpos++] = val & 0xff; buffer[bufpos++] = (val >> 8) & 0xff; } } /* Write if buffer is full */ if(bufpos >= BUF_SIZE - 4) { if(write(fd, buffer, bufpos) == -1) { perror("write()"); return(ERR_WRITE_FAILED); } bufpos = 0; } /* Add waveform generation values */ c1 += add1; c2 += add2; } /* Write the rest of the buffer */ if(write(fd, buffer, bufpos) == -1) { perror("write()"); return(ERR_WRITE_FAILED); } return(0); }/* calls dtmf_encode() for each character in a string */int dtmf_encode_string(int fd, char *string, int msec, int sample_rate, int bits, int channels, int verbose) { int i, retval; for(i = 0; string[i] != '\0'; i++) { /* If verbose, print digit */ if(verbose) { printf("%c", string[i]); fflush(stdout); } /* Generate tone for each digit */ retval = dtmf_encode(fd, string[i], msec, sample_rate, bits, channels); if(retval && retval != ERR_INVALID_DIGIT) { return(retval); } /* Add a pause between digits (to comply with standards) */ retval = dtmf_encode(fd, ',', 50, sample_rate, bits, channels); if(retval) { return(retval); } } if(verbose) { printf("\n"); } return(0); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?