Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bc11e713 authored by Marco Nelissen's avatar Marco Nelissen
Browse files

Use callbacks for Midi I/O

Instead of having the Sonivox engine directly open the file and
use stdio to read from it, use caller-provided callbacks.

Change-Id: Ie55129109060a4a7862fee3177f994401e00b6c1
parent 43637c2e
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
#include <libsonivox/jet.h>
#include <libsonivox/eas_types.h>
#include <media/AudioTrack.h>
#include <media/MidiIoWrapper.h>


namespace android {
@@ -86,15 +87,13 @@ private:

    int                 mMaxTracks; // max number of MIDI tracks, usually 32
    EAS_DATA_HANDLE     mEasData;
    EAS_FILE_LOCATOR    mEasJetFileLoc;
    sp<MidiIoWrapper>   mIoWrapper;
    EAS_PCM*            mAudioBuffer;// EAS renders the MIDI data into this buffer,
    sp<AudioTrack>      mAudioTrack; // and we play it in this audio track
    int                 mTrackBufferSize;
    S_JET_STATUS        mJetStatus;
    S_JET_STATUS        mPreviousJetStatus;

    char                mJetFilePath[PATH_MAX];

    class JetPlayerThread : public Thread {
    public:
        JetPlayerThread(JetPlayer *player) : mPlayer(player) {
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MIDI_IO_WRAPPER_H_
#define MIDI_IO_WRAPPER_H_

#include <libsonivox/eas_types.h>

namespace android {

class MidiIoWrapper : public RefBase {
public:
    MidiIoWrapper(const char *path);
    MidiIoWrapper(int fd, off64_t offset, int64_t size);

    ~MidiIoWrapper();

    int readAt(void *buffer, int offset, int size);
    int size();

    EAS_FILE_LOCATOR getLocator();

private:
    int mFd;
    off64_t mBase;
    int64_t  mLength;
    EAS_FILE mEasFile;
};


}  // namespace android

#endif // MIDI_IO_WRAPPER_H_
+1 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ LOCAL_SRC_FILES:= \
    mediarecorder.cpp \
    IMediaMetadataRetriever.cpp \
    mediametadataretriever.cpp \
    MidiIoWrapper.cpp \
    ToneGenerator.cpp \
    JetPlayer.cpp \
    IOMX.cpp \
+5 −21
Original line number Diff line number Diff line
@@ -36,7 +36,6 @@ JetPlayer::JetPlayer(void *javaJetPlayer, int maxTracks, int trackBufferSize) :
        mPaused(false),
        mMaxTracks(maxTracks),
        mEasData(NULL),
        mEasJetFileLoc(NULL),
        mTrackBufferSize(trackBufferSize)
{
    ALOGV("JetPlayer constructor");
@@ -133,10 +132,7 @@ int JetPlayer::release()
        JET_Shutdown(mEasData);
        EAS_Shutdown(mEasData);
    }
    if (mEasJetFileLoc) {
        free(mEasJetFileLoc);
        mEasJetFileLoc = NULL;
    }
    mIoWrapper.clear();
    if (mAudioTrack != 0) {
        mAudioTrack->stop();
        mAudioTrack->flush();
@@ -327,16 +323,9 @@ int JetPlayer::loadFromFile(const char* path)

    Mutex::Autolock lock(mMutex);

    mEasJetFileLoc = (EAS_FILE_LOCATOR) malloc(sizeof(EAS_FILE));
    strncpy(mJetFilePath, path, sizeof(mJetFilePath));
    mJetFilePath[sizeof(mJetFilePath) - 1] = '\0';
    mEasJetFileLoc->path = mJetFilePath;

    mEasJetFileLoc->fd = 0;
    mEasJetFileLoc->length = 0;
    mEasJetFileLoc->offset = 0;
    mIoWrapper = new MidiIoWrapper(path);

    EAS_RESULT result = JET_OpenFile(mEasData, mEasJetFileLoc);
    EAS_RESULT result = JET_OpenFile(mEasData, mIoWrapper->getLocator());
    if (result != EAS_SUCCESS)
        mState = EAS_STATE_ERROR;
    else
@@ -352,13 +341,9 @@ int JetPlayer::loadFromFD(const int fd, const long long offset, const long long

    Mutex::Autolock lock(mMutex);

    mEasJetFileLoc = (EAS_FILE_LOCATOR) malloc(sizeof(EAS_FILE));
    mEasJetFileLoc->fd = fd;
    mEasJetFileLoc->offset = offset;
    mEasJetFileLoc->length = length;
    mEasJetFileLoc->path = NULL;
    mIoWrapper = new MidiIoWrapper(fd, offset, length);

    EAS_RESULT result = JET_OpenFile(mEasData, mEasJetFileLoc);
    EAS_RESULT result = JET_OpenFile(mEasData, mIoWrapper->getLocator());
    if (result != EAS_SUCCESS)
        mState = EAS_STATE_ERROR;
    else
@@ -459,7 +444,6 @@ int JetPlayer::clearQueue()
//-------------------------------------------------------------------------------------------------
void JetPlayer::dump()
{
    ALOGE("JetPlayer dump: JET file=%s", mEasJetFileLoc->path);
}

void JetPlayer::dumpJetStatus(S_JET_STATUS* pJetStatus)
+76 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "MidiIoWrapper"
#include <utils/Log.h>
#include <utils/RefBase.h>

#include <sys/stat.h>
#include <fcntl.h>

#include "media/MidiIoWrapper.h"

static int readAt(void *handle, void *buffer, int pos, int size) {
    return ((android::MidiIoWrapper*)handle)->readAt(buffer, pos, size);
}
static int size(void *handle) {
    return ((android::MidiIoWrapper*)handle)->size();
}

namespace android {

MidiIoWrapper::MidiIoWrapper(const char *path) {
    ALOGV("MidiIoWrapper(%s)", path);
    mFd = open(path, O_RDONLY | O_LARGEFILE);
    mBase = 0;
    mLength = lseek(mFd, 0, SEEK_END);
}

MidiIoWrapper::MidiIoWrapper(int fd, off64_t offset, int64_t size) {
    ALOGV("MidiIoWrapper(fd=%d)", fd);
    mFd = dup(fd);
    mBase = offset;
    mLength = size;
}

MidiIoWrapper::~MidiIoWrapper() {
    ALOGV("~MidiIoWrapper");
    close(mFd);
}

int MidiIoWrapper::readAt(void *buffer, int offset, int size) {
    ALOGV("readAt(%p, %d, %d)", buffer, offset, size);
    lseek(mFd, mBase + offset, SEEK_SET);
    if (offset + size > mLength) {
        size = mLength - offset;
    }
    return read(mFd, buffer, size);
}

int MidiIoWrapper::size() {
    ALOGV("size() = %d", mLength);
    return mLength;
}

EAS_FILE_LOCATOR MidiIoWrapper::getLocator() {
    mEasFile.handle = this;
    mEasFile.readAt = ::readAt;
    mEasFile.size = ::size;
    return &mEasFile;
}

}  // namespace android
Loading