dtmfgen.c 8.95 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)

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
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
aymeric's avatar
aymeric committed
18 19 20
*/

#include "mediastreamer2/dtmfgen.h"
21
#include "mediastreamer2/msticker.h"
aymeric's avatar
aymeric committed
22

23 24 25
#if __APPLE__
#include "TargetConditionals.h"
#endif
aymeric's avatar
aymeric committed
26 27 28 29 30 31 32

#include <math.h>

#ifndef M_PI
#define M_PI       3.14159265358979323846
#endif

Simon Morlat's avatar
Simon Morlat committed
33

34
#define NO_SAMPLES_THRESHOLD 100 /*ms*/
Simon Morlat's avatar
Simon Morlat committed
35

36
#if defined(__ANDROID__) || TARGET_OS_IPHONE /* because android and iOS don't deal well with audio stop and restarts at each dtmf.*/
Simon Morlat's avatar
Simon Morlat committed
37 38
#define TRAILLING_SILENCE 10000 /*ms*/
#else
39
#define TRAILLING_SILENCE 500 /*ms*/
Simon Morlat's avatar
Simon Morlat committed
40
#endif
41

42

aymeric's avatar
aymeric committed
43 44
struct DtmfGenState{
	int rate;
45
	int nchannels;
aymeric's avatar
aymeric committed
46 47 48 49
	int dur;
	int pos;
	float highfreq;
	float lowfreq;
50 51
	int nosamples_time;
	int silence;
52
	int amplitude;
53
	float default_amplitude;
Simon Morlat's avatar
Simon Morlat committed
54 55 56
	int repeat_count;
	MSDtmfGenCustomTone current_tone;
	bool_t playing;
aymeric's avatar
aymeric committed
57 58 59 60 61
};

typedef struct DtmfGenState DtmfGenState;

static void dtmfgen_init(MSFilter *f){
Simon Morlat's avatar
Simon Morlat committed
62
	DtmfGenState *s=(DtmfGenState *)ms_new0(DtmfGenState,1);
aymeric's avatar
aymeric committed
63
	s->rate=8000;
64
	s->nchannels = 1;
aymeric's avatar
aymeric committed
65 66
	s->dur=s->rate/10;
	s->pos=0;
67 68
	s->nosamples_time=0;
	s->silence=0;
69 70
	s->default_amplitude=0.2f;
	s->amplitude=(int)(s->default_amplitude*0.7*32767);
Simon Morlat's avatar
Simon Morlat committed
71
	s->repeat_count=0;
aymeric's avatar
aymeric committed
72 73 74 75 76 77 78 79 80 81
	f->data=s;
}

static void dtmfgen_uninit(MSFilter *f){
	ms_free(f->data);
}

