The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* MidiFile.cpp |
| 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "MidiFile" |
| 20 | #include "utils/Log.h" |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <assert.h> |
| 24 | #include <limits.h> |
| 25 | #include <unistd.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <sched.h> |
| 28 | #include <utils/threads.h> |
| 29 | #include <libsonivox/eas_reverb.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/stat.h> |
Glenn Kasten | 0512ab5 | 2011-05-04 17:58:57 -0700 | [diff] [blame] | 32 | #include <unistd.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 34 | #include <system/audio.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 35 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | #include "MidiFile.h" |
| 37 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | // ---------------------------------------------------------------------------- |
| 39 | |
| 40 | namespace android { |
| 41 | |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | |
| 44 | // The midi engine buffers are a bit small (128 frames), so we batch them up |
| 45 | static const int NUM_BUFFERS = 4; |
| 46 | |
| 47 | // TODO: Determine appropriate return codes |
| 48 | static status_t ERROR_NOT_OPEN = -1; |
| 49 | static status_t ERROR_OPEN_FAILED = -2; |
| 50 | static status_t ERROR_EAS_FAILURE = -3; |
| 51 | static status_t ERROR_ALLOCATE_FAILED = -4; |
| 52 | |
| 53 | static const S_EAS_LIB_CONFIG* pLibConfig = NULL; |
| 54 | |
| 55 | MidiFile::MidiFile() : |
| 56 | mEasData(NULL), mEasHandle(NULL), mAudioBuffer(NULL), |
| 57 | mPlayTime(-1), mDuration(-1), mState(EAS_STATE_ERROR), |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 58 | mStreamType(AUDIO_STREAM_MUSIC), mLoop(false), mExit(false), |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | mPaused(false), mRender(false), mTid(-1) |
| 60 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 61 | ALOGV("constructor"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | // get the library configuration and do sanity check |
| 64 | if (pLibConfig == NULL) |
| 65 | pLibConfig = EAS_Config(); |
| 66 | if ((pLibConfig == NULL) || (LIB_VERSION != pLibConfig->libVersion)) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 67 | ALOGE("EAS library/header mismatch"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | goto Failed; |
| 69 | } |
| 70 | |
| 71 | // initialize EAS library |
| 72 | if (EAS_Init(&mEasData) != EAS_SUCCESS) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 73 | ALOGE("EAS_Init failed"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | goto Failed; |
| 75 | } |
| 76 | |
| 77 | // select reverb preset and enable |
| 78 | EAS_SetParameter(mEasData, EAS_MODULE_REVERB, EAS_PARAM_REVERB_PRESET, EAS_PARAM_REVERB_CHAMBER); |
| 79 | EAS_SetParameter(mEasData, EAS_MODULE_REVERB, EAS_PARAM_REVERB_BYPASS, EAS_FALSE); |
| 80 | |
| 81 | // create playback thread |
| 82 | { |
| 83 | Mutex::Autolock l(mMutex); |
Glenn Kasten | 90100b5 | 2011-06-23 17:11:35 -0700 | [diff] [blame] | 84 | mThread = new MidiFileThread(this); |
| 85 | mThread->run("midithread", ANDROID_PRIORITY_AUDIO); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | mCondition.wait(mMutex); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 87 | ALOGV("thread started"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // indicate success |
| 91 | if (mTid > 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 92 | ALOGV(" render thread(%d) started", mTid); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | mState = EAS_STATE_READY; |
| 94 | } |
| 95 | |
| 96 | Failed: |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | status_t MidiFile::initCheck() |
| 101 | { |
| 102 | if (mState == EAS_STATE_ERROR) return ERROR_EAS_FAILURE; |
| 103 | return NO_ERROR; |
| 104 | } |
| 105 | |
| 106 | MidiFile::~MidiFile() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 107 | ALOGV("MidiFile destructor"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | release(); |
| 109 | } |
| 110 | |
Andreas Huber | 2db8455 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 111 | status_t MidiFile::setDataSource( |
Mark Salyzyn | 77342f7 | 2014-06-18 16:31:32 -0700 | [diff] [blame] | 112 | const sp<IMediaHTTPService> & /*httpService*/, |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 113 | const char* path, |
| 114 | const KeyedVector<String8, String8> *) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 115 | ALOGV("MidiFile::setDataSource url=%s", path); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | Mutex::Autolock lock(mMutex); |
| 117 | |
| 118 | // file still open? |
| 119 | if (mEasHandle) { |
| 120 | reset_nosync(); |
| 121 | } |
| 122 | |
| 123 | // open file and set paused state |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame^] | 124 | mIoWrapper = new MidiIoWrapper(path); |
| 125 | EAS_RESULT result = EAS_OpenFile(mEasData, mIoWrapper->getLocator(), &mEasHandle); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | if (result == EAS_SUCCESS) { |
| 127 | updateState(); |
| 128 | } |
| 129 | |
| 130 | if (result != EAS_SUCCESS) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 131 | ALOGE("EAS_OpenFile failed: [%d]", (int)result); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | mState = EAS_STATE_ERROR; |
| 133 | return ERROR_OPEN_FAILED; |
| 134 | } |
| 135 | |
| 136 | mState = EAS_STATE_OPEN; |
| 137 | mPlayTime = 0; |
| 138 | return NO_ERROR; |
| 139 | } |
| 140 | |
| 141 | status_t MidiFile::setDataSource(int fd, int64_t offset, int64_t length) |
| 142 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 143 | ALOGV("MidiFile::setDataSource fd=%d", fd); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | Mutex::Autolock lock(mMutex); |
| 145 | |
| 146 | // file still open? |
| 147 | if (mEasHandle) { |
| 148 | reset_nosync(); |
| 149 | } |
| 150 | |
| 151 | // open file and set paused state |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame^] | 152 | mIoWrapper = new MidiIoWrapper(fd, offset, length); |
| 153 | EAS_RESULT result = EAS_OpenFile(mEasData, mIoWrapper->getLocator(), &mEasHandle); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | updateState(); |
| 155 | |
| 156 | if (result != EAS_SUCCESS) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 157 | ALOGE("EAS_OpenFile failed: [%d]", (int)result); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | mState = EAS_STATE_ERROR; |
| 159 | return ERROR_OPEN_FAILED; |
| 160 | } |
| 161 | |
| 162 | mState = EAS_STATE_OPEN; |
| 163 | mPlayTime = 0; |
| 164 | return NO_ERROR; |
| 165 | } |
| 166 | |
| 167 | status_t MidiFile::prepare() |
| 168 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 169 | ALOGV("MidiFile::prepare"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | Mutex::Autolock lock(mMutex); |
| 171 | if (!mEasHandle) { |
| 172 | return ERROR_NOT_OPEN; |
| 173 | } |
| 174 | EAS_RESULT result; |
| 175 | if ((result = EAS_Prepare(mEasData, mEasHandle)) != EAS_SUCCESS) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 176 | ALOGE("EAS_Prepare failed: [%ld]", result); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | return ERROR_EAS_FAILURE; |
| 178 | } |
| 179 | updateState(); |
| 180 | return NO_ERROR; |
| 181 | } |
| 182 | |
| 183 | status_t MidiFile::prepareAsync() |
| 184 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 185 | ALOGV("MidiFile::prepareAsync"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | status_t ret = prepare(); |
| 187 | |
| 188 | // don't hold lock during callback |
| 189 | if (ret == NO_ERROR) { |
| 190 | sendEvent(MEDIA_PREPARED); |
| 191 | } else { |
| 192 | sendEvent(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ret); |
| 193 | } |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | status_t MidiFile::start() |
| 198 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 199 | ALOGV("MidiFile::start"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | Mutex::Autolock lock(mMutex); |
| 201 | if (!mEasHandle) { |
| 202 | return ERROR_NOT_OPEN; |
| 203 | } |
| 204 | |
| 205 | // resuming after pause? |
| 206 | if (mPaused) { |
| 207 | if (EAS_Resume(mEasData, mEasHandle) != EAS_SUCCESS) { |
| 208 | return ERROR_EAS_FAILURE; |
| 209 | } |
| 210 | mPaused = false; |
| 211 | updateState(); |
| 212 | } |
| 213 | |
| 214 | mRender = true; |
Lajos Molnar | cbaffcf | 2013-08-14 18:30:38 -0700 | [diff] [blame] | 215 | if (mState == EAS_STATE_PLAY) { |
| 216 | sendEvent(MEDIA_STARTED); |
| 217 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | |
| 219 | // wake up render thread |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 220 | ALOGV(" wakeup render thread"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | mCondition.signal(); |
| 222 | return NO_ERROR; |
| 223 | } |
| 224 | |
| 225 | status_t MidiFile::stop() |
| 226 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 227 | ALOGV("MidiFile::stop"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | Mutex::Autolock lock(mMutex); |
| 229 | if (!mEasHandle) { |
| 230 | return ERROR_NOT_OPEN; |
| 231 | } |
| 232 | if (!mPaused && (mState != EAS_STATE_STOPPED)) { |
| 233 | EAS_RESULT result = EAS_Pause(mEasData, mEasHandle); |
| 234 | if (result != EAS_SUCCESS) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 235 | ALOGE("EAS_Pause returned error %ld", result); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | return ERROR_EAS_FAILURE; |
| 237 | } |
| 238 | } |
| 239 | mPaused = false; |
Lajos Molnar | cbaffcf | 2013-08-14 18:30:38 -0700 | [diff] [blame] | 240 | sendEvent(MEDIA_STOPPED); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | return NO_ERROR; |
| 242 | } |
| 243 | |
| 244 | status_t MidiFile::seekTo(int position) |
| 245 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 246 | ALOGV("MidiFile::seekTo %d", position); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | // hold lock during EAS calls |
| 248 | { |
| 249 | Mutex::Autolock lock(mMutex); |
| 250 | if (!mEasHandle) { |
| 251 | return ERROR_NOT_OPEN; |
| 252 | } |
| 253 | EAS_RESULT result; |
| 254 | if ((result = EAS_Locate(mEasData, mEasHandle, position, false)) |
| 255 | != EAS_SUCCESS) |
| 256 | { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 257 | ALOGE("EAS_Locate returned %ld", result); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | return ERROR_EAS_FAILURE; |
| 259 | } |
| 260 | EAS_GetLocation(mEasData, mEasHandle, &mPlayTime); |
| 261 | } |
| 262 | sendEvent(MEDIA_SEEK_COMPLETE); |
| 263 | return NO_ERROR; |
| 264 | } |
| 265 | |
| 266 | status_t MidiFile::pause() |
| 267 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 268 | ALOGV("MidiFile::pause"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | Mutex::Autolock lock(mMutex); |
| 270 | if (!mEasHandle) { |
| 271 | return ERROR_NOT_OPEN; |
| 272 | } |
| 273 | if ((mState == EAS_STATE_PAUSING) || (mState == EAS_STATE_PAUSED)) return NO_ERROR; |
| 274 | if (EAS_Pause(mEasData, mEasHandle) != EAS_SUCCESS) { |
| 275 | return ERROR_EAS_FAILURE; |
| 276 | } |
| 277 | mPaused = true; |
Lajos Molnar | cbaffcf | 2013-08-14 18:30:38 -0700 | [diff] [blame] | 278 | sendEvent(MEDIA_PAUSED); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | return NO_ERROR; |
| 280 | } |
| 281 | |
| 282 | bool MidiFile::isPlaying() |
| 283 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 284 | ALOGV("MidiFile::isPlaying, mState=%d", int(mState)); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | if (!mEasHandle || mPaused) return false; |
| 286 | return (mState == EAS_STATE_PLAY); |
| 287 | } |
| 288 | |
| 289 | status_t MidiFile::getCurrentPosition(int* position) |
| 290 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 291 | ALOGV("MidiFile::getCurrentPosition"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | if (!mEasHandle) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 293 | ALOGE("getCurrentPosition(): file not open"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | return ERROR_NOT_OPEN; |
| 295 | } |
| 296 | if (mPlayTime < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 297 | ALOGE("getCurrentPosition(): mPlayTime = %ld", mPlayTime); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | return ERROR_EAS_FAILURE; |
| 299 | } |
| 300 | *position = mPlayTime; |
| 301 | return NO_ERROR; |
| 302 | } |
| 303 | |
| 304 | status_t MidiFile::getDuration(int* duration) |
| 305 | { |
| 306 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 307 | ALOGV("MidiFile::getDuration"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | { |
| 309 | Mutex::Autolock lock(mMutex); |
| 310 | if (!mEasHandle) return ERROR_NOT_OPEN; |
| 311 | *duration = mDuration; |
| 312 | } |
| 313 | |
| 314 | // if no duration cached, get the duration |
| 315 | // don't need a lock here because we spin up a new engine |
| 316 | if (*duration < 0) { |
| 317 | EAS_I32 temp; |
| 318 | EAS_DATA_HANDLE easData = NULL; |
| 319 | EAS_HANDLE easHandle = NULL; |
| 320 | EAS_RESULT result = EAS_Init(&easData); |
| 321 | if (result == EAS_SUCCESS) { |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame^] | 322 | result = EAS_OpenFile(easData, mIoWrapper->getLocator(), &easHandle); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | } |
| 324 | if (result == EAS_SUCCESS) { |
| 325 | result = EAS_Prepare(easData, easHandle); |
| 326 | } |
| 327 | if (result == EAS_SUCCESS) { |
| 328 | result = EAS_ParseMetaData(easData, easHandle, &temp); |
| 329 | } |
| 330 | if (easHandle) { |
| 331 | EAS_CloseFile(easData, easHandle); |
| 332 | } |
| 333 | if (easData) { |
| 334 | EAS_Shutdown(easData); |
| 335 | } |
| 336 | |
| 337 | if (result != EAS_SUCCESS) { |
| 338 | return ERROR_EAS_FAILURE; |
| 339 | } |
| 340 | |
| 341 | // cache successful result |
| 342 | mDuration = *duration = int(temp); |
| 343 | } |
| 344 | |
| 345 | return NO_ERROR; |
| 346 | } |
| 347 | |
| 348 | status_t MidiFile::release() |
| 349 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 350 | ALOGV("MidiFile::release"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | Mutex::Autolock l(mMutex); |
| 352 | reset_nosync(); |
| 353 | |
| 354 | // wait for render thread to exit |
| 355 | mExit = true; |
| 356 | mCondition.signal(); |
| 357 | |
| 358 | // wait for thread to exit |
| 359 | if (mAudioBuffer) { |
| 360 | mCondition.wait(mMutex); |
| 361 | } |
| 362 | |
| 363 | // release resources |
| 364 | if (mEasData) { |
| 365 | EAS_Shutdown(mEasData); |
| 366 | mEasData = NULL; |
| 367 | } |
| 368 | return NO_ERROR; |
| 369 | } |
| 370 | |
| 371 | status_t MidiFile::reset() |
| 372 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 373 | ALOGV("MidiFile::reset"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | Mutex::Autolock lock(mMutex); |
| 375 | return reset_nosync(); |
| 376 | } |
| 377 | |
| 378 | // call only with mutex held |
| 379 | status_t MidiFile::reset_nosync() |
| 380 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 381 | ALOGV("MidiFile::reset_nosync"); |
Lajos Molnar | cbaffcf | 2013-08-14 18:30:38 -0700 | [diff] [blame] | 382 | sendEvent(MEDIA_STOPPED); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | // close file |
| 384 | if (mEasHandle) { |
| 385 | EAS_CloseFile(mEasData, mEasHandle); |
| 386 | mEasHandle = NULL; |
| 387 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame^] | 389 | mIoWrapper.clear(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | mPlayTime = -1; |
| 391 | mDuration = -1; |
| 392 | mLoop = false; |
| 393 | mPaused = false; |
| 394 | mRender = false; |
| 395 | return NO_ERROR; |
| 396 | } |
| 397 | |
| 398 | status_t MidiFile::setLooping(int loop) |
| 399 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 400 | ALOGV("MidiFile::setLooping"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | Mutex::Autolock lock(mMutex); |
| 402 | if (!mEasHandle) { |
| 403 | return ERROR_NOT_OPEN; |
| 404 | } |
| 405 | loop = loop ? -1 : 0; |
| 406 | if (EAS_SetRepeat(mEasData, mEasHandle, loop) != EAS_SUCCESS) { |
| 407 | return ERROR_EAS_FAILURE; |
| 408 | } |
| 409 | return NO_ERROR; |
| 410 | } |
| 411 | |
| 412 | status_t MidiFile::createOutputTrack() { |
Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 413 | if (mAudioSink->open(pLibConfig->sampleRate, pLibConfig->numChannels, |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 414 | CHANNEL_MASK_USE_CHANNEL_ORDER, AUDIO_FORMAT_PCM_16_BIT, 2 /*bufferCount*/) != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 415 | ALOGE("mAudioSink open failed"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | return ERROR_OPEN_FAILED; |
| 417 | } |
| 418 | return NO_ERROR; |
| 419 | } |
| 420 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | int MidiFile::render() { |
| 422 | EAS_RESULT result = EAS_FAILURE; |
| 423 | EAS_I32 count; |
| 424 | int temp; |
| 425 | bool audioStarted = false; |
| 426 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 427 | ALOGV("MidiFile::render"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | |
| 429 | // allocate render buffer |
| 430 | mAudioBuffer = new EAS_PCM[pLibConfig->mixBufferSize * pLibConfig->numChannels * NUM_BUFFERS]; |
| 431 | if (!mAudioBuffer) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 432 | ALOGE("mAudioBuffer allocate failed"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | goto threadExit; |
| 434 | } |
| 435 | |
| 436 | // signal main thread that we started |
| 437 | { |
| 438 | Mutex::Autolock l(mMutex); |
Glenn Kasten | 0512ab5 | 2011-05-04 17:58:57 -0700 | [diff] [blame] | 439 | mTid = gettid(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 440 | ALOGV("render thread(%d) signal", mTid); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | mCondition.signal(); |
| 442 | } |
| 443 | |
| 444 | while (1) { |
| 445 | mMutex.lock(); |
| 446 | |
| 447 | // nothing to render, wait for client thread to wake us up |
| 448 | while (!mRender && !mExit) |
| 449 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 450 | ALOGV("MidiFile::render - signal wait"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | mCondition.wait(mMutex); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 452 | ALOGV("MidiFile::render - signal rx'd"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | } |
| 454 | if (mExit) { |
| 455 | mMutex.unlock(); |
| 456 | break; |
| 457 | } |
| 458 | |
| 459 | // render midi data into the input buffer |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 460 | //ALOGV("MidiFile::render - rendering audio"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | int num_output = 0; |
| 462 | EAS_PCM* p = mAudioBuffer; |
| 463 | for (int i = 0; i < NUM_BUFFERS; i++) { |
| 464 | result = EAS_Render(mEasData, p, pLibConfig->mixBufferSize, &count); |
| 465 | if (result != EAS_SUCCESS) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 466 | ALOGE("EAS_Render returned %ld", result); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | } |
| 468 | p += count * pLibConfig->numChannels; |
| 469 | num_output += count * pLibConfig->numChannels * sizeof(EAS_PCM); |
| 470 | } |
| 471 | |
| 472 | // update playback state and position |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 473 | // ALOGV("MidiFile::render - updating state"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | EAS_GetLocation(mEasData, mEasHandle, &mPlayTime); |
| 475 | EAS_State(mEasData, mEasHandle, &mState); |
| 476 | mMutex.unlock(); |
| 477 | |
| 478 | // create audio output track if necessary |
| 479 | if (!mAudioSink->ready()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 480 | ALOGV("MidiFile::render - create output track"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | if (createOutputTrack() != NO_ERROR) |
| 482 | goto threadExit; |
| 483 | } |
| 484 | |
| 485 | // Write data to the audio hardware |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 486 | // ALOGV("MidiFile::render - writing to audio output"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | if ((temp = mAudioSink->write(mAudioBuffer, num_output)) < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 488 | ALOGE("Error in writing:%d",temp); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | return temp; |
| 490 | } |
| 491 | |
| 492 | // start audio output if necessary |
| 493 | if (!audioStarted) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 494 | //ALOGV("MidiFile::render - starting audio"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | mAudioSink->start(); |
| 496 | audioStarted = true; |
| 497 | } |
| 498 | |
| 499 | // still playing? |
| 500 | if ((mState == EAS_STATE_STOPPED) || (mState == EAS_STATE_ERROR) || |
| 501 | (mState == EAS_STATE_PAUSED)) |
| 502 | { |
| 503 | switch(mState) { |
| 504 | case EAS_STATE_STOPPED: |
| 505 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 506 | ALOGV("MidiFile::render - stopped"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | sendEvent(MEDIA_PLAYBACK_COMPLETE); |
| 508 | break; |
| 509 | } |
| 510 | case EAS_STATE_ERROR: |
| 511 | { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 512 | ALOGE("MidiFile::render - error"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | sendEvent(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN); |
| 514 | break; |
| 515 | } |
| 516 | case EAS_STATE_PAUSED: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 517 | ALOGV("MidiFile::render - paused"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | break; |
| 519 | default: |
| 520 | break; |
| 521 | } |
| 522 | mAudioSink->stop(); |
| 523 | audioStarted = false; |
| 524 | mRender = false; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | threadExit: |
| 529 | mAudioSink.clear(); |
| 530 | if (mAudioBuffer) { |
| 531 | delete [] mAudioBuffer; |
| 532 | mAudioBuffer = NULL; |
| 533 | } |
| 534 | mMutex.lock(); |
| 535 | mTid = -1; |
| 536 | mCondition.signal(); |
| 537 | mMutex.unlock(); |
| 538 | return result; |
| 539 | } |
| 540 | |
| 541 | } // end namespace android |