aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/midi_extractor.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/midi_extractor.c')
-rw-r--r--src/plugins/midi_extractor.c216
1 files changed, 108 insertions, 108 deletions
diff --git a/src/plugins/midi_extractor.c b/src/plugins/midi_extractor.c
index 4ceff6a..66ea71d 100644
--- a/src/plugins/midi_extractor.c
+++ b/src/plugins/midi_extractor.c
@@ -31,27 +31,27 @@
31 * Types of events in MIDI. 31 * Types of events in MIDI.
32 */ 32 */
33enum EventType 33enum EventType
34 { 34{
35 ET_SEQUENCE_NUMBER = 0, 35 ET_SEQUENCE_NUMBER = 0,
36 ET_TEXT_EVENT = 1, 36 ET_TEXT_EVENT = 1,
37 ET_COPYRIGHT_NOTICE = 2, 37 ET_COPYRIGHT_NOTICE = 2,
38 ET_TRACK_NAME = 3, 38 ET_TRACK_NAME = 3,
39 ET_INSTRUMENT_NAME = 4, 39 ET_INSTRUMENT_NAME = 4,
40 ET_LYRIC_TEXT = 5, 40 ET_LYRIC_TEXT = 5,
41 ET_MARKER_TEXT = 6, 41 ET_MARKER_TEXT = 6,
42 ET_CUE_POINT = 7, 42 ET_CUE_POINT = 7,
43 ET_CHANNEL_PREFIX_ASSIGNMENT = 0x20, 43 ET_CHANNEL_PREFIX_ASSIGNMENT = 0x20,
44 ET_END_OF_TRACK = 0x2F, 44 ET_END_OF_TRACK = 0x2F,
45 ET_TEMPO_SETTING = 0x51, 45 ET_TEMPO_SETTING = 0x51,
46 ET_SMPTE_OFFSET = 0x54, 46 ET_SMPTE_OFFSET = 0x54,
47 ET_TIME_SIGNATURE = 0x58, 47 ET_TIME_SIGNATURE = 0x58,
48 ET_KEY_SIGNATURE = 0x59, 48 ET_KEY_SIGNATURE = 0x59,
49 ET_SEQUENCE_SPECIRFIC_EVENT = 0x7F 49 ET_SEQUENCE_SPECIRFIC_EVENT = 0x7F
50 }; 50};
51 51
52 52
53/** 53/**
54 * Main entry method for the 'audio/midi' extraction plugin. 54 * Main entry method for the 'audio/midi' extraction plugin.
55 * 55 *
56 * @param ec extraction context provided to the plugin 56 * @param ec extraction context provided to the plugin
57 */ 57 */
@@ -66,7 +66,7 @@ EXTRACTOR_midi_extract_method (struct EXTRACTOR_ExtractContext *ec)
66 smf_t *m = NULL; 66 smf_t *m = NULL;
67 smf_event_t *event; 67 smf_event_t *event;
68 uint8_t len; 68 uint8_t len;
69 69
70 if (4 >= (iret = ec->read (ec->cls, &buf, 1024))) 70 if (4 >= (iret = ec->read (ec->cls, &buf, 1024)))
71 return; 71 return;
72 data = buf; 72 data = buf;
@@ -81,103 +81,103 @@ EXTRACTOR_midi_extract_method (struct EXTRACTOR_ExtractContext *ec)
81 memcpy (data, buf, iret); 81 memcpy (data, buf, iret);
82 off = iret; 82 off = iret;
83 while (off < size) 83 while (off < size)
84 {
85 if (0 >= (iret = ec->read (ec->cls, &buf, 16 * 1024)))
84 { 86 {
85 if (0 >= (iret = ec->read (ec->cls, &buf, 16 * 1024))) 87 free (data);
86 { 88 return;
87 free (data); 89 }
88 return; 90 memcpy (&data[off], buf, iret);
89 } 91 off += iret;
90 memcpy (&data[off], buf, iret); 92 }
91 off += iret; 93 if (0 != ec->proc (ec->cls,
92 } 94 "midi",
93 if (0 != ec->proc (ec->cls, 95 EXTRACTOR_METATYPE_MIMETYPE,
94 "midi", 96 EXTRACTOR_METAFORMAT_UTF8,
95 EXTRACTOR_METATYPE_MIMETYPE, 97 "text/plain",
96 EXTRACTOR_METAFORMAT_UTF8, 98 "audio/midi",
97 "text/plain", 99 strlen ("audio/midi") + 1))
98 "audio/midi",
99 strlen ("audio/midi") + 1))
100 goto CLEANUP; 100 goto CLEANUP;
101 if (NULL == (m = smf_load_from_memory (data, size))) 101 if (NULL == (m = smf_load_from_memory (data, size)))
102 goto CLEANUP; 102 goto CLEANUP;
103 while (NULL != (event = smf_get_next_event (m))) 103 while (NULL != (event = smf_get_next_event (m)))
104 { 104 {
105 if (! smf_event_is_metadata (event)) 105 if (! smf_event_is_metadata (event))
106 break; 106 break;
107 len = event->midi_buffer[2]; 107 len = event->midi_buffer[2];
108 if ( (len > 0) && 108 if ( (len > 0) &&
109 isspace (event->midi_buffer[2 + len])) 109 isspace (event->midi_buffer[2 + len]))
110 len--; 110 len--;
111#if 0 111#if 0
112 fprintf (stderr, 112 fprintf (stderr,
113 "type: %d, len: %d value: %.*s\n", 113 "type: %d, len: %d value: %.*s\n",
114 event->midi_buffer[1], 114 event->midi_buffer[1],
115 event->midi_buffer[2], 115 event->midi_buffer[2],
116 (int) event->midi_buffer_length - 3, 116 (int) event->midi_buffer_length - 3,
117 (char *) &event->midi_buffer[3]); 117 (char *) &event->midi_buffer[3]);
118#endif 118#endif
119 if (1 != event->track_number) 119 if (1 != event->track_number)
120 continue; /* heuristic to not get instruments */ 120 continue; /* heuristic to not get instruments */
121 if (0 == len) 121 if (0 == len)
122 continue; 122 continue;
123 switch (event->midi_buffer[1]) 123 switch (event->midi_buffer[1])
124 { 124 {
125 case ET_TEXT_EVENT: 125 case ET_TEXT_EVENT:
126 if (0 != ec->proc (ec->cls, 126 if (0 != ec->proc (ec->cls,
127 "midi", 127 "midi",
128 EXTRACTOR_METATYPE_COMMENT, 128 EXTRACTOR_METATYPE_COMMENT,
129 EXTRACTOR_METAFORMAT_UTF8, 129 EXTRACTOR_METAFORMAT_UTF8,
130 "text/plain", 130 "text/plain",
131 (void*) &event->midi_buffer[3], 131 (void*) &event->midi_buffer[3],
132 len)) 132 len))
133 goto CLEANUP; 133 goto CLEANUP;
134 break; 134 break;
135 case ET_COPYRIGHT_NOTICE: 135 case ET_COPYRIGHT_NOTICE:
136 if (0 != ec->proc (ec->cls, 136 if (0 != ec->proc (ec->cls,
137 "midi", 137 "midi",
138 EXTRACTOR_METATYPE_COPYRIGHT, 138 EXTRACTOR_METATYPE_COPYRIGHT,
139 EXTRACTOR_METAFORMAT_UTF8, 139 EXTRACTOR_METAFORMAT_UTF8,
140 "text/plain", 140 "text/plain",
141 (void*) &event->midi_buffer[3], 141 (void*) &event->midi_buffer[3],
142 len)) 142 len))
143 goto CLEANUP; 143 goto CLEANUP;
144 break; 144 break;
145 case ET_TRACK_NAME: 145 case ET_TRACK_NAME:
146 if (0 != ec->proc (ec->cls, 146 if (0 != ec->proc (ec->cls,
147 "midi", 147 "midi",
148 EXTRACTOR_METATYPE_TITLE, 148 EXTRACTOR_METATYPE_TITLE,
149 EXTRACTOR_METAFORMAT_UTF8, 149 EXTRACTOR_METAFORMAT_UTF8,
150 "text/plain", 150 "text/plain",
151 (void*) &event->midi_buffer[3], 151 (void*) &event->midi_buffer[3],
152 len)) 152 len))
153 goto CLEANUP; 153 goto CLEANUP;
154 break; 154 break;
155 case ET_INSTRUMENT_NAME: 155 case ET_INSTRUMENT_NAME:
156 if (0 != ec->proc (ec->cls, 156 if (0 != ec->proc (ec->cls,
157 "midi", 157 "midi",
158 EXTRACTOR_METATYPE_SOURCE_DEVICE, 158 EXTRACTOR_METATYPE_SOURCE_DEVICE,
159 EXTRACTOR_METAFORMAT_UTF8, 159 EXTRACTOR_METAFORMAT_UTF8,
160 "text/plain", 160 "text/plain",
161 (void*) &event->midi_buffer[3], 161 (void*) &event->midi_buffer[3],
162 len)) 162 len))
163 goto CLEANUP; 163 goto CLEANUP;
164 break; 164 break;
165 case ET_LYRIC_TEXT: 165 case ET_LYRIC_TEXT:
166 if (0 != ec->proc (ec->cls, 166 if (0 != ec->proc (ec->cls,
167 "midi", 167 "midi",
168 EXTRACTOR_METATYPE_LYRICS, 168 EXTRACTOR_METATYPE_LYRICS,
169 EXTRACTOR_METAFORMAT_UTF8, 169 EXTRACTOR_METAFORMAT_UTF8,
170 "text/plain", 170 "text/plain",
171 (void*) &event->midi_buffer[3], 171 (void*) &event->midi_buffer[3],
172 len)) 172 len))
173 goto CLEANUP; 173 goto CLEANUP;
174 break; 174 break;
175 default: 175 default:
176 break; 176 break;
177 }
178 } 177 }
179 178 }
180 CLEANUP: 179
180CLEANUP:
181 if (NULL != m) 181 if (NULL != m)
182 smf_delete (m); 182 smf_delete (m);
183 free (data); 183 free (data);