static int dtmfgen_put(MSFilter *f, void *arg){
	DtmfGenState *s=(DtmfGenState*)f->data;
	const char *dtmf=(char*)arg;
82
	
aymeric's avatar
aymeric committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 142 143 144 145 146
	switch(dtmf[0]){
		case '0':
			s->lowfreq=941;
			s->highfreq=1336;
			break;
		case '1':
			s->lowfreq=697;
			s->highfreq=1209;
			break;
		case '2':
			s->lowfreq=697;
			s->highfreq=1336;
			break;
		case '3':
			s->lowfreq=697;
			s->highfreq=1477;
			break;
		case '4':
			s->lowfreq=770;
			s->highfreq=1209;
			break;
		case '5':
			s->lowfreq=770;
			s->highfreq=1336;
			break;
		case '6':
			s->lowfreq=770;
			s->highfreq=1477;
			break;
		case '7':
			s->lowfreq=852;
			s->highfreq=1209;
			break;
		case '8':
			s->lowfreq=852;
			s->highfreq=1336;
			break;
		case '9':
			s->lowfreq=852;
			s->highfreq=1477;
			break;
		case '*':
			s->lowfreq=941;
			s->highfreq=1209;
			break;
		case '#':
			s->lowfreq=941;
			s->highfreq=1477;
			break;
		case 'A':
			s->lowfreq=697;
			s->highfreq=1633;
			break;
		case 'B':
			s->lowfreq=770;
			s->highfreq=1633;
			break;
		case 'C':
			s->lowfreq=852;
			s->highfreq=1633;
			break;
		case 'D':
			s->lowfreq=941;
			s->highfreq=1633;
147
			break;
148 149 150
		case '!':
			ms_message("flash dtmf");
			return 0;
151 152 153 154
		case ' ':
			/*ignore*/
			return 0;
			break;
aymeric's avatar
aymeric committed
155 156 157 158
		default:
			ms_warning("Not a dtmf key.");
			return -1;
	}
159
	ms_filter_lock(f);
160
	s->pos=0;
aymeric's avatar
aymeric committed
161 162
	s->lowfreq=s->lowfreq/s->rate;
	s->highfreq=s->highfreq/s->rate;
163 164
	s->dur=s->rate/10; /*100 ms duration */
	s->silence=0;
165
	s->amplitude=(int)(s->default_amplitude*32767*0.7);
Simon Morlat's avatar
Simon Morlat committed
166 167 168 169
	s->current_tone.tone_name[0]=dtmf[0];
	s->current_tone.tone_name[1]=0;
	s->current_tone.interval=0;
	s->playing=TRUE;
170 171 172 173
	ms_filter_unlock(f);
	return 0;
}

174 175 176
static int dtmfgen_play_tone(MSFilter *f, void *arg){
	DtmfGenState *s=(DtmfGenState*)f->data;
	MSDtmfGenCustomTone *def=(MSDtmfGenCustomTone*)arg;
Simon Morlat's avatar
Simon Morlat committed
177 178 179
	
	ms_message("Playing tones of frequencies %i,%i Hz, duration=%i, amplitude=%f interval=%i, repeat_count=%i",def->frequencies[0],
			   def->frequencies[1],def->duration,def->amplitude, def->interval, def->repeat_count);
180
	ms_filter_lock(f);
Simon Morlat's avatar
Simon Morlat committed
181
	s->current_tone=*def;
182
	s->pos=0;
183
	s->dur=(s->rate*def->duration)/1000;
Simon Morlat's avatar
Simon Morlat committed
184 185
	s->lowfreq=((float)def->frequencies[0])/(float)s->rate;
	s->highfreq=((float)def->frequencies[1])/(float)s->rate;;
186
	s->silence=0;
187
	s->amplitude=(int)(def->amplitude* 0.7*32767.0);
Simon Morlat's avatar
Simon Morlat committed
188 189
	s->repeat_count=0;
	s->playing=TRUE;
190 191
	ms_filter_unlock(f);
	
192 193 194
	return 0;
}

195 196 197 198 199 200 201 202 203 204 205
static int dtmfgen_start(MSFilter *f, void *arg){
	if (dtmfgen_put(f,arg)==0){
		DtmfGenState *s=(DtmfGenState*)f->data;
		s->dur=5*s->rate;
		return 0;
	}
	return -1;
}

static int dtmfgen_stop(MSFilter *f, void *arg){
	DtmfGenState *s=(DtmfGenState*)f->data;
206
	int min_duration=(100*s->rate)/1000; /*wait at least 100 ms*/
207
	ms_filter_lock(f);
208 209 210
	if (s->pos<min_duration)
		s->dur=min_duration;
	else s->dur=0;
211
	memset(&s->current_tone,0,sizeof(s->current_tone));
212
	ms_filter_unlock(f);
aymeric's avatar
aymeric committed
213 214 215 216 217 218 219 220 221
	return 0;
}

static int dtmfgen_set_rate(MSFilter *f, void *arg){
	DtmfGenState *s=(DtmfGenState*)f->data;
	s->rate=*((int*)arg);
	return 0;
}

222 223 224 225 226 227
static int dtmfgen_get_rate(MSFilter *f, void *arg) {
	DtmfGenState *s = (DtmfGenState *)f->data;
	*((int *)arg) = s->rate;
	return 0;
}

