eas_imelody.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  1. /*----------------------------------------------------------------------------
  2. *
  3. * File:
  4. * eas_imelody.c
  5. *
  6. * Contents and purpose:
  7. * iMelody parser
  8. *
  9. * Copyright Sonic Network Inc. 2005
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. *----------------------------------------------------------------------------
  23. * Revision Control:
  24. * $Revision: 797 $
  25. * $Date: 2007-08-01 00:15:56 -0700 (Wed, 01 Aug 2007) $
  26. *----------------------------------------------------------------------------
  27. */
  28. /* lint doesn't like the way some string.h files look */
  29. #ifdef _lint
  30. #include "lint_stdlib.h"
  31. #else
  32. #include <string.h>
  33. #endif
  34. #include "eas_data.h"
  35. #include "eas_miditypes.h"
  36. #include "eas_parser.h"
  37. #include "eas_report.h"
  38. #include "eas_host.h"
  39. #include "eas_midi.h"
  40. #include "eas_config.h"
  41. #include "eas_vm_protos.h"
  42. #include "eas_imelodydata.h"
  43. #include "eas_ctype.h"
  44. // #define _DEBUG_IMELODY
  45. /* increase gain for mono ringtones */
  46. #define IMELODY_GAIN_OFFSET 8
  47. /* length of 32nd note in 1/256ths of a msec for 120 BPM tempo */
  48. #define DEFAULT_TICK_CONV 16000
  49. #define TICK_CONVERT 1920000
  50. /* default channel and program for iMelody playback */
  51. #define IMELODY_CHANNEL 0
  52. #define IMELODY_PROGRAM 80
  53. #define IMELODY_VEL_MUL 4
  54. #define IMELODY_VEL_OFS 67
  55. /* multiplier for fixed point triplet conversion */
  56. #define TRIPLET_MULTIPLIER 683
  57. #define TRIPLET_SHIFT 10
  58. static const char* const tokens[] =
  59. {
  60. "BEGIN:IMELODY",
  61. "VERSION:",
  62. "FORMAT:CLASS",
  63. "NAME:",
  64. "COMPOSER:",
  65. "BEAT:",
  66. "STYLE:",
  67. "VOLUME:",
  68. "MELODY:",
  69. "END:IMELODY"
  70. };
  71. /* ledon or ledoff */
  72. static const char ledStr[] = "edo";
  73. /* vibeon or vibeoff */
  74. static const char vibeStr[] = "ibeo";
  75. /* backon or backoff */
  76. static const char backStr[] = "cko";
  77. typedef enum
  78. {
  79. TOKEN_BEGIN,
  80. TOKEN_VERSION,
  81. TOKEN_FORMAT,
  82. TOKEN_NAME,
  83. TOKEN_COMPOSER,
  84. TOKEN_BEAT,
  85. TOKEN_STYLE,
  86. TOKEN_VOLUME,
  87. TOKEN_MELODY,
  88. TOKEN_END,
  89. TOKEN_INVALID
  90. } ENUM_IMELODY_TOKENS;
  91. /* lookup table for note values */
  92. static const EAS_I8 noteTable[] = { 9, 11, 0, 2, 4, 5, 7 };
  93. /* inline functions */
  94. #ifdef _DEBUG_IMELODY
  95. static void PutBackChar (S_IMELODY_DATA *pData)
  96. {
  97. if (pData->index)
  98. pData->index--;
  99. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "PutBackChar '%c'\n", pData->buffer[pData->index]); */ }
  100. }
  101. #else
  102. EAS_INLINE void PutBackChar (S_IMELODY_DATA *pData) { if (pData->index) pData->index--; }
  103. #endif
  104. /* local prototypes */
  105. static EAS_RESULT IMY_CheckFileType (S_EAS_DATA *pEASData, EAS_FILE_HANDLE fileHandle, EAS_VOID_PTR *ppHandle, EAS_I32 offset);
  106. static EAS_RESULT IMY_Prepare (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
  107. static EAS_RESULT IMY_Time (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_U32 *pTime);
  108. static EAS_RESULT IMY_Event (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_INT parserMode);
  109. static EAS_RESULT IMY_State (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_STATE *pState);
  110. static EAS_RESULT IMY_Close (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
  111. static EAS_RESULT IMY_Reset (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
  112. static EAS_RESULT IMY_Pause (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
  113. static EAS_RESULT IMY_Resume (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
  114. static EAS_RESULT IMY_SetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 value);
  115. static EAS_RESULT IMY_GetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 *pValue);
  116. static EAS_BOOL IMY_PlayNote (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData, EAS_I8 note, EAS_INT parserMode);
  117. static EAS_BOOL IMY_PlayRest (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData);
  118. static EAS_BOOL IMY_GetDuration (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_I32 *pDuration);
  119. static EAS_BOOL IMY_GetLEDState (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData);
  120. static EAS_BOOL IMY_GetVibeState (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData);
  121. static EAS_BOOL IMY_GetBackState (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData);
  122. static EAS_BOOL IMY_GetVolume (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_BOOL inHeader);
  123. static EAS_BOOL IMY_GetNumber (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_INT *temp, EAS_BOOL inHeader);
  124. static EAS_RESULT IMY_ParseHeader (S_EAS_DATA *pEASData, S_IMELODY_DATA* pData);
  125. static EAS_I8 IMY_GetNextChar (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_BOOL inHeader);
  126. static EAS_RESULT IMY_ReadLine (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE fileHandle, EAS_I8 *buffer, EAS_I32 *pStartLine);
  127. static EAS_INT IMY_ParseLine (EAS_I8 *buffer, EAS_U8 *pIndex);
  128. /*----------------------------------------------------------------------------
  129. *
  130. * EAS_iMelody_Parser
  131. *
  132. * This structure contains the functional interface for the iMelody parser
  133. *----------------------------------------------------------------------------
  134. */
  135. const S_FILE_PARSER_INTERFACE EAS_iMelody_Parser =
  136. {
  137. IMY_CheckFileType,
  138. IMY_Prepare,
  139. IMY_Time,
  140. IMY_Event,
  141. IMY_State,
  142. IMY_Close,
  143. IMY_Reset,
  144. IMY_Pause,
  145. IMY_Resume,
  146. NULL,
  147. IMY_SetData,
  148. IMY_GetData,
  149. NULL
  150. };
  151. /*----------------------------------------------------------------------------
  152. * IMY_CheckFileType()
  153. *----------------------------------------------------------------------------
  154. * Purpose:
  155. * Check the file type to see if we can parse it
  156. *
  157. * Inputs:
  158. * pEASData - pointer to overall EAS data structure
  159. * handle - pointer to file handle
  160. *
  161. * Outputs:
  162. *
  163. *
  164. * Side Effects:
  165. *
  166. *----------------------------------------------------------------------------
  167. */
  168. static EAS_RESULT IMY_CheckFileType (S_EAS_DATA *pEASData, EAS_FILE_HANDLE fileHandle, EAS_VOID_PTR *ppHandle, EAS_I32 offset)
  169. {
  170. S_IMELODY_DATA* pData;
  171. EAS_I8 buffer[MAX_LINE_SIZE+1];
  172. EAS_U8 index;
  173. #ifdef _DEBUG_IMELODY
  174. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_CheckFileType\n"); */ }
  175. #endif
  176. /* read the first line of the file */
  177. *ppHandle = NULL;
  178. if (IMY_ReadLine(pEASData->hwInstData, fileHandle, buffer, NULL) != EAS_SUCCESS)
  179. return EAS_SUCCESS;
  180. /* check for header string */
  181. if (IMY_ParseLine(buffer, &index) == TOKEN_BEGIN)
  182. {
  183. /* check for static memory allocation */
  184. if (pEASData->staticMemoryModel)
  185. pData = EAS_CMEnumData(EAS_CM_IMELODY_DATA);
  186. else
  187. pData = EAS_HWMalloc(pEASData->hwInstData, sizeof(S_IMELODY_DATA));
  188. if (!pData)
  189. return EAS_ERROR_MALLOC_FAILED;
  190. EAS_HWMemSet(pData, 0, sizeof(S_IMELODY_DATA));
  191. /* initialize */
  192. pData->fileHandle = fileHandle;
  193. pData->fileOffset = offset;
  194. pData->state = EAS_STATE_ERROR;
  195. pData->state = EAS_STATE_OPEN;
  196. /* return a pointer to the instance data */
  197. *ppHandle = pData;
  198. }
  199. return EAS_SUCCESS;
  200. }
  201. /*----------------------------------------------------------------------------
  202. * IMY_Prepare()
  203. *----------------------------------------------------------------------------
  204. * Purpose:
  205. * Prepare to parse the file. Allocates instance data (or uses static allocation for
  206. * static memory model).
  207. *
  208. * Inputs:
  209. * pEASData - pointer to overall EAS data structure
  210. * handle - pointer to file handle
  211. *
  212. * Outputs:
  213. *
  214. *
  215. * Side Effects:
  216. *
  217. *----------------------------------------------------------------------------
  218. */
  219. static EAS_RESULT IMY_Prepare (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
  220. {
  221. S_IMELODY_DATA* pData;
  222. EAS_RESULT result;
  223. #ifdef _DEBUG_IMELODY
  224. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_Prepare\n"); */ }
  225. #endif
  226. /* check for valid state */
  227. pData = (S_IMELODY_DATA*) pInstData;
  228. if (pData->state != EAS_STATE_OPEN)
  229. return EAS_ERROR_NOT_VALID_IN_THIS_STATE;
  230. /* instantiate a synthesizer */
  231. if ((result = VMInitMIDI(pEASData, &pData->pSynth)) != EAS_SUCCESS)
  232. {
  233. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "VMInitMIDI returned %d\n", result); */ }
  234. return result;
  235. }
  236. /* parse the header */
  237. if ((result = IMY_ParseHeader(pEASData, pData)) != EAS_SUCCESS)
  238. return result;
  239. #ifdef _DEBUG_IMELODY
  240. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Prepare: state set to EAS_STATE_READY\n"); */ }
  241. #endif
  242. pData ->state = EAS_STATE_READY;
  243. return EAS_SUCCESS;
  244. }
  245. /*----------------------------------------------------------------------------
  246. * IMY_Time()
  247. *----------------------------------------------------------------------------
  248. * Purpose:
  249. * Returns the time of the next event in msecs
  250. *
  251. * Inputs:
  252. * pEASData - pointer to overall EAS data structure
  253. * handle - pointer to file handle
  254. * pTime - pointer to variable to hold time of next event (in msecs)
  255. *
  256. * Outputs:
  257. *
  258. *
  259. * Side Effects:
  260. *
  261. *----------------------------------------------------------------------------
  262. */
  263. /*lint -esym(715, pEASData) common decoder interface - pEASData not used */
  264. static EAS_RESULT IMY_Time (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_U32 *pTime)
  265. {
  266. S_IMELODY_DATA *pData;
  267. pData = (S_IMELODY_DATA*) pInstData;
  268. /* return time in milliseconds */
  269. /*lint -e{704} use shift instead of division */
  270. *pTime = pData->time >> 8;
  271. return EAS_SUCCESS;
  272. }
  273. /*----------------------------------------------------------------------------
  274. * IMY_Event()
  275. *----------------------------------------------------------------------------
  276. * Purpose:
  277. * Parse the next event in the file
  278. *
  279. * Inputs:
  280. * pEASData - pointer to overall EAS data structure
  281. * handle - pointer to file handle
  282. *
  283. * Outputs:
  284. *
  285. *
  286. * Side Effects:
  287. *
  288. *----------------------------------------------------------------------------
  289. */
  290. static EAS_RESULT IMY_Event (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_INT parserMode)
  291. {
  292. S_IMELODY_DATA* pData;
  293. EAS_RESULT result;
  294. EAS_I8 c;
  295. EAS_BOOL eof;
  296. EAS_INT temp;
  297. pData = (S_IMELODY_DATA*) pInstData;
  298. if (pData->state >= EAS_STATE_OPEN)
  299. return EAS_SUCCESS;
  300. /* initialize MIDI channel when the track starts playing */
  301. if (pData->time == 0)
  302. {
  303. #ifdef _DEBUG_IMELODY
  304. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Event: Reset\n"); */ }
  305. #endif
  306. /* set program to square lead */
  307. VMProgramChange(pEASData->pVoiceMgr, pData->pSynth, IMELODY_CHANNEL, IMELODY_PROGRAM);
  308. /* set channel volume to max */
  309. VMControlChange(pEASData->pVoiceMgr, pData->pSynth, IMELODY_CHANNEL, 7, 127);
  310. }
  311. /* check for end of note */
  312. if (pData->note)
  313. {
  314. #ifdef _DEBUG_IMELODY
  315. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Stopping note %d\n", pData->note); */ }
  316. #endif
  317. /* stop the note */
  318. VMStopNote(pEASData->pVoiceMgr, pData->pSynth, IMELODY_CHANNEL, pData->note, 0);
  319. pData->note = 0;
  320. /* check for rest between notes */
  321. if (pData->restTicks)
  322. {
  323. pData->time += pData->restTicks;
  324. pData->restTicks = 0;
  325. return EAS_SUCCESS;
  326. }
  327. }
  328. /* parse the next event */
  329. eof = EAS_FALSE;
  330. while (!eof)
  331. {
  332. /* get next character */
  333. c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_FALSE);
  334. switch (c)
  335. {
  336. /* start repeat */
  337. case '(':
  338. #ifdef _DEBUG_IMELODY
  339. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter repeat section\n", c); */ }
  340. #endif
  341. if (pData->repeatOffset < 0)
  342. {
  343. pData->repeatOffset = pData->startLine + (EAS_I32) pData->index;
  344. #ifdef _DEBUG_IMELODY
  345. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Repeat offset = %d\n", pData->repeatOffset); */ }
  346. #endif
  347. }
  348. else
  349. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Ignoring nested repeat section\n"); */ }
  350. break;
  351. /* end repeat */
  352. case ')':
  353. #ifdef _DEBUG_IMELODY
  354. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "End repeat section, repeat offset = %d\n", pData->repeatOffset); */ }
  355. #endif
  356. /* ignore invalid repeats */
  357. if (pData->repeatCount >= 0)
  358. {
  359. /* decrement repeat count (repeatCount == 0 means infinite loop) */
  360. if (pData->repeatCount > 0)
  361. {
  362. if (--pData->repeatCount == 0)
  363. {
  364. pData->repeatCount = -1;
  365. #ifdef _DEBUG_IMELODY
  366. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Repeat loop complete\n"); */ }
  367. #endif
  368. }
  369. }
  370. //2 TEMPORARY FIX: If locating, don't do infinite loops.
  371. //3 We need a different mode for metadata parsing where we don't loop at all
  372. if ((parserMode == eParserModePlay) || (pData->repeatCount != 0))
  373. {
  374. #ifdef _DEBUG_IMELODY
  375. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Rewinding file for repeat\n"); */ }
  376. #endif
  377. /* rewind to start of loop */
  378. if ((result = EAS_HWFileSeek(pEASData->hwInstData, pData->fileHandle, pData->repeatOffset)) != EAS_SUCCESS)
  379. return result;
  380. IMY_ReadLine(pEASData->hwInstData, pData->fileHandle, pData->buffer, &pData->startLine);
  381. pData->index = 0;
  382. /* if last loop, prevent future loops */
  383. if (pData->repeatCount == -1)
  384. pData->repeatOffset = -1;
  385. }
  386. }
  387. break;
  388. /* repeat count */
  389. case '@':
  390. if (!IMY_GetNumber(pEASData->hwInstData, pData, &temp, EAS_FALSE))
  391. eof = EAS_TRUE;
  392. else if (pData->repeatOffset > 0)
  393. {
  394. #ifdef _DEBUG_IMELODY
  395. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Repeat count = %dt", pData->repeatCount); */ }
  396. #endif
  397. if (pData->repeatCount < 0)
  398. pData->repeatCount = (EAS_I16) temp;
  399. }
  400. break;
  401. /* volume */
  402. case 'V':
  403. if (!IMY_GetVolume(pEASData->hwInstData, pData, EAS_FALSE))
  404. eof = EAS_TRUE;
  405. break;
  406. /* flat */
  407. case '&':
  408. pData->noteModifier = -1;
  409. break;
  410. /* sharp */
  411. case '#':
  412. pData->noteModifier = +1;
  413. break;
  414. /* octave */
  415. case '*':
  416. c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_FALSE);
  417. if (IsDigit(c))
  418. pData->octave = (EAS_U8) ((c - '0' + 1) * 12);
  419. else if (!c)
  420. eof = EAS_TRUE;
  421. break;
  422. /* ledon or ledoff */
  423. case 'l':
  424. if (!IMY_GetLEDState(pEASData, pData))
  425. eof = EAS_TRUE;
  426. break;
  427. /* vibeon or vibeoff */
  428. case 'v':
  429. if (!IMY_GetVibeState(pEASData, pData))
  430. eof = EAS_TRUE;
  431. break;
  432. /* either a B note or backon or backoff */
  433. case 'b':
  434. if (IMY_GetNextChar(pEASData->hwInstData, pData, EAS_FALSE) == 'a')
  435. {
  436. if (!IMY_GetBackState(pEASData, pData))
  437. eof = EAS_TRUE;
  438. }
  439. else
  440. {
  441. PutBackChar(pData);
  442. if (IMY_PlayNote(pEASData, pData, c, parserMode))
  443. return EAS_SUCCESS;
  444. eof = EAS_TRUE;
  445. }
  446. break;
  447. /* rest */
  448. case 'r':
  449. case 'R':
  450. if (IMY_PlayRest(pEASData, pData))
  451. return EAS_SUCCESS;
  452. eof = EAS_TRUE;
  453. break;
  454. /* EOF */
  455. case 0:
  456. #ifdef _DEBUG_IMELODY
  457. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Event: end of iMelody file detected\n"); */ }
  458. #endif
  459. eof = EAS_TRUE;
  460. break;
  461. /* must be a note */
  462. default:
  463. c = ToLower(c);
  464. if ((c >= 'a') && (c <= 'g'))
  465. {
  466. if (IMY_PlayNote(pEASData, pData, c, parserMode))
  467. return EAS_SUCCESS;
  468. eof = EAS_TRUE;
  469. }
  470. else
  471. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Ignoring unexpected character '%c' [0x%02x]\n", c, c); */ }
  472. break;
  473. }
  474. }
  475. /* handle EOF */
  476. #ifdef _DEBUG_IMELODY
  477. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Event: state set to EAS_STATE_STOPPING\n"); */ }
  478. #endif
  479. pData->state = EAS_STATE_STOPPING;
  480. VMReleaseAllVoices(pEASData->pVoiceMgr, pData->pSynth);
  481. return EAS_SUCCESS;
  482. }
  483. /*----------------------------------------------------------------------------
  484. * IMY_State()
  485. *----------------------------------------------------------------------------
  486. * Purpose:
  487. * Returns the current state of the stream
  488. *
  489. * Inputs:
  490. * pEASData - pointer to overall EAS data structure
  491. * handle - pointer to file handle
  492. * pState - pointer to variable to store state
  493. *
  494. * Outputs:
  495. *
  496. *
  497. * Side Effects:
  498. *
  499. *----------------------------------------------------------------------------
  500. */
  501. /*lint -esym(715, pEASData) common decoder interface - pEASData not used */
  502. static EAS_RESULT IMY_State (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 *pState)
  503. {
  504. S_IMELODY_DATA* pData;
  505. /* establish pointer to instance data */
  506. pData = (S_IMELODY_DATA*) pInstData;
  507. /* if stopping, check to see if synth voices are active */
  508. if (pData->state == EAS_STATE_STOPPING)
  509. {
  510. if (VMActiveVoices(pData->pSynth) == 0)
  511. {
  512. pData->state = EAS_STATE_STOPPED;
  513. #ifdef _DEBUG_IMELODY
  514. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_State: state set to EAS_STATE_STOPPED\n"); */ }
  515. #endif
  516. }
  517. }
  518. if (pData->state == EAS_STATE_PAUSING)
  519. {
  520. if (VMActiveVoices(pData->pSynth) == 0)
  521. {
  522. #ifdef _DEBUG_IMELODY
  523. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_State: state set to EAS_STATE_PAUSED\n"); */ }
  524. #endif
  525. pData->state = EAS_STATE_PAUSED;
  526. }
  527. }
  528. /* return current state */
  529. *pState = pData->state;
  530. return EAS_SUCCESS;
  531. }
  532. /*----------------------------------------------------------------------------
  533. * IMY_Close()
  534. *----------------------------------------------------------------------------
  535. * Purpose:
  536. * Close the file and clean up
  537. *
  538. * Inputs:
  539. * pEASData - pointer to overall EAS data structure
  540. * handle - pointer to file handle
  541. *
  542. * Outputs:
  543. *
  544. *
  545. * Side Effects:
  546. *
  547. *----------------------------------------------------------------------------
  548. */
  549. static EAS_RESULT IMY_Close (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
  550. {
  551. S_IMELODY_DATA* pData;
  552. EAS_RESULT result;
  553. #ifdef _DEBUG_IMELODY
  554. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Close: close file\n"); */ }
  555. #endif
  556. pData = (S_IMELODY_DATA*) pInstData;
  557. /* close the file */
  558. if ((result = EAS_HWCloseFile(pEASData->hwInstData, pData->fileHandle)) != EAS_SUCCESS)
  559. return result;
  560. /* free the synth */
  561. if (pData->pSynth != NULL)
  562. VMMIDIShutdown(pEASData, pData->pSynth);
  563. /* if using dynamic memory, free it */
  564. if (!pEASData->staticMemoryModel)
  565. EAS_HWFree(pEASData->hwInstData, pData);
  566. return EAS_SUCCESS;
  567. }
  568. /*----------------------------------------------------------------------------
  569. * IMY_Reset()
  570. *----------------------------------------------------------------------------
  571. * Purpose:
  572. * Reset the sequencer. Used for locating backwards in the file.
  573. *
  574. * Inputs:
  575. * pEASData - pointer to overall EAS data structure
  576. * handle - pointer to file handle
  577. *
  578. * Outputs:
  579. *
  580. *
  581. * Side Effects:
  582. *
  583. *----------------------------------------------------------------------------
  584. */
  585. static EAS_RESULT IMY_Reset (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
  586. {
  587. S_IMELODY_DATA* pData;
  588. EAS_RESULT result;
  589. #ifdef _DEBUG_IMELODY
  590. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Reset: reset file\n"); */ }
  591. #endif
  592. pData = (S_IMELODY_DATA*) pInstData;
  593. /* reset the synth */
  594. VMReset(pEASData->pVoiceMgr, pData->pSynth, EAS_TRUE);
  595. /* reset time to zero */
  596. pData->time = 0;
  597. pData->note = 0;
  598. /* reset file position and re-parse header */
  599. pData->state = EAS_STATE_ERROR;
  600. if ((result = EAS_HWFileSeek(pEASData->hwInstData, pData->fileHandle, pData->fileOffset)) != EAS_SUCCESS)
  601. return result;
  602. if ((result = IMY_ParseHeader (pEASData, pData)) != EAS_SUCCESS)
  603. return result;
  604. #ifdef _DEBUG_IMELODY
  605. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Reset: state set to EAS_STATE_ERROR\n"); */ }
  606. #endif
  607. pData->state = EAS_STATE_READY;
  608. return EAS_SUCCESS;
  609. }
  610. /*----------------------------------------------------------------------------
  611. * IMY_Pause()
  612. *----------------------------------------------------------------------------
  613. * Purpose:
  614. * Pauses the sequencer. Mutes all voices and sets state to pause.
  615. *
  616. * Inputs:
  617. * pEASData - pointer to overall EAS data structure
  618. * handle - pointer to file handle
  619. *
  620. * Outputs:
  621. *
  622. *
  623. * Side Effects:
  624. *
  625. *----------------------------------------------------------------------------
  626. */
  627. static EAS_RESULT IMY_Pause (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
  628. {
  629. S_IMELODY_DATA *pData;
  630. #ifdef _DEBUG_IMELODY
  631. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Pause: pause file\n"); */ }
  632. #endif
  633. /* can't pause a stopped stream */
  634. pData = (S_IMELODY_DATA*) pInstData;
  635. if (pData->state == EAS_STATE_STOPPED)
  636. return EAS_ERROR_ALREADY_STOPPED;
  637. /* mute the synthesizer */
  638. VMMuteAllVoices(pEASData->pVoiceMgr, pData->pSynth);
  639. pData->state = EAS_STATE_PAUSING;
  640. return EAS_SUCCESS;
  641. }
  642. /*----------------------------------------------------------------------------
  643. * IMY_Resume()
  644. *----------------------------------------------------------------------------
  645. * Purpose:
  646. * Resume playing after a pause, sets state back to playing.
  647. *
  648. * Inputs:
  649. * pEASData - pointer to overall EAS data structure
  650. * handle - pointer to file handle
  651. *
  652. * Outputs:
  653. *
  654. *
  655. * Side Effects:
  656. *
  657. *----------------------------------------------------------------------------
  658. */
  659. /*lint -esym(715, pEASData) common decoder interface - pEASData not used */
  660. static EAS_RESULT IMY_Resume (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
  661. {
  662. S_IMELODY_DATA *pData;
  663. #ifdef _DEBUG_IMELODY
  664. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_Resume: resume file\n"); */ }
  665. #endif
  666. /* can't resume a stopped stream */
  667. pData = (S_IMELODY_DATA*) pInstData;
  668. if (pData->state == EAS_STATE_STOPPED)
  669. return EAS_ERROR_ALREADY_STOPPED;
  670. /* nothing to do but resume playback */
  671. pData->state = EAS_STATE_PLAY;
  672. return EAS_SUCCESS;
  673. }
  674. /*----------------------------------------------------------------------------
  675. * IMY_SetData()
  676. *----------------------------------------------------------------------------
  677. * Purpose:
  678. * Adjust tempo relative to song tempo
  679. *
  680. * Inputs:
  681. * pEASData - pointer to overall EAS data structure
  682. * pInstData - pointer to iMelody instance data
  683. * rate - rate (28-bit fractional amount)
  684. *
  685. * Outputs:
  686. *
  687. *
  688. * Side Effects:
  689. *
  690. *----------------------------------------------------------------------------
  691. */
  692. /*lint -esym(715, pEASData) common decoder interface - pEASData not used */
  693. static EAS_RESULT IMY_SetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 value)
  694. {
  695. S_IMELODY_DATA *pData;
  696. pData = (S_IMELODY_DATA*) pInstData;
  697. switch (param)
  698. {
  699. /* set metadata callback */
  700. case PARSER_DATA_METADATA_CB:
  701. EAS_HWMemCpy(&pData->metadata, (void*) value, sizeof(S_METADATA_CB));
  702. break;
  703. default:
  704. return EAS_ERROR_INVALID_PARAMETER;
  705. }
  706. return EAS_SUCCESS;
  707. }
  708. /*----------------------------------------------------------------------------
  709. * IMY_GetData()
  710. *----------------------------------------------------------------------------
  711. * Purpose:
  712. * Return the file type
  713. *
  714. * Inputs:
  715. * pEASData - pointer to overall EAS data structure
  716. * pInstData - pointer to iMelody instance data
  717. *
  718. * Outputs:
  719. *
  720. *
  721. * Side Effects:
  722. *
  723. *----------------------------------------------------------------------------
  724. */
  725. /*lint -esym(715, pEASData) common decoder interface - pEASData not used */
  726. static EAS_RESULT IMY_GetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 *pValue)
  727. {
  728. S_IMELODY_DATA *pData;
  729. pData = (S_IMELODY_DATA*) pInstData;
  730. switch (param)
  731. {
  732. /* return file type as iMelody */
  733. case PARSER_DATA_FILE_TYPE:
  734. *pValue = EAS_FILE_IMELODY;
  735. break;
  736. case PARSER_DATA_SYNTH_HANDLE:
  737. *pValue = (EAS_I32) pData->pSynth;
  738. break;
  739. case PARSER_DATA_GAIN_OFFSET:
  740. *pValue = IMELODY_GAIN_OFFSET;
  741. break;
  742. default:
  743. return EAS_ERROR_INVALID_PARAMETER;
  744. }
  745. return EAS_SUCCESS;
  746. }
  747. /*----------------------------------------------------------------------------
  748. * IMY_PlayNote()
  749. *----------------------------------------------------------------------------
  750. * Purpose:
  751. *
  752. *
  753. * Inputs:
  754. *
  755. *
  756. * Outputs:
  757. *
  758. *
  759. * Side Effects:
  760. *
  761. *----------------------------------------------------------------------------
  762. */
  763. static EAS_BOOL IMY_PlayNote (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData, EAS_I8 note, EAS_INT parserMode)
  764. {
  765. EAS_I32 duration;
  766. EAS_U8 velocity;
  767. #ifdef _DEBUG_IMELODY
  768. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_PlayNote: start note %d\n", note); */ }
  769. #endif
  770. /* get the duration */
  771. if (!IMY_GetDuration(pEASData->hwInstData, pData, &duration))
  772. return EAS_FALSE;
  773. /* save note value */
  774. pData->note = (EAS_U8) (pData->octave + noteTable[note - 'a'] + pData->noteModifier);
  775. velocity = (EAS_U8) (pData->volume ? pData->volume * IMELODY_VEL_MUL + IMELODY_VEL_OFS : 0);
  776. /* start note only if in play mode */
  777. if (parserMode == eParserModePlay)
  778. VMStartNote(pEASData->pVoiceMgr, pData->pSynth, IMELODY_CHANNEL, pData->note, velocity);
  779. #ifdef _DEBUG_IMELODY
  780. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_PlayNote: Start note %d, duration %d\n", pData->note, duration); */ }
  781. #endif
  782. /* determine note length */
  783. switch (pData->style)
  784. {
  785. case 0:
  786. /*lint -e{704} shift for performance */
  787. pData->restTicks = duration >> 4;
  788. break;
  789. case 1:
  790. pData->restTicks = 0;
  791. break;
  792. case 2:
  793. /*lint -e{704} shift for performance */
  794. pData->restTicks = duration >> 1;
  795. break;
  796. default:
  797. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "IMY_PlayNote: Note style out of range: %d\n", pData->style); */ }
  798. /*lint -e{704} shift for performance */
  799. pData->restTicks = duration >> 4;
  800. break;
  801. }
  802. /* next event is at end of this note */
  803. pData->time += duration - pData->restTicks;
  804. /* reset the flat/sharp modifier */
  805. pData->noteModifier = 0;
  806. return EAS_TRUE;
  807. }
  808. /*----------------------------------------------------------------------------
  809. * IMY_PlayRest()
  810. *----------------------------------------------------------------------------
  811. * Purpose:
  812. *
  813. *
  814. * Inputs:
  815. *
  816. *
  817. * Outputs:
  818. *
  819. *
  820. * Side Effects:
  821. *
  822. *----------------------------------------------------------------------------
  823. */
  824. static EAS_BOOL IMY_PlayRest (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData)
  825. {
  826. EAS_I32 duration;
  827. #ifdef _DEBUG_IMELODY
  828. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_PlayRest]n"); */ }
  829. #endif
  830. /* get the duration */
  831. if (!IMY_GetDuration(pEASData->hwInstData, pData, &duration))
  832. return EAS_FALSE;
  833. #ifdef _DEBUG_IMELODY
  834. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_PlayRest: note duration %d\n", duration); */ }
  835. #endif
  836. /* next event is at end of this note */
  837. pData->time += duration;
  838. return EAS_TRUE;
  839. }
  840. /*----------------------------------------------------------------------------
  841. * IMY_GetDuration()
  842. *----------------------------------------------------------------------------
  843. * Purpose:
  844. *
  845. *
  846. * Inputs:
  847. *
  848. *
  849. * Outputs:
  850. *
  851. *
  852. * Side Effects:
  853. *
  854. *----------------------------------------------------------------------------
  855. */
  856. static EAS_BOOL IMY_GetDuration (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_I32 *pDuration)
  857. {
  858. EAS_I32 duration;
  859. EAS_I8 c;
  860. /* get the duration */
  861. *pDuration = 0;
  862. c = IMY_GetNextChar(hwInstData, pData, EAS_FALSE);
  863. if (!c)
  864. return EAS_FALSE;
  865. if ((c < '0') || (c > '5'))
  866. {
  867. #ifdef _DEBUG_IMELODY
  868. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetDuration: error in duration '%c'\n", c); */ }
  869. #endif
  870. return EAS_FALSE;
  871. }
  872. /* calculate total length of note */
  873. duration = pData->tick * (1 << ('5' - c));
  874. /* check for duration modifier */
  875. c = IMY_GetNextChar(hwInstData, pData, EAS_FALSE);
  876. if (c)
  877. {
  878. if (c == '.')
  879. /*lint -e{704} shift for performance */
  880. duration += duration >> 1;
  881. else if (c == ':')
  882. /*lint -e{704} shift for performance */
  883. duration += (duration >> 1) + (duration >> 2);
  884. else if (c == ';')
  885. /*lint -e{704} shift for performance */
  886. duration = (duration * TRIPLET_MULTIPLIER) >> TRIPLET_SHIFT;
  887. else
  888. PutBackChar(pData);
  889. }
  890. *pDuration = duration;
  891. return EAS_TRUE;
  892. }
  893. /*----------------------------------------------------------------------------
  894. * IMY_GetLEDState()
  895. *----------------------------------------------------------------------------
  896. * Purpose:
  897. *
  898. *
  899. * Inputs:
  900. *
  901. *
  902. * Outputs:
  903. *
  904. *
  905. * Side Effects:
  906. *
  907. *----------------------------------------------------------------------------
  908. */
  909. static EAS_BOOL IMY_GetLEDState (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData)
  910. {
  911. EAS_I8 c;
  912. EAS_INT i;
  913. #ifdef _DEBUG_IMELODY
  914. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_GetLEDState\n"); */ }
  915. #endif
  916. for (i = 0; i < 5; i++)
  917. {
  918. c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_FALSE);
  919. if (!c)
  920. return EAS_FALSE;
  921. switch (i)
  922. {
  923. case 3:
  924. if (c == 'n')
  925. {
  926. #ifdef _DEBUG_IMELODY
  927. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetLEDState: LED on\n"); */ }
  928. #endif
  929. EAS_HWLED(pEASData->hwInstData, EAS_TRUE);
  930. return EAS_TRUE;
  931. }
  932. else if (c != 'f')
  933. return EAS_FALSE;
  934. break;
  935. case 4:
  936. if (c == 'f')
  937. {
  938. #ifdef _DEBUG_IMELODY
  939. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetLEDState: LED off\n"); */ }
  940. #endif
  941. EAS_HWLED(pEASData->hwInstData, EAS_FALSE);
  942. return EAS_TRUE;
  943. }
  944. return EAS_FALSE;
  945. default:
  946. if (c != ledStr[i])
  947. return EAS_FALSE;
  948. break;
  949. }
  950. }
  951. return EAS_FALSE;
  952. }
  953. /*----------------------------------------------------------------------------
  954. * IMY_GetVibeState()
  955. *----------------------------------------------------------------------------
  956. * Purpose:
  957. *
  958. *
  959. * Inputs:
  960. *
  961. *
  962. * Outputs:
  963. *
  964. *
  965. * Side Effects:
  966. *
  967. *----------------------------------------------------------------------------
  968. */
  969. static EAS_BOOL IMY_GetVibeState (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData)
  970. {
  971. EAS_I8 c;
  972. EAS_INT i;
  973. #ifdef _DEBUG_IMELODY
  974. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_GetVibeState\n"); */ }
  975. #endif
  976. for (i = 0; i < 6; i++)
  977. {
  978. c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_FALSE);
  979. if (!c)
  980. return EAS_FALSE;
  981. switch (i)
  982. {
  983. case 4:
  984. if (c == 'n')
  985. {
  986. #ifdef _DEBUG_IMELODY
  987. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetVibeState: vibrate on\n"); */ }
  988. #endif
  989. EAS_HWVibrate(pEASData->hwInstData, EAS_TRUE);
  990. return EAS_TRUE;
  991. }
  992. else if (c != 'f')
  993. return EAS_FALSE;
  994. break;
  995. case 5:
  996. if (c == 'f')
  997. {
  998. #ifdef _DEBUG_IMELODY
  999. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetVibeState: vibrate off\n"); */ }
  1000. #endif
  1001. EAS_HWVibrate(pEASData->hwInstData, EAS_FALSE);
  1002. return EAS_TRUE;
  1003. }
  1004. return EAS_FALSE;
  1005. default:
  1006. if (c != vibeStr[i])
  1007. return EAS_FALSE;
  1008. break;
  1009. }
  1010. }
  1011. return EAS_FALSE;
  1012. }
  1013. /*----------------------------------------------------------------------------
  1014. * IMY_GetBackState()
  1015. *----------------------------------------------------------------------------
  1016. * Purpose:
  1017. *
  1018. *
  1019. * Inputs:
  1020. *
  1021. *
  1022. * Outputs:
  1023. *
  1024. *
  1025. * Side Effects:
  1026. *
  1027. *----------------------------------------------------------------------------
  1028. */
  1029. static EAS_BOOL IMY_GetBackState (S_EAS_DATA *pEASData, S_IMELODY_DATA *pData)
  1030. {
  1031. EAS_I8 c;
  1032. EAS_INT i;
  1033. #ifdef _DEBUG_IMELODY
  1034. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_GetBackState\n"); */ }
  1035. #endif
  1036. for (i = 0; i < 5; i++)
  1037. {
  1038. c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_FALSE);
  1039. if (!c)
  1040. return EAS_FALSE;
  1041. switch (i)
  1042. {
  1043. case 3:
  1044. if (c == 'n')
  1045. {
  1046. #ifdef _DEBUG_IMELODY
  1047. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetBackState: backlight on\n"); */ }
  1048. #endif
  1049. EAS_HWBackLight(pEASData->hwInstData, EAS_TRUE);
  1050. return EAS_TRUE;
  1051. }
  1052. else if (c != 'f')
  1053. return EAS_FALSE;
  1054. break;
  1055. case 4:
  1056. if (c == 'f')
  1057. {
  1058. #ifdef _DEBUG_IMELODY
  1059. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetBackState: backlight off\n"); */ }
  1060. #endif
  1061. EAS_HWBackLight(pEASData->hwInstData, EAS_FALSE);
  1062. return EAS_TRUE;
  1063. }
  1064. return EAS_FALSE;
  1065. default:
  1066. if (c != backStr[i])
  1067. return EAS_FALSE;
  1068. break;
  1069. }
  1070. }
  1071. return EAS_FALSE;
  1072. }
  1073. /*----------------------------------------------------------------------------
  1074. * IMY_GetVolume()
  1075. *----------------------------------------------------------------------------
  1076. * Purpose:
  1077. *
  1078. *
  1079. * Inputs:
  1080. *
  1081. *
  1082. * Outputs:
  1083. *
  1084. *
  1085. * Side Effects:
  1086. *
  1087. *----------------------------------------------------------------------------
  1088. */
  1089. static EAS_BOOL IMY_GetVolume (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_BOOL inHeader)
  1090. {
  1091. EAS_INT temp;
  1092. EAS_I8 c;
  1093. #ifdef _DEBUG_IMELODY
  1094. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_GetVolume\n"); */ }
  1095. #endif
  1096. c = IMY_GetNextChar(hwInstData, pData, inHeader);
  1097. if (c == '+')
  1098. {
  1099. if (pData->volume < 15)
  1100. pData->volume++;
  1101. return EAS_TRUE;
  1102. }
  1103. else if (c == '-')
  1104. {
  1105. if (pData->volume > 0)
  1106. pData->volume--;
  1107. return EAS_TRUE;
  1108. }
  1109. else if (IsDigit(c))
  1110. temp = c - '0';
  1111. else
  1112. return EAS_FALSE;
  1113. c = IMY_GetNextChar(hwInstData, pData, inHeader);
  1114. if (IsDigit(c))
  1115. temp = temp * 10 + c - '0';
  1116. else if (c)
  1117. PutBackChar(pData);
  1118. if ((temp >= 0) && (temp <= 15))
  1119. {
  1120. if (inHeader && (temp == 0))
  1121. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Ignoring V0 encountered in header\n"); */ }
  1122. else
  1123. pData->volume = (EAS_U8) temp;
  1124. }
  1125. return EAS_TRUE;
  1126. }
  1127. /*----------------------------------------------------------------------------
  1128. * IMY_GetNumber()
  1129. *----------------------------------------------------------------------------
  1130. * Purpose:
  1131. *
  1132. *
  1133. * Inputs:
  1134. *
  1135. *
  1136. * Outputs:
  1137. *
  1138. *
  1139. * Side Effects:
  1140. *
  1141. *----------------------------------------------------------------------------
  1142. */
  1143. static EAS_BOOL IMY_GetNumber (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_INT *temp, EAS_BOOL inHeader)
  1144. {
  1145. EAS_BOOL ok;
  1146. EAS_I8 c;
  1147. #ifdef _DEBUG_IMELODY
  1148. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_GetNumber\n"); */ }
  1149. #endif
  1150. *temp = 0;
  1151. ok = EAS_FALSE;
  1152. for (;;)
  1153. {
  1154. c = IMY_GetNextChar(hwInstData, pData, inHeader);
  1155. if (IsDigit(c))
  1156. {
  1157. *temp = *temp * 10 + c - '0';
  1158. ok = EAS_TRUE;
  1159. }
  1160. else
  1161. {
  1162. if (c)
  1163. PutBackChar(pData);
  1164. #ifdef _DEBUG_IMELODY
  1165. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetNumber: value %d\n", *temp); */ }
  1166. #endif
  1167. return ok;
  1168. }
  1169. }
  1170. }
  1171. /*----------------------------------------------------------------------------
  1172. * IMY_GetVersion()
  1173. *----------------------------------------------------------------------------
  1174. * Purpose:
  1175. *
  1176. *
  1177. * Inputs:
  1178. *
  1179. *
  1180. * Outputs:
  1181. *
  1182. *
  1183. * Side Effects:
  1184. *
  1185. *----------------------------------------------------------------------------
  1186. */
  1187. static EAS_BOOL IMY_GetVersion (S_IMELODY_DATA *pData, EAS_INT *pVersion)
  1188. {
  1189. EAS_I8 c;
  1190. EAS_INT temp;
  1191. EAS_INT version;
  1192. version = temp = 0;
  1193. for (;;)
  1194. {
  1195. c = pData->buffer[pData->index++];
  1196. if ((c == 0) || (c == '.'))
  1197. {
  1198. /*lint -e{701} use shift for performance */
  1199. version = (version << 8) + temp;
  1200. if (c == 0)
  1201. {
  1202. #ifdef _DEBUG_IMELODY
  1203. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetVersion: version 0x%04x\n", version); */ }
  1204. #endif
  1205. *pVersion = version;
  1206. return EAS_TRUE;
  1207. }
  1208. temp = 0;
  1209. }
  1210. else if (IsDigit(c))
  1211. temp = (temp * 10) + c - '0';
  1212. }
  1213. }
  1214. /*----------------------------------------------------------------------------
  1215. * IMY_MetaData()
  1216. *----------------------------------------------------------------------------
  1217. * Purpose:
  1218. * Prepare to parse the file. Allocates instance data (or uses static allocation for
  1219. * static memory model).
  1220. *
  1221. * Inputs:
  1222. * pEASData - pointer to overall EAS data structure
  1223. * handle - pointer to file handle
  1224. *
  1225. * Outputs:
  1226. *
  1227. *
  1228. * Side Effects:
  1229. *
  1230. *----------------------------------------------------------------------------
  1231. */
  1232. static void IMY_MetaData (S_IMELODY_DATA *pData, E_EAS_METADATA_TYPE metaType, EAS_I8 *buffer)
  1233. {
  1234. EAS_I32 len;
  1235. /* check for callback */
  1236. if (!pData->metadata.callback)
  1237. return;
  1238. /* copy data to host buffer */
  1239. len = (EAS_I32) strlen((char*) buffer);
  1240. if (len >pData->metadata.bufferSize)
  1241. len = pData->metadata.bufferSize;
  1242. strncpy((char*) pData->metadata.buffer, (char*) buffer, (size_t) len);
  1243. pData->metadata.buffer[len] = 0;
  1244. /* callback to host */
  1245. pData->metadata.callback(metaType, pData->metadata.buffer, pData->metadata.pUserData);
  1246. }
  1247. /*----------------------------------------------------------------------------
  1248. * IMY_ParseHeader()
  1249. *----------------------------------------------------------------------------
  1250. * Purpose:
  1251. * Prepare to parse the file. Allocates instance data (or uses static allocation for
  1252. * static memory model).
  1253. *
  1254. * Inputs:
  1255. * pEASData - pointer to overall EAS data structure
  1256. * handle - pointer to file handle
  1257. *
  1258. * Outputs:
  1259. *
  1260. *
  1261. * Side Effects:
  1262. *
  1263. *----------------------------------------------------------------------------
  1264. */
  1265. static EAS_RESULT IMY_ParseHeader (S_EAS_DATA *pEASData, S_IMELODY_DATA* pData)
  1266. {
  1267. EAS_RESULT result;
  1268. EAS_INT token;
  1269. EAS_INT temp;
  1270. EAS_I8 c;
  1271. #ifdef _DEBUG_IMELODY
  1272. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Enter IMY_ParseHeader\n"); */ }
  1273. #endif
  1274. /* initialize some defaults */
  1275. pData->time = 0;
  1276. pData->tick = DEFAULT_TICK_CONV;
  1277. pData->note = 0;
  1278. pData->noteModifier = 0;
  1279. pData ->restTicks = 0;
  1280. pData->volume = 7;
  1281. pData->octave = 60;
  1282. pData->repeatOffset = -1;
  1283. pData->repeatCount = -1;
  1284. pData->style = 0;
  1285. /* force the read of the first line */
  1286. pData->index = 1;
  1287. /* read data until we get to melody */
  1288. for (;;)
  1289. {
  1290. /* read a line from the file and parse the token */
  1291. if (pData->index != 0)
  1292. {
  1293. if ((result = IMY_ReadLine(pEASData->hwInstData, pData->fileHandle, pData->buffer, &pData->startLine)) != EAS_SUCCESS)
  1294. {
  1295. #ifdef _DEBUG_IMELODY
  1296. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_ParseHeader: IMY_ReadLine returned %d\n", result); */ }
  1297. #endif
  1298. return result;
  1299. }
  1300. }
  1301. token = IMY_ParseLine(pData->buffer, &pData->index);
  1302. switch (token)
  1303. {
  1304. /* ignore these valid tokens */
  1305. case TOKEN_BEGIN:
  1306. break;
  1307. case TOKEN_FORMAT:
  1308. if (!IMY_GetVersion(pData, &temp))
  1309. {
  1310. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Invalid FORMAT field '%s'\n", pData->buffer); */ }
  1311. return EAS_ERROR_FILE_FORMAT;
  1312. }
  1313. if ((temp != 0x0100) && (temp != 0x0200))
  1314. {
  1315. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Unsupported FORMAT %02x\n", temp); */ }
  1316. return EAS_ERROR_UNRECOGNIZED_FORMAT;
  1317. }
  1318. break;
  1319. case TOKEN_VERSION:
  1320. if (!IMY_GetVersion(pData, &temp))
  1321. {
  1322. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Invalid VERSION field '%s'\n", pData->buffer); */ }
  1323. return EAS_ERROR_FILE_FORMAT;
  1324. }
  1325. if ((temp != 0x0100) && (temp != 0x0102))
  1326. {
  1327. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Unsupported VERSION %02x\n", temp); */ }
  1328. return EAS_ERROR_UNRECOGNIZED_FORMAT;
  1329. }
  1330. break;
  1331. case TOKEN_NAME:
  1332. IMY_MetaData(pData, EAS_METADATA_TITLE, pData->buffer + pData->index);
  1333. break;
  1334. case TOKEN_COMPOSER:
  1335. IMY_MetaData(pData, EAS_METADATA_AUTHOR, pData->buffer + pData->index);
  1336. break;
  1337. /* handle beat */
  1338. case TOKEN_BEAT:
  1339. IMY_GetNumber(pEASData->hwInstData, pData, &temp, EAS_TRUE);
  1340. if ((temp >= 25) && (temp <= 900))
  1341. pData->tick = TICK_CONVERT / temp;
  1342. break;
  1343. /* handle style */
  1344. case TOKEN_STYLE:
  1345. c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_TRUE);
  1346. if (c == 'S')
  1347. c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_TRUE);
  1348. if ((c >= '0') && (c <= '2'))
  1349. pData->style = (EAS_U8) (c - '0');
  1350. else
  1351. {
  1352. PutBackChar(pData);
  1353. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Error in style command: %s\n", pData->buffer); */ }
  1354. }
  1355. break;
  1356. /* handle volume */
  1357. case TOKEN_VOLUME:
  1358. c = IMY_GetNextChar(pEASData->hwInstData, pData, EAS_TRUE);
  1359. if (c != 'V')
  1360. {
  1361. PutBackChar(pData);
  1362. if (!IsDigit(c))
  1363. {
  1364. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Error in volume command: %s\n", pData->buffer); */ }
  1365. break;
  1366. }
  1367. }
  1368. IMY_GetVolume(pEASData->hwInstData, pData, EAS_TRUE);
  1369. break;
  1370. case TOKEN_MELODY:
  1371. #ifdef _DEBUG_IMELODY
  1372. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Header successfully parsed\n"); */ }
  1373. #endif
  1374. return EAS_SUCCESS;
  1375. case TOKEN_END:
  1376. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Unexpected END:IMELODY encountered\n"); */ }
  1377. return EAS_ERROR_FILE_FORMAT;
  1378. default:
  1379. /* force a read of the next line */
  1380. pData->index = 1;
  1381. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Ignoring unrecognized token in iMelody file: %s\n", pData->buffer); */ }
  1382. break;
  1383. }
  1384. }
  1385. }
  1386. /*----------------------------------------------------------------------------
  1387. * IMY_GetNextChar()
  1388. *----------------------------------------------------------------------------
  1389. * Purpose:
  1390. *
  1391. *
  1392. * Inputs:
  1393. *
  1394. *
  1395. * Outputs:
  1396. *
  1397. *
  1398. * Side Effects:
  1399. *
  1400. *----------------------------------------------------------------------------
  1401. */
  1402. static EAS_I8 IMY_GetNextChar (EAS_HW_DATA_HANDLE hwInstData, S_IMELODY_DATA *pData, EAS_BOOL inHeader)
  1403. {
  1404. EAS_I8 c;
  1405. EAS_U8 index;
  1406. for (;;)
  1407. {
  1408. /* get next character */
  1409. c = pData->buffer[pData->index++];
  1410. /* buffer empty, read more */
  1411. if (!c)
  1412. {
  1413. /* don't read the next line in the header */
  1414. if (inHeader)
  1415. return 0;
  1416. pData->index = 0;
  1417. pData->buffer[0] = 0;
  1418. if (IMY_ReadLine(hwInstData, pData->fileHandle, pData->buffer, &pData->startLine) != EAS_SUCCESS)
  1419. {
  1420. #ifdef _DEBUG_IMELODY
  1421. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetNextChar: EOF\n"); */ }
  1422. #endif
  1423. return 0;
  1424. }
  1425. /* check for END:IMELODY token */
  1426. if (IMY_ParseLine(pData->buffer, &index) == TOKEN_END)
  1427. {
  1428. #ifdef _DEBUG_IMELODY
  1429. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetNextChar: found END:IMELODY\n"); */ }
  1430. #endif
  1431. pData->buffer[0] = 0;
  1432. return 0;
  1433. }
  1434. continue;
  1435. }
  1436. /* ignore white space */
  1437. if (!IsSpace(c))
  1438. {
  1439. #ifdef _DEBUG_IMELODY
  1440. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_GetNextChar returned '%c'\n", c); */ }
  1441. #endif
  1442. return c;
  1443. }
  1444. }
  1445. }
  1446. /*----------------------------------------------------------------------------
  1447. * IMY_ReadLine()
  1448. *----------------------------------------------------------------------------
  1449. * Purpose:
  1450. * Reads a line of input from the file, discarding the CR/LF
  1451. *
  1452. * Inputs:
  1453. *
  1454. *
  1455. * Outputs:
  1456. *
  1457. *
  1458. * Side Effects:
  1459. *
  1460. *----------------------------------------------------------------------------
  1461. */
  1462. static EAS_RESULT IMY_ReadLine (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE fileHandle, EAS_I8 *buffer, EAS_I32 *pStartLine)
  1463. {
  1464. EAS_RESULT result;
  1465. EAS_INT i;
  1466. EAS_I8 c;
  1467. /* fetch current file position and save it */
  1468. if (pStartLine != NULL)
  1469. {
  1470. if ((result = EAS_HWFilePos(hwInstData, fileHandle, pStartLine)) != EAS_SUCCESS)
  1471. {
  1472. #ifdef _DEBUG_IMELODY
  1473. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_ParseHeader: EAS_HWFilePos returned %d\n", result); */ }
  1474. #endif
  1475. return result;
  1476. }
  1477. }
  1478. buffer[0] = 0;
  1479. for (i = 0; i < MAX_LINE_SIZE; )
  1480. {
  1481. if ((result = EAS_HWGetByte(hwInstData, fileHandle, &c)) != EAS_SUCCESS)
  1482. {
  1483. if ((result == EAS_EOF) && (i > 0))
  1484. break;
  1485. return result;
  1486. }
  1487. /* return on LF or end of data */
  1488. if (c == '\n')
  1489. break;
  1490. /* store characters in buffer */
  1491. if (c != '\r')
  1492. buffer[i++] = c;
  1493. }
  1494. buffer[i] = 0;
  1495. #ifdef _DEBUG_IMELODY
  1496. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_ReadLine read %s\n", buffer); */ }
  1497. #endif
  1498. return EAS_SUCCESS;
  1499. }
  1500. /*----------------------------------------------------------------------------
  1501. * IMY_ParseLine()
  1502. *----------------------------------------------------------------------------
  1503. * Purpose:
  1504. *
  1505. *
  1506. * Inputs:
  1507. *
  1508. *
  1509. * Outputs:
  1510. *
  1511. *
  1512. * Side Effects:
  1513. *
  1514. *----------------------------------------------------------------------------
  1515. */
  1516. static EAS_INT IMY_ParseLine (EAS_I8 *buffer, EAS_U8 *pIndex)
  1517. {
  1518. EAS_INT i;
  1519. EAS_INT j;
  1520. /* there's no strnicmp() in stdlib, so we have to roll our own */
  1521. for (i = 0; i < TOKEN_INVALID; i++)
  1522. {
  1523. for (j = 0; ; j++)
  1524. {
  1525. /* end of token, must be a match */
  1526. if (tokens[i][j] == 0)
  1527. {
  1528. #ifdef _DEBUG_IMELODY
  1529. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_ParseLine found token %d\n", i); */ }
  1530. #endif
  1531. *pIndex = (EAS_U8) j;
  1532. return i;
  1533. }
  1534. if (tokens[i][j] != ToUpper(buffer[j]))
  1535. break;
  1536. }
  1537. }
  1538. #ifdef _DEBUG_IMELODY
  1539. { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "IMY_ParseLine: no token found\n"); */ }
  1540. #endif
  1541. return TOKEN_INVALID;
  1542. }