BaseMachine Sequencer
Dependencies: Amp AverageAnalogIn BaseMachineUIController Envelope ExioBufferdController FilterController MCP23S17 PinDetect RotaryEncoder Sequence SequencerDisplay mbed-rtos mbed st7567LCD AT24C1024 OscController
main.cpp@5:e909232c913e, 2016-11-17 (annotated)
- Committer:
- ryood
- Date:
- Thu Nov 17 07:22:56 2016 +0000
- Revision:
- 5:e909232c913e
- Parent:
- 2:6056b9559541
- Child:
- 6:fd15586f72ff
Remove glitches while rests
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryood | 0:1afb83a21a25 | 1 | /* |
ryood | 0:1afb83a21a25 | 2 | * main.cpp |
ryood | 0:1afb83a21a25 | 3 | * BaseMachine Sequencer |
ryood | 0:1afb83a21a25 | 4 | * |
ryood | 0:1afb83a21a25 | 5 | * mbed Rev 121 / mbed-rtos Rev 117 |
ryood | 0:1afb83a21a25 | 6 | * |
ryood | 0:1afb83a21a25 | 7 | * 2016.11.07 UIを統合 |
ryood | 0:1afb83a21a25 | 8 | * |
ryood | 0:1afb83a21a25 | 9 | */ |
ryood | 0:1afb83a21a25 | 10 | |
ryood | 0:1afb83a21a25 | 11 | #include "mbed.h" |
ryood | 0:1afb83a21a25 | 12 | #include "rtos.h" |
ryood | 0:1afb83a21a25 | 13 | |
ryood | 0:1afb83a21a25 | 14 | #define UART_TRACE (0) |
ryood | 0:1afb83a21a25 | 15 | #include "BaseMachineCommon.h" |
ryood | 0:1afb83a21a25 | 16 | #include "BaseMachineUIController.h" |
ryood | 0:1afb83a21a25 | 17 | #include "SpiSequenceSender.h" |
ryood | 0:1afb83a21a25 | 18 | #include "EnvelopeGenerator.h" |
ryood | 0:1afb83a21a25 | 19 | #include "SpiAmpController.h" |
ryood | 0:1afb83a21a25 | 20 | #include "SpiFilterController.h" |
ryood | 0:1afb83a21a25 | 21 | |
ryood | 0:1afb83a21a25 | 22 | #define SPI1_RATE (1000000) |
ryood | 0:1afb83a21a25 | 23 | |
ryood | 0:1afb83a21a25 | 24 | const int samplingPeriod = 1; // ms |
ryood | 0:1afb83a21a25 | 25 | const int bpm = 120; |
ryood | 0:1afb83a21a25 | 26 | const int envelopeLength = (60 * 1000 / (bpm * 4)) / samplingPeriod; |
ryood | 0:1afb83a21a25 | 27 | const int waveShape = SpiSequenceSender::WAVESHAPE_SQUARE; |
ryood | 0:1afb83a21a25 | 28 | |
ryood | 0:1afb83a21a25 | 29 | // Devices |
ryood | 0:1afb83a21a25 | 30 | // |
ryood | 0:1afb83a21a25 | 31 | BaseMachineUIController UIController; |
ryood | 0:1afb83a21a25 | 32 | |
ryood | 0:1afb83a21a25 | 33 | //SPI1 (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC) |
ryood | 0:1afb83a21a25 | 34 | SPI SpiMaster(PA_7, PA_6, PA_5); |
ryood | 0:1afb83a21a25 | 35 | |
ryood | 0:1afb83a21a25 | 36 | Sequence sequences[SEQUENCE_N]; |
ryood | 0:1afb83a21a25 | 37 | SpiSequenceSender SequenceSender(&SpiMaster, D9, sequences, SEQUENCE_N, samplingPeriod, bpm); |
ryood | 0:1afb83a21a25 | 38 | |
ryood | 0:1afb83a21a25 | 39 | Envelope envelope(4095, envelopeLength, envelopeLength*3/4, envelopeLength/2, 2047); |
ryood | 0:1afb83a21a25 | 40 | EnvelopeGenerator EnvelopeGenerator; |
ryood | 0:1afb83a21a25 | 41 | SpiAmpController AmpController(&SpiMaster, D8, D7); |
ryood | 0:1afb83a21a25 | 42 | |
ryood | 0:1afb83a21a25 | 43 | SpiFilterController FilterController(&SpiMaster, D10); |
ryood | 0:1afb83a21a25 | 44 | |
ryood | 0:1afb83a21a25 | 45 | // Grobal Variables |
ryood | 0:1afb83a21a25 | 46 | // |
ryood | 0:1afb83a21a25 | 47 | int playingStep = 0; |
ryood | 0:1afb83a21a25 | 48 | int editingStep = 0; |
ryood | 0:1afb83a21a25 | 49 | bool isRunning = false; |
ryood | 0:1afb83a21a25 | 50 | |
ryood | 0:1afb83a21a25 | 51 | //------------------------------------------------------------------------ |
ryood | 0:1afb83a21a25 | 52 | // Callback functions |
ryood | 0:1afb83a21a25 | 53 | //------------------------------------------------------------------------ |
ryood | 0:1afb83a21a25 | 54 | void updateTicks(int ticks) |
ryood | 0:1afb83a21a25 | 55 | { |
ryood | 5:e909232c913e | 56 | if (sequences[playingStep].isNoteOn()) |
ryood | 5:e909232c913e | 57 | { |
ryood | 5:e909232c913e | 58 | uint16_t level = EnvelopeGenerator.update(); |
ryood | 5:e909232c913e | 59 | if (!sequences[SequenceSender.getStep()].isAccent()) |
ryood | 5:e909232c913e | 60 | { |
ryood | 5:e909232c913e | 61 | level = level * 1 / 2; |
ryood | 5:e909232c913e | 62 | } |
ryood | 5:e909232c913e | 63 | AmpController.outDca(level); |
ryood | 5:e909232c913e | 64 | //printf("%d %d %d\r\n", playingStep, ticks, level); |
ryood | 5:e909232c913e | 65 | } |
ryood | 5:e909232c913e | 66 | else |
ryood | 5:e909232c913e | 67 | { |
ryood | 5:e909232c913e | 68 | AmpController.outDca(0); |
ryood | 5:e909232c913e | 69 | } |
ryood | 5:e909232c913e | 70 | |
ryood | 5:e909232c913e | 71 | FilterController.outDcf(); |
ryood | 5:e909232c913e | 72 | |
ryood | 0:1afb83a21a25 | 73 | if (ticks == 0) |
ryood | 0:1afb83a21a25 | 74 | { |
ryood | 0:1afb83a21a25 | 75 | EnvelopeGenerator.init(envelope); |
ryood | 0:1afb83a21a25 | 76 | playingStep = SequenceSender.getStep(); |
ryood | 0:1afb83a21a25 | 77 | UIController.setPlayingStep(playingStep); |
ryood | 0:1afb83a21a25 | 78 | } |
ryood | 0:1afb83a21a25 | 79 | } |
ryood | 0:1afb83a21a25 | 80 | |
ryood | 0:1afb83a21a25 | 81 | //------------------------------------------------------------------------ |
ryood | 0:1afb83a21a25 | 82 | // Functions |
ryood | 0:1afb83a21a25 | 83 | //------------------------------------------------------------------------ |
ryood | 1:87f7d2e0a123 | 84 | void setParams() |
ryood | 1:87f7d2e0a123 | 85 | { |
ryood | 1:87f7d2e0a123 | 86 | UIController.getSequences(&sequences); |
ryood | 0:1afb83a21a25 | 87 | |
ryood | 1:87f7d2e0a123 | 88 | SequenceSender.setBpm(UIController.getBpm()); |
ryood | 0:1afb83a21a25 | 89 | |
ryood | 1:87f7d2e0a123 | 90 | // ToDo: Impl. accentLevel |
ryood | 1:87f7d2e0a123 | 91 | |
ryood | 1:87f7d2e0a123 | 92 | OscillatorParam osc; |
ryood | 1:87f7d2e0a123 | 93 | UIController.getOscillatorParam(&osc); |
ryood | 1:87f7d2e0a123 | 94 | SequenceSender.setWaveShape(osc.waveShape); |
ryood | 2:6056b9559541 | 95 | SequenceSender.setPulseWidth(osc.pulseWidth << 1); |
ryood | 1:87f7d2e0a123 | 96 | |
ryood | 1:87f7d2e0a123 | 97 | FilterParam flt; |
ryood | 1:87f7d2e0a123 | 98 | UIController.getFilterParam(&flt); |
ryood | 1:87f7d2e0a123 | 99 | FilterController.setCutoff(flt.cutoff << 1); |
ryood | 1:87f7d2e0a123 | 100 | FilterController.setResonance(flt.resonance << 1); |
ryood | 1:87f7d2e0a123 | 101 | |
ryood | 1:87f7d2e0a123 | 102 | EnvelopeParam env; |
ryood | 1:87f7d2e0a123 | 103 | UIController.getEnvelopeParam(&env); |
ryood | 1:87f7d2e0a123 | 104 | envelope.setLevel(env.level << 5); |
ryood | 1:87f7d2e0a123 | 105 | //ToDo: Impl. envelope length |
ryood | 1:87f7d2e0a123 | 106 | envelope.setLength(envelopeLength); |
ryood | 1:87f7d2e0a123 | 107 | envelope.setDuration((float)env.duration / 128.0f * envelopeLength); |
ryood | 1:87f7d2e0a123 | 108 | envelope.setDecay((float)env.decay / 128.0f * envelopeLength); |
ryood | 1:87f7d2e0a123 | 109 | envelope.setSustain(env.sustain << 5); |
ryood | 1:87f7d2e0a123 | 110 | |
ryood | 1:87f7d2e0a123 | 111 | bool _isRunning = UIController.getIsRunning(); |
ryood | 5:e909232c913e | 112 | if (_isRunning != isRunning) |
ryood | 5:e909232c913e | 113 | { |
ryood | 1:87f7d2e0a123 | 114 | isRunning = _isRunning; |
ryood | 5:e909232c913e | 115 | if (isRunning) |
ryood | 5:e909232c913e | 116 | { |
ryood | 1:87f7d2e0a123 | 117 | SequenceSender.run(playingStep); |
ryood | 5:e909232c913e | 118 | } |
ryood | 5:e909232c913e | 119 | else |
ryood | 5:e909232c913e | 120 | { |
ryood | 1:87f7d2e0a123 | 121 | AmpController.outDca(0); |
ryood | 1:87f7d2e0a123 | 122 | SequenceSender.stop(); |
ryood | 1:87f7d2e0a123 | 123 | } |
ryood | 1:87f7d2e0a123 | 124 | } |
ryood | 1:87f7d2e0a123 | 125 | } |
ryood | 0:1afb83a21a25 | 126 | |
ryood | 0:1afb83a21a25 | 127 | //------------------------------------------------------------------------ |
ryood | 0:1afb83a21a25 | 128 | // Main routine |
ryood | 0:1afb83a21a25 | 129 | //------------------------------------------------------------------------ |
ryood | 0:1afb83a21a25 | 130 | int main() |
ryood | 0:1afb83a21a25 | 131 | { |
ryood | 0:1afb83a21a25 | 132 | #if (UART_TRACE) |
ryood | 0:1afb83a21a25 | 133 | printf("*** BaseMachine Sequencer ***\r\n"); |
ryood | 0:1afb83a21a25 | 134 | #endif |
ryood | 0:1afb83a21a25 | 135 | |
ryood | 0:1afb83a21a25 | 136 | //-------------------------------------------------------------------- |
ryood | 0:1afb83a21a25 | 137 | // Setup Devices |
ryood | 0:1afb83a21a25 | 138 | // |
ryood | 0:1afb83a21a25 | 139 | SpiMaster.format(8, 0); |
ryood | 0:1afb83a21a25 | 140 | SpiMaster.frequency(SPI1_RATE); |
ryood | 0:1afb83a21a25 | 141 | |
ryood | 0:1afb83a21a25 | 142 | // Mute output |
ryood | 0:1afb83a21a25 | 143 | AmpController.outDca(0); |
ryood | 0:1afb83a21a25 | 144 | |
ryood | 0:1afb83a21a25 | 145 | UIController.init(); |
ryood | 0:1afb83a21a25 | 146 | |
ryood | 0:1afb83a21a25 | 147 | //-------------------------------------------------------------------- |
ryood | 0:1afb83a21a25 | 148 | // Initialize objects |
ryood | 0:1afb83a21a25 | 149 | // |
ryood | 0:1afb83a21a25 | 150 | Sequence::setBaseNoteNumber(baseNoteNumber); |
ryood | 0:1afb83a21a25 | 151 | |
ryood | 0:1afb83a21a25 | 152 | for (int i = 0; i < SEQUENCE_N; i++) |
ryood | 0:1afb83a21a25 | 153 | { |
ryood | 0:1afb83a21a25 | 154 | Sequence& seq = SequenceSender.getSequences()[i]; |
ryood | 0:1afb83a21a25 | 155 | seq.setPitch(pitch[i]); |
ryood | 0:1afb83a21a25 | 156 | seq.setNoteOn(noteOn[i]); |
ryood | 0:1afb83a21a25 | 157 | seq.setTie(tie[i]); |
ryood | 0:1afb83a21a25 | 158 | seq.setAccent(accent[i]); |
ryood | 0:1afb83a21a25 | 159 | } |
ryood | 0:1afb83a21a25 | 160 | |
ryood | 0:1afb83a21a25 | 161 | EnvelopeGenerator.init(envelope); |
ryood | 0:1afb83a21a25 | 162 | |
ryood | 0:1afb83a21a25 | 163 | SequenceSender.attachUpdate(&updateTicks); |
ryood | 0:1afb83a21a25 | 164 | SequenceSender.setWaveShape(waveShape); |
ryood | 0:1afb83a21a25 | 165 | |
ryood | 0:1afb83a21a25 | 166 | //-------------------------------------------------------------------- |
ryood | 0:1afb83a21a25 | 167 | // Main loop |
ryood | 0:1afb83a21a25 | 168 | // |
ryood | 0:1afb83a21a25 | 169 | for (;;) |
ryood | 0:1afb83a21a25 | 170 | { |
ryood | 0:1afb83a21a25 | 171 | UIController.update(); |
ryood | 1:87f7d2e0a123 | 172 | setParams(); |
ryood | 2:6056b9559541 | 173 | |
ryood | 2:6056b9559541 | 174 | Thread::wait(10); |
ryood | 0:1afb83a21a25 | 175 | } |
ryood | 0:1afb83a21a25 | 176 | } |