228 229 230 231 232 233
static int dtmfgen_set_nchannels(MSFilter *f, void *arg) {
	DtmfGenState *s = (DtmfGenState *)f->data;
	s->nchannels = *(int *)arg;
	return 0;
}

234 235 236 237 238 239
static int dtmfgen_get_nchannels(MSFilter *f, void *arg) {
	DtmfGenState *s = (DtmfGenState *)f->data;
	*((int *)arg) = s->nchannels;
	return 0;
}

240 241 242 243 244 245
static int dtmfgen_set_amp(MSFilter *f, void *arg){
	DtmfGenState *s=(DtmfGenState*)f->data;
	s->default_amplitude=*(float*)arg;
	return 0;
}

246 247

static void write_dtmf(DtmfGenState *s , int16_t *sample, int nsamples){
248 249
	int i, j;
	int16_t dtmf_sample;
250
	for (i=0;i<nsamples && s->pos<s->dur;i++,s->pos++){
251 252 253 254 255
		dtmf_sample = (int16_t)(((float)s->amplitude)*sin(2*M_PI*(float)s->pos*s->lowfreq));
		if (s->highfreq!=0) dtmf_sample += (int16_t)(((float)s->amplitude)*sin(2*M_PI*(float)s->pos*s->highfreq));
		for (j = 0; j < s->nchannels; j++) {
			sample[(i * s->nchannels) + j] = dtmf_sample;
		}
256
	}
257
	for (;i<nsamples;++i){
258 259 260
		for (j = 0; j < s->nchannels; j++) {
			sample[(i * s->nchannels) + j] = 0;
		}
261 262
	}
	if (s->pos>=s->dur){
263
		s->pos=0;
Simon Morlat's avatar
Simon Morlat committed
264 265 266 267 268 269
		if (s->current_tone.interval > 0) {
			s->silence=s->current_tone.interval;
			s->repeat_count++;
			if (s->current_tone.repeat_count>0 && s->repeat_count>=s->current_tone.repeat_count){
				s->playing=FALSE;
			}
270
		} else {
Simon Morlat's avatar
Simon Morlat committed
271
			s->playing=FALSE;
272 273
			s->silence=TRAILLING_SILENCE;
		}
274 275 276
	}
}

aymeric's avatar
aymeric committed
277 278 279
static void dtmfgen_process(MSFilter *f){
	mblk_t *m;
	DtmfGenState *s=(DtmfGenState*)f->data;
280
	int nsamples;
aymeric's avatar
aymeric committed
281

282 283 284
	ms_filter_lock(f);
	if (ms_queue_empty(f->inputs[0])){
		s->nosamples_time+=f->ticker->interval;
Simon Morlat's avatar
Simon Morlat committed
285
		if ((s->playing || s->silence!=0) && s->nosamples_time>NO_SAMPLES_THRESHOLD){
286 287 288
			/*after 100 ms without stream we decide to generate our own sample
			 instead of writing into incoming stream samples*/
			nsamples=(f->ticker->interval*s->rate)/1000;
289
			m=allocb(nsamples*s->nchannels*2,0);
290
			if (s->silence==0){
291 292 293
				if (s->pos==0){
					MSDtmfGenEvent ev;
					ev.tone_start_time=f->ticker->time;
Simon Morlat's avatar
Simon Morlat committed
294
					strncpy(ev.tone_name,s->current_tone.tone_name,sizeof(ev.tone_name));
295 296
					ms_filter_notify(f,MS_DTMF_GEN_EVENT,&ev);
				}
297 298
				write_dtmf(s,(int16_t*)m->b_wptr,nsamples);
			}else{
299
				memset(m->b_wptr,0,nsamples*s->nchannels*2);
300 301
				s->silence-=f->ticker->interval;
				if (s->silence<0) s->silence=0;
aymeric's avatar
aymeric committed
302
			}
303
			m->b_wptr+=nsamples*s->nchannels*2;
304 305 306 307
			ms_queue_put(f->outputs[0],m);
		}
	}else{
		s->nosamples_time=0;
Simon Morlat's avatar
Simon Morlat committed
308
		if (s->current_tone.interval > 0) {
309 310 311
			s->silence-=f->ticker->interval;
			if (s->silence<0) s->silence=0;
		} else s->silence=0;
312
		while((m=ms_queue_get(f->inputs[0]))!=NULL){
Simon Morlat's avatar
Simon Morlat committed
313
			if (s->playing && s->silence==0){
314 315 316 317 318 319
				if (s->pos==0){
					MSDtmfGenEvent ev;
					ev.tone_start_time=f->ticker->time;
					strncpy(ev.tone_name,s->current_tone.tone_name,sizeof(ev.tone_name));
					ms_filter_notify(f,MS_DTMF_GEN_EVENT,&ev);
				}
320
				nsamples=(int)(m->b_wptr-m->b_rptr)/(2*s->nchannels);
321
				write_dtmf(s, (int16_t*)m->b_rptr,nsamples);
aymeric's avatar
aymeric committed
322
			}
323
			ms_queue_put(f->outputs[0],m);
aymeric's avatar
aymeric committed
324 325
		}
	}
326
	ms_filter_unlock(f);
aymeric's avatar
aymeric committed
327 328 329 330
}

