1 #include "AmRingTone.h"
2 #include <math.h>
3 
4 #include "log.h"
5 
6 #define PI 3.14159
7 
AmRingTone(int length,int on,int off,int f,int f2)8 AmRingTone::AmRingTone(int length, int on, int off, int f, int f2)
9   : AmAudio(),
10     on_period(on),
11     off_period(off),
12     freq(f),freq2(f2),
13     length(length)
14 {
15   if (on_period==0 && off_period==0)
16     on_period = 1; // sanity
17 }
18 
~AmRingTone()19 AmRingTone::~AmRingTone()
20 {}
21 
read(unsigned int user_ts,unsigned int size)22 int AmRingTone::read(unsigned int user_ts, unsigned int size)
23 {
24   int ts_on = on_period*(SYSTEM_SAMPLECLOCK_RATE/1000);
25   int ts_off = off_period*(SYSTEM_SAMPLECLOCK_RATE/1000);
26   int t = user_ts % (ts_on + ts_off);
27 
28   if(length < 0)
29     return -1;
30 
31   if(t >= ts_on){
32 
33     memset((unsigned char*)samples,0,size);
34 
35     return size;
36   }
37 
38   short* s = (short*)((unsigned char*)samples);
39   for(unsigned int i=0; i<PCM16_B2S(size); i++, s++, t++){
40 
41     if(t < ts_on){
42       float fs = sin((float(t*freq)/(float)SYSTEM_SAMPLECLOCK_RATE)*2.0*PI)*15000.0;
43       if(freq2 != 0)
44 	fs += sin((float(t*freq2)/(float)SYSTEM_SAMPLECLOCK_RATE)*2.0*PI)*15000.0;
45       *s = (short)(fs);
46     }
47     else
48       *s = 0;
49 
50   }
51 
52   if(length != 0){
53     length -= PCM16_B2S(size) / (SYSTEM_SAMPLECLOCK_RATE/1000);
54     if(!length)
55       length--;
56   }
57 
58   return size;
59 }
60 
write(unsigned int user_ts,unsigned int size)61 int AmRingTone::write(unsigned int user_ts, unsigned int size)
62 {
63   return -1;
64 }
65