virtual float scoreFactory(const sp<IMediaPlayer>& client, int fd, int64_t offset, int64_t length, float curScore) { static const float kOurScore = 0.8; if (kOurScore <= curScore) return 0.0; // Some kind of MIDI? EAS_DATA_HANDLE easdata; if (EAS_Init(&easdata) == EAS_SUCCESS) { EAS_FILE locator; locator.path = NULL; locator.fd = fd; locator.offset = offset; locator.length = length; EAS_HANDLE eashandle; if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) { EAS_CloseFile(easdata, eashandle); EAS_Shutdown(easdata); return kOurScore; } EAS_Shutdown(easdata); } return 0.0; }
// 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; }
//------------------------------------------------------------------------------------------------- int JetPlayer::init() { //Mutex::Autolock lock(&mMutex); EAS_RESULT result; // retrieve the EAS library settings if (pLibConfig == NULL) pLibConfig = EAS_Config(); if (pLibConfig == NULL) { ALOGE("JetPlayer::init(): EAS library configuration could not be retrieved, aborting."); return EAS_FAILURE; } // init the EAS library result = EAS_Init(&mEasData); if (result != EAS_SUCCESS) { ALOGE("JetPlayer::init(): Error initializing Sonivox EAS library, aborting."); mState = EAS_STATE_ERROR; return result; } // init the JET library with the default app event controller range result = JET_Init(mEasData, NULL, sizeof(S_JET_CONFIG)); if (result != EAS_SUCCESS) { ALOGE("JetPlayer::init(): Error initializing JET library, aborting."); mState = EAS_STATE_ERROR; return result; } // create the output AudioTrack mAudioTrack = new AudioTrack(); mAudioTrack->set(AUDIO_STREAM_MUSIC, //TODO parameterize this pLibConfig->sampleRate, AUDIO_FORMAT_PCM_16_BIT, audio_channel_out_mask_from_count(pLibConfig->numChannels), mTrackBufferSize, AUDIO_OUTPUT_FLAG_NONE); // create render and playback thread { Mutex::Autolock l(mMutex); ALOGV("JetPlayer::init(): trying to start render thread"); mThread = new JetPlayerThread(this); mThread->run("jetRenderThread", ANDROID_PRIORITY_AUDIO); mCondition.wait(mMutex); } if (mTid > 0) { // render thread started, we're ready ALOGV("JetPlayer::init(): render thread(%d) successfully started.", mTid); mState = EAS_STATE_READY; } else { ALOGE("JetPlayer::init(): failed to start render thread."); mState = EAS_STATE_ERROR; return EAS_FAILURE; } return EAS_SUCCESS; }
//------------------------------------------------------------------------------------------------- int JetPlayer::init() { //Mutex::Autolock lock(&mMutex); EAS_RESULT result; // retrieve the EAS library settings if (pLibConfig == NULL) pLibConfig = EAS_Config(); if (pLibConfig == NULL) { LOGE("JetPlayer::init(): EAS library configuration could not be retrieved, aborting."); return EAS_FAILURE; } // init the EAS library result = EAS_Init(&mEasData); if( result != EAS_SUCCESS) { LOGE("JetPlayer::init(): Error initializing Sonivox EAS library, aborting."); mState = EAS_STATE_ERROR; return result; } // init the JET library with the default app event controller range result = JET_Init(mEasData, NULL, sizeof(S_JET_CONFIG)); if( result != EAS_SUCCESS) { LOGE("JetPlayer::init(): Error initializing JET library, aborting."); mState = EAS_STATE_ERROR; return result; } // create the output AudioTrack mAudioTrack = new AudioTrack(); mAudioTrack->set(AudioSystem::MUSIC, //TODO parametrize this pLibConfig->sampleRate, 1, // format = PCM 16bits per sample, pLibConfig->numChannels, mTrackBufferSize, 0); // create render and playback thread { Mutex::Autolock l(mMutex); LOGV("JetPlayer::init(): trying to start render thread"); createThreadEtc(renderThread, this, "jetRenderThread", ANDROID_PRIORITY_AUDIO); mCondition.wait(mMutex); } if (mTid > 0) { // render thread started, we're ready LOGV("JetPlayer::init(): render thread(%d) successfully started.", mTid); mState = EAS_STATE_READY; } else { LOGE("JetPlayer::init(): failed to start render thread."); mState = EAS_STATE_ERROR; return EAS_FAILURE; } return EAS_SUCCESS; }
static MediaScanResult HandleMIDI( const char *filename, MediaScannerClient *client) { // get the library configuration and do sanity check const S_EAS_LIB_CONFIG* pLibConfig = EAS_Config(); if ((pLibConfig == NULL) || (LIB_VERSION != pLibConfig->libVersion)) { LOGE("EAS library/header mismatch\n"); return MEDIA_SCAN_RESULT_ERROR; } EAS_I32 temp; // spin up a new EAS engine EAS_DATA_HANDLE easData = NULL; EAS_HANDLE easHandle = NULL; EAS_RESULT result = EAS_Init(&easData); if (result == EAS_SUCCESS) { EAS_FILE file; file.path = filename; file.fd = 0; file.offset = 0; file.length = 0; result = EAS_OpenFile(easData, &file, &easHandle); } if (result == EAS_SUCCESS) { result = EAS_Prepare(easData, easHandle); } if (result == EAS_SUCCESS) { result = EAS_ParseMetaData(easData, easHandle, &temp); } if (easHandle) { EAS_CloseFile(easData, easHandle); } if (easData) { EAS_Shutdown(easData); } if (result != EAS_SUCCESS) { #ifndef ANDROID_DEFAULT_CODE client->setMimeType("bad mime type"); #endif // ANDROID_DEFAULT_CODE return MEDIA_SCAN_RESULT_SKIPPED; } char buffer[20]; sprintf(buffer, "%ld", temp); status_t status = client->addStringTag("duration", buffer); if (status != OK) { return MEDIA_SCAN_RESULT_ERROR; } #ifndef ANDROID_DEFAULT_CODE status_t status_res = client->addStringTag("isAccurateDuration", "1"); if (status_res) { return MEDIA_SCAN_RESULT_ERROR; } #endif // ANDROID_DEFAULT_CODE return MEDIA_SCAN_RESULT_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; }
static status_t HandleMIDI( const char *filename, MediaScannerClient *client) { // get the library configuration and do sanity check const S_EAS_LIB_CONFIG* pLibConfig = EAS_Config(); if ((pLibConfig == NULL) || (LIB_VERSION != pLibConfig->libVersion)) { LOGE("EAS library/header mismatch\n"); return UNKNOWN_ERROR; } EAS_I32 temp; // spin up a new EAS engine EAS_DATA_HANDLE easData = NULL; EAS_HANDLE easHandle = NULL; EAS_RESULT result = EAS_Init(&easData); if (result == EAS_SUCCESS) { EAS_FILE file; file.path = filename; file.fd = 0; file.offset = 0; file.length = 0; result = EAS_OpenFile(easData, &file, &easHandle); } if (result == EAS_SUCCESS) { result = EAS_Prepare(easData, easHandle); } if (result == EAS_SUCCESS) { result = EAS_ParseMetaData(easData, easHandle, &temp); } if (easHandle) { EAS_CloseFile(easData, easHandle); } if (easData) { EAS_Shutdown(easData); } if (result != EAS_SUCCESS) { return UNKNOWN_ERROR; } char buffer[20]; sprintf(buffer, "%ld", temp); if (!client->addStringTag("duration", buffer)) return UNKNOWN_ERROR; return OK; }
status_t MidiFile::getDuration(int* duration) { ALOGV("MidiFile::getDuration"); { Mutex::Autolock lock(mMutex); if (!mEasHandle) return ERROR_NOT_OPEN; *duration = mDuration; } // if no duration cached, get the duration // don't need a lock here because we spin up a new engine if (*duration < 0) { EAS_I32 temp; EAS_DATA_HANDLE easData = NULL; EAS_HANDLE easHandle = NULL; EAS_RESULT result = EAS_Init(&easData); if (result == EAS_SUCCESS) { result = EAS_OpenFile(easData, &mFileLocator, &easHandle); } if (result == EAS_SUCCESS) { result = EAS_Prepare(easData, easHandle); } if (result == EAS_SUCCESS) { result = EAS_ParseMetaData(easData, easHandle, &temp); } if (easHandle) { EAS_CloseFile(easData, easHandle); } if (easData) { EAS_Shutdown(easData); } if (result != EAS_SUCCESS) { return ERROR_EAS_FAILURE; } // cache successful result mDuration = *duration = int(temp); } return NO_ERROR; }
MidiEngine::MidiEngine(const sp<DataSource> &dataSource, const sp<MetaData> &fileMetadata, const sp<MetaData> &trackMetadata) : mGroup(NULL), mEasData(NULL), mEasHandle(NULL), mEasConfig(NULL), mIsInitialized(false) { mIoWrapper = new MidiIoWrapper(dataSource); // spin up a new EAS engine EAS_I32 temp; EAS_RESULT result = EAS_Init(&mEasData); if (result == EAS_SUCCESS) { result = EAS_OpenFile(mEasData, mIoWrapper->getLocator(), &mEasHandle); } if (result == EAS_SUCCESS) { result = EAS_Prepare(mEasData, mEasHandle); } if (result == EAS_SUCCESS) { result = EAS_ParseMetaData(mEasData, mEasHandle, &temp); } if (result != EAS_SUCCESS) { return; } if (fileMetadata != NULL) { fileMetadata->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MIDI); } if (trackMetadata != NULL) { trackMetadata->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW); trackMetadata->setInt64(kKeyDuration, 1000ll * temp); // milli->micro mEasConfig = EAS_Config(); trackMetadata->setInt32(kKeySampleRate, mEasConfig->sampleRate); trackMetadata->setInt32(kKeyChannelCount, mEasConfig->numChannels); } mIsInitialized = true; }
static player_type getPlayerType(int fd, int64_t offset, int64_t length) { char buf[20]; lseek(fd, offset, SEEK_SET); read(fd, buf, sizeof(buf)); lseek(fd, offset, SEEK_SET); long ident = *((long*)buf); // Ogg vorbis? if (ident == 0x5367674f) // 'OggS' return VORBIS_PLAYER; // Some kind of MIDI? EAS_DATA_HANDLE easdata; if (EAS_Init(&easdata) == EAS_SUCCESS) { EAS_FILE locator; locator.path = NULL; locator.fd = fd; locator.offset = offset; locator.length = length; EAS_HANDLE eashandle; if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) { EAS_CloseFile(easdata, eashandle); EAS_Shutdown(easdata); return SONIVOX_PLAYER; } EAS_Shutdown(easdata); } #ifdef BUILD_WITH_GST // if GST_CONFIG_FILE exists, replace pv player with gst player if (access(GST_CONFIG_FILE, 0) == 0) return GST_PLAYER; #endif // BUILD_WITH_GST // Fall through to PV return PV_PLAYER; }
/*---------------------------------------------------------------------------- * main() *---------------------------------------------------------------------------- * Purpose: The entry point for the EAS sample application * * Inputs: * * Outputs: * *---------------------------------------------------------------------------- */ int main( int argc, char **argv ) { EAS_DATA_HANDLE easData; const S_EAS_LIB_CONFIG *pLibConfig; void *buffer; EAS_RESULT result, playResult; EAS_I32 bufferSize; int i; int temp; FILE *debugFile; char *outputFile = NULL; /* set the error reporting level */ EAS_SetDebugLevel(_EAS_SEVERITY_INFO); debugFile = NULL; /* process command-line arguments */ for (i = 1; i < argc; i++) { /* check for switch */ if (argv[i][0] == '-') { switch (argv[i][1]) { case 'd': temp = argv[i][2]; if ((temp >= '0') || (temp <= '9')) EAS_SetDebugLevel(temp); else { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Invalid debug level %d\n", temp); */ } break; case 'f': if ((debugFile = fopen(&argv[i][2],"w")) == NULL) { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Unable to create debug file %s\n", &argv[i][2]); */ } else EAS_SetDebugFile(debugFile, EAS_TRUE); break; case 'o': outputFile = &argv[i][2]; break; case 'p': polyphony = atoi(&argv[i][2]); if (polyphony < 1) polyphony = 1; { /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "Polyphony set to %d\n", polyphony); */ } break; default: break; } continue; } } /* assume success */ playResult = EAS_SUCCESS; /* get the library configuration */ pLibConfig = EAS_Config(); if (!EASLibraryCheck(pLibConfig)) return -1; if (polyphony > pLibConfig->maxVoices) polyphony = pLibConfig->maxVoices; /* calculate buffer size */ bufferSize = pLibConfig->mixBufferSize * pLibConfig->numChannels * (EAS_I32)sizeof(EAS_PCM) * NUM_BUFFERS; /* allocate output buffer memory */ buffer = malloc((EAS_U32)bufferSize); if (!buffer) { { /* dpp: EAS_ReportEx(_EAS_SEVERITY_FATAL, "Error allocating memory for audio buffer\n"); */ } return EAS_FAILURE; } /* initialize the EAS library */ polyphony = pLibConfig->maxVoices; if ((result = EAS_Init(&easData)) != EAS_SUCCESS) { { /* dpp: EAS_ReportEx(_EAS_SEVERITY_FATAL, "EAS_Init returned %ld - aborting!\n", result); */ } free(buffer); return result; } /* * Some debugging environments don't allow for passed parameters. * In this case, just play the default MIDI file "test.mid" */ if (argc < 2) { { /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "Playing '%s'\n", defaultTestFile); */ } if ((playResult = PlayFile(easData, defaultTestFile, NULL, pLibConfig, buffer, bufferSize)) != EAS_SUCCESS) { { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Error %d playing file %s\n", playResult, defaultTestFile); */ } } } /* iterate through the list of files to be played */ else { for (i = 1; i < argc; i++) { /* check for switch */ if (argv[i][0] != '-') { { /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "Playing '%s'\n", argv[i]); */ } if ((playResult = PlayFile(easData, argv[i], outputFile, pLibConfig, buffer, bufferSize)) != EAS_SUCCESS) { { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Error %d playing file %s\n", playResult, argv[i]); */ } break; } } } } /* shutdown the EAS library */ if ((result = EAS_Shutdown(easData)) != EAS_SUCCESS) { { /* dpp: EAS_ReportEx(_EAS_SEVERITY_FATAL, "EAS_Shutdown returned %ld\n", result); */ } } /* free the output buffer */ free(buffer); /* close the debug file */ if (debugFile) fclose(debugFile); /* play errors take precedence over shutdown errors */ if (playResult != EAS_SUCCESS) return playResult; return result; } /* end main */
player_type getPlayerType_l(int fd, int64_t offset, int64_t length, bool check_cedar) { int r_size; int file_format; char buf[4096]; lseek(fd, offset, SEEK_SET); r_size = read(fd, buf, sizeof(buf)); lseek(fd, offset, SEEK_SET); long ident = *((long*)buf); // Ogg vorbis? if (ident == 0x5367674f) // 'OggS' return STAGEFRIGHT_PLAYER; // Some kind of MIDI? EAS_DATA_HANDLE easdata; if (EAS_Init(&easdata) == EAS_SUCCESS) { EAS_FILE locator; locator.path = NULL; locator.fd = fd; locator.offset = offset; locator.length = length; EAS_HANDLE eashandle; if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) { EAS_CloseFile(easdata, eashandle); EAS_Shutdown(easdata); return SONIVOX_PLAYER; } EAS_Shutdown(easdata); } if (check_cedar) { file_format = audio_format_detect((unsigned char*)buf, r_size, fd, offset); ALOGV("getPlayerType: %d",file_format); if (file_format == MEDIA_FORMAT_MKV) { bool ret = MkvGetCodecId(fd, offset, length); if (ret) { return STAGEFRIGHT_PLAYER; } else { return CEDARX_PLAYER; } } else if (file_format == MEDIA_FORMAT_3GP) { return STAGEFRIGHT_PLAYER; } else if (file_format == MEDIA_FORMAT_M4A) { int audio_only; audio_only = MovAudioOnlyDetect1(fd, offset, length); lseek(fd, offset, SEEK_SET); return audio_only ? STAGEFRIGHT_PLAYER : CEDARX_PLAYER; } else { if(file_format < MEDIA_FORMAT_STAGEFRIGHT_MAX && file_format > MEDIA_FORMAT_STAGEFRIGHT_MIN){ ALOGV("use STAGEFRIGHT_PLAYER"); return STAGEFRIGHT_PLAYER; } else if(file_format < MEDIA_FORMAT_CEDARA_MAX && file_format > MEDIA_FORMAT_CEDARA_MIN){ ALOGV("use CEDARA_PLAYER"); return CEDARA_PLAYER; } else if(file_format < MEDIA_FORMAT_CEDARX_MAX && file_format > MEDIA_FORMAT_CEDARX_MIN){ ALOGV("use CEDARX_PLAYER"); return CEDARX_PLAYER; } } } //return STAGEFRIGHT_PLAYER; return CEDARX_PLAYER; }