SHARE
TWEET

Untitled

a guest Mar 26th, 2020 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // HEADER HEADER
  2. #pragma once
  3. #include "Main/Common.h"
  4. #include "Main/AppModel.h"
  5.  
  6. struct Player
  7. : public AudioSource
  8. {
  9. public:
  10.     Player(AppModel& model, MidiKeyboardState& midistate);
  11.    
  12.     void setup(File* file);
  13.     void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override;
  14.     void releaseResources() override;
  15.     void getNextAudioBlock(const AudioSourceChannelInfo& bufferToFill) override;
  16.        
  17.     MidiKeyboardState&   midiState;
  18.     MidiMessageCollector midiCollector;
  19.     Synthesiser          synth;
  20.    
  21. private:
  22.     struct Voice : public SamplerVoice
  23.     {
  24.     public:
  25.         Voice(AppModel& model);
  26.        
  27.     private:
  28.         AppModel& mAppModel;
  29.     };
  30.    
  31. private:
  32.     AppModel& mAppModel;
  33.     AudioFormatManager mFormatManager;
  34. };
  35.  
  36.  
  37. // CPP CPP
  38.  
  39. #include "Player.h"
  40.  
  41. Player::Player(AppModel& model, MidiKeyboardState& midistate) : mAppModel(model), midiState(midistate)
  42. {
  43.     mFormatManager.registerBasicFormats();
  44. }
  45.  
  46. void Player::setup(File* file)
  47. {
  48.     if (file != nullptr)
  49.     {
  50.         synth.clearVoices();
  51.         synth.addVoice (new Voice(mAppModel));
  52.        
  53.         std::unique_ptr<AudioFormatReader> reader(mFormatManager.createReaderFor(*file));
  54.        
  55.         BigInteger allNotes;
  56.         allNotes.setRange(0, 128, true);
  57.        
  58.         synth.clearSounds();
  59.         synth.addSound(new SamplerSound("current file", *reader, allNotes, 60, 0.0, 0.1, 1000.0));
  60.     }
  61. }
  62.  
  63. void Player::prepareToPlay(int samplesPerBlockExpected, double sampleRate)
  64. {
  65.     midiCollector.reset(sampleRate);
  66.     synth.setCurrentPlaybackSampleRate(sampleRate);
  67. }
  68.  
  69. void Player::releaseResources()
  70. {
  71. }
  72.  
  73. void Player::getNextAudioBlock(const AudioSourceChannelInfo& bufferToFill)
  74. {
  75.     bufferToFill.clearActiveBufferRegion();
  76.    
  77.     MidiBuffer incomingMidi;
  78.     midiCollector.removeNextBlockOfMessages(incomingMidi, bufferToFill.numSamples);
  79.     midiState.processNextMidiBuffer (incomingMidi, 0, bufferToFill.numSamples, true);
  80.     synth.renderNextBlock(*bufferToFill.buffer, incomingMidi, 2000, bufferToFill.numSamples);
  81. }
  82.  
  83.  
  84. Player::Voice::Voice(AppModel& model) : mAppModel(model)
  85. {
  86. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top