#include #include #include "telephone.h" int writesample(FILE *stream, long sample) { int i; for (i = 0; i < SAMPLEDEPTH; i += 8) if (putc((sample>>i)&0xFF, stream) == EOF) return EOF; return 0; } int add_silence(FILE *stream, float length) { size_t i; length *= SAMPLERATE; for (i = 0; i < length; ++i) writesample(stream, 0); return 0; } int add_dtmf(FILE *stream, float f1, float f2, float length) { float angle1, angle2; size_t i; length *= SAMPLERATE; for (i = 0; i < length; ++i) { angle1 = 2.*M_PI*f1*i/SAMPLERATE; angle2 = 2.*M_PI*f2*i/SAMPLERATE; writesample(stream, (long)floor((SAMPLERANGE>>2)*sinf(angle1) + (SAMPLERANGE>>2)*sinf(angle2))); } return 0; } /* output a sine wave starting at phase 'start' ((0,1]) and return end phase */ /* starting phase is for maintaining phase */ float add_sine(FILE *stream, float freq, float length, float phase) { float time = 0., unused; size_t i; length *= SAMPLERATE; for (i = 0; time = phase+(float)i*freq/SAMPLERATE, i < length; ++i) writesample(stream, (long)floor((SAMPLERANGE>>1)*sinf(2.*M_PI*time))); return modff(time, &unused); }