BaseMachine Sequencer V2

Dependencies:   AT24C1024 Amp AverageAnalogIn BaseMachineUIController Envelope ExioBufferdController FilterController MCP23S17 OscController PinDetect RotaryEncoder Sequence SequencerDisplay mbed-rtos mbed st7567LCD

Fork of BaseMachine by Ryo Od

Committer:
ryood
Date:
Mon May 22 23:00:08 2017 +0000
Branch:
FilterEnvelope
Revision:
29:5ca5aef9e7ca
Parent:
27:173ad2312d03
Impl. FilterEnvelope

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ryood 0:1afb83a21a25 1 /*
ryood 0:1afb83a21a25 2 * main.cpp
ryood 0:1afb83a21a25 3 * BaseMachine Sequencer
ryood 0:1afb83a21a25 4 *
ryood 20:bc7bd7faa8d5 5 * mbed Rev 135 / mbed-rtos Rev 123
ryood 0:1afb83a21a25 6 *
ryood 20:bc7bd7faa8d5 7 * 2017.02.14 mbed Rev 135 / mbed-rtos Rev 123に変更
ryood 0:1afb83a21a25 8 * 2016.11.07 UIを統合
ryood 0:1afb83a21a25 9 *
ryood 0:1afb83a21a25 10 */
ryood 0:1afb83a21a25 11
ryood 0:1afb83a21a25 12 #include "mbed.h"
ryood 0:1afb83a21a25 13 #include "rtos.h"
ryood 0:1afb83a21a25 14
ryood 0:1afb83a21a25 15 #define UART_TRACE (0)
ryood 0:1afb83a21a25 16 #include "BaseMachineCommon.h"
ryood 0:1afb83a21a25 17 #include "BaseMachineUIController.h"
ryood 21:0bbca9e3ba30 18 #include "SequenceSender.h"
ryood 0:1afb83a21a25 19 #include "EnvelopeGenerator.h"
ryood 21:0bbca9e3ba30 20 #include "SpiOscController.h"
ryood 0:1afb83a21a25 21 #include "SpiAmpController.h"
ryood 0:1afb83a21a25 22 #include "SpiFilterController.h"
ryood 26:fa2bd913530b 23 #include "SpiVcvsLpfController.h"
ryood 26:fa2bd913530b 24 #include "SpiSvfController.h"
ryood 0:1afb83a21a25 25
ryood 0:1afb83a21a25 26 #define SPI1_RATE (1000000)
ryood 10:0b7f4eb37197 27 #define I2C1_RATE (100000)
ryood 0:1afb83a21a25 28
ryood 0:1afb83a21a25 29 const int samplingPeriod = 1; // ms
ryood 0:1afb83a21a25 30 const int bpm = 120;
ryood 0:1afb83a21a25 31 const int envelopeLength = (60 * 1000 / (bpm * 4)) / samplingPeriod;
ryood 0:1afb83a21a25 32
ryood 0:1afb83a21a25 33 // Devices
ryood 0:1afb83a21a25 34 //
ryood 0:1afb83a21a25 35 BaseMachineUIController UIController;
ryood 0:1afb83a21a25 36
ryood 0:1afb83a21a25 37 //SPI1 (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC)
ryood 0:1afb83a21a25 38 SPI SpiMaster(PA_7, PA_6, PA_5);
ryood 17:bc1f0fea5bc9 39 DigitalOut SpiMasterReset(PB_10, 0);
ryood 0:1afb83a21a25 40
ryood 0:1afb83a21a25 41 Sequence sequences[SEQUENCE_N];
ryood 21:0bbca9e3ba30 42 SequenceSender SequenceSender(sequences, SEQUENCE_N, samplingPeriod, bpm);
ryood 0:1afb83a21a25 43
ryood 0:1afb83a21a25 44 Envelope envelope(4095, envelopeLength, envelopeLength*3/4, envelopeLength/2, 2047);
ryood 0:1afb83a21a25 45 EnvelopeGenerator EnvelopeGenerator;
ryood 21:0bbca9e3ba30 46
ryood 21:0bbca9e3ba30 47 SpiOscController OscController(&SpiMaster, D9);
ryood 0:1afb83a21a25 48 SpiAmpController AmpController(&SpiMaster, D8, D7);
ryood 0:1afb83a21a25 49
ryood 27:173ad2312d03 50 #if USE_VCVS
ryood 27:173ad2312d03 51 SpiVcvsLpfController FilterController(&SpiMaster, D10);
ryood 29:5ca5aef9e7ca 52 #elif USE_SVF
ryood 27:173ad2312d03 53 SpiSvfController FilterController(&SpiMaster, D10);
ryood 27:173ad2312d03 54 #endif
ryood 0:1afb83a21a25 55
ryood 23:099d8f1b0914 56 InterruptIn SyncIn(PC_4);
ryood 23:099d8f1b0914 57 //DigitalOut led(D13);
ryood 23:099d8f1b0914 58
ryood 0:1afb83a21a25 59 // Grobal Variables
ryood 0:1afb83a21a25 60 //
ryood 0:1afb83a21a25 61 int playingStep = 0;
ryood 0:1afb83a21a25 62 int editingStep = 0;
ryood 0:1afb83a21a25 63 bool isRunning = false;
ryood 0:1afb83a21a25 64
ryood 0:1afb83a21a25 65 //------------------------------------------------------------------------
ryood 0:1afb83a21a25 66 // Callback functions
ryood 0:1afb83a21a25 67 //------------------------------------------------------------------------
ryood 0:1afb83a21a25 68 void updateTicks(int ticks)
ryood 0:1afb83a21a25 69 {
ryood 29:5ca5aef9e7ca 70 uint16_t level = EnvelopeGenerator.update(sequences[SequenceSender.getStep()].isTie());
ryood 29:5ca5aef9e7ca 71
ryood 29:5ca5aef9e7ca 72 // Amp
ryood 29:5ca5aef9e7ca 73 uint16_t ampLevel = level;
ryood 6:fd15586f72ff 74 if (sequences[SequenceSender.getStep()].isNoteOn())
ryood 5:e909232c913e 75 {
ryood 29:5ca5aef9e7ca 76 ampLevel = ampLevel >> 1;
ryood 8:4e38bb44c72e 77 if (sequences[SequenceSender.getStep()].isAccent())
ryood 5:e909232c913e 78 {
ryood 29:5ca5aef9e7ca 79 ampLevel = ampLevel * (1.0f + ((float)UIController.getAccentLevel()) / 128.0f);
ryood 5:e909232c913e 80 }
ryood 5:e909232c913e 81 }
ryood 5:e909232c913e 82 else
ryood 5:e909232c913e 83 {
ryood 29:5ca5aef9e7ca 84 ampLevel = 0;
ryood 5:e909232c913e 85 }
ryood 29:5ca5aef9e7ca 86 AmpController.outDca(ampLevel);
ryood 5:e909232c913e 87
ryood 29:5ca5aef9e7ca 88 // Oscillator
ryood 21:0bbca9e3ba30 89 OscController.setFrequency10(SequenceSender.getFrequency10());
ryood 21:0bbca9e3ba30 90 OscController.outDco();
ryood 21:0bbca9e3ba30 91
ryood 29:5ca5aef9e7ca 92 // Filter Env Mod
ryood 29:5ca5aef9e7ca 93 uint16_t cutoff16 = FilterController.getCutoff();
ryood 29:5ca5aef9e7ca 94 uint8_t resonance = FilterController.getResonance();
ryood 29:5ca5aef9e7ca 95 uint8_t mod = FilterController.getModLevel();
ryood 29:5ca5aef9e7ca 96 float modCoeff = (float)mod / (float)(cutoff16 << 1);
ryood 29:5ca5aef9e7ca 97 cutoff16 = cutoff16 * (1.0f + ((float)level / 0x8000) * modCoeff);
ryood 29:5ca5aef9e7ca 98 if (cutoff16 > 255) {
ryood 29:5ca5aef9e7ca 99 cutoff16 = 255;
ryood 29:5ca5aef9e7ca 100 }
ryood 29:5ca5aef9e7ca 101 FilterController.outDcf(cutoff16, resonance);
ryood 5:e909232c913e 102
ryood 0:1afb83a21a25 103 if (ticks == 0)
ryood 0:1afb83a21a25 104 {
ryood 0:1afb83a21a25 105 EnvelopeGenerator.init(envelope);
ryood 0:1afb83a21a25 106 playingStep = SequenceSender.getStep();
ryood 0:1afb83a21a25 107 UIController.setPlayingStep(playingStep);
ryood 0:1afb83a21a25 108 }
ryood 0:1afb83a21a25 109 }
ryood 0:1afb83a21a25 110
ryood 0:1afb83a21a25 111 //------------------------------------------------------------------------
ryood 0:1afb83a21a25 112 // Functions
ryood 0:1afb83a21a25 113 //------------------------------------------------------------------------
ryood 1:87f7d2e0a123 114 void setParams()
ryood 1:87f7d2e0a123 115 {
ryood 1:87f7d2e0a123 116 UIController.getSequences(&sequences);
ryood 0:1afb83a21a25 117
ryood 1:87f7d2e0a123 118 SequenceSender.setBpm(UIController.getBpm());
ryood 0:1afb83a21a25 119
ryood 1:87f7d2e0a123 120 OscillatorParam osc;
ryood 1:87f7d2e0a123 121 UIController.getOscillatorParam(&osc);
ryood 21:0bbca9e3ba30 122 OscController.setWaveShape(osc.waveShape);
ryood 21:0bbca9e3ba30 123 OscController.setPulseWidth(osc.pulseWidth << 1);
ryood 1:87f7d2e0a123 124
ryood 1:87f7d2e0a123 125 FilterParam flt;
ryood 1:87f7d2e0a123 126 UIController.getFilterParam(&flt);
ryood 1:87f7d2e0a123 127 FilterController.setCutoff(flt.cutoff << 1);
ryood 1:87f7d2e0a123 128 FilterController.setResonance(flt.resonance << 1);
ryood 1:87f7d2e0a123 129
ryood 1:87f7d2e0a123 130 EnvelopeParam env;
ryood 1:87f7d2e0a123 131 UIController.getEnvelopeParam(&env);
ryood 29:5ca5aef9e7ca 132 //envelope.setLevel(env.level << 5);
ryood 29:5ca5aef9e7ca 133 FilterController.setModLevel(env.level << 1);
ryood 1:87f7d2e0a123 134 //ToDo: Impl. envelope length
ryood 1:87f7d2e0a123 135 envelope.setLength(envelopeLength);
ryood 1:87f7d2e0a123 136 envelope.setDuration((float)env.duration / 128.0f * envelopeLength);
ryood 1:87f7d2e0a123 137 envelope.setDecay((float)env.decay / 128.0f * envelopeLength);
ryood 1:87f7d2e0a123 138 envelope.setSustain(env.sustain << 5);
ryood 1:87f7d2e0a123 139
ryood 1:87f7d2e0a123 140 bool _isRunning = UIController.getIsRunning();
ryood 5:e909232c913e 141 if (_isRunning != isRunning)
ryood 5:e909232c913e 142 {
ryood 1:87f7d2e0a123 143 isRunning = _isRunning;
ryood 5:e909232c913e 144 if (isRunning)
ryood 5:e909232c913e 145 {
ryood 23:099d8f1b0914 146 playingStep = 0;
ryood 1:87f7d2e0a123 147 SequenceSender.run(playingStep);
ryood 5:e909232c913e 148 }
ryood 5:e909232c913e 149 else
ryood 5:e909232c913e 150 {
ryood 1:87f7d2e0a123 151 AmpController.outDca(0);
ryood 1:87f7d2e0a123 152 SequenceSender.stop();
ryood 1:87f7d2e0a123 153 }
ryood 1:87f7d2e0a123 154 }
ryood 1:87f7d2e0a123 155 }
ryood 0:1afb83a21a25 156
ryood 23:099d8f1b0914 157 void syncFunction()
ryood 23:099d8f1b0914 158 {
ryood 23:099d8f1b0914 159 //led = !led;
ryood 23:099d8f1b0914 160 SequenceSender.sync();
ryood 23:099d8f1b0914 161 }
ryood 23:099d8f1b0914 162
ryood 0:1afb83a21a25 163 //------------------------------------------------------------------------
ryood 0:1afb83a21a25 164 // Main routine
ryood 0:1afb83a21a25 165 //------------------------------------------------------------------------
ryood 0:1afb83a21a25 166 int main()
ryood 0:1afb83a21a25 167 {
ryood 0:1afb83a21a25 168 #if (UART_TRACE)
ryood 0:1afb83a21a25 169 printf("*** BaseMachine Sequencer ***\r\n");
ryood 0:1afb83a21a25 170 #endif
ryood 0:1afb83a21a25 171
ryood 0:1afb83a21a25 172 //--------------------------------------------------------------------
ryood 7:a47420a0c4bf 173 // Initialize objects
ryood 7:a47420a0c4bf 174 //
ryood 7:a47420a0c4bf 175 Sequence::setBaseNoteNumber(baseNoteNumber);
ryood 7:a47420a0c4bf 176
ryood 7:a47420a0c4bf 177 //--------------------------------------------------------------------
ryood 0:1afb83a21a25 178 // Setup Devices
ryood 0:1afb83a21a25 179 //
ryood 17:bc1f0fea5bc9 180 SpiMasterReset = 1;
ryood 0:1afb83a21a25 181 SpiMaster.format(8, 0);
ryood 0:1afb83a21a25 182 SpiMaster.frequency(SPI1_RATE);
ryood 0:1afb83a21a25 183
ryood 0:1afb83a21a25 184 // Mute output
ryood 0:1afb83a21a25 185 AmpController.outDca(0);
ryood 0:1afb83a21a25 186
ryood 0:1afb83a21a25 187 UIController.init();
ryood 7:a47420a0c4bf 188 setParams();
ryood 0:1afb83a21a25 189
ryood 0:1afb83a21a25 190 EnvelopeGenerator.init(envelope);
ryood 23:099d8f1b0914 191
ryood 23:099d8f1b0914 192 // Sync Interrupt
ryood 23:099d8f1b0914 193 SyncIn.rise(&syncFunction);
ryood 0:1afb83a21a25 194
ryood 0:1afb83a21a25 195 SequenceSender.attachUpdate(&updateTicks);
ryood 17:bc1f0fea5bc9 196
ryood 0:1afb83a21a25 197 //--------------------------------------------------------------------
ryood 0:1afb83a21a25 198 // Main loop
ryood 0:1afb83a21a25 199 //
ryood 0:1afb83a21a25 200 for (;;)
ryood 0:1afb83a21a25 201 {
ryood 0:1afb83a21a25 202 UIController.update();
ryood 1:87f7d2e0a123 203 setParams();
ryood 2:6056b9559541 204
ryood 2:6056b9559541 205 Thread::wait(10);
ryood 0:1afb83a21a25 206 }
ryood 0:1afb83a21a25 207 }