blob: e57f4a46aa6690d96d0f8f2c2c3742dff92fc02c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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 "SoundPool"
19#include <utils/Log.h>
20
Eric Laurent45fce582009-07-22 11:12:31 -070021//#define USE_SHARED_MEM_BUFFER
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
23// XXX needed for timing latency
24#include <utils/Timers.h>
25
26#include <sys/resource.h>
27#include <media/AudioTrack.h>
28#include <media/mediaplayer.h>
29
30#include "SoundPool.h"
31#include "SoundPoolThread.h"
32
33namespace android
34{
35
36int kDefaultBufferCount = 4;
37uint32_t kMaxSampleRate = 48000;
38uint32_t kDefaultSampleRate = 44100;
39uint32_t kDefaultFrameCount = 1200;
40
Dave Sparksc0e3ddf2009-12-07 12:36:20 -080041SoundPool::SoundPool(int maxChannels, int streamType, int srcQuality)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042{
43 LOGV("SoundPool constructor: maxChannels=%d, streamType=%d, srcQuality=%d",
44 maxChannels, streamType, srcQuality);
45
Dave Sparks3c8704b2009-05-29 19:12:11 -070046 // check limits
47 mMaxChannels = maxChannels;
48 if (mMaxChannels < 1) {
49 mMaxChannels = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 }
Dave Sparks3c8704b2009-05-29 19:12:11 -070051 else if (mMaxChannels > 32) {
52 mMaxChannels = 32;
53 }
54 LOGW_IF(maxChannels != mMaxChannels, "App requested %d channels", maxChannels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
56 mQuit = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 mDecodeThread = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 mStreamType = streamType;
59 mSrcQuality = srcQuality;
60 mAllocated = 0;
61 mNextSampleID = 0;
62 mNextChannelID = 0;
63
Dave Sparksf6e43bf2009-12-08 08:10:42 -080064 mCallback = 0;
65 mUserData = 0;
66
Dave Sparks3c8704b2009-05-29 19:12:11 -070067 mChannelPool = new SoundChannel[mMaxChannels];
68 for (int i = 0; i < mMaxChannels; ++i) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 mChannelPool[i].init(this);
70 mChannels.push_back(&mChannelPool[i]);
71 }
72
73 // start decode thread
74 startThreads();
75}
76
77SoundPool::~SoundPool()
78{
79 LOGV("SoundPool destructor");
80 mDecodeThread->quit();
81 quit();
82
83 Mutex::Autolock lock(&mLock);
84 mChannels.clear();
85 if (mChannelPool)
86 delete [] mChannelPool;
87
88 // clean up samples
89 LOGV("clear samples");
90 mSamples.clear();
91
92 if (mDecodeThread)
93 delete mDecodeThread;
94}
95
96void SoundPool::addToRestartList(SoundChannel* channel)
97{
Eric Laurentfd8c0e12009-07-31 06:29:13 -070098 Mutex::Autolock lock(&mRestartLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 mRestart.push_back(channel);
100 mCondition.signal();
101}
102
103int SoundPool::beginThread(void* arg)
104{
105 SoundPool* p = (SoundPool*)arg;
106 return p->run();
107}
108
109int SoundPool::run()
110{
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700111 mRestartLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 while (!mQuit) {
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700113 mCondition.wait(mRestartLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 LOGV("awake");
115 if (mQuit) break;
116
117 while (!mRestart.empty()) {
118 SoundChannel* channel;
119 LOGV("Getting channel from list");
120 List<SoundChannel*>::iterator iter = mRestart.begin();
121 channel = *iter;
122 mRestart.erase(iter);
123 if (channel) channel->nextEvent();
124 if (mQuit) break;
125 }
126 }
127
128 mRestart.clear();
129 mCondition.signal();
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700130 mRestartLock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 LOGV("goodbye");
132 return 0;
133}
134
135void SoundPool::quit()
136{
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700137 mRestartLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 mQuit = true;
139 mCondition.signal();
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700140 mCondition.wait(mRestartLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 LOGV("return from quit");
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700142 mRestartLock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143}
144
145bool SoundPool::startThreads()
146{
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800147 createThreadEtc(beginThread, this, "SoundPoolThread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 if (mDecodeThread == NULL)
149 mDecodeThread = new SoundPoolThread(this);
150 return mDecodeThread != NULL;
151}
152
153SoundChannel* SoundPool::findChannel(int channelID)
154{
155 for (int i = 0; i < mMaxChannels; ++i) {
156 if (mChannelPool[i].channelID() == channelID) {
157 return &mChannelPool[i];
158 }
159 }
160 return NULL;
161}
162
163SoundChannel* SoundPool::findNextChannel(int channelID)
164{
165 for (int i = 0; i < mMaxChannels; ++i) {
166 if (mChannelPool[i].nextChannelID() == channelID) {
167 return &mChannelPool[i];
168 }
169 }
170 return NULL;
171}
172
173int SoundPool::load(const char* path, int priority)
174{
175 LOGV("load: path=%s, priority=%d", path, priority);
176 Mutex::Autolock lock(&mLock);
177 sp<Sample> sample = new Sample(++mNextSampleID, path);
178 mSamples.add(sample->sampleID(), sample);
179 doLoad(sample);
180 return sample->sampleID();
181}
182
183int SoundPool::load(int fd, int64_t offset, int64_t length, int priority)
184{
185 LOGV("load: fd=%d, offset=%lld, length=%lld, priority=%d",
186 fd, offset, length, priority);
187 Mutex::Autolock lock(&mLock);
188 sp<Sample> sample = new Sample(++mNextSampleID, fd, offset, length);
189 mSamples.add(sample->sampleID(), sample);
190 doLoad(sample);
191 return sample->sampleID();
192}
193
194void SoundPool::doLoad(sp<Sample>& sample)
195{
196 LOGV("doLoad: loading sample sampleID=%d", sample->sampleID());
197 sample->startLoad();
198 mDecodeThread->loadSample(sample->sampleID());
199}
200
201bool SoundPool::unload(int sampleID)
202{
203 LOGV("unload: sampleID=%d", sampleID);
204 Mutex::Autolock lock(&mLock);
205 return mSamples.removeItem(sampleID);
206}
207
208int SoundPool::play(int sampleID, float leftVolume, float rightVolume,
209 int priority, int loop, float rate)
210{
211 LOGV("sampleID=%d, leftVolume=%f, rightVolume=%f, priority=%d, loop=%d, rate=%f",
212 sampleID, leftVolume, rightVolume, priority, loop, rate);
213 sp<Sample> sample;
214 SoundChannel* channel;
215 int channelID;
216
217 // scope for lock
218 {
219 Mutex::Autolock lock(&mLock);
220
221 // is sample ready?
222 sample = findSample(sampleID);
223 if ((sample == 0) || (sample->state() != Sample::READY)) {
224 LOGW(" sample %d not READY", sampleID);
225 return 0;
226 }
227
228 dump();
229
230 // allocate a channel
231 channel = allocateChannel(priority);
232
233 // no channel allocated - return 0
234 if (!channel) {
235 LOGV("No channel allocated");
236 return 0;
237 }
238
239 channelID = ++mNextChannelID;
240 }
241
242 LOGV("channel state = %d", channel->state());
243 channel->play(sample, channelID, leftVolume, rightVolume, priority, loop, rate);
244 return channelID;
245}
246
247SoundChannel* SoundPool::allocateChannel(int priority)
248{
249 List<SoundChannel*>::iterator iter;
250 SoundChannel* channel = NULL;
251
252 // allocate a channel
253 if (!mChannels.empty()) {
254 iter = mChannels.begin();
255 if (priority >= (*iter)->priority()) {
256 channel = *iter;
257 mChannels.erase(iter);
258 LOGV("Allocated active channel");
259 }
260 }
261
262 // update priority and put it back in the list
263 if (channel) {
264 channel->setPriority(priority);
265 for (iter = mChannels.begin(); iter != mChannels.end(); ++iter) {
266 if (priority < (*iter)->priority()) {
267 break;
268 }
269 }
270 mChannels.insert(iter, channel);
271 }
272 return channel;
273}
274
275// move a channel from its current position to the front of the list
276void SoundPool::moveToFront(SoundChannel* channel)
277{
278 for (List<SoundChannel*>::iterator iter = mChannels.begin(); iter != mChannels.end(); ++iter) {
279 if (*iter == channel) {
280 mChannels.erase(iter);
281 mChannels.push_front(channel);
282 break;
283 }
284 }
285}
286
287void SoundPool::pause(int channelID)
288{
289 LOGV("pause(%d)", channelID);
290 Mutex::Autolock lock(&mLock);
291 SoundChannel* channel = findChannel(channelID);
292 if (channel) {
293 channel->pause();
294 }
295}
296
297void SoundPool::resume(int channelID)
298{
299 LOGV("resume(%d)", channelID);
300 Mutex::Autolock lock(&mLock);
301 SoundChannel* channel = findChannel(channelID);
302 if (channel) {
303 channel->resume();
304 }
305}
306
307void SoundPool::stop(int channelID)
308{
309 LOGV("stop(%d)", channelID);
310 Mutex::Autolock lock(&mLock);
311 SoundChannel* channel = findChannel(channelID);
312 if (channel) {
313 channel->stop();
314 } else {
315 channel = findNextChannel(channelID);
316 if (channel)
317 channel->clearNextEvent();
318 }
319}
320
321void SoundPool::setVolume(int channelID, float leftVolume, float rightVolume)
322{
323 Mutex::Autolock lock(&mLock);
324 SoundChannel* channel = findChannel(channelID);
325 if (channel) {
326 channel->setVolume(leftVolume, rightVolume);
327 }
328}
329
330void SoundPool::setPriority(int channelID, int priority)
331{
332 LOGV("setPriority(%d, %d)", channelID, priority);
333 Mutex::Autolock lock(&mLock);
334 SoundChannel* channel = findChannel(channelID);
335 if (channel) {
336 channel->setPriority(priority);
337 }
338}
339
340void SoundPool::setLoop(int channelID, int loop)
341{
342 LOGV("setLoop(%d, %d)", channelID, loop);
343 Mutex::Autolock lock(&mLock);
344 SoundChannel* channel = findChannel(channelID);
345 if (channel) {
346 channel->setLoop(loop);
347 }
348}
349
350void SoundPool::setRate(int channelID, float rate)
351{
352 LOGV("setRate(%d, %f)", channelID, rate);
353 Mutex::Autolock lock(&mLock);
354 SoundChannel* channel = findChannel(channelID);
355 if (channel) {
356 channel->setRate(rate);
357 }
358}
359
360// call with lock held
361void SoundPool::done(SoundChannel* channel)
362{
363 LOGV("done(%d)", channel->channelID());
364
365 // if "stolen", play next event
366 if (channel->nextChannelID() != 0) {
367 LOGV("add to restart list");
368 addToRestartList(channel);
369 }
370
371 // return to idle state
372 else {
373 LOGV("move to front");
374 moveToFront(channel);
375 }
376}
377
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800378void SoundPool::setCallback(SoundPoolCallback* callback, void* user)
379{
380 Mutex::Autolock lock(&mCallbackLock);
381 mCallback = callback;
382 mUserData = user;
383}
384
385void SoundPool::notify(SoundPoolEvent event)
386{
387 Mutex::Autolock lock(&mCallbackLock);
388 if (mCallback != NULL) {
389 mCallback(event, this, mUserData);
390 }
391}
392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393void SoundPool::dump()
394{
395 for (int i = 0; i < mMaxChannels; ++i) {
396 mChannelPool[i].dump();
397 }
398}
399
400
401Sample::Sample(int sampleID, const char* url)
402{
403 init();
404 mSampleID = sampleID;
405 mUrl = strdup(url);
406 LOGV("create sampleID=%d, url=%s", mSampleID, mUrl);
407}
408
409Sample::Sample(int sampleID, int fd, int64_t offset, int64_t length)
410{
411 init();
412 mSampleID = sampleID;
413 mFd = dup(fd);
414 mOffset = offset;
415 mLength = length;
416 LOGV("create sampleID=%d, fd=%d, offset=%lld, length=%lld", mSampleID, mFd, mLength, mOffset);
417}
418
419void Sample::init()
420{
421 mData = 0;
422 mSize = 0;
423 mRefCount = 0;
424 mSampleID = 0;
425 mState = UNLOADED;
426 mFd = -1;
427 mOffset = 0;
428 mLength = 0;
429 mUrl = 0;
430}
431
432Sample::~Sample()
433{
434 LOGV("Sample::destructor sampleID=%d, fd=%d", mSampleID, mFd);
435 if (mFd > 0) {
436 LOGV("close(%d)", mFd);
437 ::close(mFd);
438 }
439 mData.clear();
440 delete mUrl;
441}
442
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800443status_t Sample::doLoad()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444{
445 uint32_t sampleRate;
446 int numChannels;
447 int format;
448 sp<IMemory> p;
449 LOGV("Start decode");
450 if (mUrl) {
451 p = MediaPlayer::decode(mUrl, &sampleRate, &numChannels, &format);
452 } else {
453 p = MediaPlayer::decode(mFd, mOffset, mLength, &sampleRate, &numChannels, &format);
454 LOGV("close(%d)", mFd);
455 ::close(mFd);
456 mFd = -1;
457 }
458 if (p == 0) {
459 LOGE("Unable to load sample: %s", mUrl);
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800460 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462 LOGV("pointer = %p, size = %u, sampleRate = %u, numChannels = %d",
463 p->pointer(), p->size(), sampleRate, numChannels);
464
465 if (sampleRate > kMaxSampleRate) {
466 LOGE("Sample rate (%u) out of range", sampleRate);
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800467 return - 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 }
469
470 if ((numChannels < 1) || (numChannels > 2)) {
471 LOGE("Sample channel count (%d) out of range", numChannels);
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800472 return - 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 }
474
475 //_dumpBuffer(p->pointer(), p->size());
476 uint8_t* q = static_cast<uint8_t*>(p->pointer()) + p->size() - 10;
477 //_dumpBuffer(q, 10, 10, false);
478
479 mData = p;
480 mSize = p->size();
481 mSampleRate = sampleRate;
482 mNumChannels = numChannels;
483 mFormat = format;
484 mState = READY;
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800485 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486}
487
488
489void SoundChannel::init(SoundPool* soundPool)
490{
491 mSoundPool = soundPool;
492}
493
494void SoundChannel::play(const sp<Sample>& sample, int nextChannelID, float leftVolume,
495 float rightVolume, int priority, int loop, float rate)
496{
497 AudioTrack* oldTrack;
498
499 LOGV("play %p: sampleID=%d, channelID=%d, leftVolume=%f, rightVolume=%f, priority=%d, loop=%d, rate=%f",
500 this, sample->sampleID(), nextChannelID, leftVolume, rightVolume, priority, loop, rate);
501
502 // if not idle, this voice is being stolen
503 if (mState != IDLE) {
504 LOGV("channel %d stolen - event queued for channel %d", channelID(), nextChannelID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 mNextEvent.set(sample, nextChannelID, leftVolume, rightVolume, priority, loop, rate);
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700506 stop();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 return;
508 }
509
510 // initialize track
511 int afFrameCount;
512 int afSampleRate;
513 int streamType = mSoundPool->streamType();
514 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
515 afFrameCount = kDefaultFrameCount;
516 }
517 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
518 afSampleRate = kDefaultSampleRate;
519 }
520 int numChannels = sample->numChannels();
521 uint32_t sampleRate = uint32_t(float(sample->sampleRate()) * rate + 0.5);
Dave Sparks66d28ce2009-12-14 21:12:05 -0800522 uint32_t totalFrames = (kDefaultBufferCount * afFrameCount * sampleRate) / afSampleRate;
523 uint32_t bufferFrames = (totalFrames + (kDefaultBufferCount - 1)) / kDefaultBufferCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 uint32_t frameCount = 0;
525
526 if (loop) {
527 frameCount = sample->size()/numChannels/((sample->format() == AudioSystem::PCM_16_BIT) ? sizeof(int16_t) : sizeof(uint8_t));
528 }
529
Eric Laurent9648e4b2009-05-07 03:14:31 -0700530#ifndef USE_SHARED_MEM_BUFFER
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 // Ensure minimum audio buffer size in case of short looped sample
Dave Sparks66d28ce2009-12-14 21:12:05 -0800532 if(frameCount < totalFrames) {
533 frameCount = totalFrames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 }
Eric Laurent9648e4b2009-05-07 03:14:31 -0700535#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536
537 AudioTrack* newTrack;
Dave Sparks66d28ce2009-12-14 21:12:05 -0800538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 // mToggle toggles each time a track is started on a given channel.
540 // The toggle is concatenated with the SoundChannel address and passed to AudioTrack
541 // as callback user data. This enables the detection of callbacks received from the old
542 // audio track while the new one is being started and avoids processing them with
543 // wrong audio audio buffer size (mAudioBufferSize)
544 unsigned long toggle = mToggle ^ 1;
545 void *userData = (void *)((unsigned long)this | toggle);
Eric Laurenta553c252009-07-17 12:17:14 -0700546 uint32_t channels = (numChannels == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO;
Dave Sparks66d28ce2009-12-14 21:12:05 -0800547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548#ifdef USE_SHARED_MEM_BUFFER
549 newTrack = new AudioTrack(streamType, sampleRate, sample->format(),
Eric Laurenta553c252009-07-17 12:17:14 -0700550 channels, sample->getIMemory(), 0, callback, userData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551#else
552 newTrack = new AudioTrack(streamType, sampleRate, sample->format(),
Eric Laurenta553c252009-07-17 12:17:14 -0700553 channels, frameCount, 0, callback, userData, bufferFrames);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554#endif
555 if (newTrack->initCheck() != NO_ERROR) {
556 LOGE("Error creating AudioTrack");
557 delete newTrack;
558 return;
559 }
560 LOGV("setVolume %p", newTrack);
561 newTrack->setVolume(leftVolume, rightVolume);
562 newTrack->setLoop(0, frameCount, loop);
563
564 {
565 Mutex::Autolock lock(&mLock);
566 // From now on, AudioTrack callbacks recevieved with previous toggle value will be ignored.
567 mToggle = toggle;
568 oldTrack = mAudioTrack;
569 mAudioTrack = newTrack;
570 mPos = 0;
571 mSample = sample;
572 mChannelID = nextChannelID;
573 mPriority = priority;
574 mLoop = loop;
575 mLeftVolume = leftVolume;
576 mRightVolume = rightVolume;
577 mNumChannels = numChannels;
578 mRate = rate;
579 clearNextEvent();
580 mState = PLAYING;
581 mAudioTrack->start();
582 mAudioBufferSize = newTrack->frameCount()*newTrack->frameSize();
583 }
584
585 LOGV("delete oldTrack %p", oldTrack);
586 delete oldTrack;
587}
588
589void SoundChannel::nextEvent()
590{
591 sp<Sample> sample;
592 int nextChannelID;
593 float leftVolume;
594 float rightVolume;
595 int priority;
596 int loop;
597 float rate;
598
599 // check for valid event
600 {
601 Mutex::Autolock lock(&mLock);
602 nextChannelID = mNextEvent.channelID();
603 if (nextChannelID == 0) {
604 LOGV("stolen channel has no event");
605 return;
606 }
607
608 sample = mNextEvent.sample();
609 leftVolume = mNextEvent.leftVolume();
610 rightVolume = mNextEvent.rightVolume();
611 priority = mNextEvent.priority();
612 loop = mNextEvent.loop();
613 rate = mNextEvent.rate();
614 }
615
616 LOGV("Starting stolen channel %d -> %d", channelID(), nextChannelID);
617 play(sample, nextChannelID, leftVolume, rightVolume, priority, loop, rate);
618}
619
620void SoundChannel::callback(int event, void* user, void *info)
621{
622 unsigned long toggle = (unsigned long)user & 1;
623 SoundChannel* channel = static_cast<SoundChannel*>((void *)((unsigned long)user & ~1));
624
625 if (channel->mToggle != toggle) {
626 LOGV("callback with wrong toggle");
627 return;
628 }
629 channel->process(event, info);
630}
631
632void SoundChannel::process(int event, void *info)
633{
634 //LOGV("process(%d)", mChannelID);
635 sp<Sample> sample = mSample;
636
637// LOGV("SoundChannel::process event %d", event);
638
639 if (event == AudioTrack::EVENT_MORE_DATA) {
640 AudioTrack::Buffer* b = static_cast<AudioTrack::Buffer *>(info);
641
642 // check for stop state
643 if (b->size == 0) return;
644
645 if (sample != 0) {
646 // fill buffer
647 uint8_t* q = (uint8_t*) b->i8;
648 size_t count = 0;
649
650 if (mPos < (int)sample->size()) {
651 uint8_t* p = sample->data() + mPos;
652 count = sample->size() - mPos;
653 if (count > b->size) {
654 count = b->size;
655 }
656 memcpy(q, p, count);
657 LOGV("fill: q=%p, p=%p, mPos=%u, b->size=%u, count=%d", q, p, mPos, b->size, count);
658 } else if (mPos < mAudioBufferSize) {
659 count = mAudioBufferSize - mPos;
660 if (count > b->size) {
661 count = b->size;
662 }
663 memset(q, 0, count);
664 LOGV("fill extra: q=%p, mPos=%u, b->size=%u, count=%d", q, mPos, b->size, count);
665 }
666
667 mPos += count;
668 b->size = count;
669 //LOGV("buffer=%p, [0]=%d", b->i16, b->i16[0]);
670 }
671 } else if (event == AudioTrack::EVENT_UNDERRUN) {
672 LOGV("stopping track");
673 stop();
674 } else if (event == AudioTrack::EVENT_LOOP_END) {
675 LOGV("End loop: %d", *(int *)info);
676 }
677}
678
679
680// call with lock held
681void SoundChannel::stop_l()
682{
683 if (mState != IDLE) {
684 setVolume_l(0, 0);
685 LOGV("stop");
686 mAudioTrack->stop();
687 mSample.clear();
688 mState = IDLE;
689 mPriority = IDLE_PRIORITY;
690 }
691}
692
693void SoundChannel::stop()
694{
695 {
696 Mutex::Autolock lock(&mLock);
697 stop_l();
698 }
699 mSoundPool->done(this);
700}
701
702//FIXME: Pause is a little broken right now
703void SoundChannel::pause()
704{
705 Mutex::Autolock lock(&mLock);
706 if (mState == PLAYING) {
707 LOGV("pause track");
708 mState = PAUSED;
709 mAudioTrack->pause();
710 }
711}
712
713void SoundChannel::resume()
714{
715 Mutex::Autolock lock(&mLock);
716 if (mState == PAUSED) {
717 LOGV("resume track");
718 mState = PLAYING;
719 mAudioTrack->start();
720 }
721}
722
723void SoundChannel::setRate(float rate)
724{
725 Mutex::Autolock lock(&mLock);
726 if (mAudioTrack != 0 && mSample.get() != 0) {
727 uint32_t sampleRate = uint32_t(float(mSample->sampleRate()) * rate + 0.5);
728 mAudioTrack->setSampleRate(sampleRate);
729 mRate = rate;
730 }
731}
732
733// call with lock held
734void SoundChannel::setVolume_l(float leftVolume, float rightVolume)
735{
736 mLeftVolume = leftVolume;
737 mRightVolume = rightVolume;
738 if (mAudioTrack != 0) mAudioTrack->setVolume(leftVolume, rightVolume);
739}
740
741void SoundChannel::setVolume(float leftVolume, float rightVolume)
742{
743 Mutex::Autolock lock(&mLock);
744 setVolume_l(leftVolume, rightVolume);
745}
746
747void SoundChannel::setLoop(int loop)
748{
749 Mutex::Autolock lock(&mLock);
750 if (mAudioTrack != 0 && mSample.get() != 0) {
751 mAudioTrack->setLoop(0, mSample->size()/mNumChannels/((mSample->format() == AudioSystem::PCM_16_BIT) ? sizeof(int16_t) : sizeof(uint8_t)), loop);
752 mLoop = loop;
753 }
754}
755
756SoundChannel::~SoundChannel()
757{
758 LOGV("SoundChannel destructor");
759 if (mAudioTrack) {
760 LOGV("stop track");
761 mAudioTrack->stop();
762 delete mAudioTrack;
763 }
764 clearNextEvent();
765 mSample.clear();
766}
767
768void SoundChannel::dump()
769{
770 LOGV("mState = %d mChannelID=%d, mNumChannels=%d, mPos = %d, mPriority=%d, mLoop=%d",
771 mState, mChannelID, mNumChannels, mPos, mPriority, mLoop);
772}
773
774void SoundEvent::set(const sp<Sample>& sample, int channelID, float leftVolume,
775 float rightVolume, int priority, int loop, float rate)
776{
777 mSample =sample;
778 mChannelID = channelID;
779 mLeftVolume = leftVolume;
780 mRightVolume = rightVolume;
781 mPriority = priority;
782 mLoop = loop;
783 mRate =rate;
784}
785
786} // end namespace android
787