summaryrefslogtreecommitdiff
path: root/apps/plugins/midi/midifile.c
diff options
context:
space:
mode:
authorSolomon Peachy <pizza@shaftnet.org>2019-08-04 16:58:20 -0400
committerSolomon Peachy <pizza@shaftnet.org>2019-08-05 20:48:40 +0200
commited724fecb15d90d8075ed1edb963f455cb91b0a1 (patch)
tree6b72250278e5170bc7ed4b8263f48eb8e8dc1c61 /apps/plugins/midi/midifile.c
parenteea5bfc9aec35686406296641b37bcc2c731fbbb (diff)
downloadrockbox-ed724fecb15d90d8075ed1edb963f455cb91b0a1.tar.gz
rockbox-ed724fecb15d90d8075ed1edb963f455cb91b0a1.zip
Midiplay plugin ehancements
- Improved robustness - Improved sound quality - Use mixer and DSP Patch by Igor Poretsky Change-Id: I6fa617158cbaa53ae842295cdbdbe3a478e49ded
Diffstat (limited to 'apps/plugins/midi/midifile.c')
-rw-r--r--apps/plugins/midi/midifile.c23
1 files changed, 19 insertions, 4 deletions
diff --git a/apps/plugins/midi/midifile.c b/apps/plugins/midi/midifile.c
index 86544fd944..2340c6d14f 100644
--- a/apps/plugins/midi/midifile.c
+++ b/apps/plugins/midi/midifile.c
@@ -106,6 +106,11 @@ struct MIDIfile * loadFile(const char * filename)
106 if(id == ID_MTRK) 106 if(id == ID_MTRK)
107 { 107 {
108 mfload->tracks[track] = readTrack(file); 108 mfload->tracks[track] = readTrack(file);
109 if (!mfload->tracks[track])
110 {
111 rb->close(file);
112 return NULL;
113 }
109 track++; 114 track++;
110 } else 115 } else
111 { 116 {
@@ -125,7 +130,7 @@ struct MIDIfile * loadFile(const char * filename)
125 * and then track 2 starts loading */ 130 * and then track 2 starts loading */
126 131
127int rStatus = 0; 132int rStatus = 0;
128/* Returns 0 if done, 1 if keep going */ 133/* Returns 0 if done, 1 if keep going and -1 in case of error */
129static int readEvent(int file, void * dest) 134static int readEvent(int file, void * dest)
130{ 135{
131 struct Event dummy; 136 struct Event dummy;
@@ -152,6 +157,8 @@ static int readEvent(int file, void * dest)
152 { 157 {
153 /* Null-terminate for text events */ 158 /* Null-terminate for text events */
154 ev->evData = malloc(ev->len+1); /* Extra byte for the null termination */ 159 ev->evData = malloc(ev->len+1); /* Extra byte for the null termination */
160 if (!ev->evData)
161 return -1;
155 162
156 rb->read(file, ev->evData, ev->len); 163 rb->read(file, ev->evData, ev->len);
157 ev->evData[ev->len] = 0; 164 ev->evData[ev->len] = 0;
@@ -272,27 +279,35 @@ struct Track * readTrack(int file)
272 279
273 int pos = rb->lseek(file, 0, SEEK_CUR); 280 int pos = rb->lseek(file, 0, SEEK_CUR);
274 281
275 while(readEvent(file, NULL)) /* Memory saving technique */ 282 int evstat;
283
284 while ((evstat = readEvent(file, NULL)) > 0) /* Memory saving technique */
276 numEvents++; /* Attempt to read in events, count how many */ 285 numEvents++; /* Attempt to read in events, count how many */
277 /* THEN allocate memory and read them in */ 286 /* THEN allocate memory and read them in */
287 if (evstat < 0)
288 return NULL;
278 rb->lseek(file, pos, SEEK_SET); 289 rb->lseek(file, pos, SEEK_SET);
279 290
280 int trackSize = (numEvents+1) * sizeof(struct Event); 291 int trackSize = (numEvents+1) * sizeof(struct Event);
281 void * dataPtr = malloc(trackSize); 292 void * dataPtr = malloc(trackSize);
293 if (!dataPtr)
294 return NULL;
282 trk->dataBlock = dataPtr; 295 trk->dataBlock = dataPtr;
283 296
284 numEvents=0; 297 numEvents=0;
285 298
286 while(readEvent(file, dataPtr)) 299 while ((evstat = readEvent(file, dataPtr)) > 0)
287 { 300 {
288 if(trackSize < dataPtr-trk->dataBlock) 301 if(trackSize < dataPtr-trk->dataBlock)
289 { 302 {
290 midi_debug("Track parser memory out of bounds"); 303 midi_debug("Track parser memory out of bounds");
291 exit(1); 304 return NULL;
292 } 305 }
293 dataPtr+=sizeof(struct Event); 306 dataPtr+=sizeof(struct Event);
294 numEvents++; 307 numEvents++;
295 } 308 }
309 if (evstat < 0)
310 return NULL;
296 trk->numEvents = numEvents; 311 trk->numEvents = numEvents;
297 312
298 return trk; 313 return trk;