Skip to content

Commit 5687880

Browse files
committed
Error Corrections
1 parent 2dbb9ec commit 5687880

File tree

7 files changed

+63
-38
lines changed

7 files changed

+63
-38
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
.vscode
2-
/build
2+
build/

examples/parse/CMakeLists.txt renamed to examples/midi-parse/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ cmake_minimum_required(VERSION 3.20)
44
project(midi-parse)
55
set (CMAKE_CXX_STANDARD 11)
66
set (DCMAKE_CXX_FLAGS "-Werror")
7+
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
78

89
# Build with library
910
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
1011
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../.. ${CMAKE_CURRENT_BINARY_DIR}/midi-file )
1112
endif()
1213

1314
# build sketch as executable
14-
SET_SOURCE_FILES_PROPERTIES(parse.ino PROPERTIES LANGUAGE CXX)
15-
add_executable (midi-parse parse.ino ../main.cpp)
15+
SET_SOURCE_FILES_PROPERTIES(midi-parse.ino PROPERTIES LANGUAGE CXX)
16+
add_executable (midi-parse midi-parse.ino ../main.cpp)
1617

1718
# specify libraries
1819
target_link_libraries(midi-parse midi-file)
File renamed without changes.

examples/parse/parse.ino renamed to examples/midi-parse/midi-parse.ino

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,49 @@
55
const int write_size = 256;
66
MidiFile mf;
77
int pos = 0;
8+
bool debug = false;
89

910
void setup() {
1011
#ifdef ARDUINO
1112
Serial.begin(19200);
1213
#endif
13-
mf.begin(false, 256*5);
14+
mf.begin(debug, 256 * 5);
1415
}
1516

