The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Laurent | 45fce58 | 2009-07-22 11:12:31 -0700 | [diff] [blame] | 21 | //#define USE_SHARED_MEM_BUFFER |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
| 23 | // XXX needed for timing latency |
| 24 | #include <utils/Timers.h> |
| 25 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <media/AudioTrack.h> |
| 27 | #include <media/mediaplayer.h> |
| 28 | |
Dima Zavin | 34bb419 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 29 | #include <system/audio.h> |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 30 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include "SoundPool.h" |
| 32 | #include "SoundPoolThread.h" |
| 33 | |
| 34 | namespace android |
| 35 | { |
| 36 | |
| 37 | int kDefaultBufferCount = 4; |
| 38 | uint32_t kMaxSampleRate = 48000; |
| 39 | uint32_t kDefaultSampleRate = 44100; |
| 40 | uint32_t kDefaultFrameCount = 1200; |
| 41 | |
Dave Sparks | c0e3ddf | 2009-12-07 12:36:20 -0800 | [diff] [blame] | 42 | SoundPool::SoundPool(int maxChannels, int streamType, int srcQuality) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | { |
| 44 | LOGV("SoundPool constructor: maxChannels=%d, streamType=%d, srcQuality=%d", |
| 45 | maxChannels, streamType, srcQuality); |
| 46 | |
Dave Sparks | 3c8704b | 2009-05-29 19:12:11 -0700 | [diff] [blame] | 47 | // check limits |
| 48 | mMaxChannels = maxChannels; |
| 49 | if (mMaxChannels < 1) { |
| 50 | mMaxChannels = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | } |
Dave Sparks | 3c8704b | 2009-05-29 19:12:11 -0700 | [diff] [blame] | 52 | else if (mMaxChannels > 32) { |
| 53 | mMaxChannels = 32; |
| 54 | } |
| 55 | LOGW_IF(maxChannels != mMaxChannels, "App requested %d channels", maxChannels); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
| 57 | mQuit = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | mDecodeThread = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | mStreamType = streamType; |
| 60 | mSrcQuality = srcQuality; |
| 61 | mAllocated = 0; |
| 62 | mNextSampleID = 0; |
| 63 | mNextChannelID = 0; |
| 64 | |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 65 | mCallback = 0; |
| 66 | mUserData = 0; |
| 67 | |
Dave Sparks | 3c8704b | 2009-05-29 19:12:11 -0700 | [diff] [blame] | 68 | mChannelPool = new SoundChannel[mMaxChannels]; |
| 69 | for (int i = 0; i < mMaxChannels; ++i) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | mChannelPool[i].init(this); |
| 71 | mChannels.push_back(&mChannelPool[i]); |
| 72 | } |
| 73 | |
| 74 | // start decode thread |
| 75 | startThreads(); |
| 76 | } |
| 77 | |
| 78 | SoundPool::~SoundPool() |
| 79 | { |
| 80 | LOGV("SoundPool destructor"); |
| 81 | mDecodeThread->quit(); |
| 82 | quit(); |
| 83 | |
| 84 | Mutex::Autolock lock(&mLock); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 85 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | mChannels.clear(); |
| 87 | if (mChannelPool) |
| 88 | delete [] mChannelPool; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | // clean up samples |
| 90 | LOGV("clear samples"); |
| 91 | mSamples.clear(); |
| 92 | |
| 93 | if (mDecodeThread) |
| 94 | delete mDecodeThread; |
| 95 | } |
| 96 | |
| 97 | void SoundPool::addToRestartList(SoundChannel* channel) |
| 98 | { |
Eric Laurent | fd8c0e1 | 2009-07-31 06:29:13 -0700 | [diff] [blame] | 99 | Mutex::Autolock lock(&mRestartLock); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 100 | if (!mQuit) { |
| 101 | mRestart.push_back(channel); |
| 102 | mCondition.signal(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void SoundPool::addToStopList(SoundChannel* channel) |
| 107 | { |
| 108 | Mutex::Autolock lock(&mRestartLock); |
| 109 | if (!mQuit) { |
| 110 | mStop.push_back(channel); |
| 111 | mCondition.signal(); |
| 112 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | int SoundPool::beginThread(void* arg) |
| 116 | { |
| 117 | SoundPool* p = (SoundPool*)arg; |
| 118 | return p->run(); |
| 119 | } |
| 120 | |
| 121 | int SoundPool::run() |
| 122 | { |
Eric Laurent | fd8c0e1 | 2009-07-31 06:29:13 -0700 | [diff] [blame] | 123 | mRestartLock.lock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | while (!mQuit) { |
Eric Laurent | fd8c0e1 | 2009-07-31 06:29:13 -0700 | [diff] [blame] | 125 | mCondition.wait(mRestartLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | LOGV("awake"); |
| 127 | if (mQuit) break; |
| 128 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 129 | while (!mStop.empty()) { |
| 130 | SoundChannel* channel; |
| 131 | LOGV("Getting channel from stop list"); |
| 132 | List<SoundChannel* >::iterator iter = mStop.begin(); |
| 133 | channel = *iter; |
| 134 | mStop.erase(iter); |
| 135 | mRestartLock.unlock(); |
| 136 | if (channel != 0) { |
| 137 | Mutex::Autolock lock(&mLock); |
| 138 | channel->stop(); |
| 139 | } |
| 140 | mRestartLock.lock(); |
| 141 | if (mQuit) break; |
| 142 | } |
| 143 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | while (!mRestart.empty()) { |
| 145 | SoundChannel* channel; |
| 146 | LOGV("Getting channel from list"); |
| 147 | List<SoundChannel*>::iterator iter = mRestart.begin(); |
| 148 | channel = *iter; |
| 149 | mRestart.erase(iter); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 150 | mRestartLock.unlock(); |
| 151 | if (channel != 0) { |
| 152 | Mutex::Autolock lock(&mLock); |
| 153 | channel->nextEvent(); |
| 154 | } |
| 155 | mRestartLock.lock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | if (mQuit) break; |
| 157 | } |
| 158 | } |
| 159 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 160 | mStop.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | mRestart.clear(); |
| 162 | mCondition.signal(); |
Eric Laurent | fd8c0e1 | 2009-07-31 06:29:13 -0700 | [diff] [blame] | 163 | mRestartLock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | LOGV("goodbye"); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | void SoundPool::quit() |
| 169 | { |
Eric Laurent | fd8c0e1 | 2009-07-31 06:29:13 -0700 | [diff] [blame] | 170 | mRestartLock.lock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | mQuit = true; |
| 172 | mCondition.signal(); |
Eric Laurent | fd8c0e1 | 2009-07-31 06:29:13 -0700 | [diff] [blame] | 173 | mCondition.wait(mRestartLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | LOGV("return from quit"); |
Eric Laurent | fd8c0e1 | 2009-07-31 06:29:13 -0700 | [diff] [blame] | 175 | mRestartLock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | bool SoundPool::startThreads() |
| 179 | { |
Steve Howard | 09468db | 2010-03-11 13:51:52 -0800 | [diff] [blame] | 180 | createThreadEtc(beginThread, this, "SoundPool"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | if (mDecodeThread == NULL) |
| 182 | mDecodeThread = new SoundPoolThread(this); |
| 183 | return mDecodeThread != NULL; |
| 184 | } |
| 185 | |
| 186 | SoundChannel* SoundPool::findChannel(int channelID) |
| 187 | { |
| 188 | for (int i = 0; i < mMaxChannels; ++i) { |
| 189 | if (mChannelPool[i].channelID() == channelID) { |
| 190 | return &mChannelPool[i]; |
| 191 | } |
| 192 | } |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | SoundChannel* SoundPool::findNextChannel(int channelID) |
| 197 | { |
| 198 | for (int i = 0; i < mMaxChannels; ++i) { |
| 199 | if (mChannelPool[i].nextChannelID() == channelID) { |
| 200 | return &mChannelPool[i]; |
| 201 | } |
| 202 | } |
| 203 | return NULL; |
| 204 | } |
| 205 | |
| 206 | int SoundPool::load(const char* path, int priority) |
| 207 | { |
| 208 | LOGV("load: path=%s, priority=%d", path, priority); |
| 209 | Mutex::Autolock lock(&mLock); |
| 210 | sp<Sample> sample = new Sample(++mNextSampleID, path); |
| 211 | mSamples.add(sample->sampleID(), sample); |
| 212 | doLoad(sample); |
| 213 | return sample->sampleID(); |
| 214 | } |
| 215 | |
| 216 | int SoundPool::load(int fd, int64_t offset, int64_t length, int priority) |
| 217 | { |
| 218 | LOGV("load: fd=%d, offset=%lld, length=%lld, priority=%d", |
| 219 | fd, offset, length, priority); |
| 220 | Mutex::Autolock lock(&mLock); |
| 221 | sp<Sample> sample = new Sample(++mNextSampleID, fd, offset, length); |
| 222 | mSamples.add(sample->sampleID(), sample); |
| 223 | doLoad(sample); |
| 224 | return sample->sampleID(); |
| 225 | } |
| 226 | |
| 227 | void SoundPool::doLoad(sp<Sample>& sample) |
| 228 | { |
| 229 | LOGV("doLoad: loading sample sampleID=%d", sample->sampleID()); |
| 230 | sample->startLoad(); |
| 231 | mDecodeThread->loadSample(sample->sampleID()); |
| 232 | } |
| 233 | |
| 234 | bool SoundPool::unload(int sampleID) |
| 235 | { |
| 236 | LOGV("unload: sampleID=%d", sampleID); |
| 237 | Mutex::Autolock lock(&mLock); |
| 238 | return mSamples.removeItem(sampleID); |
| 239 | } |
| 240 | |
| 241 | int SoundPool::play(int sampleID, float leftVolume, float rightVolume, |
| 242 | int priority, int loop, float rate) |
| 243 | { |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 244 | LOGV("play sampleID=%d, leftVolume=%f, rightVolume=%f, priority=%d, loop=%d, rate=%f", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | sampleID, leftVolume, rightVolume, priority, loop, rate); |
| 246 | sp<Sample> sample; |
| 247 | SoundChannel* channel; |
| 248 | int channelID; |
| 249 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 250 | Mutex::Autolock lock(&mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 252 | if (mQuit) { |
| 253 | return 0; |
| 254 | } |
| 255 | // is sample ready? |
| 256 | sample = findSample(sampleID); |
| 257 | if ((sample == 0) || (sample->state() != Sample::READY)) { |
| 258 | LOGW(" sample %d not READY", sampleID); |
| 259 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 262 | dump(); |
| 263 | |
| 264 | // allocate a channel |
| 265 | channel = allocateChannel_l(priority); |
| 266 | |
| 267 | // no channel allocated - return 0 |
| 268 | if (!channel) { |
| 269 | LOGV("No channel allocated"); |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | channelID = ++mNextChannelID; |
| 274 | |
| 275 | LOGV("play channel %p state = %d", channel, channel->state()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | channel->play(sample, channelID, leftVolume, rightVolume, priority, loop, rate); |
| 277 | return channelID; |
| 278 | } |
| 279 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 280 | SoundChannel* SoundPool::allocateChannel_l(int priority) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | { |
| 282 | List<SoundChannel*>::iterator iter; |
| 283 | SoundChannel* channel = NULL; |
| 284 | |
| 285 | // allocate a channel |
| 286 | if (!mChannels.empty()) { |
| 287 | iter = mChannels.begin(); |
| 288 | if (priority >= (*iter)->priority()) { |
| 289 | channel = *iter; |
| 290 | mChannels.erase(iter); |
| 291 | LOGV("Allocated active channel"); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // update priority and put it back in the list |
| 296 | if (channel) { |
| 297 | channel->setPriority(priority); |
| 298 | for (iter = mChannels.begin(); iter != mChannels.end(); ++iter) { |
| 299 | if (priority < (*iter)->priority()) { |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | mChannels.insert(iter, channel); |
| 304 | } |
| 305 | return channel; |
| 306 | } |
| 307 | |
| 308 | // move a channel from its current position to the front of the list |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 309 | void SoundPool::moveToFront_l(SoundChannel* channel) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | { |
| 311 | for (List<SoundChannel*>::iterator iter = mChannels.begin(); iter != mChannels.end(); ++iter) { |
| 312 | if (*iter == channel) { |
| 313 | mChannels.erase(iter); |
| 314 | mChannels.push_front(channel); |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | void SoundPool::pause(int channelID) |
| 321 | { |
| 322 | LOGV("pause(%d)", channelID); |
| 323 | Mutex::Autolock lock(&mLock); |
| 324 | SoundChannel* channel = findChannel(channelID); |
| 325 | if (channel) { |
| 326 | channel->pause(); |
| 327 | } |
| 328 | } |
| 329 | |
Dave Sparks | f992cbb | 2010-02-09 13:00:09 -0800 | [diff] [blame] | 330 | void SoundPool::autoPause() |
| 331 | { |
Dave Sparks | 7bef5a5 | 2010-02-17 09:09:24 -0800 | [diff] [blame] | 332 | LOGV("autoPause()"); |
Dave Sparks | f992cbb | 2010-02-09 13:00:09 -0800 | [diff] [blame] | 333 | Mutex::Autolock lock(&mLock); |
| 334 | for (int i = 0; i < mMaxChannels; ++i) { |
| 335 | SoundChannel* channel = &mChannelPool[i]; |
| 336 | channel->autoPause(); |
| 337 | } |
| 338 | } |
| 339 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | void SoundPool::resume(int channelID) |
| 341 | { |
| 342 | LOGV("resume(%d)", channelID); |
| 343 | Mutex::Autolock lock(&mLock); |
| 344 | SoundChannel* channel = findChannel(channelID); |
| 345 | if (channel) { |
| 346 | channel->resume(); |
| 347 | } |
| 348 | } |
| 349 | |
Dave Sparks | f992cbb | 2010-02-09 13:00:09 -0800 | [diff] [blame] | 350 | void SoundPool::autoResume() |
| 351 | { |
Dave Sparks | 7bef5a5 | 2010-02-17 09:09:24 -0800 | [diff] [blame] | 352 | LOGV("autoResume()"); |
Dave Sparks | f992cbb | 2010-02-09 13:00:09 -0800 | [diff] [blame] | 353 | Mutex::Autolock lock(&mLock); |
| 354 | for (int i = 0; i < mMaxChannels; ++i) { |
| 355 | SoundChannel* channel = &mChannelPool[i]; |
| 356 | channel->autoResume(); |
| 357 | } |
| 358 | } |
| 359 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | void SoundPool::stop(int channelID) |
| 361 | { |
| 362 | LOGV("stop(%d)", channelID); |
| 363 | Mutex::Autolock lock(&mLock); |
| 364 | SoundChannel* channel = findChannel(channelID); |
| 365 | if (channel) { |
| 366 | channel->stop(); |
| 367 | } else { |
| 368 | channel = findNextChannel(channelID); |
| 369 | if (channel) |
| 370 | channel->clearNextEvent(); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | void SoundPool::setVolume(int channelID, float leftVolume, float rightVolume) |
| 375 | { |
| 376 | Mutex::Autolock lock(&mLock); |
| 377 | SoundChannel* channel = findChannel(channelID); |
| 378 | if (channel) { |
| 379 | channel->setVolume(leftVolume, rightVolume); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void SoundPool::setPriority(int channelID, int priority) |
| 384 | { |
| 385 | LOGV("setPriority(%d, %d)", channelID, priority); |
| 386 | Mutex::Autolock lock(&mLock); |
| 387 | SoundChannel* channel = findChannel(channelID); |
| 388 | if (channel) { |
| 389 | channel->setPriority(priority); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | void SoundPool::setLoop(int channelID, int loop) |
| 394 | { |
| 395 | LOGV("setLoop(%d, %d)", channelID, loop); |
| 396 | Mutex::Autolock lock(&mLock); |
| 397 | SoundChannel* channel = findChannel(channelID); |
| 398 | if (channel) { |
| 399 | channel->setLoop(loop); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | void SoundPool::setRate(int channelID, float rate) |
| 404 | { |
| 405 | LOGV("setRate(%d, %f)", channelID, rate); |
| 406 | Mutex::Autolock lock(&mLock); |
| 407 | SoundChannel* channel = findChannel(channelID); |
| 408 | if (channel) { |
| 409 | channel->setRate(rate); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // call with lock held |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 414 | void SoundPool::done_l(SoundChannel* channel) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | { |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 416 | LOGV("done_l(%d)", channel->channelID()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | // if "stolen", play next event |
| 418 | if (channel->nextChannelID() != 0) { |
| 419 | LOGV("add to restart list"); |
| 420 | addToRestartList(channel); |
| 421 | } |
| 422 | |
| 423 | // return to idle state |
| 424 | else { |
| 425 | LOGV("move to front"); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 426 | moveToFront_l(channel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 430 | void SoundPool::setCallback(SoundPoolCallback* callback, void* user) |
| 431 | { |
| 432 | Mutex::Autolock lock(&mCallbackLock); |
| 433 | mCallback = callback; |
| 434 | mUserData = user; |
| 435 | } |
| 436 | |
| 437 | void SoundPool::notify(SoundPoolEvent event) |
| 438 | { |
| 439 | Mutex::Autolock lock(&mCallbackLock); |
| 440 | if (mCallback != NULL) { |
| 441 | mCallback(event, this, mUserData); |
| 442 | } |
| 443 | } |
| 444 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | void SoundPool::dump() |
| 446 | { |
| 447 | for (int i = 0; i < mMaxChannels; ++i) { |
| 448 | mChannelPool[i].dump(); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | |
| 453 | Sample::Sample(int sampleID, const char* url) |
| 454 | { |
| 455 | init(); |
| 456 | mSampleID = sampleID; |
| 457 | mUrl = strdup(url); |
| 458 | LOGV("create sampleID=%d, url=%s", mSampleID, mUrl); |
| 459 | } |
| 460 | |
| 461 | Sample::Sample(int sampleID, int fd, int64_t offset, int64_t length) |
| 462 | { |
| 463 | init(); |
| 464 | mSampleID = sampleID; |
| 465 | mFd = dup(fd); |
| 466 | mOffset = offset; |
| 467 | mLength = length; |
| 468 | LOGV("create sampleID=%d, fd=%d, offset=%lld, length=%lld", mSampleID, mFd, mLength, mOffset); |
| 469 | } |
| 470 | |
| 471 | void Sample::init() |
| 472 | { |
| 473 | mData = 0; |
| 474 | mSize = 0; |
| 475 | mRefCount = 0; |
| 476 | mSampleID = 0; |
| 477 | mState = UNLOADED; |
| 478 | mFd = -1; |
| 479 | mOffset = 0; |
| 480 | mLength = 0; |
| 481 | mUrl = 0; |
| 482 | } |
| 483 | |
| 484 | Sample::~Sample() |
| 485 | { |
| 486 | LOGV("Sample::destructor sampleID=%d, fd=%d", mSampleID, mFd); |
| 487 | if (mFd > 0) { |
| 488 | LOGV("close(%d)", mFd); |
| 489 | ::close(mFd); |
| 490 | } |
| 491 | mData.clear(); |
| 492 | delete mUrl; |
| 493 | } |
| 494 | |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 495 | status_t Sample::doLoad() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | { |
| 497 | uint32_t sampleRate; |
| 498 | int numChannels; |
| 499 | int format; |
| 500 | sp<IMemory> p; |
| 501 | LOGV("Start decode"); |
| 502 | if (mUrl) { |
| 503 | p = MediaPlayer::decode(mUrl, &sampleRate, &numChannels, &format); |
| 504 | } else { |
| 505 | p = MediaPlayer::decode(mFd, mOffset, mLength, &sampleRate, &numChannels, &format); |
| 506 | LOGV("close(%d)", mFd); |
| 507 | ::close(mFd); |
| 508 | mFd = -1; |
| 509 | } |
| 510 | if (p == 0) { |
| 511 | LOGE("Unable to load sample: %s", mUrl); |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 512 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | } |
| 514 | LOGV("pointer = %p, size = %u, sampleRate = %u, numChannels = %d", |
| 515 | p->pointer(), p->size(), sampleRate, numChannels); |
| 516 | |
| 517 | if (sampleRate > kMaxSampleRate) { |
| 518 | LOGE("Sample rate (%u) out of range", sampleRate); |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 519 | return - 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | if ((numChannels < 1) || (numChannels > 2)) { |
| 523 | LOGE("Sample channel count (%d) out of range", numChannels); |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 524 | return - 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | //_dumpBuffer(p->pointer(), p->size()); |
| 528 | uint8_t* q = static_cast<uint8_t*>(p->pointer()) + p->size() - 10; |
| 529 | //_dumpBuffer(q, 10, 10, false); |
| 530 | |
| 531 | mData = p; |
| 532 | mSize = p->size(); |
| 533 | mSampleRate = sampleRate; |
| 534 | mNumChannels = numChannels; |
| 535 | mFormat = format; |
| 536 | mState = READY; |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 537 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | |
| 541 | void SoundChannel::init(SoundPool* soundPool) |
| 542 | { |
| 543 | mSoundPool = soundPool; |
| 544 | } |
| 545 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 546 | // call with sound pool lock held |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | void SoundChannel::play(const sp<Sample>& sample, int nextChannelID, float leftVolume, |
| 548 | float rightVolume, int priority, int loop, float rate) |
| 549 | { |
| 550 | AudioTrack* oldTrack; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 551 | AudioTrack* newTrack; |
| 552 | status_t status; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 554 | { // scope for the lock |
| 555 | Mutex::Autolock lock(&mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 557 | LOGV("SoundChannel::play %p: sampleID=%d, channelID=%d, leftVolume=%f, rightVolume=%f," |
| 558 | " priority=%d, loop=%d, rate=%f", |
| 559 | this, sample->sampleID(), nextChannelID, leftVolume, rightVolume, |
| 560 | priority, loop, rate); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 562 | // if not idle, this voice is being stolen |
| 563 | if (mState != IDLE) { |
| 564 | LOGV("channel %d stolen - event queued for channel %d", channelID(), nextChannelID); |
| 565 | mNextEvent.set(sample, nextChannelID, leftVolume, rightVolume, priority, loop, rate); |
| 566 | stop_l(); |
| 567 | return; |
| 568 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 570 | // initialize track |
| 571 | int afFrameCount; |
| 572 | int afSampleRate; |
| 573 | int streamType = mSoundPool->streamType(); |
| 574 | if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) { |
| 575 | afFrameCount = kDefaultFrameCount; |
| 576 | } |
| 577 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 578 | afSampleRate = kDefaultSampleRate; |
| 579 | } |
| 580 | int numChannels = sample->numChannels(); |
| 581 | uint32_t sampleRate = uint32_t(float(sample->sampleRate()) * rate + 0.5); |
| 582 | uint32_t totalFrames = (kDefaultBufferCount * afFrameCount * sampleRate) / afSampleRate; |
| 583 | uint32_t bufferFrames = (totalFrames + (kDefaultBufferCount - 1)) / kDefaultBufferCount; |
| 584 | uint32_t frameCount = 0; |
| 585 | |
| 586 | if (loop) { |
| 587 | frameCount = sample->size()/numChannels/ |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 588 | ((sample->format() == AUDIO_FORMAT_PCM_16_BIT) ? sizeof(int16_t) : sizeof(uint8_t)); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 589 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | |
Eric Laurent | 9648e4b | 2009-05-07 03:14:31 -0700 | [diff] [blame] | 591 | #ifndef USE_SHARED_MEM_BUFFER |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 592 | // Ensure minimum audio buffer size in case of short looped sample |
| 593 | if(frameCount < totalFrames) { |
| 594 | frameCount = totalFrames; |
| 595 | } |
Eric Laurent | 9648e4b | 2009-05-07 03:14:31 -0700 | [diff] [blame] | 596 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 598 | // mToggle toggles each time a track is started on a given channel. |
| 599 | // The toggle is concatenated with the SoundChannel address and passed to AudioTrack |
| 600 | // as callback user data. This enables the detection of callbacks received from the old |
| 601 | // audio track while the new one is being started and avoids processing them with |
| 602 | // wrong audio audio buffer size (mAudioBufferSize) |
| 603 | unsigned long toggle = mToggle ^ 1; |
| 604 | void *userData = (void *)((unsigned long)this | toggle); |
| 605 | uint32_t channels = (numChannels == 2) ? |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 606 | AUDIO_CHANNEL_OUT_STEREO : AUDIO_CHANNEL_OUT_MONO; |
Dave Sparks | 66d28ce | 2009-12-14 21:12:05 -0800 | [diff] [blame] | 607 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 608 | // do not create a new audio track if current track is compatible with sample parameters |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | #ifdef USE_SHARED_MEM_BUFFER |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 610 | newTrack = new AudioTrack(streamType, sampleRate, sample->format(), |
| 611 | channels, sample->getIMemory(), 0, callback, userData); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | #else |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 613 | newTrack = new AudioTrack(streamType, sampleRate, sample->format(), |
| 614 | channels, frameCount, 0, callback, userData, bufferFrames); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | #endif |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 616 | oldTrack = mAudioTrack; |
| 617 | status = newTrack->initCheck(); |
| 618 | if (status != NO_ERROR) { |
| 619 | LOGE("Error creating AudioTrack"); |
| 620 | goto exit; |
| 621 | } |
| 622 | LOGV("setVolume %p", newTrack); |
| 623 | newTrack->setVolume(leftVolume, rightVolume); |
| 624 | newTrack->setLoop(0, frameCount, loop); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | // From now on, AudioTrack callbacks recevieved with previous toggle value will be ignored. |
| 627 | mToggle = toggle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | mAudioTrack = newTrack; |
| 629 | mPos = 0; |
| 630 | mSample = sample; |
| 631 | mChannelID = nextChannelID; |
| 632 | mPriority = priority; |
| 633 | mLoop = loop; |
| 634 | mLeftVolume = leftVolume; |
| 635 | mRightVolume = rightVolume; |
| 636 | mNumChannels = numChannels; |
| 637 | mRate = rate; |
| 638 | clearNextEvent(); |
| 639 | mState = PLAYING; |
| 640 | mAudioTrack->start(); |
| 641 | mAudioBufferSize = newTrack->frameCount()*newTrack->frameSize(); |
| 642 | } |
| 643 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 644 | exit: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | LOGV("delete oldTrack %p", oldTrack); |
| 646 | delete oldTrack; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 647 | if (status != NO_ERROR) { |
| 648 | delete newTrack; |
| 649 | mAudioTrack = NULL; |
| 650 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | void SoundChannel::nextEvent() |
| 654 | { |
| 655 | sp<Sample> sample; |
| 656 | int nextChannelID; |
| 657 | float leftVolume; |
| 658 | float rightVolume; |
| 659 | int priority; |
| 660 | int loop; |
| 661 | float rate; |
| 662 | |
| 663 | // check for valid event |
| 664 | { |
| 665 | Mutex::Autolock lock(&mLock); |
| 666 | nextChannelID = mNextEvent.channelID(); |
| 667 | if (nextChannelID == 0) { |
| 668 | LOGV("stolen channel has no event"); |
| 669 | return; |
| 670 | } |
| 671 | |
| 672 | sample = mNextEvent.sample(); |
| 673 | leftVolume = mNextEvent.leftVolume(); |
| 674 | rightVolume = mNextEvent.rightVolume(); |
| 675 | priority = mNextEvent.priority(); |
| 676 | loop = mNextEvent.loop(); |
| 677 | rate = mNextEvent.rate(); |
| 678 | } |
| 679 | |
| 680 | LOGV("Starting stolen channel %d -> %d", channelID(), nextChannelID); |
| 681 | play(sample, nextChannelID, leftVolume, rightVolume, priority, loop, rate); |
| 682 | } |
| 683 | |
Glenn Kasten | e46a86f | 2011-06-01 15:20:35 -0700 | [diff] [blame] | 684 | void SoundChannel::callback(int event, void* user, void *info) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 685 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | SoundChannel* channel = static_cast<SoundChannel*>((void *)((unsigned long)user & ~1)); |
| 687 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 688 | channel->process(event, info, (unsigned long)user & 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | } |
| 690 | |
Glenn Kasten | e46a86f | 2011-06-01 15:20:35 -0700 | [diff] [blame] | 691 | void SoundChannel::process(int event, void *info, unsigned long toggle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | { |
| 693 | //LOGV("process(%d)", mChannelID); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 694 | |
| 695 | Mutex::Autolock lock(&mLock); |
| 696 | |
| 697 | AudioTrack::Buffer* b = NULL; |
| 698 | if (event == AudioTrack::EVENT_MORE_DATA) { |
| 699 | b = static_cast<AudioTrack::Buffer *>(info); |
| 700 | } |
| 701 | |
| 702 | if (mToggle != toggle) { |
| 703 | LOGV("process wrong toggle %p channel %d", this, mChannelID); |
| 704 | if (b != NULL) { |
| 705 | b->size = 0; |
| 706 | } |
| 707 | return; |
| 708 | } |
| 709 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | sp<Sample> sample = mSample; |
| 711 | |
| 712 | // LOGV("SoundChannel::process event %d", event); |
| 713 | |
| 714 | if (event == AudioTrack::EVENT_MORE_DATA) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 | |
| 716 | // check for stop state |
| 717 | if (b->size == 0) return; |
| 718 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 719 | if (mState == IDLE) { |
| 720 | b->size = 0; |
| 721 | return; |
| 722 | } |
| 723 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | if (sample != 0) { |
| 725 | // fill buffer |
| 726 | uint8_t* q = (uint8_t*) b->i8; |
| 727 | size_t count = 0; |
| 728 | |
| 729 | if (mPos < (int)sample->size()) { |
| 730 | uint8_t* p = sample->data() + mPos; |
| 731 | count = sample->size() - mPos; |
| 732 | if (count > b->size) { |
| 733 | count = b->size; |
| 734 | } |
| 735 | memcpy(q, p, count); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 736 | // LOGV("fill: q=%p, p=%p, mPos=%u, b->size=%u, count=%d", q, p, mPos, b->size, count); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | } else if (mPos < mAudioBufferSize) { |
| 738 | count = mAudioBufferSize - mPos; |
| 739 | if (count > b->size) { |
| 740 | count = b->size; |
| 741 | } |
| 742 | memset(q, 0, count); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 743 | // LOGV("fill extra: q=%p, mPos=%u, b->size=%u, count=%d", q, mPos, b->size, count); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | mPos += count; |
| 747 | b->size = count; |
| 748 | //LOGV("buffer=%p, [0]=%d", b->i16, b->i16[0]); |
| 749 | } |
| 750 | } else if (event == AudioTrack::EVENT_UNDERRUN) { |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 751 | LOGV("process %p channel %d EVENT_UNDERRUN", this, mChannelID); |
| 752 | mSoundPool->addToStopList(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | } else if (event == AudioTrack::EVENT_LOOP_END) { |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 754 | LOGV("End loop %p channel %d count %d", this, mChannelID, *(int *)info); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
| 758 | |
| 759 | // call with lock held |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 760 | bool SoundChannel::doStop_l() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 761 | { |
| 762 | if (mState != IDLE) { |
| 763 | setVolume_l(0, 0); |
| 764 | LOGV("stop"); |
| 765 | mAudioTrack->stop(); |
| 766 | mSample.clear(); |
| 767 | mState = IDLE; |
| 768 | mPriority = IDLE_PRIORITY; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 769 | return true; |
| 770 | } |
| 771 | return false; |
| 772 | } |
| 773 | |
| 774 | // call with lock held and sound pool lock held |
| 775 | void SoundChannel::stop_l() |
| 776 | { |
| 777 | if (doStop_l()) { |
| 778 | mSoundPool->done_l(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 782 | // call with sound pool lock held |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 783 | void SoundChannel::stop() |
| 784 | { |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 785 | bool stopped; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 786 | { |
| 787 | Mutex::Autolock lock(&mLock); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 788 | stopped = doStop_l(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 790 | |
| 791 | if (stopped) { |
| 792 | mSoundPool->done_l(this); |
| 793 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | //FIXME: Pause is a little broken right now |
| 797 | void SoundChannel::pause() |
| 798 | { |
| 799 | Mutex::Autolock lock(&mLock); |
| 800 | if (mState == PLAYING) { |
| 801 | LOGV("pause track"); |
| 802 | mState = PAUSED; |
| 803 | mAudioTrack->pause(); |
| 804 | } |
| 805 | } |
| 806 | |
Dave Sparks | f992cbb | 2010-02-09 13:00:09 -0800 | [diff] [blame] | 807 | void SoundChannel::autoPause() |
| 808 | { |
| 809 | Mutex::Autolock lock(&mLock); |
| 810 | if (mState == PLAYING) { |
| 811 | LOGV("pause track"); |
| 812 | mState = PAUSED; |
| 813 | mAutoPaused = true; |
| 814 | mAudioTrack->pause(); |
| 815 | } |
| 816 | } |
| 817 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | void SoundChannel::resume() |
| 819 | { |
| 820 | Mutex::Autolock lock(&mLock); |
| 821 | if (mState == PAUSED) { |
| 822 | LOGV("resume track"); |
| 823 | mState = PLAYING; |
Dave Sparks | f992cbb | 2010-02-09 13:00:09 -0800 | [diff] [blame] | 824 | mAutoPaused = false; |
| 825 | mAudioTrack->start(); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | void SoundChannel::autoResume() |
| 830 | { |
| 831 | Mutex::Autolock lock(&mLock); |
| 832 | if (mAutoPaused && (mState == PAUSED)) { |
| 833 | LOGV("resume track"); |
| 834 | mState = PLAYING; |
| 835 | mAutoPaused = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | mAudioTrack->start(); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | void SoundChannel::setRate(float rate) |
| 841 | { |
| 842 | Mutex::Autolock lock(&mLock); |
| 843 | if (mAudioTrack != 0 && mSample.get() != 0) { |
| 844 | uint32_t sampleRate = uint32_t(float(mSample->sampleRate()) * rate + 0.5); |
| 845 | mAudioTrack->setSampleRate(sampleRate); |
| 846 | mRate = rate; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | // call with lock held |
| 851 | void SoundChannel::setVolume_l(float leftVolume, float rightVolume) |
| 852 | { |
| 853 | mLeftVolume = leftVolume; |
| 854 | mRightVolume = rightVolume; |
| 855 | if (mAudioTrack != 0) mAudioTrack->setVolume(leftVolume, rightVolume); |
| 856 | } |
| 857 | |
| 858 | void SoundChannel::setVolume(float leftVolume, float rightVolume) |
| 859 | { |
| 860 | Mutex::Autolock lock(&mLock); |
| 861 | setVolume_l(leftVolume, rightVolume); |
| 862 | } |
| 863 | |
| 864 | void SoundChannel::setLoop(int loop) |
| 865 | { |
| 866 | Mutex::Autolock lock(&mLock); |
| 867 | if (mAudioTrack != 0 && mSample.get() != 0) { |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 868 | uint32_t loopEnd = mSample->size()/mNumChannels/ |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 869 | ((mSample->format() == AUDIO_FORMAT_PCM_16_BIT) ? sizeof(int16_t) : sizeof(uint8_t)); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 870 | mAudioTrack->setLoop(0, loopEnd, loop); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | mLoop = loop; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | SoundChannel::~SoundChannel() |
| 876 | { |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 877 | LOGV("SoundChannel destructor %p", this); |
| 878 | { |
| 879 | Mutex::Autolock lock(&mLock); |
| 880 | clearNextEvent(); |
| 881 | doStop_l(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 883 | // do not call AudioTrack destructor with mLock held as it will wait for the AudioTrack |
| 884 | // callback thread to exit which may need to execute process() and acquire the mLock. |
| 885 | delete mAudioTrack; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | void SoundChannel::dump() |
| 889 | { |
| 890 | LOGV("mState = %d mChannelID=%d, mNumChannels=%d, mPos = %d, mPriority=%d, mLoop=%d", |
| 891 | mState, mChannelID, mNumChannels, mPos, mPriority, mLoop); |
| 892 | } |
| 893 | |
| 894 | void SoundEvent::set(const sp<Sample>& sample, int channelID, float leftVolume, |
| 895 | float rightVolume, int priority, int loop, float rate) |
| 896 | { |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 897 | mSample = sample; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | mChannelID = channelID; |
| 899 | mLeftVolume = leftVolume; |
| 900 | mRightVolume = rightVolume; |
| 901 | mPriority = priority; |
| 902 | mLoop = loop; |
| 903 | mRate =rate; |
| 904 | } |
| 905 | |
| 906 | } // end namespace android |