Skip to content

Commit a649f77

Browse files
committed
feat: Add MIDI analyzer from raw byte dump as CSV
1 parent 99fe61c commit a649f77

File tree

8 files changed

+177
-0
lines changed

8 files changed

+177
-0
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
add_subdirectory(mocks)
22
add_subdirectory(unit-tests)
3+
add_subdirectory(analyzer)

test/analyzer/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
include(CMakeToolsHelpers OPTIONAL)
2+
3+
project(analyzer)
4+
5+
include_directories(
6+
"${analyzer_SOURCE_DIR}"
7+
)
8+
9+
add_executable(analyzer
10+
analyzer.cpp
11+
analyzer.h
12+
analyzer_Namespace.h
13+
14+
analyzer_MIDI.cpp
15+
analyzer_MIDI.h
16+
)
17+
18+
target_link_libraries(analyzer
19+
midi
20+
test-mocks
21+
)
22+

test/analyzer/analyzer.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "analyzer.h"
2+
#include "analyzer_MIDI.h"
3+
#include <stdio.h>
4+
#include <iterator>
5+
#include <iostream>
6+
#include <fstream>
7+
#include <sstream>
8+
#include <vector>
9+
#include <string>
10+
11+
USING_NAMESPACE_ANALYZER
12+
13+
int main(int argc, char **argv) {
14+
MIDIAnalyzer analyzer;
15+
analyzer.setup();
16+
17+
std::ifstream csvFile("../bytes.csv");
18+
std::cout << "File open" << std::endl;
19+
std::string line = "";
20+
while (std::getline(csvFile, line))
21+
{
22+
uint8_t n = std::stoi(line, nullptr, 16);
23+
analyzer.process(n);
24+
}
25+
}

test/analyzer/analyzer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#include "analyzer_Namespace.h"
4+
5+
BEGIN_ANALYZER_NAMESPACE
6+
7+
END_ANALYZER_NAMESPACE

test/analyzer/analyzer_MIDI.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include "analyzer_MIDI.h"
2+
#include <iostream>
3+
4+
BEGIN_ANALYZER_NAMESPACE
5+
6+
void handleNoteOn(byte inChannel, byte inPitch, byte inVelocity)
7+
{
8+
std::cout << "NoteOn Ch " << int(inChannel) << " Pitch " << int(inPitch) << " Vel " << int(inVelocity);
9+
if (inPitch > 127) {
10+
std::cout << "--------------- Pitch greater than 127 detected ";
11+
}
12+
if (inVelocity > 127) {
13+
std::cout << "--------------- Velocity greater than 127 detected ";
14+
}
15+
std::cout << std::endl;
16+
}
17+
void handleNoteOff(byte inChannel, byte inPitch, byte inVelocity)
18+
{
19+
std::cout << "NoteOff Ch " << int(inChannel) << " Pitch " << int(inPitch) << " Vel " << int(inVelocity);
20+
if (inPitch > 127) {
21+
std::cout << "--------------- Pitch greater than 127 detected ";
22+
}
23+
if (inVelocity > 127) {
24+
std::cout << "--------------- Velocity greater than 127 detected ";
25+
}
26+
std::cout << std::endl;
27+
}
28+
void handleControlChange(byte inChannel, byte inControl, byte inValue)
29+
{
30+
std::cout << "ControlChange Ch " << int(inChannel) << " Cntrl " << int(inControl) << " Val " << int(inValue);
31+
if (inControl > 127) {
32+
std::cout << "--------------- Control greater than 127 detected ";
33+
}
34+
if (inValue > 127) {
35+
std::cout << "--------------- Value greater than 127 detected ";
36+
}
37+
std::cout << std::endl;
38+
}
39+
void handleProgramChange(byte inChannel, byte inProgram)
40+
{
41+
std::cout << "ProgramChange Ch " << int(inChannel) << " Progm " << int(inProgram);
42+
if (inProgram > 127) {
43+
std::cout << "--------------- Program greater than 127 detected ";
44+
}
45+
std::cout << std::endl;
46+
}
47+
void handleChannelPressure(byte inChannel, byte inPressure)
48+
{
49+
std::cout << "AftertouchChannel Ch " << int(inChannel) << " Press " << int(inPressure);
50+
if (inPressure > 127) {
51+
std::cout << "--------------- Pressure greater than 127 detected ";
52+
}
53+
std::cout << std::endl;
54+
}
55+
56+
MIDIAnalyzer::MIDIAnalyzer()
57+
: mSerialBuffer()
58+
, mTransport(mSerialBuffer)
59+
, mMIDI((Transport&)mTransport)
60+
{
61+
}
62+
63+
void MIDIAnalyzer::setup()
64+
{
65+
mMIDI.begin(MIDI_CHANNEL_OMNI);
66+
mMIDI.turnThruOff();
67+
mMIDI.setHandleNoteOn(handleNoteOn);
68+
mMIDI.setHandleNoteOff(handleNoteOff);
69+
mMIDI.setHandleControlChange(handleControlChange);
70+
mMIDI.setHandleProgramChange(handleProgramChange);
71+
mMIDI.setHandleAfterTouchChannel(handleChannelPressure);
72+
}
73+
74+
void MIDIAnalyzer::process(uint8_t inByte)
75+
{
76+
std::cout << "Processing byte " << std::hex <<int(inByte) << std::dec << std::endl;
77+
mSerialBuffer.mRxBuffer.write(inByte);
78+
mMIDI.read();
79+
}
80+
81+
END_ANALYZER_NAMESPACE

test/analyzer/analyzer_MIDI.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include "analyzer.h"
4+
#include <test/mocks/test-mocks_SerialMock.h>
5+
#include <src/MIDI.h>
6+
7+
BEGIN_ANALYZER_NAMESPACE
8+
9+
using SerialMock = test_mocks::SerialMock<32>;
10+
using Transport = midi::SerialMIDI<SerialMock>;
11+
using MidiInterface = midi::MidiInterface<Transport>;
12+
13+
struct MIDIAnalyzer {
14+
public:
15+
MIDIAnalyzer();
16+
17+
public:
18+
void setup();
19+
void process(uint8_t inByte);
20+
21+
public:
22+
SerialMock mSerialBuffer;
23+
Transport mTransport;
24+
MidiInterface mMIDI;
25+
};
26+
27+
END_ANALYZER_NAMESPACE

test/analyzer/analyzer_Namespace.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#define ANALYZER_NAMESPACE analyzer
4+
#define BEGIN_ANALYZER_NAMESPACE namespace ANALYZER_NAMESPACE {
5+
#define END_ANALYZER_NAMESPACE }
6+
#define BEGIN_UNNAMED_NAMESPACE namespace {
7+
#define END_UNNAMED_NAMESPACE }
8+
9+
#define USING_NAMESPACE_ANALYZER using namespace ANALYZER_NAMESPACE;
10+
11+
BEGIN_ANALYZER_NAMESPACE
12+
13+
END_ANALYZER_NAMESPACE

test/mocks/test-mocks_SerialMock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "test-mocks.h"
44
#include <inttypes.h>
5+
#include <cstring>
56

67
BEGIN_TEST_MOCKS_NAMESPACE
78

0 commit comments

Comments
 (0)