1617
void loop() {
18+
if (!mf) return;
1719

1820
// try to keep buffer filled
1921
if (mf.availableForWrite() > write_size) {
2022
int len = std::min(write_size, (int)example_mid_len - len);
21-
mf.write(example_mid + pos, len);
23+
if (pos < example_mid_len) {
24+
mf.write(example_mid + pos, len);
25+
} else {
26+
// when there is no more data we let the parser know
27+
mf.end();
28+
}
2229
pos += 256;
2330
}
2431

2532
// parse midi
2633
auto state = mf.parse();
27-
if (state.status == MIDI_PARSER_TRACK_MIDI) {
34+
switch (state.status)
35+
case MIDI_PARSER_TRACK_MIDI: {
2836
// process midi
2937
printf("process");
3038
printf(" time: %ld", (long)state.vtime);
3139
printf(" status: %d [%s]", state.midi.status,
3240
mf.midi_status_name(state.midi.status));
3341
printf(" channel: %d", state.midi.channel);
3442
printf(" param1: %d", state.midi.param1);
35-
printf(" param2: %d", state.midi.param2);
43+
printf(" param2: %d\n", state.midi.param2);
44+
break;
45+
case MIDI_PARSER_ERROR:
46+
printf("Error\n");
47+
case MIDI_PARSER_EOB:
48+
#ifndef ARDUINO
49+
exit(-1);
50+
#endif
51+
break;
3652
}
3753
}

src/MidiFile.h

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "MidiFileCommon.h"
1919

2020
#define MIDI_BUFFER_SIZE 1024 * 2
21-
21+
#define MIDI_MIN_REFILL_SIZE 512
2222
/**
2323
* @brief Midi File parser. Provide the data via write: You should try to keep
2424
* the buffer as full as possible while parsing. You get the next parsing result
@@ -29,8 +29,8 @@ class MidiFile {
2929
public:
3030
bool begin(bool log = true, int bufferSize = MIDI_BUFFER_SIZE) {
3131
log_active = log;
32-
has_more_data = true;
3332
parser_state.in.resize(bufferSize);
33+
is_ok = true;
3434
reset();
3535
return true;
3636
}
@@ -39,9 +39,7 @@ class MidiFile {
3939
// store actual processing length
4040
if (len > 0) {
4141
write_len = len;
42-
} else {
43-
has_more_data = false;
44-
}
42+
}
4543
// write only if data is available
4644
if (len < parser_state.in.availableForWrite()) {
4745
return parser_state.in.write(data, len);
@@ -52,27 +50,28 @@ class MidiFile {
5250
int availableForWrite() { return parser_state.in.availableForWrite(); }
5351

5452
/// Parse and provide next midi element
55-
midi_parser_state parse() {
56-
// make sure that we do not run out of data
57-
if (parser_state.in.available() < 100 && has_more_data) {
58-
printf("MIDI_PARSER_MORE_DATA\n");
59-
parser_state.status_result = MIDI_PARSER_MORE_DATA;
60-
return parser_state;
61-
}
53+
midi_parser_state &parse() {
6254
// parse next element
55+
parser_state.status = MIDI_PARSER_EOB;
6356
if (!parser_state.in.isEmpty()) {
6457
midi_parser_status status = midi_parse();
65-
parser_state.status_result = status;
58+
parser_state.status = status;
6659
if (log_active) {
6760
logStatus(status);
6861
}
6962
}
63+
//
64+
if (parser_state.status == MIDI_PARSER_EOB || parser_state.status == MIDI_PARSER_ERROR ){
65+
is_ok = false;
66+
}
7067
return parser_state;
7168
}
7269

70+
operator bool() {
71+
return is_ok;
72+
}
73+
7374
void end() {
74-
reset();
75-
has_more_data = false;
7675
}
7776

7877
const char *midi_status_name(int status) {
@@ -153,7 +152,7 @@ class MidiFile {
153152
bool log_active = false;
154153
int write_len = 256;
155154
midi_parser_state parser_state;
156-
bool has_more_data = true;
155+
bool is_ok = true;
157156

158157
void logStatus(midi_parser_status status) {
159158
switch (status) {
@@ -166,11 +165,11 @@ class MidiFile {
166165
break;
167166

168167
case MIDI_PARSER_INIT:
169-
printf("MIDI_PARSER_INIT");
168+
printf("MIDI_PARSER_INIT\n");
170169
break;
171170

172171
case MIDI_PARSER_HEADER:
173-
printf("header\n");
172+
printf("\nheader\n");
174173
printf(" size: %d\n", parser_state.header.size);
175174
printf(" format: %d [%s]\n", parser_state.header.format,
176175
midi_file_format_name(parser_state.header.format));
@@ -179,12 +178,12 @@ class MidiFile {
179178
break;
180179

181180
case MIDI_PARSER_TRACK:
182-
printf("track\n");
181+
printf("\ntrack\n");
183182
printf(" length: %d\n", parser_state.track.size);
184183
break;
185184

186185
case MIDI_PARSER_TRACK_MIDI:
187-
printf("track-midi\n");
186+
printf("\ntrack-midi\n");
188187
printf(" time: %ld\n", (long)parser_state.vtime);
189188
printf(" status: %d [%s]\n", parser_state.midi.status,
190189
midi_status_name(parser_state.midi.status));
@@ -194,27 +193,28 @@ class MidiFile {
194193
break;
195194

196195
case MIDI_PARSER_TRACK_META:
197-
printf("track-meta\n");
196+
printf("\ntrack-meta\n");
198197
printf(" time: %ld\n", (long)parser_state.vtime);
199198
printf(" type: %d [%s]\n", parser_state.meta.type,
200199
midi_meta_name(parser_state.meta.type));
201200
printf(" length: %d\n", parser_state.meta.length);
202201
break;
203202

204203
case MIDI_PARSER_TRACK_SYSEX:
205-
printf("track-sysex\n");
204+
printf("\ntrack-sysex\n");
206205
printf(" time: %ld\n", (long)parser_state.vtime);
207206
break;
208207

209208
default:
210-
printf("unhandled state: %d\n", status);
209+
printf("\nunhandled state: %d\n", status);
211210
break;
212211
}
213212
}
214213

215214
void reset() {
216215
parser_state.in.reset();
217216
parser_state.status = MIDI_PARSER_INIT;
217+
parser_state.status_internal = MIDI_PARSER_INIT;
218218
}
219219

220220
int midi_event_datalen(int status) {
@@ -251,7 +251,7 @@ class MidiFile {
251251
if (parser_state.in.available() < 14)
252252
return MIDI_PARSER_EOB;
253253

254-
if (parser_state.in.equals("MThd"))
254+
if (!parser_state.in.equals("MThd"))
255255
return MIDI_PARSER_ERROR;
256256

257257
parser_state.header.size = midi_parse_be32(parser_state.in.peekStr(4, 4));
@@ -262,7 +262,7 @@ class MidiFile {
262262
midi_parse_be16(parser_state.in.peekStr(12, 1));
263263

264264
parser_state.in.consume(14);
265-
parser_state.status = MIDI_PARSER_HEADER;
265+
parser_state.status_internal = MIDI_PARSER_HEADER;
266266
return MIDI_PARSER_HEADER;
267267
}
268268

@@ -271,7 +271,7 @@ class MidiFile {
271271
return MIDI_PARSER_EOB;
272272

273273
parser_state.track.size = midi_parse_be32(parser_state.in.peekStr(0, 4));
274-
parser_state.status = MIDI_PARSER_TRACK;
274+
parser_state.status_internal = MIDI_PARSER_TRACK;
275275
parser_state.in.consume(8);
276276
// parser.in.available() -= 8;
277277
parser_state.buffered_status = MIDI_STATUS_NA;
@@ -305,6 +305,10 @@ class MidiFile {
305305
cont = b & 0x80;
306306
}
307307

308+
if (parser_state.in.available()<nbytes){
309+
return false;
310+
}
311+
308312
parser_state.in.consume(nbytes);
309313
// parser.in.available() -= nbytes;
310314
parser_state.track.size -= nbytes;
@@ -441,7 +445,7 @@ class MidiFile {
441445
if (parser_state.in.isEmpty())
442446
return MIDI_PARSER_EOB;
443447

444-
switch (parser_state.status) {
448+
switch (parser_state.status_internal) {
445449
case MIDI_PARSER_INIT:
446450
return midi_parse_header();
447451

@@ -451,7 +455,7 @@ class MidiFile {
451455
case MIDI_PARSER_TRACK:
452456
if (parser_state.track.size == 0) {
453457
// we reached the end of the track
454-
parser_state.status = MIDI_PARSER_HEADER;
458+
parser_state.status_internal = MIDI_PARSER_HEADER;
455459
return midi_parse();
456460
}
457461
return midi_parse_event();

src/MidiFileCommon.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include "RingBuffer.h"
33

44
enum midi_parser_status {
5-
MIDI_PARSER_MORE_DATA = -3,
65
MIDI_PARSER_EOB = -2,
76
MIDI_PARSER_ERROR = -1,
87
MIDI_PARSER_INIT = 0,
@@ -80,8 +79,8 @@ struct midi_sysex_event {
8079
};
8180

8281
struct midi_parser_state {
83-
enum midi_parser_status status_result;
8482
enum midi_parser_status status;
83+
enum midi_parser_status status_internal;
8584
enum midi_status buffered_status;
8685
unsigned buffered_channel;
8786

src/RingBuffer.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class RingBuffer {
1515
public:
1616
RingBuffer() = default;
1717

18+
RingBuffer(RingBuffer &t){
19+
_aucBuffer = nullptr;
20+
reset();
21+
}
22+
1823
RingBuffer(int size) {
1924
resize(size);
2025
reset();
@@ -135,7 +140,7 @@ class RingBuffer {
135140

136141
/// @brief Removes the next n characters from the ringbuffer
137142
void consume(int offset) {
138-
assert(available() > offset);
143+
assert(available() >= offset);
139144
for (int j = 0; j < offset; j++) {
140145
read();
141146
}

0 commit comments

Comments
 (0)