blob: 856059328e4b5f6b1271ba18358a6c4b725ebe10 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "ToneGenerator"
19#include <utils/threads.h>
20
21#include <stdio.h>
22#include <math.h>
23#include <utils/Log.h>
24#include <sys/resource.h>
25#include <utils/RefBase.h>
26#include <utils/Timers.h>
27#include "media/ToneGenerator.h"
28
29namespace android {
30
31// Descriptors for all available tones (See ToneGenerator::ToneDescriptor class declaration for details)
32const ToneGenerator::ToneDescriptor
33 ToneGenerator::toneDescriptors[NUM_TONES] = {
34 // waveFreq[] segments[] repeatCnt
35 { { 1336, 941, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_0
36 { { 1209, 697, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_1
37 { { 1336, 697, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_2
38 { { 1477, 697, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_3
39 { { 1209, 770, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_4
40 { { 1336, 770, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_5
41 { { 1477, 770, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_6
42 { { 1209, 852, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_7
43 { { 1336, 852, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_8
44 { { 1477, 852, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_9
45 { { 1209, 941, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_S
46 { { 1477, 941, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_P
47 { { 1633, 697, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_A
48 { { 1633, 770, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_B
49 { { 1633, 852, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_C
50 { { 1633, 941, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_DTMF_D
51 { { 425, 0 }, { ToneGenerator::TONEGEN_INF, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_SUP_DIAL
52 { { 425, 0 }, { 500, 500, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_SUP_BUSY
53 { { 425, 0 }, { 200, 200, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_SUP_CONGESTION
54 { { 425, 0 }, { 200, 0 }, 0 }, // TONE_SUP_RADIO_ACK
55 { { 425, 0 }, { 200, 200, 0 }, 2 }, // TONE_SUP_RADIO_NOTAVAIL
56 { { 950, 1400, 1800, 0 }, { 330, 1000, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_SUP_ERROR
57 { { 425, 0 }, { 200, 600, 200, 3000, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_SUP_CALL_WAITING
58 { { 425, 0 }, { 1000, 4000, 0 }, ToneGenerator::TONEGEN_INF }, // TONE_SUP_RINGTONE
59 { { 400, 1200, 0 }, { 40, 0 }, 0 }, // TONE_PROP_BEEP
60 { { 1200, 0 }, { 100, 100, 0 }, 1 }, // TONE_PROP_ACK
61 { { 300, 400, 500, 0 }, { 400, 0 }, 0 }, // TONE_PROP_NACK
62 { { 400, 1200, 0 }, { 200, 0 }, 0 }, // TONE_PROP_PROMPT
63 { { 400, 1200, 0 }, { 40, 200, 40, 0 }, 0 } // TONE_PROP_BEEP2
64 };
65
66////////////////////////////////////////////////////////////////////////////////
67// ToneGenerator class Implementation
68////////////////////////////////////////////////////////////////////////////////
69
70
71//---------------------------------- public methods ----------------------------
72
73
74////////////////////////////////////////////////////////////////////////////////
75//
76// Method: ToneGenerator::ToneGenerator()
77//
78// Description: Constructor. Initializes the tone sequencer, intantiates required sine wave
79// generators, instantiates output audio track.
80//
81// Input:
82// toneType: Type of tone generated (values in enum tone_type)
83// streamType: Type of stream used for tone playback (enum AudioTrack::stream_type)
84// volume: volume applied to tone (0.0 to 1.0)
85//
86// Output:
87// none
88//
89////////////////////////////////////////////////////////////////////////////////
90ToneGenerator::ToneGenerator(int streamType, float volume) {
91
92 LOGV("ToneGenerator constructor: streamType=%d, volume=%f\n", streamType, volume);
93
94 mState = TONE_IDLE;
95
96 if (AudioSystem::getOutputSamplingRate(&mSamplingRate, streamType) != NO_ERROR) {
97 LOGE("Unable to marshal AudioFlinger");
98 return;
99 }
100 mStreamType = streamType;
101 mVolume = volume;
102 mpAudioTrack = 0;
103 mpToneDesc = 0;
104 mpNewToneDesc = 0;
105 // Generate tone by chunks of 20 ms to keep cadencing precision
106 mProcessSize = (mSamplingRate * 20) / 1000;
107
108 if (initAudioTrack()) {
109 LOGV("ToneGenerator INIT OK, time: %d\n", (unsigned int)(systemTime()/1000000));
110 } else {
111 LOGV("!!!ToneGenerator INIT FAILED!!!\n");
112 }
113}
114
115
116
117
118////////////////////////////////////////////////////////////////////////////////
119//
120// Method: ToneGenerator::~ToneGenerator()
121//
122// Description: Destructor. Stop sound playback and delete audio track if
123// needed and delete sine wave generators.
124//
125// Input:
126// none
127//
128// Output:
129// none
130//
131////////////////////////////////////////////////////////////////////////////////
132ToneGenerator::~ToneGenerator() {
133 LOGV("ToneGenerator destructor\n");
134
135 if (mpAudioTrack) {
136 stopTone();
137 LOGV("Delete Track: %p\n", mpAudioTrack);
138 delete mpAudioTrack;
139 }
140}
141
142////////////////////////////////////////////////////////////////////////////////
143//
144// Method: ToneGenerator::startTone()
145//
146// Description: Starts tone playback.
147//
148// Input:
149// none
150//
151// Output:
152// none
153//
154////////////////////////////////////////////////////////////////////////////////
155bool ToneGenerator::startTone(int toneType) {
156 bool lResult = false;
157
158 if (toneType >= NUM_TONES)
159 return lResult;
160
161 if (mState == TONE_IDLE) {
162 LOGV("startTone: try to re-init AudioTrack");
163 if (!initAudioTrack()) {
164 return lResult;
165 }
166 }
167
168 LOGV("startTone\n");
169
170 mLock.lock();
171
172 // Get descriptor for requested tone
173 mpNewToneDesc = &toneDescriptors[toneType];
174
175 if (mState == TONE_INIT) {
176 if (prepareWave()) {
177 LOGV("Immediate start, time %d\n", (unsigned int)(systemTime()/1000000));
178 lResult = true;
179 mState = TONE_STARTING;
180 mLock.unlock();
181 mpAudioTrack->start();
182 mLock.lock();
183 if (mState == TONE_STARTING) {
184 LOGV("Wait for start callback");
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700185 status_t lStatus = mWaitCbkCond.waitRelative(mLock, seconds(1));
186 if (lStatus != NO_ERROR) {
187 LOGE("--- Immediate start timed out, status %d", lStatus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 mState = TONE_IDLE;
189 lResult = false;
190 }
191 }
192 } else {
193 mState == TONE_IDLE;
194 }
195 } else {
196 LOGV("Delayed start\n");
197
198 mState = TONE_RESTARTING;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700199 status_t lStatus = mWaitCbkCond.waitRelative(mLock, seconds(1));
200 if (lStatus == NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 if (mState != TONE_IDLE) {
202 lResult = true;
203 }
204 LOGV("cond received");
205 } else {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700206 LOGE("--- Delayed start timed out, status %d", lStatus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 mState = TONE_IDLE;
208 }
209 }
210 mLock.unlock();
211
212 LOGV_IF(lResult, "Tone started, time %d\n", (unsigned int)(systemTime()/1000000));
213 LOGW_IF(!lResult, "Tone start failed!!!, time %d\n", (unsigned int)(systemTime()/1000000));
214
215 return lResult;
216}
217
218////////////////////////////////////////////////////////////////////////////////
219//
220// Method: ToneGenerator::stopTone()
221//
222// Description: Stops tone playback.
223//
224// Input:
225// none
226//
227// Output:
228// none
229//
230////////////////////////////////////////////////////////////////////////////////
231void ToneGenerator::stopTone() {
232 LOGV("stopTone");
233
234 mLock.lock();
235 if (mState == TONE_PLAYING || mState == TONE_STARTING || mState == TONE_RESTARTING) {
236 mState = TONE_STOPPING;
237 LOGV("waiting cond");
238 status_t lStatus = mWaitCbkCond.waitRelative(mLock, seconds(1));
239 if (lStatus == NO_ERROR) {
240 LOGV("track stop complete, time %d", (unsigned int)(systemTime()/1000000));
241 } else {
242 LOGE("--- Stop timed out");
243 mState = TONE_IDLE;
244 mpAudioTrack->stop();
245 }
246 }
247
248 clearWaveGens();
249
250 mLock.unlock();
251}
252
253//---------------------------------- private methods ---------------------------
254
255
256
257
258////////////////////////////////////////////////////////////////////////////////
259//
260// Method: ToneGenerator::initAudioTrack()
261//
262// Description: Allocates and configures AudioTrack used for PCM output.
263//
264// Input:
265// none
266//
267// Output:
268// none
269//
270////////////////////////////////////////////////////////////////////////////////
271bool ToneGenerator::initAudioTrack() {
272
273 if (mpAudioTrack) {
274 delete mpAudioTrack;
275 mpAudioTrack = 0;
276 }
277
278 // Open audio track in mono, PCM 16bit, default sampling rate, default buffer size
279 mpAudioTrack
280 = new AudioTrack(mStreamType, 0, AudioSystem::PCM_16_BIT, 1, 0, 0, audioCallback, this, 0);
281
282 if (mpAudioTrack == 0) {
283 LOGE("AudioTrack allocation failed");
284 goto initAudioTrack_exit;
285 }
286 LOGV("Create Track: %p\n", mpAudioTrack);
287
288 if (mpAudioTrack->initCheck() != NO_ERROR) {
289 LOGE("AudioTrack->initCheck failed");
290 goto initAudioTrack_exit;
291 }
292
293 mpAudioTrack->setVolume(mVolume, mVolume);
294
295 mState = TONE_INIT;
296
297 return true;
298
299initAudioTrack_exit:
300
301 // Cleanup
302 if (mpAudioTrack) {
303 LOGV("Delete Track I: %p\n", mpAudioTrack);
304 delete mpAudioTrack;
305 mpAudioTrack = 0;
306 }
307
308 return false;
309}
310
311
312////////////////////////////////////////////////////////////////////////////////
313//
314// Method: ToneGenerator::audioCallback()
315//
316// Description: AudioTrack callback implementation. Generates a block of
317// PCM samples
318// and manages tone generator sequencer: tones pulses, tone duration...
319//
320// Input:
321// user reference (pointer to our ToneGenerator)
322// info audio buffer descriptor
323//
324// Output:
325// returned value: always true.
326//
327////////////////////////////////////////////////////////////////////////////////
328void ToneGenerator::audioCallback(int event, void* user, void *info) {
329
330 if (event != AudioTrack::EVENT_MORE_DATA) return;
331
332 const AudioTrack::Buffer *buffer = static_cast<const AudioTrack::Buffer *>(info);
333 ToneGenerator *lpToneGen = static_cast<ToneGenerator *>(user);
334 short *lpOut = buffer->i16;
335 unsigned int lNumSmp = buffer->size/sizeof(short);
336
337 if (buffer->size == 0) return;
338
339
340 // Clear output buffer: WaveGenerator accumulates into lpOut buffer
341 memset(lpOut, 0, buffer->size);
342
343 while (lNumSmp) {
344 unsigned int lReqSmp = lNumSmp < lpToneGen->mProcessSize*2 ? lNumSmp : lpToneGen->mProcessSize;
345 unsigned int lGenSmp;
346 unsigned int lWaveCmd = WaveGenerator::WAVEGEN_CONT;
347 bool lSignal = false;
348
349 lpToneGen->mLock.lock();
350
351 // Update pcm frame count and end time (current time at the end of this process)
352 lpToneGen->mTotalSmp += lReqSmp;
353
354 // Update tone gen state machine and select wave gen command
355 switch (lpToneGen->mState) {
356 case TONE_PLAYING:
357 lWaveCmd = WaveGenerator::WAVEGEN_CONT;
358 break;
359 case TONE_STARTING:
360 LOGV("Starting Cbk");
361
362 lWaveCmd = WaveGenerator::WAVEGEN_START;
363 break;
364 case TONE_STOPPING:
365 case TONE_RESTARTING:
366 LOGV("Stop/restart Cbk");
367
368 lWaveCmd = WaveGenerator::WAVEGEN_STOP;
369 lpToneGen->mNextSegSmp = TONEGEN_INF; // forced to skip state machine management below
370 break;
371 default:
372 LOGV("Extra Cbk");
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700373 // Force loop exit
374 lNumSmp = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 goto audioCallback_EndLoop;
376 }
377
378
379 // Exit if tone sequence is over
380 if (lpToneGen->mpToneDesc->segments[lpToneGen->mCurSegment] == 0) {
381 if (lpToneGen->mState == TONE_PLAYING) {
382 lpToneGen->mState = TONE_STOPPING;
383 }
384 goto audioCallback_EndLoop;
385 }
386
387 if (lpToneGen->mTotalSmp > lpToneGen->mNextSegSmp) {
388 // Time to go to next sequence segment
389
390 LOGV("End Segment, time: %d\n", (unsigned int)(systemTime()/1000000));
391
392 lGenSmp = lReqSmp;
393
394 if (lpToneGen->mCurSegment & 0x0001) {
395 // If odd segment, OFF -> ON transition : reset wave generator
396 lWaveCmd = WaveGenerator::WAVEGEN_START;
397
398 LOGV("OFF->ON, lGenSmp: %d, lReqSmp: %d\n", lGenSmp, lReqSmp);
399 } else {
400 // If even segment, ON -> OFF transition : ramp volume down
401 lWaveCmd = WaveGenerator::WAVEGEN_STOP;
402
403 LOGV("ON->OFF, lGenSmp: %d, lReqSmp: %d\n", lGenSmp, lReqSmp);
404 }
405
406 // Pre increment segment index and handle loop if last segment reached
407 if (lpToneGen->mpToneDesc->segments[++lpToneGen->mCurSegment] == 0) {
408 LOGV("Last Seg: %d\n", lpToneGen->mCurSegment);
409
410 // Pre increment loop count and restart if total count not reached. Stop sequence otherwise
411 if (++lpToneGen->mCurCount <= lpToneGen->mpToneDesc->repeatCnt) {
412 LOGV("Repeating Count: %d\n", lpToneGen->mCurCount);
413
414 lpToneGen->mCurSegment = 0;
415
416 LOGV("New segment %d, Next Time: %d\n", lpToneGen->mCurSegment,
417 (lpToneGen->mNextSegSmp*1000)/lpToneGen->mSamplingRate);
418
419 } else {
420 LOGV("End repeat, time: %d\n", (unsigned int)(systemTime()/1000000));
421
422 // Cancel OFF->ON transition in case previous segment tone state was OFF
423 if (!(lpToneGen->mCurSegment & 0x0001)) {
424 lGenSmp = 0;
425 }
426 }
427 } else {
428 LOGV("New segment %d, Next Time: %d\n", lpToneGen->mCurSegment,
429 (lpToneGen->mNextSegSmp*1000)/lpToneGen->mSamplingRate);
430 }
431
432 // Update next segment transition position. No harm to do it also for last segment as lpToneGen->mNextSegSmp won't be used any more
433 lpToneGen->mNextSegSmp
434 += (lpToneGen->mpToneDesc->segments[lpToneGen->mCurSegment] * lpToneGen->mSamplingRate) / 1000;
435
436 } else {
437 // Inside a segment keep tone ON or OFF
438 if (lpToneGen->mCurSegment & 0x0001) {
439 lGenSmp = 0; // If odd segment, tone is currently OFF
440 } else {
441 lGenSmp = lReqSmp; // If event segment, tone is currently ON
442 }
443 }
444
445 if (lGenSmp) {
446 // If samples must be generated, call all active wave generators and acumulate waves in lpOut
447 unsigned int lWaveIdx;
448
449 for (lWaveIdx = 0; lWaveIdx < (unsigned int)lpToneGen->mWaveGens.size(); lWaveIdx++) {
450 WaveGenerator *lpWaveGen = lpToneGen->mWaveGens[lWaveIdx];
451 lpWaveGen->getSamples(lpOut, lGenSmp, lWaveCmd);
452 }
453 }
454
455 lNumSmp -= lReqSmp;
456 lpOut += lReqSmp;
457
458audioCallback_EndLoop:
459
460 switch (lpToneGen->mState) {
461 case TONE_RESTARTING:
462 LOGV("Cbk restarting track\n");
463 if (lpToneGen->prepareWave()) {
464 lpToneGen->mState = TONE_STARTING;
465 } else {
466 LOGW("Cbk restarting prepareWave() failed\n");
467 lpToneGen->mState = TONE_IDLE;
468 lpToneGen->mpAudioTrack->stop();
469 // Force loop exit
470 lNumSmp = 0;
471 }
472 lSignal = true;
473 break;
474 case TONE_STOPPING:
475 lpToneGen->mState = TONE_INIT;
476 LOGV("Cbk Stopping track\n");
477 lSignal = true;
478 lpToneGen->mpAudioTrack->stop();
479
480 // Force loop exit
481 lNumSmp = 0;
482 break;
483 case TONE_STARTING:
484 LOGV("Cbk starting track\n");
485 lpToneGen->mState = TONE_PLAYING;
486 lSignal = true;
487 break;
488 default:
489 break;
490 }
491
492 if (lSignal)
493 lpToneGen->mWaitCbkCond.signal();
494 lpToneGen->mLock.unlock();
495 }
496}
497
498
499////////////////////////////////////////////////////////////////////////////////
500//
501// Method: ToneGenerator::prepareWave()
502//
503// Description: Prepare wave generators and reset tone sequencer state machine.
504// mpNewToneDesc must have been initialized befoire calling this function.
505// Input:
506// none
507//
508// Output:
509// returned value: true if wave generators have been created, false otherwise
510//
511////////////////////////////////////////////////////////////////////////////////
512bool ToneGenerator::prepareWave() {
513 unsigned int lCnt = 0;
514 unsigned int lNumWaves;
515
516 if (!mpNewToneDesc) {
517 return false;
518 }
519 // Remove existing wave generators if any
520 clearWaveGens();
521
522 mpToneDesc = mpNewToneDesc;
523
524 // Get total number of sine waves: needed to adapt sine wave gain.
525 lNumWaves = numWaves();
526
527 // Instantiate as many wave generators as listed in descriptor
528 while (lCnt < lNumWaves) {
529 ToneGenerator::WaveGenerator *lpWaveGen =
530 new ToneGenerator::WaveGenerator((unsigned short)mSamplingRate,
531 mpToneDesc->waveFreq[lCnt],
532 TONEGEN_GAIN/lNumWaves);
533 if (lpWaveGen == 0) {
534 goto prepareWave_exit;
535 }
536
537 mWaveGens.push(lpWaveGen);
538 LOGV("Create sine: %d\n", mpToneDesc->waveFreq[lCnt]);
539 lCnt++;
540 }
541
542 // Initialize tone sequencer
543 mTotalSmp = 0;
544 mCurSegment = 0;
545 mCurCount = 0;
546 mNextSegSmp = (mpToneDesc->segments[0] * mSamplingRate) / 1000;
547
548 return true;
549
550prepareWave_exit:
551
552 clearWaveGens();
553
554 return false;
555}
556
557
558////////////////////////////////////////////////////////////////////////////////
559//
560// Method: ToneGenerator::numWaves()
561//
562// Description: Count number of sine waves needed to generate tone (e.g 2 for DTMF).
563//
564// Input:
565// none
566//
567// Output:
568// returned value: nummber of sine waves
569//
570////////////////////////////////////////////////////////////////////////////////
571unsigned int ToneGenerator::numWaves() {
572 unsigned int lCnt = 0;
573
574 while (mpToneDesc->waveFreq[lCnt]) {
575 lCnt++;
576 }
577
578 return lCnt;
579}
580
581
582////////////////////////////////////////////////////////////////////////////////
583//
584// Method: ToneGenerator::clearWaveGens()
585//
586// Description: Removes all wave generators.
587//
588// Input:
589// none
590//
591// Output:
592// none
593//
594////////////////////////////////////////////////////////////////////////////////
595void ToneGenerator::clearWaveGens() {
596 LOGV("Clearing mWaveGens:");
597
598 while (!mWaveGens.isEmpty()) {
599 delete mWaveGens.top();
600 mWaveGens.pop();
601 }
602}
603
604
605////////////////////////////////////////////////////////////////////////////////
606// WaveGenerator::WaveGenerator class Implementation
607////////////////////////////////////////////////////////////////////////////////
608
609//---------------------------------- public methods ----------------------------
610
611////////////////////////////////////////////////////////////////////////////////
612//
613// Method: WaveGenerator::WaveGenerator()
614//
615// Description: Constructor.
616//
617// Input:
618// samplingRate: Output sampling rate in Hz
619// frequency: Frequency of the sine wave to generate in Hz
620// volume: volume (0.0 to 1.0)
621//
622// Output:
623// none
624//
625////////////////////////////////////////////////////////////////////////////////
626ToneGenerator::WaveGenerator::WaveGenerator(unsigned short samplingRate,
627 unsigned short frequency, float volume) {
628 double d0;
629 double F_div_Fs; // frequency / samplingRate
630
631 F_div_Fs = frequency / (double)samplingRate;
632 d0 = - (float)GEN_AMP * sin(2 * M_PI * F_div_Fs);
633 mS2_0 = (short)d0;
634 mS1 = 0;
635 mS2 = mS2_0;
636
637 mAmplitude_Q15 = (short)(32767. * 32767. * volume / GEN_AMP);
638 // take some margin for amplitude fluctuation
639 if (mAmplitude_Q15 > 32500)
640 mAmplitude_Q15 = 32500;
641
642 d0 = 32768.0 * cos(2 * M_PI * F_div_Fs); // Q14*2*cos()
643 if (d0 > 32767)
644 d0 = 32767;
645 mA1_Q14 = (short) d0;
646
647 LOGV("WaveGenerator init, mA1_Q14: %d, mS2_0: %d, mAmplitude_Q15: %d\n",
648 mA1_Q14, mS2_0, mAmplitude_Q15);
649}
650
651////////////////////////////////////////////////////////////////////////////////
652//
653// Method: WaveGenerator::~WaveGenerator()
654//
655// Description: Destructor.
656//
657// Input:
658// none
659//
660// Output:
661// none
662//
663////////////////////////////////////////////////////////////////////////////////
664ToneGenerator::WaveGenerator::~WaveGenerator() {
665}
666
667////////////////////////////////////////////////////////////////////////////////
668//
669// Method: WaveGenerator::getSamples()
670//
671// Description: Generates count samples of a sine wave and accumulates
672// result in outBuffer.
673//
674// Input:
675// outBuffer: Output buffer where to accumulate samples.
676// count: number of samples to produce.
677// command: special action requested (see enum gen_command).
678//
679// Output:
680// none
681//
682////////////////////////////////////////////////////////////////////////////////
683void ToneGenerator::WaveGenerator::getSamples(short *outBuffer,
684 unsigned int count, unsigned int command) {
685 long lS1, lS2;
686 long lA1, lAmplitude;
687 long Sample; // current sample
688
689 // init local
690 if (command == WAVEGEN_START) {
691 lS1 = (long)0;
692 lS2 = (long)mS2_0;
693 } else {
694 lS1 = (long)mS1;
695 lS2 = (long)mS2;
696 }
697 lA1 = (long)mA1_Q14;
698 lAmplitude = (long)mAmplitude_Q15;
699
700 if (command == WAVEGEN_STOP) {
701 lAmplitude <<= 16;
702 if (count == 0) {
703 return;
704 }
705 long dec = lAmplitude/count;
706 // loop generation
707 while (count--) {
708 Sample = ((lA1 * lS1) >> S_Q14) - lS2;
709 // shift delay
710 lS2 = lS1;
711 lS1 = Sample;
712 Sample = ((lAmplitude>>16) * Sample) >> S_Q15;
713 *(outBuffer++) += (short)Sample; // put result in buffer
714 lAmplitude -= dec;
715 }
716 } else {
717 // loop generation
718 while (count--) {
719 Sample = ((lA1 * lS1) >> S_Q14) - lS2;
720 // shift delay
721 lS2 = lS1;
722 lS1 = Sample;
723 Sample = (lAmplitude * Sample) >> S_Q15;
724 *(outBuffer++) += (short)Sample; // put result in buffer
725 }
726 }
727
728 // save status
729 mS1 = (short)lS1;
730 mS2 = (short)lS2;
731}
732
733} // end namespace android
734