// init EAS midi EAS_RESULT initEAS() { EAS_RESULT result; // get the library configuration pLibConfig = EAS_Config(); if (pLibConfig == NULL || pLibConfig->libVersion != LIB_VERSION) return EAS_FAILURE; // calculate buffer size bufferSize = pLibConfig->mixBufferSize * pLibConfig->numChannels * NUM_BUFFERS; // init library if ((result = EAS_Init(&pEASData)) != EAS_SUCCESS) return result; // select reverb preset and enable EAS_SetParameter(pEASData, EAS_MODULE_REVERB, EAS_PARAM_REVERB_PRESET, EAS_PARAM_REVERB_CHAMBER); EAS_SetParameter(pEASData, EAS_MODULE_REVERB, EAS_PARAM_REVERB_BYPASS, EAS_FALSE); // open midi stream if (result = EAS_OpenMIDIStream(pEASData, &midiHandle, NULL) != EAS_SUCCESS) return result; return EAS_SUCCESS; }
status_t MidiEngine::allocateBuffers() { // select reverb preset and enable EAS_SetParameter(mEasData, EAS_MODULE_REVERB, EAS_PARAM_REVERB_PRESET, EAS_PARAM_REVERB_CHAMBER); EAS_SetParameter(mEasData, EAS_MODULE_REVERB, EAS_PARAM_REVERB_BYPASS, EAS_FALSE); mGroup = new MediaBufferGroup; int bufsize = sizeof(EAS_PCM) * mEasConfig->mixBufferSize * mEasConfig->numChannels * NUM_COMBINE_BUFFERS; ALOGV("using %d byte buffer", bufsize); mGroup->add_buffer(new MediaBuffer(bufsize)); return OK; }
MidiFile::MidiFile() : mEasData(NULL), mEasHandle(NULL), mAudioBuffer(NULL), mPlayTime(-1), mDuration(-1), mState(EAS_STATE_ERROR), mStreamType(AUDIO_STREAM_MUSIC), mLoop(false), mExit(false), mPaused(false), mRender(false), mTid(-1) { ALOGV("constructor"); mFileLocator.path = NULL; mFileLocator.fd = -1; mFileLocator.offset = 0; mFileLocator.length = 0; // get the library configuration and do sanity check if (pLibConfig == NULL) pLibConfig = EAS_Config(); if ((pLibConfig == NULL) || (LIB_VERSION != pLibConfig->libVersion)) { ALOGE("EAS library/header mismatch"); goto Failed; } // initialize EAS library if (EAS_Init(&mEasData) != EAS_SUCCESS) { ALOGE("EAS_Init failed"); goto Failed; } // select reverb preset and enable EAS_SetParameter(mEasData, EAS_MODULE_REVERB, EAS_PARAM_REVERB_PRESET, EAS_PARAM_REVERB_CHAMBER); EAS_SetParameter(mEasData, EAS_MODULE_REVERB, EAS_PARAM_REVERB_BYPASS, EAS_FALSE); // create playback thread { Mutex::Autolock l(mMutex); mThread = new MidiFileThread(this); mThread->run("midithread", ANDROID_PRIORITY_AUDIO); mCondition.wait(mMutex); ALOGV("thread started"); } // indicate success if (mTid > 0) { ALOGV(" render thread(%d) started", mTid); mState = EAS_STATE_READY; } Failed: return; }
// midi init MIDI_RESULT MIDI_Init() { EAS_RESULT result; // get the library configuration pLibConfig = EAS_Config(); if ((pLibConfig == NULL) || (pLibConfig->libVersion != LIB_VERSION)) return MIDI_FAILURE; // init library if ((result = EAS_Init(&pEASData)) != EAS_SUCCESS) return result; // select reverb preset and enable EAS_SetParameter(pEASData, EAS_MODULE_REVERB, EAS_PARAM_REVERB_PRESET, EAS_PARAM_REVERB_CHAMBER); EAS_SetParameter(pEASData, EAS_MODULE_REVERB, EAS_PARAM_REVERB_BYPASS, EAS_FALSE); // open pcm stream if ((result = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) return result; // set stream parameters: 16bit, interleaved, channels and sample // rate from the EAS config and 50ms latency if ((result = snd_pcm_set_params(handle, SND_PCM_FORMAT_S16, SND_PCM_ACCESS_RW_INTERLEAVED, pLibConfig->numChannels, pLibConfig->sampleRate, 1, 50000)) < 0) return result; // calculate buffer size in samples bufferSize = pLibConfig->mixBufferSize * pLibConfig->numChannels * NUM_BUFFERS; // allocate buffer in bytes buffer = malloc(bufferSize * sizeof(EAS_PCM)); // clear flag flag = FALSE; // start rendering thread pthread_create(&thread, NULL, render, NULL); return MIDI_SUCCESS; }