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