blob: 5416629b8e10af8c7a0fb6847d124a2efc11918a [file] [log] [blame]
The Android Open Source Project89fa4ad2009-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");
185 if (mWaitCbkCond.waitRelative(mLock, seconds(1)) != NO_ERROR) {
186 LOGE("--- Immediate start timed out");
187 mState = TONE_IDLE;
188 lResult = false;
189 }
190 }
191 } else {
192 mState == TONE_IDLE;
193 }
194 } else {
195 LOGV("Delayed start\n");
196
197 mState = TONE_RESTARTING;
198 if (mWaitCbkCond.waitRelative(mLock, seconds(1)) == NO_ERROR) {
199 if (mState != TONE_IDLE) {
200 lResult = true;
201 }
202 LOGV("cond received");
203 } else {
204 LOGE("--- Delayed start timed out");
205 mState = TONE_IDLE;
206 }
207 }
208 mLock.unlock();
209
210 LOGV_IF(lResult, "Tone started, time %d\n", (unsigned int)(systemTime()/1000000));
211 LOGW_IF(!lResult, "Tone start failed!!!, time %d\n", (unsigned int)(systemTime()/1000000));
212
213 return lResult;
214}
215
216////////////////////////////////////////////////////////////////////////////////
217//
218// Method: ToneGenerator::stopTone()
219//
220// Description: Stops tone playback.
221//
222// Input:
223// none
224//
225// Output:
226// none
227//
228////////////////////////////////////////////////////////////////////////////////
229void ToneGenerator::stopTone() {
230 LOGV("stopTone");
231
232 mLock.lock();
233 if (mState == TONE_PLAYING || mState == TONE_STARTING || mState == TONE_RESTARTING) {
234 mState = TONE_STOPPING;
235 LOGV("waiting cond");
236 status_t lStatus = mWaitCbkCond.waitRelative(mLock, seconds(1));
237 if (lStatus == NO_ERROR) {
238 LOGV("track stop complete, time %d", (unsigned int)(systemTime()/1000000));
239 } else {
240 LOGE("--- Stop timed out");
241 mState = TONE_IDLE;
242 mpAudioTrack->stop();
243 }
244 }
245
246 clearWaveGens();
247
248 mLock.unlock();
249}
250
251//---------------------------------- private methods ---------------------------
252
253
254
255
256////////////////////////////////////////////////////////////////////////////////
257//
258// Method: ToneGenerator::initAudioTrack()
259//
260// Description: Allocates and configures AudioTrack used for PCM output.
261//
262// Input:
263// none
264//
265// Output:
266// none
267//
268////////////////////////////////////////////////////////////////////////////////
269bool ToneGenerator::initAudioTrack() {
270
271 if (mpAudioTrack) {
272 delete mpAudioTrack;
273 mpAudioTrack = 0;
274 }
275
276 // Open audio track in mono, PCM 16bit, default sampling rate, default buffer size
277 mpAudioTrack
278 = new AudioTrack(mStreamType, 0, AudioSystem::PCM_16_BIT, 1, 0, 0, audioCallback, this, 0);
279
280 if (mpAudioTrack == 0) {
281 LOGE("AudioTrack allocation failed");
282 goto initAudioTrack_exit;
283 }
284 LOGV("Create Track: %p\n", mpAudioTrack);
285
286 if (mpAudioTrack->initCheck() != NO_ERROR) {
287 LOGE("AudioTrack->initCheck failed");
288 goto initAudioTrack_exit;
289 }
290
291 mpAudioTrack->setVolume(mVolume, mVolume);
292
293 mState = TONE_INIT;
294
295 return true;
296
297initAudioTrack_exit:
298
299 // Cleanup
300 if (mpAudioTrack) {
301 LOGV("Delete Track I: %p\n", mpAudioTrack);
302 delete mpAudioTrack;
303 mpAudioTrack = 0;
304 }
305
306 return false;
307}
308
309
310////////////////////////////////////////////////////////////////////////////////
311//
312// Method: ToneGenerator::audioCallback()
313//
314// Description: AudioTrack callback implementation. Generates a block of
315// PCM samples
316// and manages tone generator sequencer: tones pulses, tone duration...
317//
318// Input:
319// user reference (pointer to our ToneGenerator)
320// info audio buffer descriptor
321//
322// Output:
323// returned value: always true.
324//
325////////////////////////////////////////////////////////////////////////////////
326void ToneGenerator::audioCallback(int event, void* user, void *info) {
327
328 if (event != AudioTrack::EVENT_MORE_DATA) return;
329
330 const AudioTrack::Buffer *buffer = static_cast<const AudioTrack::Buffer *>(info);
331 ToneGenerator *lpToneGen = static_cast<ToneGenerator *>(user);
332 short *lpOut = buffer->i16;
333 unsigned int lNumSmp = buffer->size/sizeof(short);
334
335 if (buffer->size == 0) return;
336
337
338 // Clear output buffer: WaveGenerator accumulates into lpOut buffer
339 memset(lpOut, 0, buffer->size);
340
341 while (lNumSmp) {
342 unsigned int lReqSmp = lNumSmp < lpToneGen->mProcessSize*2 ? lNumSmp : lpToneGen->mProcessSize;
343 unsigned int lGenSmp;
344 unsigned int lWaveCmd = WaveGenerator::WAVEGEN_CONT;
345 bool lSignal = false;
346
347 lpToneGen->mLock.lock();
348
349 // Update pcm frame count and end time (current time at the end of this process)
350 lpToneGen->mTotalSmp += lReqSmp;
351
352 // Update tone gen state machine and select wave gen command
353 switch (lpToneGen->mState) {
354 case TONE_PLAYING:
355 lWaveCmd = WaveGenerator::WAVEGEN_CONT;
356 break;
357 case TONE_STARTING:
358 LOGV("Starting Cbk");
359
360 lWaveCmd = WaveGenerator::WAVEGEN_START;
361 break;
362 case TONE_STOPPING:
363 case TONE_RESTARTING:
364 LOGV("Stop/restart Cbk");
365
366 lWaveCmd = WaveGenerator::WAVEGEN_STOP;
367 lpToneGen->mNextSegSmp = TONEGEN_INF; // forced to skip state machine management below
368 break;
369 default:
370 LOGV("Extra Cbk");
371 goto audioCallback_EndLoop;
372 }
373
374
375 // Exit if tone sequence is over
376 if (lpToneGen->mpToneDesc->segments[lpToneGen->mCurSegment] == 0) {
377 if (lpToneGen->mState == TONE_PLAYING) {
378 lpToneGen->mState = TONE_STOPPING;
379 }
380 goto audioCallback_EndLoop;
381 }
382
383 if (lpToneGen->mTotalSmp > lpToneGen->mNextSegSmp) {
384 // Time to go to next sequence segment
385
386 LOGV("End Segment, time: %d\n", (unsigned int)(systemTime()/1000000));
387
388 lGenSmp = lReqSmp;
389
390 if (lpToneGen->mCurSegment & 0x0001) {
391 // If odd segment, OFF -> ON transition : reset wave generator
392 lWaveCmd = WaveGenerator::WAVEGEN_START;
393
394 LOGV("OFF->ON, lGenSmp: %d, lReqSmp: %d\n", lGenSmp, lReqSmp);
395 } else {
396 // If even segment, ON -> OFF transition : ramp volume down
397 lWaveCmd = WaveGenerator::WAVEGEN_STOP;
398
399 LOGV("ON->OFF, lGenSmp: %d, lReqSmp: %d\n", lGenSmp, lReqSmp);
400 }
401
402 // Pre increment segment index and handle loop if last segment reached
403 if (lpToneGen->mpToneDesc->segments[++lpToneGen->mCurSegment] == 0) {
404 LOGV("Last Seg: %d\n", lpToneGen->mCurSegment);
405
406 // Pre increment loop count and restart if total count not reached. Stop sequence otherwise
407 if (++lpToneGen->mCurCount <= lpToneGen->mpToneDesc->repeatCnt) {
408 LOGV("Repeating Count: %d\n", lpToneGen->mCurCount);
409
410 lpToneGen->mCurSegment = 0;
411
412 LOGV("New segment %d, Next Time: %d\n", lpToneGen->mCurSegment,
413 (lpToneGen->mNextSegSmp*1000)/lpToneGen->mSamplingRate);
414
415 } else {
416 LOGV("End repeat, time: %d\n", (unsigned int)(systemTime()/1000000));
417
418 // Cancel OFF->ON transition in case previous segment tone state was OFF
419 if (!(lpToneGen->mCurSegment & 0x0001)) {
420 lGenSmp = 0;
421 }
422 }
423 } else {
424 LOGV("New segment %d, Next Time: %d\n", lpToneGen->mCurSegment,
425 (lpToneGen->mNextSegSmp*1000)/lpToneGen->mSamplingRate);
426 }
427
428 // Update next segment transition position. No harm to do it also for last segment as lpToneGen->mNextSegSmp won't be used any more
429 lpToneGen->mNextSegSmp
430 += (lpToneGen->mpToneDesc->segments[lpToneGen->mCurSegment] * lpToneGen->mSamplingRate) / 1000;
431
432 } else {
433 // Inside a segment keep tone ON or OFF
434 if (lpToneGen->mCurSegment & 0x0001) {
435 lGenSmp = 0; // If odd segment, tone is currently OFF
436 } else {
437 lGenSmp = lReqSmp; // If event segment, tone is currently ON
438 }
439 }
440
441 if (lGenSmp) {
442 // If samples must be generated, call all active wave generators and acumulate waves in lpOut
443 unsigned int lWaveIdx;
444
445 for (lWaveIdx = 0; lWaveIdx < (unsigned int)lpToneGen->mWaveGens.size(); lWaveIdx++) {
446 WaveGenerator *lpWaveGen = lpToneGen->mWaveGens[lWaveIdx];
447 lpWaveGen->getSamples(lpOut, lGenSmp, lWaveCmd);
448 }
449 }
450
451 lNumSmp -= lReqSmp;
452 lpOut += lReqSmp;
453
454audioCallback_EndLoop:
455
456 switch (lpToneGen->mState) {
457 case TONE_RESTARTING:
458 LOGV("Cbk restarting track\n");
459 if (lpToneGen->prepareWave()) {
460 lpToneGen->mState = TONE_STARTING;
461 } else {
462 LOGW("Cbk restarting prepareWave() failed\n");
463 lpToneGen->mState = TONE_IDLE;
464 lpToneGen->mpAudioTrack->stop();
465 // Force loop exit
466 lNumSmp = 0;
467 }
468 lSignal = true;
469 break;
470 case TONE_STOPPING:
471 lpToneGen->mState = TONE_INIT;
472 LOGV("Cbk Stopping track\n");
473 lSignal = true;
474 lpToneGen->mpAudioTrack->stop();
475
476 // Force loop exit
477 lNumSmp = 0;
478 break;
479 case TONE_STARTING:
480 LOGV("Cbk starting track\n");
481 lpToneGen->mState = TONE_PLAYING;
482 lSignal = true;
483 break;
484 default:
485 break;
486 }
487
488 if (lSignal)
489 lpToneGen->mWaitCbkCond.signal();
490 lpToneGen->mLock.unlock();
491 }
492}
493
494
495////////////////////////////////////////////////////////////////////////////////
496//
497// Method: ToneGenerator::prepareWave()
498//
499// Description: Prepare wave generators and reset tone sequencer state machine.
500// mpNewToneDesc must have been initialized befoire calling this function.
501// Input:
502// none
503//
504// Output:
505// returned value: true if wave generators have been created, false otherwise
506//
507////////////////////////////////////////////////////////////////////////////////
508bool ToneGenerator::prepareWave() {
509 unsigned int lCnt = 0;
510 unsigned int lNumWaves;
511
512 if (!mpNewToneDesc) {
513 return false;
514 }
515 // Remove existing wave generators if any
516 clearWaveGens();
517
518 mpToneDesc = mpNewToneDesc;
519
520 // Get total number of sine waves: needed to adapt sine wave gain.
521 lNumWaves = numWaves();
522
523 // Instantiate as many wave generators as listed in descriptor
524 while (lCnt < lNumWaves) {
525 ToneGenerator::WaveGenerator *lpWaveGen =
526 new ToneGenerator::WaveGenerator((unsigned short)mSamplingRate,
527 mpToneDesc->waveFreq[lCnt],
528 TONEGEN_GAIN/lNumWaves);
529 if (lpWaveGen == 0) {
530 goto prepareWave_exit;
531 }
532
533 mWaveGens.push(lpWaveGen);
534 LOGV("Create sine: %d\n", mpToneDesc->waveFreq[lCnt]);
535 lCnt++;
536 }
537
538 // Initialize tone sequencer
539 mTotalSmp = 0;
540 mCurSegment = 0;
541 mCurCount = 0;
542 mNextSegSmp = (mpToneDesc->segments[0] * mSamplingRate) / 1000;
543
544 return true;
545
546prepareWave_exit:
547
548 clearWaveGens();
549
550 return false;
551}
552
553
554////////////////////////////////////////////////////////////////////////////////
555//
556// Method: ToneGenerator::numWaves()
557//
558// Description: Count number of sine waves needed to generate tone (e.g 2 for DTMF).
559//
560// Input:
561// none
562//
563// Output:
564// returned value: nummber of sine waves
565//
566////////////////////////////////////////////////////////////////////////////////
567unsigned int ToneGenerator::numWaves() {
568 unsigned int lCnt = 0;
569
570 while (mpToneDesc->waveFreq[lCnt]) {
571 lCnt++;
572 }
573
574 return lCnt;
575}
576
577
578////////////////////////////////////////////////////////////////////////////////
579//
580// Method: ToneGenerator::clearWaveGens()
581//
582// Description: Removes all wave generators.
583//
584// Input:
585// none
586//
587// Output:
588// none
589//
590////////////////////////////////////////////////////////////////////////////////
591void ToneGenerator::clearWaveGens() {
592 LOGV("Clearing mWaveGens:");
593
594 while (!mWaveGens.isEmpty()) {
595 delete mWaveGens.top();
596 mWaveGens.pop();
597 }
598}
599
600
601////////////////////////////////////////////////////////////////////////////////
602// WaveGenerator::WaveGenerator class Implementation
603////////////////////////////////////////////////////////////////////////////////
604
605//---------------------------------- public methods ----------------------------
606
607////////////////////////////////////////////////////////////////////////////////
608//
609// Method: WaveGenerator::WaveGenerator()
610//
611// Description: Constructor.
612//
613// Input:
614// samplingRate: Output sampling rate in Hz
615// frequency: Frequency of the sine wave to generate in Hz
616// volume: volume (0.0 to 1.0)
617//
618// Output:
619// none
620//
621////////////////////////////////////////////////////////////////////////////////
622ToneGenerator::WaveGenerator::WaveGenerator(unsigned short samplingRate,
623 unsigned short frequency, float volume) {
624 double d0;
625 double F_div_Fs; // frequency / samplingRate
626
627 F_div_Fs = frequency / (double)samplingRate;
628 d0 = - (float)GEN_AMP * sin(2 * M_PI * F_div_Fs);
629 mS2_0 = (short)d0;
630 mS1 = 0;
631 mS2 = mS2_0;
632
633 mAmplitude_Q15 = (short)(32767. * 32767. * volume / GEN_AMP);
634 // take some margin for amplitude fluctuation
635 if (mAmplitude_Q15 > 32500)
636 mAmplitude_Q15 = 32500;
637
638 d0 = 32768.0 * cos(2 * M_PI * F_div_Fs); // Q14*2*cos()
639 if (d0 > 32767)
640 d0 = 32767;
641 mA1_Q14 = (short) d0;
642
643 LOGV("WaveGenerator init, mA1_Q14: %d, mS2_0: %d, mAmplitude_Q15: %d\n",
644 mA1_Q14, mS2_0, mAmplitude_Q15);
645}
646
647////////////////////////////////////////////////////////////////////////////////
648//
649// Method: WaveGenerator::~WaveGenerator()
650//
651// Description: Destructor.
652//
653// Input:
654// none
655//
656// Output:
657// none
658//
659////////////////////////////////////////////////////////////////////////////////
660ToneGenerator::WaveGenerator::~WaveGenerator() {
661}
662
663////////////////////////////////////////////////////////////////////////////////
664//
665// Method: WaveGenerator::getSamples()
666//
667// Description: Generates count samples of a sine wave and accumulates
668// result in outBuffer.
669//
670// Input:
671// outBuffer: Output buffer where to accumulate samples.
672// count: number of samples to produce.
673// command: special action requested (see enum gen_command).
674//
675// Output:
676// none
677//
678////////////////////////////////////////////////////////////////////////////////
679void ToneGenerator::WaveGenerator::getSamples(short *outBuffer,
680 unsigned int count, unsigned int command) {
681 long lS1, lS2;
682 long lA1, lAmplitude;
683 long Sample; // current sample
684
685 // init local
686 if (command == WAVEGEN_START) {
687 lS1 = (long)0;
688 lS2 = (long)mS2_0;
689 } else {
690 lS1 = (long)mS1;
691 lS2 = (long)mS2;
692 }
693 lA1 = (long)mA1_Q14;
694 lAmplitude = (long)mAmplitude_Q15;
695
696 if (command == WAVEGEN_STOP) {
697 lAmplitude <<= 16;
698 if (count == 0) {
699 return;
700 }
701 long dec = lAmplitude/count;
702 // loop generation
703 while (count--) {
704 Sample = ((lA1 * lS1) >> S_Q14) - lS2;
705 // shift delay
706 lS2 = lS1;
707 lS1 = Sample;
708 Sample = ((lAmplitude>>16) * Sample) >> S_Q15;
709 *(outBuffer++) += (short)Sample; // put result in buffer
710 lAmplitude -= dec;
711 }
712 } else {
713 // loop generation
714 while (count--) {
715 Sample = ((lA1 * lS1) >> S_Q14) - lS2;
716 // shift delay
717 lS2 = lS1;
718 lS1 = Sample;
719 Sample = (lAmplitude * Sample) >> S_Q15;
720 *(outBuffer++) += (short)Sample; // put result in buffer
721 }
722 }
723
724 // save status
725 mS1 = (short)lS1;
726 mS2 = (short)lS2;
727}
728
729} // end namespace android
730