Commit c5959fa4 authored by jehan's avatar jehan

Merge branch 'master' of git.linphone.org:mediastreamer2

parents 83d06f42 dce5ac7d
......@@ -47,6 +47,22 @@ typedef struct _MSDtmfGenCustomTone MSDtmfGenCustomTone;
/**Play a custom tone according to the supplied tone description*/
#define MS_DTMF_GEN_PLAY_CUSTOM MS_FILTER_METHOD(MS_DTMF_GEN_ID,3,MSDtmfGenCustomTone)
/**
* Structure carried by MS_DTMF_GEN_EVENT
**/
struct _MSDtmfGenEvent{
uint64_t tone_start_time;
char tone_name[8];
};
typedef struct _MSDtmfGenEvent MSDtmfGenEvent;
/**
* Event sent by the filter each time a tone or dtmf is generated.
**/
#define MS_DTMF_GEN_EVENT MS_FILTER_EVENT(MS_DTMF_GEN_ID,0,MSDtmfGenEvent)
extern MSFilterDesc ms_dtmf_gen_desc;
#endif
......@@ -155,12 +155,17 @@ static int dtmfgen_put(MSFilter *f, void *arg){
static int dtmfgen_play_tone(MSFilter *f, void *arg){
DtmfGenState *s=(DtmfGenState*)f->data;
MSDtmfGenCustomTone *def=(MSDtmfGenCustomTone*)arg;
ms_message("Playing tone of frequency %i, duration=%i, amplitude=%f",def->frequency,def->duration,def->amplitude);
ms_filter_lock(f);
s->pos=0;
s->dur=(s->rate*def->duration)/1000;
s->lowfreq=def->frequency;
s->lowfreq=((float)def->frequency)/(float)s->rate;
s->highfreq=0;
s->silence=0;
s->amplitude=def->amplitude* 0.7*32767.0;
s->amplitude=((float)def->amplitude)* 0.7*32767.0;
s->dtmf='?';
ms_filter_unlock(f);
return 0;
}
......@@ -222,6 +227,13 @@ static void dtmfgen_process(MSFilter *f){
nsamples=(f->ticker->interval*s->rate)/1000;
m=allocb(nsamples*2,0);
if (s->silence==0){
if (s->pos==0){
MSDtmfGenEvent ev;
ev.tone_start_time=f->ticker->time;
ev.tone_name[0]=s->dtmf;
ev.tone_name[1]='\0';
ms_filter_notify(f,MS_DTMF_GEN_EVENT,&ev);
}
write_dtmf(s,(int16_t*)m->b_wptr,nsamples);
}else{
memset(m->b_wptr,0,nsamples*2);
......
......@@ -64,11 +64,13 @@ static float compute_energy(int16_t *samples, int nsamples){
typedef struct _DetectorState{
MSToneDetectorDef tone_def;
GoertzelState tone_gs;
int dur;
uint64_t starttime;
MSBufferizer *buf;
int dur;
int rate;
int framesize;
int frame_ms;
bool_t event_sent;
}DetectorState;
static void detector_init(MSFilter *f){
......@@ -100,32 +102,41 @@ static int detector_clear_scans(MSFilter *f, void *arg){
return 0;
}
static void end_tone(DetectorState *s){
s->dur=0;
s->event_sent=FALSE;
}
static void detector_process(MSFilter *f){
DetectorState *s=(DetectorState *)f->data;
mblk_t *m;
if (s->tone_def.frequency==0){
/*nothing to do, just bypass*/
while ((m=ms_queue_get(f->inputs[0]))!=NULL)
ms_queue_put(f->outputs[0],m);
}else{
while ((m=ms_queue_get(f->inputs[0]))!=NULL){
ms_queue_put(f->outputs[0],m);
if (s->tone_def.frequency!=0){
ms_bufferizer_put(s->buf,dupmsg(m));
}
}
if (s->tone_def.frequency!=0){
uint8_t *buf=alloca(s->framesize);
ms_bufferizer_put_from_queue (s->buf,f->inputs[0]);
while(ms_bufferizer_read(s->buf,buf,s->framesize)!=0){
float en=compute_energy((int16_t*)buf,s->framesize/2);
if (en>energy_min){
float freq_en=goertzel_state_run(&s->tone_gs,(int16_t*)buf,s->framesize/2,en);
if (freq_en>=s->tone_def.min_amplitude){
if (s->dur==0) s->starttime=f->ticker->time;
s->dur+=s->frame_ms;
if (s->dur>s->tone_def.min_duration){
if (s->dur>s->tone_def.min_duration && !s->event_sent){
MSToneDetectorEvent event;
strncpy(event.tone_name,s->tone_def.tone_name,sizeof(event.tone_name));
event.tone_start_time=f->ticker->time;
event.tone_start_time=s->starttime;
ms_filter_notify(f,MS_TONE_DETECTOR_EVENT,&event);
s->event_sent=TRUE;
}
}else s->dur=0;
}else s->dur=0;
}else end_tone(s);
}else end_tone(s);
}
}
}
......
......@@ -23,8 +23,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "mediastreamer2/dtmfgen.h"
#include "mediastreamer2/msticker.h"
static void tone_detected_cb(void *data, MSToneDetectorEvent *ev){
ms_message("Tone detected !");
static void tone_detected_cb(void *data, MSFilter *f, unsigned int event_id, MSToneDetectorEvent *ev){
ms_message("Tone detected at time %u",(unsigned int)ev->tone_start_time);
}
static void tone_sent_cb(void *data, MSFilter *f, unsigned int event_id, MSDtmfGenEvent *ev){
ms_message("Tone sent at time %u",(unsigned int)ev->tone_start_time);
}
int main(int argc, char *argv[]){
......@@ -32,6 +36,7 @@ int main(int argc, char *argv[]){
MSTicker *ticker;
ms_init();
ortp_set_log_level_mask (ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
src=ms_filter_new(MS_FILE_PLAYER_ID);
rec=ms_filter_new(MS_FILE_REC_ID);
......@@ -40,6 +45,7 @@ int main(int argc, char *argv[]){
ms_filter_link(src,0,gen,0);
ms_filter_link(gen,0,det,0);
//ms_filter_link(gen,0,rec,0);
ms_filter_link(det,0,rec,0);
ticker=ms_ticker_new();
......@@ -52,6 +58,7 @@ int main(int argc, char *argv[]){
/*generate and detect the tones*/
MSDtmfGenCustomTone tone;
MSToneDetectorDef expected_tone;
char dtmf='*';
tone.frequency=2000;
tone.duration=400;
......@@ -60,21 +67,32 @@ int main(int argc, char *argv[]){
expected_tone.frequency=2000;
expected_tone.min_duration=200;
expected_tone.min_amplitude=0.5;
ms_filter_set_notify_callback (det,(MSFilterNotifyFunc)tone_detected_cb,NULL);
ms_filter_set_notify_callback(det,(MSFilterNotifyFunc)tone_detected_cb,NULL);
ms_filter_set_notify_callback(gen,(MSFilterNotifyFunc)tone_sent_cb,NULL);
ms_filter_call_method(det,MS_TONE_DETECTOR_ADD_SCAN,&expected_tone);
ms_filter_call_method(gen,MS_DTMF_GEN_PLAY,&dtmf);
sleep(1);
ms_filter_call_method(gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
sleep(1);
ms_filter_call_method(gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
sleep(1);
ms_filter_call_method(gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
sleep(1);
tone.frequency=1500;
tone.amplitude=1.0;
ms_filter_call_method(gen,MS_DTMF_GEN_PLAY_CUSTOM,&tone);
sleep(1);
}
ms_filter_call_method_noarg(rec,MS_FILE_REC_CLOSE);
ms_ticker_detach(ticker,src);
ms_filter_unlink(src,0,gen,0);
ms_filter_unlink(gen,0,det,0);
//ms_filter_unlink(gen,0,rec,0);
ms_filter_unlink(det,0,rec,0);
ms_ticker_destroy(ticker);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment