dtmfgenerator.cpp 4.13 KB
Newer Older
1
/*
jpbl's avatar
jpbl committed
2 3 4 5 6 7 8 9
 * Copyright (C) 2004-2005 Savoir-Faire Linux inc.
 * Author: Yan Morin <yan.morin@savoirfairelinux.com>
 * Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com> 
 *
 * Portions (c) 2003 iptel.org
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or (at your
jpbl's avatar
jpbl committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
 * 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 Library General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 *
 */

#include <math.h>

#include "dtmfgenerator.h"
#include "../global.h"


/*
 * Tone frequencies
 */
const DTMFGenerator::DTMFTone DTMFGenerator::tones[NUM_TONES] = {
	{'0', 941, 1336},
	{'1', 697, 1209},
	{'2', 697, 1336},
	{'3', 697, 1477},
	{'4', 770, 1209},
	{'5', 770, 1336},
	{'6', 770, 1477},
	{'7', 852, 1209},
	{'8', 852, 1336},
	{'9', 852, 1477},
	{'A', 697, 1633},
	{'B', 770, 1633},
	{'C', 852, 1633},
	{'D', 941, 1633},
	{'*', 941, 1209},
	{'#', 941, 1477}
};


DTMFException::DTMFException(const char* _reason) throw()
{
	reason = _reason;
}


DTMFException::~DTMFException() throw()
{
}


const char* DTMFException::what() const throw()
{
	return reason;
}



/*
 * Initialize the generator
 */
75
DTMFGenerator::DTMFGenerator(unsigned int sampleRate) : tone("", sampleRate) 
jpbl's avatar
jpbl committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
{
  _sampleRate = sampleRate;
  state.offset = 0;
  state.sample = 0;

  for(int i = 0; i < NUM_TONES; i++) {
    samples[i] = generateSample(i);
  }
}
	

DTMFGenerator::~DTMFGenerator() {
  for(int i = 0; i < NUM_TONES; i++) {
    delete[] samples[i]; samples[i] = NULL;
  }
}


/*
 * Get n samples of the signal of code code
 */
97
void DTMFGenerator::getSamples(SFLDataFormat* buffer, size_t n, unsigned char code) throw(DTMFException) {
jpbl's avatar
jpbl committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	size_t i;
	if (!buffer) {
	  throw DTMFException("Invalid parameter value");
	}

	switch(code) {
	case '0': state.sample = samples[0];  break;
	case '1': state.sample = samples[1];  break;
	case '2': state.sample = samples[2];  break;
	case '3': state.sample = samples[3];  break;
	case '4': state.sample = samples[4];  break;
	case '5': state.sample = samples[5];  break;
	case '6': state.sample = samples[6];  break;
	case '7': state.sample = samples[7];  break;
	case '8': state.sample = samples[8];  break;
	case '9': state.sample = samples[9];  break;
	case 'A':
	case 'a': state.sample = samples[10]; break;
	case 'B':
	case 'b': state.sample = samples[11]; break;
	case 'C':
	case 'c': state.sample = samples[12]; break;
	case 'D':
	case 'd': state.sample = samples[13]; break;
	case '*': state.sample = samples[14]; break;
	case '#': state.sample = samples[15]; break;
	default:
			throw DTMFException("Invalid code");
		return;
		break;
	}

	for(i = 0; i < n; i++) {
		buffer[i] = state.sample[i % _sampleRate];
	}

	state.offset = i % _sampleRate;
}


/*
 * Get next n samples (continues where previous call to
 * genSample or genNextSamples stopped
 */
142
void DTMFGenerator::getNextSamples(SFLDataFormat* buffer, size_t n) throw(DTMFException)
jpbl's avatar
jpbl committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
{
	size_t i;

	if (!buffer) {
	  throw DTMFException("Invalid parameter");
	}

	if (state.sample == 0) {
	  throw DTMFException("DTMF generator not initialized");
	}

	for(i = 0; i < n; i++) {
	  buffer[i] = state.sample[(state.offset + i) % _sampleRate];
	}
	
	state.offset = (state.offset + i) % _sampleRate;
}


/*
 * Generate a tone sample
 */
165 166
SFLDataFormat* DTMFGenerator::generateSample(unsigned char code) throw (DTMFException) {
	SFLDataFormat* ptr;
jpbl's avatar
jpbl committed
167 168

	try {
169
		ptr = new SFLDataFormat[_sampleRate]; 
jpbl's avatar
jpbl committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183
		if (!ptr) {
			  throw new DTMFException("No memory left");
			return 0;
		}
		  
		tone.genSin(ptr, tones[code].higher, tones[code].lower,  _sampleRate);
		
		return ptr;
	} catch(...) {
			throw new DTMFException("No memory left");
			return 0;
	}

}