|
| 1 | +#ifndef ESSENTIA_AUDIO2MIDI_H |
| 2 | +#define ESSENTIA_AUDIO2MIDI_H |
| 3 | + |
| 4 | +#include "algorithmfactory.h" |
| 5 | + |
| 6 | +namespace essentia { |
| 7 | +namespace standard { |
| 8 | + |
| 9 | + class Audio2Midi : public Algorithm { |
| 10 | + protected: |
| 11 | + Input<std::vector<Real>> _frame; |
| 12 | + Output<Real> _pitch; |
| 13 | + Output<Real> _loudness; |
| 14 | + Output<std::vector<std::string> > _messageType; |
| 15 | + Output<std::vector<Real> > _midiNoteNumber; |
| 16 | + Output<std::vector<Real> > _timeCompensation; |
| 17 | + |
| 18 | + Algorithm* _lowpass; |
| 19 | + Algorithm* _framebuffer; |
| 20 | + Algorithm* _audio2pitch; |
| 21 | + Algorithm* _pitch2midi; |
| 22 | + |
| 23 | + Real _sampleRate; |
| 24 | + int _frameSize; |
| 25 | + int _hopSize; |
| 26 | + std::string _pitchAlgorithm = "pitchyinfft"; |
| 27 | + std::string _loudnessAlgorithm = "rms"; |
| 28 | + Real _minFrequency; |
| 29 | + Real _maxFrequency; |
| 30 | + int _tuningFrequency; |
| 31 | + Real _pitchConfidenceThreshold, _loudnessThreshold, _minOccurrenceRate; |
| 32 | + Real _midiBufferDuration; |
| 33 | + Real _minNoteChangePeriod; |
| 34 | + Real _minOnsetCheckPeriod; |
| 35 | + Real _minOffsetCheckPeriod; |
| 36 | + |
| 37 | + bool _applyTimeCompensation; |
| 38 | + int _transposition; |
| 39 | + |
| 40 | + // Containers |
| 41 | + std::vector<Real> lpFrame, analysisFrame; |
| 42 | + Real pitch, pitchConfidence, loudness; |
| 43 | + std::vector<Real> midiNoteNumber, timeCompensation; |
| 44 | + std::vector<std::string> messageType; |
| 45 | + Real onsetTimeCompensation, offsetTimeCompensation; |
| 46 | + |
| 47 | + int voiced; |
| 48 | + |
| 49 | + public: |
| 50 | + Audio2Midi() { |
| 51 | + declareInput(_frame, "frame", "the input frame to analyse"); |
| 52 | + declareOutput(_pitch, "pitch", "pitch given in Hz"); |
| 53 | + declareOutput(_loudness, "loudness", "detected loudness in decibels"); |
| 54 | + declareOutput(_messageType, "messageType", "the output of MIDI message type, as string, {noteoff, noteon, noteoff-noteon}"); |
| 55 | + declareOutput(_midiNoteNumber, "midiNoteNumber", "the output of detected MIDI note number, as integer, in range [0,127]"); |
| 56 | + declareOutput(_timeCompensation, "timeCompensation", "time to be compensated in the messages"); |
| 57 | + |
| 58 | + _lowpass = AlgorithmFactory::create("LowPass"); |
| 59 | + _framebuffer = AlgorithmFactory::create("FrameBuffer"); |
| 60 | + _audio2pitch = AlgorithmFactory::create("Audio2Pitch"); |
| 61 | + _pitch2midi = AlgorithmFactory::create("Pitch2Midi"); |
| 62 | + } |
| 63 | + |
| 64 | + ~Audio2Midi() { |
| 65 | + delete _lowpass; |
| 66 | + delete _framebuffer; |
| 67 | + delete _audio2pitch; |
| 68 | + delete _pitch2midi; |
| 69 | + } |
| 70 | + |
| 71 | + void declareParameters() { |
| 72 | + declareParameter("sampleRate", "sample rate of incoming audio frames", "[8000,inf)", 44100); |
| 73 | + declareParameter("hopSize", "equivalent to I/O buffer size", "[1,inf)", 32); |
| 74 | + declareParameter("minFrequency", "minimum frequency to detect in Hz", "[10,20000]", 60.0); |
| 75 | + declareParameter("maxFrequency", "maximum frequency to detect in Hz", "[10,20000]", 2300.0); |
| 76 | + declareParameter("tuningFrequency", "tuning frequency for semitone index calculation, corresponding to A3 [Hz]", "{432,440}", 440); |
| 77 | + declareParameter("pitchConfidenceThreshold", "level of pitch confidence above which note ON/OFF start to be considered", "[0,1]", 0.25); |
| 78 | + declareParameter("loudnessThreshold", "loudness level above/below which note ON/OFF start to be considered, in decibels", "[-inf,0]", -51.0); |
| 79 | + declareParameter("transpositionAmount", "Apply transposition (in semitones) to the detected MIDI notes.", "(-69,50)", 0); |
| 80 | + declareParameter("minOccurrenceRate", "rate of predominant pitch occurrence in MidiPool buffer to consider note ON event", "[0,1]", 0.5); |
| 81 | + declareParameter("midiBufferDuration", "duration in seconds of buffer used for voting in MidiPool algorithm", "[0.005,0.5]", 0.05); // 15ms |
| 82 | + declareParameter("minNoteChangePeriod", "minimum time to wait until a note change is detected (testing only)", "(0,1]", 0.030); |
| 83 | + declareParameter("minOnsetCheckPeriod", "minimum time to wait until an onset is detected (testing only)", "(0,1]", 0.075); |
| 84 | + declareParameter("minOffsetCheckPeriod", "minimum time to wait until an offset is detected (testing only)", "(0,1]", 0.2); |
| 85 | + declareParameter("applyTimeCompensation", "whether to apply time compensation correction to MIDI note detection", "{true,false}", true); |
| 86 | + } |
| 87 | + |
| 88 | + void configure(); |
| 89 | + void compute(); |
| 90 | + |
| 91 | + static const char* name; |
| 92 | + static const char* category; |
| 93 | + static const char* description; |
| 94 | + }; |
| 95 | + |
| 96 | + |
| 97 | +} // namespace standard |
| 98 | +} // namespace essentia |
| 99 | + |
| 100 | +#endif |
0 commit comments