To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / CollidoscopeApp / src / CollidoscopeApp.cpp @ 9:20bb004a36de

History | View | Annotate | Download (14.3 KB)

1 5:75b744078d66 f
/*
2

3
 Copyright (C) 2016  Queen Mary University of London
4
 Author: Fiore Martin
5

6
 This file is part of Collidoscope.
7

8
 Collidoscope is free software: you can redistribute it and/or modify
9
 it under the terms of the GNU General Public License as published by
10
 the Free Software Foundation, either version 3 of the License, or
11
 (at your option) any later version.
12

13
 This program is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 GNU General Public License for more details.
17

18
 You should have received a copy of the GNU General Public License
19
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
*/
21
22 6:4c0e82b725d9 f
23 0:02467299402e f
#include "cinder/app/App.h"
24
#include "cinder/app/RendererGl.h"
25
#include "cinder/gl/gl.h"
26
#include "cinder/Exception.h"
27 6:4c0e82b725d9 f
#include <stdexcept>
28 0:02467299402e f
29
30
#include "Config.h"
31
#include "Wave.h"
32
#include "DrawInfo.h"
33
#include "Log.h"
34
#include "AudioEngine.h"
35
#include "Oscilloscope.h"
36
#include "Messages.h"
37
#include "MIDI.h"
38
39
using namespace ci;
40
using namespace ci::app;
41
42
using namespace std;
43
44
45
class CollidoscopeApp : public App {
46
  public:
47
48 5:75b744078d66 f
    void setup() override;
49 0:02467299402e f
    void setupGraphics();
50
51 6:4c0e82b725d9 f
    /** Receives MIDI command messages from MIDI thread */
52 0:02467299402e f
    void receiveCommands();
53 6:4c0e82b725d9 f
    /** Prints command line usage */
54
    void usage();
55 0:02467299402e f
56 5:75b744078d66 f
    void keyDown( KeyEvent event ) override;
57
    void update() override;
58
    void draw() override;
59 0:02467299402e f
    void resize() override;
60
61 5:75b744078d66 f
    Config mConfig;
62 0:02467299402e f
    collidoscope::MIDI mMIDI;
63
    AudioEngine mAudioEngine;
64 5:75b744078d66 f
65 0:02467299402e f
    array< shared_ptr< Wave >, NUM_WAVES > mWaves;
66
    array< shared_ptr< DrawInfo >, NUM_WAVES > mDrawInfos;
67
    array< shared_ptr< Oscilloscope >, NUM_WAVES > mOscilloscopes;
68
    // buffers to read the wave messages as a new wave gets recorded
69
    array< RecordWaveMsg*, NUM_WAVES> mRecordWaveMessageBuffers;
70
    array< vector< CursorTriggerMsg >, NUM_WAVES > mCursorTriggerMessagesBuffers;
71
72
    double mSecondsPerChunk;
73
74
    ~CollidoscopeApp();
75
76
};
77
78
79
void CollidoscopeApp::setup()
80
{
81
    hideCursor();
82
    /* setup is logged: setup steps and errors */
83
84
    /*try {
85
        mConfig.loadFromFile( "./collidoscope_config.xml" );
86
    }
87
    catch ( const Exception &e ){
88
        logError( string("Exception loading config from file:") + e.what() );
89
    }*/
90
91
    // setup buffers to read messages from audio thread
92
    for ( size_t i = 0; i < NUM_WAVES; i++ ){
93
        mRecordWaveMessageBuffers[i] = new RecordWaveMsg[mConfig.getNumChunks()];
94
        mCursorTriggerMessagesBuffers[i].reserve( mConfig.getCursorTriggerMessageBufSize() );
95
    }
96
97
    mAudioEngine.setup( mConfig );
98
99
    setupGraphics();
100
101
    mSecondsPerChunk = mConfig.getWaveLen() / mConfig.getNumChunks();
102
103
    try {
104
        mMIDI.setup( mConfig );
105
    }
106
    catch ( const collidoscope::MIDIException &e ){
107
        logError( string( "Exception opening MIDI input device: " ) + e.getMessage() );
108
    }
109
110
}
111
112
void CollidoscopeApp::setupGraphics()
113
{
114
    for ( size_t i = 0; i < NUM_WAVES; i++ ){
115
116
        mDrawInfos[i] = make_shared< DrawInfo >( i );
117
        mWaves[i] = make_shared< Wave >(mConfig.getNumChunks(), mConfig.getWaveSelectionColor(i) );
118
        mOscilloscopes[i] = make_shared< Oscilloscope >( mAudioEngine.getAudioOutputBuffer( i ).getNumFrames() / mConfig.getOscilloscopeNumPointsDivider() );
119
120
    }
121
}
122
123
void CollidoscopeApp::keyDown( KeyEvent event )
124
{
125
    char c = event.getChar();
126
127 9:20bb004a36de f
    const size_t waveIdx = 0;
128
129 0:02467299402e f
    switch (c){
130
    case 'r' :
131 9:20bb004a36de f
        mAudioEngine.record( waveIdx );
132 0:02467299402e f
        break;
133
134
    case 'w': {
135 6:4c0e82b725d9 f
136 9:20bb004a36de f
        mWaves[waveIdx]->getSelection().setSize(mWaves[waveIdx]->getSelection().getSize() + 1);
137 0:02467299402e f
138 9:20bb004a36de f
        size_t numSelectionChunks = mWaves[waveIdx]->getSelection().getSize();
139 0:02467299402e f
        // how many samples in one selection ?
140
        size_t selectionSize = numSelectionChunks * (mConfig.getWaveLen() * mAudioEngine.getSampleRate() / mConfig.getNumChunks());
141
142 9:20bb004a36de f
        mAudioEngine.setSelectionSize(waveIdx, selectionSize);
143 0:02467299402e f
    };
144
        break;
145
146
    case 's': {
147
148 9:20bb004a36de f
        mWaves[waveIdx]->getSelection().setSize( mWaves[waveIdx]->getSelection().getSize() - 1 );
149 0:02467299402e f
150 9:20bb004a36de f
        size_t selectionSize = mWaves[waveIdx]->getSelection().getSize() *(mConfig.getWaveLen() * mAudioEngine.getSampleRate() / mConfig.getNumChunks());
151
        mAudioEngine.setSelectionSize( waveIdx, selectionSize );
152 0:02467299402e f
    };
153
        break;
154
155
    case 'd': {
156
157 9:20bb004a36de f
        size_t selectionStart = mWaves[waveIdx]->getSelection().getStart();
158
        mWaves[waveIdx]->getSelection().setStart( selectionStart + 1 );
159 0:02467299402e f
160 9:20bb004a36de f
        selectionStart = mWaves[waveIdx]->getSelection().getStart();
161
        mAudioEngine.setSelectionStart( waveIdx, selectionStart * (mConfig.getWaveLen() * mAudioEngine.getSampleRate() / mConfig.getNumChunks()) );
162 0:02467299402e f
    };
163
164
        break;
165
166
    case 'a': {
167 9:20bb004a36de f
        size_t selectionStart = mWaves[waveIdx]->getSelection().getStart();
168 0:02467299402e f
169
        if ( selectionStart == 0 )
170
            return;
171
172 9:20bb004a36de f
        mWaves[waveIdx]->getSelection().setStart( selectionStart - 1 );
173 0:02467299402e f
174 9:20bb004a36de f
        selectionStart = mWaves[waveIdx]->getSelection().getStart();
175 0:02467299402e f
176 9:20bb004a36de f
        mAudioEngine.setSelectionStart( waveIdx, selectionStart * (mConfig.getWaveLen() * mAudioEngine.getSampleRate() / mConfig.getNumChunks()) );
177 0:02467299402e f
    };
178
        break;
179
180
    case 'f':
181
        setFullScreen( !isFullScreen() );
182
        break;
183
184
    case ' ': {
185
        static bool isOn = false;
186
        isOn = !isOn;
187
        if ( isOn ){
188 9:20bb004a36de f
            mAudioEngine.loopOn( waveIdx );
189 0:02467299402e f
        }
190
        else{
191 9:20bb004a36de f
            mAudioEngine.loopOff( waveIdx );
192 0:02467299402e f
        }
193
    };
194
        break;
195
196
    case '9': {
197 9:20bb004a36de f
        int c = mWaves[waveIdx]->getSelection().getParticleSpread();
198 0:02467299402e f
        if ( c == 1 )
199
            return;
200
        else
201
            c -= 1;
202
203 9:20bb004a36de f
        mAudioEngine.setGrainDurationCoeff( waveIdx, c );
204
        mWaves[waveIdx]->getSelection().setParticleSpread( float( c ) );
205
206 0:02467299402e f
    }; break;
207
208
    case '0': {
209 9:20bb004a36de f
        int c = mWaves[waveIdx]->getSelection().getParticleSpread();
210 0:02467299402e f
        if ( c == 8 )
211
            return;
212
        else
213
            c += 1;
214
215 9:20bb004a36de f
        mAudioEngine.setGrainDurationCoeff( waveIdx, c );
216
        mWaves[waveIdx]->getSelection().setParticleSpread( float( c ) );
217 0:02467299402e f
    }; break;
218
    }
219
220
}
221
222
void CollidoscopeApp::update()
223
{
224
    // check incoming commands
225
    receiveCommands();
226
227
    // check new wave chunks from recorder buffer
228
    for ( size_t i = 0; i < NUM_WAVES; i++ ){
229
        size_t availableRead = mAudioEngine.getRecordWaveAvailable( i );
230
        mAudioEngine.readRecordWave( i, mRecordWaveMessageBuffers[i], availableRead );
231
232
        for ( size_t msgIndex = 0; msgIndex < availableRead; msgIndex++ ){
233
            const RecordWaveMsg & msg = mRecordWaveMessageBuffers[i][msgIndex];
234
235
            if ( msg.cmd == Command::WAVE_CHUNK ){
236
                mWaves[i]->setChunk( msg.index, msg.arg1, msg.arg2 );
237
            }
238
            else if ( msg.cmd == Command::WAVE_START ){
239
                mWaves[i]->reset( true ); // reset only chunks but leave selection
240
            }
241
242
        }
243
    }
244
245
    // check if new cursors have been triggered
246
    for ( size_t i = 0; i < NUM_WAVES; i++ ){
247
248
        mAudioEngine.checkCursorTriggers( i, mCursorTriggerMessagesBuffers[i] );
249
        for ( auto & trigger : mCursorTriggerMessagesBuffers[i] ){
250
            const int nodeID = trigger.synthID;
251
252
            switch ( trigger.cmd ){
253
254
            case Command::TRIGGER_UPDATE: {
255
                mWaves[i]->setCursorPos( nodeID, mWaves[i]->getSelection().getStart(), *mDrawInfos[i] );
256
            };
257
                break;
258
259
            case Command::TRIGGER_END: {
260
                mWaves[i]->removeCursor( nodeID );
261
            };
262
                break;
263
264
            }
265
266
        }
267
        mCursorTriggerMessagesBuffers[i].clear();
268
    }
269
270
    // update cursors
271
    for ( size_t i = 0; i < NUM_WAVES; i++ ){
272
        mWaves[i]->update( mSecondsPerChunk, *mDrawInfos[i] );
273
    }
274
275
    // update oscilloscope
276
277
    for ( size_t i = 0; i < NUM_WAVES; i++ ){
278
        const audio::Buffer &audioOutBuffer = mAudioEngine.getAudioOutputBuffer( i );
279
        // one oscilloscope sample
280
281
        for ( size_t j = 0; j < mOscilloscopes[i]->getNumPoints(); j++ ){
282
            mOscilloscopes[i]->setPoint( j, audioOutBuffer.getData()[j], *mDrawInfos[i] );
283
        }
284
    }
285
286
287
288
}
289
290
void CollidoscopeApp::draw()
291
{
292 5:75b744078d66 f
    gl::clear( Color( 0, 0, 0 ) );
293 0:02467299402e f
294
    for ( int i = 0; i < NUM_WAVES; i++ ){
295
        if ( i == 1 ){
296
            /* for the upper wave flip the x over the center of the screen which is
297
            the composition of rotate on the y-axis and translate by -screenwidth*/
298
            gl::pushModelMatrix();
299
            gl::rotate( float(M_PI), ci::vec3( 0, 1, 0 ) );
300
            gl::translate( float( -getWindowWidth() ), 0.0f );
301
            mOscilloscopes[i]->draw();
302
            mWaves[i]->draw( *mDrawInfos[i] );
303
            gl::popModelMatrix();
304
        }
305
        else{
306
307
            mOscilloscopes[i]->draw();
308
            mWaves[i]->draw( *mDrawInfos[i] );
309
        }
310
    }
311
}
312
313
void CollidoscopeApp::resize()
314
{
315
    App::resize();
316
317
    for ( int i = 0; i < NUM_WAVES; i++ ){
318
        // reset the drawing information with the new windows size and same shrink factor
319
        mDrawInfos[i]->reset( getWindow()->getBounds(), 3.0f / 5.0f );
320
321
        /* reset the oscilloscope points to zero */
322
        for ( int j = 0; j < mOscilloscopes[i]->getNumPoints(); j++ ){
323
            mOscilloscopes[i]->setPoint(j, 0.0f, *mDrawInfos[i] );
324
        }
325
    }
326
}
327
328
329
330
void CollidoscopeApp::receiveCommands()
331
{
332
    // check new midi messages
333
    static std::vector<collidoscope::MIDIMessage> midiMessages;
334
    mMIDI.checkMessages( midiMessages );
335
336
337
    for ( auto &m : midiMessages ){
338
339
        const size_t waveIdx = mConfig.getWaveForMIDIChannel( m.getChannel() );
340
        if ( waveIdx >= NUM_WAVES )
341
            continue;
342
343
        if ( m.getVoice() == collidoscope::MIDIMessage::Voice::eNoteOn ){
344
            int midiNote = m.getData_1();
345
            mAudioEngine.noteOn( waveIdx, midiNote );
346
        }
347
        else if ( m.getVoice() == collidoscope::MIDIMessage::Voice::eNoteOff ){
348
            int midiNote = m.getData_1();
349
            mAudioEngine.noteOff( waveIdx, midiNote );
350
        }
351
        else if ( m.getVoice() == collidoscope::MIDIMessage::Voice::ePitchBend ){
352
            const uint16_t MSB = m.getData_2() << 7;
353
            uint16_t value = m.getData_1(); // LSB
354
355
            value |= MSB;
356
357
358
            // value ranges from 0 to 1050. check boundaries in case sensor gives bad values
359
            if ( value > 149 ){ // FIXME pareametrizer
360
                continue;
361
            }
362
363
            size_t startChunk = value;
364
365
            const size_t selectionSizeBeforeStartUpdate = mWaves[waveIdx]->getSelection().getSize();
366
            mWaves[waveIdx]->getSelection().setStart( startChunk );
367
368
            mAudioEngine.setSelectionStart( waveIdx, startChunk * (mConfig.getWaveLen() * mAudioEngine.getSampleRate() / mConfig.getNumChunks()) );
369
370
            const size_t newSelectionSize = mWaves[waveIdx]->getSelection().getSize();
371
            if ( selectionSizeBeforeStartUpdate != newSelectionSize ){
372
                mAudioEngine.setSelectionSize( waveIdx, newSelectionSize * (mConfig.getWaveLen() * mAudioEngine.getSampleRate() / mConfig.getNumChunks()) );
373
            }
374
375
376
        }
377
        else if ( m.getVoice() == collidoscope::MIDIMessage::Voice::eControlChange ){
378
379
            switch ( m.getData_1() ){ //controller number
380
            case 1: { // selection size
381
                const size_t midiVal = m.getData_2();
382
                size_t numSelectionChunks = ci::lmap<size_t>( midiVal, 0, 127, 1, mConfig.getMaxSelectionNumChunks() );
383
384
                mWaves[waveIdx]->getSelection().setSize( numSelectionChunks );
385
386
                // how many samples in one selection ?
387 5:75b744078d66 f
                size_t selectionSize = mWaves[waveIdx]->getSelection().getSize() * (mConfig.getWaveLen() * mAudioEngine.getSampleRate() / mConfig.getNumChunks());
388 0:02467299402e f
                mAudioEngine.setSelectionSize( waveIdx, selectionSize );
389
390
            };
391
                break;
392
393
            case 4: { // loop on off
394
                unsigned char midiVal = m.getData_2();
395
396
                if ( midiVal > 0 )
397
                    mAudioEngine.loopOn( waveIdx );
398
                else
399
                    mAudioEngine.loopOff( waveIdx );
400
            };
401
                break;
402
403
            case 5: // trigger record
404
                mAudioEngine.record( waveIdx );
405
                break;
406
407
            case 2: { // duration
408
                const double midiVal = m.getData_2(); // 0-127
409
                const double coeff = ci::lmap<double>( midiVal, 0.0, 127, 1.0, mConfig.getMaxGrainDurationCoeff() );
410
                mAudioEngine.setGrainDurationCoeff( waveIdx, coeff );
411
                mWaves[waveIdx]->getSelection().setParticleSpread( float( coeff ) );
412
            };
413
                break;
414
415
            case 7: { // filter
416
                const double midiVal = m.getData_2(); // 0-127
417
                const double minCutoff = mConfig.getMinFilterCutoffFreq();
418
                const double maxCutoff = mConfig.getMaxFilterCutoffFreq();
419
                const double cutoff = pow( maxCutoff / 200., midiVal / 127.0 ) * minCutoff;
420
                mAudioEngine.setFilterCutoff( waveIdx, cutoff );
421
                const float alpha = ci::lmap<double>( midiVal, 0.0f, 127.0f, 0.f, 1.f );
422
                mWaves[waveIdx]->setselectionAlpha( alpha );
423
            };
424
                break;
425
426
427
428
            }
429
        }
430
    }
431
432
    midiMessages.clear();
433
}
434
435
436
437
CollidoscopeApp::~CollidoscopeApp()
438
{
439
    for ( int chan = 0; chan < NUM_WAVES; chan++ ){
440
        /* delete the array for wave messages from audio thread */
441
        delete[] mRecordWaveMessageBuffers[chan];
442
    }
443
}
444
445 6:4c0e82b725d9 f
446 0:02467299402e f
CINDER_APP( CollidoscopeApp, RendererGl, [] ( App::Settings *settings) {
447 6:4c0e82b725d9 f
448
    const std::vector< string > args = settings->getCommandLineArgs();
449
450
    int width = 0;
451
    int height = 0;
452
453
    try {
454
        if( args.size() != 3 )
455
            throw std::invalid_argument("");
456
457
        width  = std::stoi( args[1] );
458
        height = std::stoi( args[2] );
459
460
    }
461
    catch( std::invalid_argument & e ){
462
        console() << "Error: invalid arguments" << std::endl;
463
        console() << "Usage: ./Collidoscope window_width window_height" << std::endl;
464
        console() << "For example: ./Collidoscope 1024 768 " << std::endl;
465
466
        settings->setShouldQuit( true );
467
        return;
468
    }
469
470
    settings->setWindowSize( width, height );
471
    settings->setMultiTouchEnabled( false );
472
    settings->disableFrameRate();
473 0:02467299402e f
474
} )