12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739 |
- /*----------------------------------------------------------------------------
- *
- * File:
- * eas_imelody.c
- *
- * Contents and purpose:
- * iMelody parser
- *
- * Copyright Sonic Network Inc. 2005
- * 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.
- *
- *----------------------------------------------------------------------------
- * Revision Control:
- * $Revision: 797 $
- * $Date: 2007-08-01 00:15:56 -0700 (Wed, 01 Aug 2007) $
- *----------------------------------------------------------------------------
- */
- /* lint doesn't like the way some string.h files look */
- #ifdef _lint
- #include "lint_stdlib.h"
- #else
- #include <string.h>
- #endif
- #include "eas_data.h"
- #include "eas_miditypes.h"
- #include "eas_parser.h"
- #include "eas_report.h"
- #include "eas_host.h"
- #include "eas_midi.h"
- #include "eas_config.h"
- #include "eas_vm_protos.h"
- #include "eas_imelodydata.h"
- #include "eas_ctype.h"
- // #define _DEBUG_IMELODY
- /* increase gain for mono ringtones */
- #define IMELODY_GAIN_OFFSET 8
- /* length of 32nd note in 1/256ths of a msec for 120 BPM tempo */
- #define DEFAULT_TICK_CONV 16000
- #define TICK_CONVERT 1920000
- /* default channel and program for iMelody playback */
- #define IMELODY_CHANNEL 0
- #define IMELODY_PROGRAM 80
- #define IMELODY_VEL_MUL 4
- #define IMELODY_VEL_OFS 67
- /* multiplier for fixed point triplet conversion */
- #define TRIPLET_MULTIPLIER 683
- #define TRIPLET_SHIFT 10
- static const char* const tokens[] =
- {
- "BEGIN:IMELODY",
- "VERSION:",
- "FORMAT:CLASS",
- "NAME:",
- "COMPOSER:",
- "BEAT:",
- "STYLE:",
- "VOLUME:",
- "MELODY:",
- "END:IMELODY"
- };
- /* ledon or ledoff */
- static const char ledStr[] = "edo";
- /* vibeon or vibeoff */
- static const char vibeStr[] = "ibeo";
- /* backon or backoff */
- static const char backStr[] = "cko";
- typedef enum
- {
- TOKEN_BEGIN,
- TOKEN_VERSION,
- TOKEN_FORMAT,
- TOKEN_NAME,
- TOKEN_COMPOSER,
- TOKEN_BEAT,
- TOKEN_STYLE,
- TOKEN_VOLUME,
- TOKEN_MELODY,
- TOKEN_END,
- TOKEN_INVALID
- } ENUM_IMELODY_TOKENS;
- /* lookup table for note values */
- static const EAS_I8 noteTable[] = { 9, 11, 0, 2, 4, 5, 7 };
- /* inline functions */
- #ifdef _DEBUG_IMELODY
- static void PutBackChar (S_IMELODY_DATA *pData)
- {
- if (pData->index)
- pData->index--;
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "PutBackChar '%c'\n", pData->buffer[pData->index]); */ }
- }
- #else
- EAS_INLINE void PutBackChar (S_IMELODY_DATA *pData) { if (pData->index) pData->index--; }
- #endif
- /* local prototypes */
- static EAS_RESULT IMY_CheckFileType (S_EAS_DATA *pEASData, EAS_FILE_HANDLE fileHandle, EAS_VOID_PTR *ppHandle, EAS_I32 offset);
- static EAS_RESULT IMY_Prepare (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
- static EAS_RESULT IMY_Time (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_U32 *pTime);
- static EAS_RESULT IMY_Event (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_INT parserMode);
- static EAS_RESULT IMY_State (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_STATE *pState);
- static EAS_RESULT IMY_Close (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
- static EAS_RESULT IMY_Reset (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
- static EAS_RESULT IMY_Pause (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
- static EAS_RESULT IMY_Resume (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
- static EAS_RESULT IMY_SetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 value);
- static EAS_RESULT IMY_GetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 *pValue);
- static EAS_BOOL IMY_PlayNote (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData, EAS_I8 note, EAS_INT parserMode);
- static EAS_BOOL IMY_PlayRest (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData);
- static EAS_BOOL IMY_GetDuration (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_I32 *pDuration);
- static EAS_BOOL IMY_GetLEDState (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData);
- static EAS_BOOL IMY_GetVibeState (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData);
- static EAS_BOOL IMY_GetBackState (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData);
- static EAS_BOOL IMY_GetVolume (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_BOOL inHeader);
- static EAS_BOOL IMY_GetNumber (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_INT *temp, EAS_BOOL inHeader);
- static EAS_RESULT IMY_ParseHeader (S_EAS_DATA *pEASData, S_IMELODY_DATA* pData);
- static EAS_I8 IMY_GetNextChar (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_BOOL inHeader);
- static EAS_RESULT IMY_ReadLine (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE fileHandle, EAS_I8 *buffer, EAS_I32 *pStartLine);
- static EAS_INT IMY_ParseLine (EAS_I8 *buffer, EAS_U8 *pIndex);
- /*----------------------------------------------------------------------------
- *
- * EAS_iMelody_Parser
- *
- * This structure contains the functional interface for the iMelody parser
- *----------------------------------------------------------------------------
- */
- const S_FILE_PARSER_INTERFACE EAS_iMelody_Parser =
- {
- IMY_CheckFileType,
- IMY_Prepare,
- IMY_Time,
- IMY_Event,
- IMY_State,
- IMY_Close,
- IMY_Reset,
- IMY_Pause,
- IMY_Resume,
- NULL,
- IMY_SetData,
- IMY_GetData,
- NULL
- };
- /*----------------------------------------------------------------------------
- * IMY_CheckFileType()
- *----------------------------------------------------------------------------
- * Purpose:
- * Check the file type to see if we can parse it
- *
- * Inputs:
- * pEASData - pointer to overall EAS data structure
- * handle - pointer to file handle
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_RESULT IMY_CheckFileType (S_EAS_DATA *pEASData, EAS_FILE_HANDLE fileHandle, EAS_VOID_PTR *ppHandle, EAS_I32 offset)
- {
- S_IMELODY_DATA* pData;
- EAS_I8 buffer[MAX_LINE_SIZE+1];
- EAS_U8 index;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_CheckFileType\n"); */ }
- #endif
- /* read the first line of the file */
- *ppHandle = NULL;
- if (IMY_ReadLine(pEASData->hwInstData, fileHandle, buffer, NULL) != EAS_SUCCESS)
- return EAS_SUCCESS;
- /* check for header string */
- if (IMY_ParseLine(buffer, &index) == TOKEN_BEGIN)
- {
- /* check for static memory allocation */
- if (pEASData->staticMemoryModel)
- pData = EAS_CMEnumData(EAS_CM_IMELODY_DATA);
- else
- pData = EAS_HWMalloc(pEASData->hwInstData, sizeof(S_IMELODY_DATA));
- if (!pData)
- return EAS_ERROR_MALLOC_FAILED;
- EAS_HWMemSet(pData, 0, sizeof(S_IMELODY_DATA));
- /* initialize */
- pData->fileHandle = fileHandle;
- pData->fileOffset = offset;
- pData->state = EAS_STATE_ERROR;
- pData->state = EAS_STATE_OPEN;
- /* return a pointer to the instance data */
- *ppHandle = pData;
- }
- return EAS_SUCCESS;
- }
- /*----------------------------------------------------------------------------
- * IMY_Prepare()
- *----------------------------------------------------------------------------
- * Purpose:
- * Prepare to parse the file. Allocates instance data (or uses static allocation for
- * static memory model).
- *
- * Inputs:
- * pEASData - pointer to overall EAS data structure
- * handle - pointer to file handle
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_RESULT IMY_Prepare (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
- {
- S_IMELODY_DATA* pData;
- EAS_RESULT result;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_Prepare\n"); */ }
- #endif
- /* check for valid state */
- pData = (S_IMELODY_DATA*) pInstData;
- if (pData->state != EAS_STATE_OPEN)
- return EAS_ERROR_NOT_VALID_IN_THIS_STATE;
- /* instantiate a synthesizer */
- if ((result = VMInitMIDI(pEASData, &pData->pSynth)) != EAS_SUCCESS)
- {
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "VMInitMIDI returned %d\n", result); */ }
- return result;
- }
- /* parse the header */
- if ((result = IMY_ParseHeader(pEASData, pData)) != EAS_SUCCESS)
- return result;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Prepare: state set to EAS_STATE_READY\n"); */ }
- #endif
- pData ->state = EAS_STATE_READY;
- return EAS_SUCCESS;
- }
- /*----------------------------------------------------------------------------
- * IMY_Time()
- *----------------------------------------------------------------------------
- * Purpose:
- * Returns the time of the next event in msecs
- *
- * Inputs:
- * pEASData - pointer to overall EAS data structure
- * handle - pointer to file handle
- * pTime - pointer to variable to hold time of next event (in msecs)
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- /*lint -esym(715, pEASData) common decoder interface - pEASData not used */
- static EAS_RESULT IMY_Time (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_U32 *pTime)
- {
- S_IMELODY_DATA *pData;
- pData = (S_IMELODY_DATA*) pInstData;
- /* return time in milliseconds */
- /*lint -e{704} use shift instead of division */
- *pTime = pData->time >> 8;
- return EAS_SUCCESS;
- }
- /*----------------------------------------------------------------------------
- * IMY_Event()
- *----------------------------------------------------------------------------
- * Purpose:
- * Parse the next event in the file
- *
- * Inputs:
- * pEASData - pointer to overall EAS data structure
- * handle - pointer to file handle
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_RESULT IMY_Event (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_INT parserMode)
- {
- S_IMELODY_DATA* pData;
- EAS_RESULT result;
- EAS_I8 c;
- EAS_BOOL eof;
- EAS_INT temp;
- pData = (S_IMELODY_DATA*) pInstData;
- if (pData->state >= EAS_STATE_OPEN)
- return EAS_SUCCESS;
- /* initialize MIDI channel when the track starts playing */
- if (pData->time == 0)
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Event: Reset\n"); */ }
- #endif
- /* set program to square lead */
- VMProgramChange(pEASData->pVoiceMgr, pData->pSynth, IMELODY_CHANNEL, IMELODY_PROGRAM);
- /* set channel volume to max */
- VMControlChange(pEASData->pVoiceMgr, pData->pSynth, IMELODY_CHANNEL, 7, 127);
- }
- /* check for end of note */
- if (pData->note)
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Stopping note %d\n", pData->note); */ }
- #endif
- /* stop the note */
- VMStopNote(pEASData->pVoiceMgr, pData->pSynth, IMELODY_CHANNEL, pData->note, 0);
- pData->note = 0;
- /* check for rest between notes */
- if (pData->restTicks)
- {
- pData->time += pData->restTicks;
- pData->restTicks = 0;
- return EAS_SUCCESS;
- }
- }
- /* parse the next event */
- eof = EAS_FALSE;
- while (!eof)
- {
- /* get next character */
- c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_FALSE);
- switch (c)
- {
- /* start repeat */
- case '(':
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter repeat section\n", c); */ }
- #endif
- if (pData->repeatOffset < 0)
- {
- pData->repeatOffset = pData->startLine + (EAS_I32) pData->index;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Repeat offset = %d\n", pData->repeatOffset); */ }
- #endif
- }
- else
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Ignoring nested repeat section\n"); */ }
- break;
- /* end repeat */
- case ')':
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "End repeat section, repeat offset = %d\n", pData->repeatOffset); */ }
- #endif
- /* ignore invalid repeats */
- if (pData->repeatCount >= 0)
- {
- /* decrement repeat count (repeatCount == 0 means infinite loop) */
- if (pData->repeatCount > 0)
- {
- if (--pData->repeatCount == 0)
- {
- pData->repeatCount = -1;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Repeat loop complete\n"); */ }
- #endif
- }
- }
- //2 TEMPORARY FIX: If locating, don't do infinite loops.
- //3 We need a different mode for metadata parsing where we don't loop at all
- if ((parserMode == eParserModePlay) || (pData->repeatCount != 0))
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Rewinding file for repeat\n"); */ }
- #endif
- /* rewind to start of loop */
- if ((result = EAS_HWFileSeek(pEASData->hwInstData, pData->fileHandle, pData->repeatOffset)) != EAS_SUCCESS)
- return result;
- IMY_ReadLine(pEASData->hwInstData, pData->fileHandle, pData->buffer, &pData->startLine);
- pData->index = 0;
- /* if last loop, prevent future loops */
- if (pData->repeatCount == -1)
- pData->repeatOffset = -1;
- }
- }
- break;
- /* repeat count */
- case '@':
- if (!IMY_GetNumber(pEASData->hwInstData, pData, &temp, EAS_FALSE))
- eof = EAS_TRUE;
- else if (pData->repeatOffset > 0)
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Repeat count = %dt", pData->repeatCount); */ }
- #endif
- if (pData->repeatCount < 0)
- pData->repeatCount = (EAS_I16) temp;
- }
- break;
- /* volume */
- case 'V':
- if (!IMY_GetVolume(pEASData->hwInstData, pData, EAS_FALSE))
- eof = EAS_TRUE;
- break;
- /* flat */
- case '&':
- pData->noteModifier = -1;
- break;
- /* sharp */
- case '#':
- pData->noteModifier = +1;
- break;
- /* octave */
- case '*':
- c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_FALSE);
- if (IsDigit(c))
- pData->octave = (EAS_U8) ((c - '0' + 1) * 12);
- else if (!c)
- eof = EAS_TRUE;
- break;
- /* ledon or ledoff */
- case 'l':
- if (!IMY_GetLEDState(pEASData, pData))
- eof = EAS_TRUE;
- break;
- /* vibeon or vibeoff */
- case 'v':
- if (!IMY_GetVibeState(pEASData, pData))
- eof = EAS_TRUE;
- break;
- /* either a B note or backon or backoff */
- case 'b':
- if (IMY_GetNextChar(pEASData->hwInstData, pData, EAS_FALSE) == 'a')
- {
- if (!IMY_GetBackState(pEASData, pData))
- eof = EAS_TRUE;
- }
- else
- {
- PutBackChar(pData);
- if (IMY_PlayNote(pEASData, pData, c, parserMode))
- return EAS_SUCCESS;
- eof = EAS_TRUE;
- }
- break;
- /* rest */
- case 'r':
- case 'R':
- if (IMY_PlayRest(pEASData, pData))
- return EAS_SUCCESS;
- eof = EAS_TRUE;
- break;
- /* EOF */
- case 0:
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Event: end of iMelody file detected\n"); */ }
- #endif
- eof = EAS_TRUE;
- break;
- /* must be a note */
- default:
- c = ToLower(c);
- if ((c >= 'a') && (c <= 'g'))
- {
- if (IMY_PlayNote(pEASData, pData, c, parserMode))
- return EAS_SUCCESS;
- eof = EAS_TRUE;
- }
- else
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Ignoring unexpected character '%c' [0x%02x]\n", c, c); */ }
- break;
- }
- }
- /* handle EOF */
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Event: state set to EAS_STATE_STOPPING\n"); */ }
- #endif
- pData->state = EAS_STATE_STOPPING;
- VMReleaseAllVoices(pEASData->pVoiceMgr, pData->pSynth);
- return EAS_SUCCESS;
- }
- /*----------------------------------------------------------------------------
- * IMY_State()
- *----------------------------------------------------------------------------
- * Purpose:
- * Returns the current state of the stream
- *
- * Inputs:
- * pEASData - pointer to overall EAS data structure
- * handle - pointer to file handle
- * pState - pointer to variable to store state
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- /*lint -esym(715, pEASData) common decoder interface - pEASData not used */
- static EAS_RESULT IMY_State (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 *pState)
- {
- S_IMELODY_DATA* pData;
- /* establish pointer to instance data */
- pData = (S_IMELODY_DATA*) pInstData;
- /* if stopping, check to see if synth voices are active */
- if (pData->state == EAS_STATE_STOPPING)
- {
- if (VMActiveVoices(pData->pSynth) == 0)
- {
- pData->state = EAS_STATE_STOPPED;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_State: state set to EAS_STATE_STOPPED\n"); */ }
- #endif
- }
- }
- if (pData->state == EAS_STATE_PAUSING)
- {
- if (VMActiveVoices(pData->pSynth) == 0)
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_State: state set to EAS_STATE_PAUSED\n"); */ }
- #endif
- pData->state = EAS_STATE_PAUSED;
- }
- }
- /* return current state */
- *pState = pData->state;
- return EAS_SUCCESS;
- }
- /*----------------------------------------------------------------------------
- * IMY_Close()
- *----------------------------------------------------------------------------
- * Purpose:
- * Close the file and clean up
- *
- * Inputs:
- * pEASData - pointer to overall EAS data structure
- * handle - pointer to file handle
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_RESULT IMY_Close (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
- {
- S_IMELODY_DATA* pData;
- EAS_RESULT result;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Close: close file\n"); */ }
- #endif
- pData = (S_IMELODY_DATA*) pInstData;
- /* close the file */
- if ((result = EAS_HWCloseFile(pEASData->hwInstData, pData->fileHandle)) != EAS_SUCCESS)
- return result;
- /* free the synth */
- if (pData->pSynth != NULL)
- VMMIDIShutdown(pEASData, pData->pSynth);
- /* if using dynamic memory, free it */
- if (!pEASData->staticMemoryModel)
- EAS_HWFree(pEASData->hwInstData, pData);
- return EAS_SUCCESS;
- }
- /*----------------------------------------------------------------------------
- * IMY_Reset()
- *----------------------------------------------------------------------------
- * Purpose:
- * Reset the sequencer. Used for locating backwards in the file.
- *
- * Inputs:
- * pEASData - pointer to overall EAS data structure
- * handle - pointer to file handle
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_RESULT IMY_Reset (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
- {
- S_IMELODY_DATA* pData;
- EAS_RESULT result;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Reset: reset file\n"); */ }
- #endif
- pData = (S_IMELODY_DATA*) pInstData;
- /* reset the synth */
- VMReset(pEASData->pVoiceMgr, pData->pSynth, EAS_TRUE);
- /* reset time to zero */
- pData->time = 0;
- pData->note = 0;
- /* reset file position and re-parse header */
- pData->state = EAS_STATE_ERROR;
- if ((result = EAS_HWFileSeek(pEASData->hwInstData, pData->fileHandle, pData->fileOffset)) != EAS_SUCCESS)
- return result;
- if ((result = IMY_ParseHeader (pEASData, pData)) != EAS_SUCCESS)
- return result;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Reset: state set to EAS_STATE_ERROR\n"); */ }
- #endif
- pData->state = EAS_STATE_READY;
- return EAS_SUCCESS;
- }
- /*----------------------------------------------------------------------------
- * IMY_Pause()
- *----------------------------------------------------------------------------
- * Purpose:
- * Pauses the sequencer. Mutes all voices and sets state to pause.
- *
- * Inputs:
- * pEASData - pointer to overall EAS data structure
- * handle - pointer to file handle
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_RESULT IMY_Pause (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
- {
- S_IMELODY_DATA *pData;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Pause: pause file\n"); */ }
- #endif
- /* can't pause a stopped stream */
- pData = (S_IMELODY_DATA*) pInstData;
- if (pData->state == EAS_STATE_STOPPED)
- return EAS_ERROR_ALREADY_STOPPED;
- /* mute the synthesizer */
- VMMuteAllVoices(pEASData->pVoiceMgr, pData->pSynth);
- pData->state = EAS_STATE_PAUSING;
- return EAS_SUCCESS;
- }
- /*----------------------------------------------------------------------------
- * IMY_Resume()
- *----------------------------------------------------------------------------
- * Purpose:
- * Resume playing after a pause, sets state back to playing.
- *
- * Inputs:
- * pEASData - pointer to overall EAS data structure
- * handle - pointer to file handle
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- /*lint -esym(715, pEASData) common decoder interface - pEASData not used */
- static EAS_RESULT IMY_Resume (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
- {
- S_IMELODY_DATA *pData;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Resume: resume file\n"); */ }
- #endif
- /* can't resume a stopped stream */
- pData = (S_IMELODY_DATA*) pInstData;
- if (pData->state == EAS_STATE_STOPPED)
- return EAS_ERROR_ALREADY_STOPPED;
- /* nothing to do but resume playback */
- pData->state = EAS_STATE_PLAY;
- return EAS_SUCCESS;
- }
- /*----------------------------------------------------------------------------
- * IMY_SetData()
- *----------------------------------------------------------------------------
- * Purpose:
- * Adjust tempo relative to song tempo
- *
- * Inputs:
- * pEASData - pointer to overall EAS data structure
- * pInstData - pointer to iMelody instance data
- * rate - rate (28-bit fractional amount)
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- /*lint -esym(715, pEASData) common decoder interface - pEASData not used */
- static EAS_RESULT IMY_SetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 value)
- {
- S_IMELODY_DATA *pData;
- pData = (S_IMELODY_DATA*) pInstData;
- switch (param)
- {
- /* set metadata callback */
- case PARSER_DATA_METADATA_CB:
- EAS_HWMemCpy(&pData->metadata, (void*) value, sizeof(S_METADATA_CB));
- break;
- default:
- return EAS_ERROR_INVALID_PARAMETER;
- }
- return EAS_SUCCESS;
- }
- /*----------------------------------------------------------------------------
- * IMY_GetData()
- *----------------------------------------------------------------------------
- * Purpose:
- * Return the file type
- *
- * Inputs:
- * pEASData - pointer to overall EAS data structure
- * pInstData - pointer to iMelody instance data
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- /*lint -esym(715, pEASData) common decoder interface - pEASData not used */
- static EAS_RESULT IMY_GetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 *pValue)
- {
- S_IMELODY_DATA *pData;
- pData = (S_IMELODY_DATA*) pInstData;
- switch (param)
- {
- /* return file type as iMelody */
- case PARSER_DATA_FILE_TYPE:
- *pValue = EAS_FILE_IMELODY;
- break;
- case PARSER_DATA_SYNTH_HANDLE:
- *pValue = (EAS_I32) pData->pSynth;
- break;
- case PARSER_DATA_GAIN_OFFSET:
- *pValue = IMELODY_GAIN_OFFSET;
- break;
- default:
- return EAS_ERROR_INVALID_PARAMETER;
- }
- return EAS_SUCCESS;
- }
- /*----------------------------------------------------------------------------
- * IMY_PlayNote()
- *----------------------------------------------------------------------------
- * Purpose:
- *
- *
- * Inputs:
- *
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_BOOL IMY_PlayNote (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData, EAS_I8 note, EAS_INT parserMode)
- {
- EAS_I32 duration;
- EAS_U8 velocity;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_PlayNote: start note %d\n", note); */ }
- #endif
- /* get the duration */
- if (!IMY_GetDuration(pEASData->hwInstData, pData, &duration))
- return EAS_FALSE;
- /* save note value */
- pData->note = (EAS_U8) (pData->octave + noteTable[note - 'a'] + pData->noteModifier);
- velocity = (EAS_U8) (pData->volume ? pData->volume * IMELODY_VEL_MUL + IMELODY_VEL_OFS : 0);
- /* start note only if in play mode */
- if (parserMode == eParserModePlay)
- VMStartNote(pEASData->pVoiceMgr, pData->pSynth, IMELODY_CHANNEL, pData->note, velocity);
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_PlayNote: Start note %d, duration %d\n", pData->note, duration); */ }
- #endif
- /* determine note length */
- switch (pData->style)
- {
- case 0:
- /*lint -e{704} shift for performance */
- pData->restTicks = duration >> 4;
- break;
- case 1:
- pData->restTicks = 0;
- break;
- case 2:
- /*lint -e{704} shift for performance */
- pData->restTicks = duration >> 1;
- break;
- default:
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "IMY_PlayNote: Note style out of range: %d\n", pData->style); */ }
- /*lint -e{704} shift for performance */
- pData->restTicks = duration >> 4;
- break;
- }
- /* next event is at end of this note */
- pData->time += duration - pData->restTicks;
- /* reset the flat/sharp modifier */
- pData->noteModifier = 0;
- return EAS_TRUE;
- }
- /*----------------------------------------------------------------------------
- * IMY_PlayRest()
- *----------------------------------------------------------------------------
- * Purpose:
- *
- *
- * Inputs:
- *
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_BOOL IMY_PlayRest (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData)
- {
- EAS_I32 duration;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_PlayRest]n"); */ }
- #endif
- /* get the duration */
- if (!IMY_GetDuration(pEASData->hwInstData, pData, &duration))
- return EAS_FALSE;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_PlayRest: note duration %d\n", duration); */ }
- #endif
- /* next event is at end of this note */
- pData->time += duration;
- return EAS_TRUE;
- }
- /*----------------------------------------------------------------------------
- * IMY_GetDuration()
- *----------------------------------------------------------------------------
- * Purpose:
- *
- *
- * Inputs:
- *
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_BOOL IMY_GetDuration (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_I32 *pDuration)
- {
- EAS_I32 duration;
- EAS_I8 c;
- /* get the duration */
- *pDuration = 0;
- c = IMY_GetNextChar(hwInstData, pData, EAS_FALSE);
- if (!c)
- return EAS_FALSE;
- if ((c < '0') || (c > '5'))
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetDuration: error in duration '%c'\n", c); */ }
- #endif
- return EAS_FALSE;
- }
- /* calculate total length of note */
- duration = pData->tick * (1 << ('5' - c));
- /* check for duration modifier */
- c = IMY_GetNextChar(hwInstData, pData, EAS_FALSE);
- if (c)
- {
- if (c == '.')
- /*lint -e{704} shift for performance */
- duration += duration >> 1;
- else if (c == ':')
- /*lint -e{704} shift for performance */
- duration += (duration >> 1) + (duration >> 2);
- else if (c == ';')
- /*lint -e{704} shift for performance */
- duration = (duration * TRIPLET_MULTIPLIER) >> TRIPLET_SHIFT;
- else
- PutBackChar(pData);
- }
- *pDuration = duration;
- return EAS_TRUE;
- }
- /*----------------------------------------------------------------------------
- * IMY_GetLEDState()
- *----------------------------------------------------------------------------
- * Purpose:
- *
- *
- * Inputs:
- *
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_BOOL IMY_GetLEDState (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData)
- {
- EAS_I8 c;
- EAS_INT i;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_GetLEDState\n"); */ }
- #endif
- for (i = 0; i < 5; i++)
- {
- c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_FALSE);
- if (!c)
- return EAS_FALSE;
- switch (i)
- {
- case 3:
- if (c == 'n')
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetLEDState: LED on\n"); */ }
- #endif
- EAS_HWLED(pEASData->hwInstData, EAS_TRUE);
- return EAS_TRUE;
- }
- else if (c != 'f')
- return EAS_FALSE;
- break;
- case 4:
- if (c == 'f')
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetLEDState: LED off\n"); */ }
- #endif
- EAS_HWLED(pEASData->hwInstData, EAS_FALSE);
- return EAS_TRUE;
- }
- return EAS_FALSE;
- default:
- if (c != ledStr[i])
- return EAS_FALSE;
- break;
- }
- }
- return EAS_FALSE;
- }
- /*----------------------------------------------------------------------------
- * IMY_GetVibeState()
- *----------------------------------------------------------------------------
- * Purpose:
- *
- *
- * Inputs:
- *
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_BOOL IMY_GetVibeState (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData)
- {
- EAS_I8 c;
- EAS_INT i;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_GetVibeState\n"); */ }
- #endif
- for (i = 0; i < 6; i++)
- {
- c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_FALSE);
- if (!c)
- return EAS_FALSE;
- switch (i)
- {
- case 4:
- if (c == 'n')
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetVibeState: vibrate on\n"); */ }
- #endif
- EAS_HWVibrate(pEASData->hwInstData, EAS_TRUE);
- return EAS_TRUE;
- }
- else if (c != 'f')
- return EAS_FALSE;
- break;
- case 5:
- if (c == 'f')
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetVibeState: vibrate off\n"); */ }
- #endif
- EAS_HWVibrate(pEASData->hwInstData, EAS_FALSE);
- return EAS_TRUE;
- }
- return EAS_FALSE;
- default:
- if (c != vibeStr[i])
- return EAS_FALSE;
- break;
- }
- }
- return EAS_FALSE;
- }
- /*----------------------------------------------------------------------------
- * IMY_GetBackState()
- *----------------------------------------------------------------------------
- * Purpose:
- *
- *
- * Inputs:
- *
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_BOOL IMY_GetBackState (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData)
- {
- EAS_I8 c;
- EAS_INT i;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_GetBackState\n"); */ }
- #endif
- for (i = 0; i < 5; i++)
- {
- c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_FALSE);
- if (!c)
- return EAS_FALSE;
- switch (i)
- {
- case 3:
- if (c == 'n')
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetBackState: backlight on\n"); */ }
- #endif
- EAS_HWBackLight(pEASData->hwInstData, EAS_TRUE);
- return EAS_TRUE;
- }
- else if (c != 'f')
- return EAS_FALSE;
- break;
- case 4:
- if (c == 'f')
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetBackState: backlight off\n"); */ }
- #endif
- EAS_HWBackLight(pEASData->hwInstData, EAS_FALSE);
- return EAS_TRUE;
- }
- return EAS_FALSE;
- default:
- if (c != backStr[i])
- return EAS_FALSE;
- break;
- }
- }
- return EAS_FALSE;
- }
- /*----------------------------------------------------------------------------
- * IMY_GetVolume()
- *----------------------------------------------------------------------------
- * Purpose:
- *
- *
- * Inputs:
- *
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_BOOL IMY_GetVolume (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_BOOL inHeader)
- {
- EAS_INT temp;
- EAS_I8 c;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_GetVolume\n"); */ }
- #endif
- c = IMY_GetNextChar(hwInstData, pData, inHeader);
- if (c == '+')
- {
- if (pData->volume < 15)
- pData->volume++;
- return EAS_TRUE;
- }
- else if (c == '-')
- {
- if (pData->volume > 0)
- pData->volume--;
- return EAS_TRUE;
- }
- else if (IsDigit(c))
- temp = c - '0';
- else
- return EAS_FALSE;
- c = IMY_GetNextChar(hwInstData, pData, inHeader);
- if (IsDigit(c))
- temp = temp * 10 + c - '0';
- else if (c)
- PutBackChar(pData);
- if ((temp >= 0) && (temp <= 15))
- {
- if (inHeader && (temp == 0))
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Ignoring V0 encountered in header\n"); */ }
- else
- pData->volume = (EAS_U8) temp;
- }
- return EAS_TRUE;
- }
- /*----------------------------------------------------------------------------
- * IMY_GetNumber()
- *----------------------------------------------------------------------------
- * Purpose:
- *
- *
- * Inputs:
- *
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_BOOL IMY_GetNumber (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_INT *temp, EAS_BOOL inHeader)
- {
- EAS_BOOL ok;
- EAS_I8 c;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_GetNumber\n"); */ }
- #endif
- *temp = 0;
- ok = EAS_FALSE;
- for (;;)
- {
- c = IMY_GetNextChar(hwInstData, pData, inHeader);
- if (IsDigit(c))
- {
- *temp = *temp * 10 + c - '0';
- ok = EAS_TRUE;
- }
- else
- {
- if (c)
- PutBackChar(pData);
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetNumber: value %d\n", *temp); */ }
- #endif
- return ok;
- }
- }
- }
- /*----------------------------------------------------------------------------
- * IMY_GetVersion()
- *----------------------------------------------------------------------------
- * Purpose:
- *
- *
- * Inputs:
- *
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_BOOL IMY_GetVersion (S_IMELODY_DATA *pData, EAS_INT *pVersion)
- {
- EAS_I8 c;
- EAS_INT temp;
- EAS_INT version;
- version = temp = 0;
- for (;;)
- {
- c = pData->buffer[pData->index++];
- if ((c == 0) || (c == '.'))
- {
- /*lint -e{701} use shift for performance */
- version = (version << 8) + temp;
- if (c == 0)
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetVersion: version 0x%04x\n", version); */ }
- #endif
- *pVersion = version;
- return EAS_TRUE;
- }
- temp = 0;
- }
- else if (IsDigit(c))
- temp = (temp * 10) + c - '0';
- }
- }
- /*----------------------------------------------------------------------------
- * IMY_MetaData()
- *----------------------------------------------------------------------------
- * Purpose:
- * Prepare to parse the file. Allocates instance data (or uses static allocation for
- * static memory model).
- *
- * Inputs:
- * pEASData - pointer to overall EAS data structure
- * handle - pointer to file handle
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static void IMY_MetaData (S_IMELODY_DATA *pData, E_EAS_METADATA_TYPE metaType, EAS_I8 *buffer)
- {
- EAS_I32 len;
- /* check for callback */
- if (!pData->metadata.callback)
- return;
- /* copy data to host buffer */
- len = (EAS_I32) strlen((char*) buffer);
- if (len >pData->metadata.bufferSize)
- len = pData->metadata.bufferSize;
- strncpy((char*) pData->metadata.buffer, (char*) buffer, (size_t) len);
- pData->metadata.buffer[len] = 0;
- /* callback to host */
- pData->metadata.callback(metaType, pData->metadata.buffer, pData->metadata.pUserData);
- }
- /*----------------------------------------------------------------------------
- * IMY_ParseHeader()
- *----------------------------------------------------------------------------
- * Purpose:
- * Prepare to parse the file. Allocates instance data (or uses static allocation for
- * static memory model).
- *
- * Inputs:
- * pEASData - pointer to overall EAS data structure
- * handle - pointer to file handle
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_RESULT IMY_ParseHeader (S_EAS_DATA *pEASData, S_IMELODY_DATA* pData)
- {
- EAS_RESULT result;
- EAS_INT token;
- EAS_INT temp;
- EAS_I8 c;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_ParseHeader\n"); */ }
- #endif
- /* initialize some defaults */
- pData->time = 0;
- pData->tick = DEFAULT_TICK_CONV;
- pData->note = 0;
- pData->noteModifier = 0;
- pData ->restTicks = 0;
- pData->volume = 7;
- pData->octave = 60;
- pData->repeatOffset = -1;
- pData->repeatCount = -1;
- pData->style = 0;
- /* force the read of the first line */
- pData->index = 1;
- /* read data until we get to melody */
- for (;;)
- {
- /* read a line from the file and parse the token */
- if (pData->index != 0)
- {
- if ((result = IMY_ReadLine(pEASData->hwInstData, pData->fileHandle, pData->buffer, &pData->startLine)) != EAS_SUCCESS)
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_ParseHeader: IMY_ReadLine returned %d\n", result); */ }
- #endif
- return result;
- }
- }
- token = IMY_ParseLine(pData->buffer, &pData->index);
- switch (token)
- {
- /* ignore these valid tokens */
- case TOKEN_BEGIN:
- break;
- case TOKEN_FORMAT:
- if (!IMY_GetVersion(pData, &temp))
- {
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Invalid FORMAT field '%s'\n", pData->buffer); */ }
- return EAS_ERROR_FILE_FORMAT;
- }
- if ((temp != 0x0100) && (temp != 0x0200))
- {
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Unsupported FORMAT %02x\n", temp); */ }
- return EAS_ERROR_UNRECOGNIZED_FORMAT;
- }
- break;
- case TOKEN_VERSION:
- if (!IMY_GetVersion(pData, &temp))
- {
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Invalid VERSION field '%s'\n", pData->buffer); */ }
- return EAS_ERROR_FILE_FORMAT;
- }
- if ((temp != 0x0100) && (temp != 0x0102))
- {
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Unsupported VERSION %02x\n", temp); */ }
- return EAS_ERROR_UNRECOGNIZED_FORMAT;
- }
- break;
- case TOKEN_NAME:
- IMY_MetaData(pData, EAS_METADATA_TITLE, pData->buffer + pData->index);
- break;
- case TOKEN_COMPOSER:
- IMY_MetaData(pData, EAS_METADATA_AUTHOR, pData->buffer + pData->index);
- break;
- /* handle beat */
- case TOKEN_BEAT:
- IMY_GetNumber(pEASData->hwInstData, pData, &temp, EAS_TRUE);
- if ((temp >= 25) && (temp <= 900))
- pData->tick = TICK_CONVERT / temp;
- break;
- /* handle style */
- case TOKEN_STYLE:
- c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_TRUE);
- if (c == 'S')
- c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_TRUE);
- if ((c >= '0') && (c <= '2'))
- pData->style = (EAS_U8) (c - '0');
- else
- {
- PutBackChar(pData);
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Error in style command: %s\n", pData->buffer); */ }
- }
- break;
- /* handle volume */
- case TOKEN_VOLUME:
- c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_TRUE);
- if (c != 'V')
- {
- PutBackChar(pData);
- if (!IsDigit(c))
- {
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Error in volume command: %s\n", pData->buffer); */ }
- break;
- }
- }
- IMY_GetVolume(pEASData->hwInstData, pData, EAS_TRUE);
- break;
- case TOKEN_MELODY:
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Header successfully parsed\n"); */ }
- #endif
- return EAS_SUCCESS;
- case TOKEN_END:
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Unexpected END:IMELODY encountered\n"); */ }
- return EAS_ERROR_FILE_FORMAT;
- default:
- /* force a read of the next line */
- pData->index = 1;
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Ignoring unrecognized token in iMelody file: %s\n", pData->buffer); */ }
- break;
- }
- }
- }
- /*----------------------------------------------------------------------------
- * IMY_GetNextChar()
- *----------------------------------------------------------------------------
- * Purpose:
- *
- *
- * Inputs:
- *
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_I8 IMY_GetNextChar (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_BOOL inHeader)
- {
- EAS_I8 c;
- EAS_U8 index;
- for (;;)
- {
- /* get next character */
- c = pData->buffer[pData->index++];
- /* buffer empty, read more */
- if (!c)
- {
- /* don't read the next line in the header */
- if (inHeader)
- return 0;
- pData->index = 0;
- pData->buffer[0] = 0;
- if (IMY_ReadLine(hwInstData, pData->fileHandle, pData->buffer, &pData->startLine) != EAS_SUCCESS)
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetNextChar: EOF\n"); */ }
- #endif
- return 0;
- }
- /* check for END:IMELODY token */
- if (IMY_ParseLine(pData->buffer, &index) == TOKEN_END)
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetNextChar: found END:IMELODY\n"); */ }
- #endif
- pData->buffer[0] = 0;
- return 0;
- }
- continue;
- }
- /* ignore white space */
- if (!IsSpace(c))
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetNextChar returned '%c'\n", c); */ }
- #endif
- return c;
- }
- }
- }
- /*----------------------------------------------------------------------------
- * IMY_ReadLine()
- *----------------------------------------------------------------------------
- * Purpose:
- * Reads a line of input from the file, discarding the CR/LF
- *
- * Inputs:
- *
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_RESULT IMY_ReadLine (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE fileHandle, EAS_I8 *buffer, EAS_I32 *pStartLine)
- {
- EAS_RESULT result;
- EAS_INT i;
- EAS_I8 c;
- /* fetch current file position and save it */
- if (pStartLine != NULL)
- {
- if ((result = EAS_HWFilePos(hwInstData, fileHandle, pStartLine)) != EAS_SUCCESS)
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_ParseHeader: EAS_HWFilePos returned %d\n", result); */ }
- #endif
- return result;
- }
- }
- buffer[0] = 0;
- for (i = 0; i < MAX_LINE_SIZE; )
- {
- if ((result = EAS_HWGetByte(hwInstData, fileHandle, &c)) != EAS_SUCCESS)
- {
- if ((result == EAS_EOF) && (i > 0))
- break;
- return result;
- }
- /* return on LF or end of data */
- if (c == '\n')
- break;
- /* store characters in buffer */
- if (c != '\r')
- buffer[i++] = c;
- }
- buffer[i] = 0;
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_ReadLine read %s\n", buffer); */ }
- #endif
- return EAS_SUCCESS;
- }
- /*----------------------------------------------------------------------------
- * IMY_ParseLine()
- *----------------------------------------------------------------------------
- * Purpose:
- *
- *
- * Inputs:
- *
- *
- * Outputs:
- *
- *
- * Side Effects:
- *
- *----------------------------------------------------------------------------
- */
- static EAS_INT IMY_ParseLine (EAS_I8 *buffer, EAS_U8 *pIndex)
- {
- EAS_INT i;
- EAS_INT j;
- /* there's no strnicmp() in stdlib, so we have to roll our own */
- for (i = 0; i < TOKEN_INVALID; i++)
- {
- for (j = 0; ; j++)
- {
- /* end of token, must be a match */
- if (tokens[i][j] == 0)
- {
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_ParseLine found token %d\n", i); */ }
- #endif
- *pIndex = (EAS_U8) j;
- return i;
- }
- if (tokens[i][j] != ToUpper(buffer[j]))
- break;
- }
- }
- #ifdef _DEBUG_IMELODY
- { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_ParseLine: no token found\n"); */ }
- #endif
- return TOKEN_INVALID;
- }
|