MSFilterMethod dtmfgen_methods[]={
	{	MS_FILTER_SET_SAMPLE_RATE	,	dtmfgen_set_rate	},
331
	{	MS_FILTER_GET_SAMPLE_RATE	,	dtmfgen_get_rate	},
332
	{	MS_FILTER_SET_NCHANNELS		,	dtmfgen_set_nchannels	},
333
	{	MS_FILTER_GET_NCHANNELS		,	dtmfgen_get_nchannels	},
Simon Morlat's avatar
Simon Morlat committed
334 335 336
	{	MS_DTMF_GEN_PLAY		,	dtmfgen_put		},
	{	MS_DTMF_GEN_START		,   dtmfgen_start },
	{	MS_DTMF_GEN_STOP		, 	dtmfgen_stop },
337
	{	MS_DTMF_GEN_PLAY_CUSTOM, dtmfgen_play_tone },
338
	{	MS_DTMF_GEN_SET_DEFAULT_AMPLITUDE, dtmfgen_set_amp },
aymeric's avatar
aymeric committed
339 340 341 342 343 344 345 346
	{	0				,	NULL			}
};

#ifdef _MSC_VER

MSFilterDesc ms_dtmf_gen_desc={
	MS_DTMF_GEN_ID,
	"MSDtmfGen",
347
	N_("DTMF generator"),
aymeric's avatar
aymeric committed
348 349
	MS_FILTER_OTHER,
	NULL,
Simon Morlat's avatar
Simon Morlat committed
350
	1,
aymeric's avatar
aymeric committed
351 352 353
	1,
	dtmfgen_init,
	NULL,
Simon Morlat's avatar
Simon Morlat committed
354
	dtmfgen_process,
aymeric's avatar
aymeric committed
355
	NULL,
Simon Morlat's avatar
Simon Morlat committed
356
	dtmfgen_uninit,
357 358
	dtmfgen_methods,
	MS_FILTER_IS_PUMP
aymeric's avatar
aymeric committed
359 360 361 362 363 364 365
};

#else

MSFilterDesc ms_dtmf_gen_desc={
	.id=MS_DTMF_GEN_ID,
	.name="MSDtmfGen",
366
	.text=N_("DTMF generator"),
aymeric's avatar
aymeric committed
367 368 369 370 371 372
	.category=MS_FILTER_OTHER,
	.ninputs=1,
	.noutputs=1,
	.init=dtmfgen_init,
	.process=dtmfgen_process,
	.uninit=dtmfgen_uninit,
373 374
	.methods=dtmfgen_methods,
	.flags=MS_FILTER_IS_PUMP
aymeric's avatar
aymeric committed
375 376 377 378 379 380
};

#endif

MS_FILTER_DESC_EXPORT(ms_dtmf_gen_desc)