Chris@305: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@305: Chris@305: /* Chris@305: Sonic Visualiser Chris@305: An audio file viewer and annotation editor. Chris@305: Centre for Digital Music, Queen Mary, University of London. Chris@305: This file copyright 2006 Chris Cannam, 2006-2014 QMUL. Chris@305: Chris@305: This program is free software; you can redistribute it and/or Chris@305: modify it under the terms of the GNU General Public License as Chris@305: published by the Free Software Foundation; either version 2 of the Chris@305: License, or (at your option) any later version. See the file Chris@305: COPYING included with this distribution for more information. Chris@305: */ Chris@305: Chris@312: #ifndef CLIP_MIXER_H Chris@312: #define CLIP_MIXER_H Chris@305: Chris@305: #include Chris@305: #include Chris@305: Chris@305: /** Chris@305: * Mix in synthetic notes produced by resampling a prerecorded Chris@312: * clip. (i.e. this is an implementation of a digital sampler in the Chris@312: * musician's sense.) This can mix any number of notes of arbitrary Chris@312: * frequency, so long as they all use the same sample clip. Chris@305: */ Chris@305: Chris@305: class ClipMixer Chris@305: { Chris@305: public: Chris@305: ClipMixer(int channels, int sampleRate, int blockSize); Chris@305: ~ClipMixer(); Chris@305: Chris@308: void setChannelCount(int channels); Chris@312: Chris@312: /** Chris@312: * Load a sample clip from a wav file. This can only happen once: Chris@349: * construct a new ClipMixer if you want a different clip. The Chris@349: * clip was recorded at a pitch with fundamental frequency clipF0, Chris@349: * and should be scaled by level (in the range 0-1) when playing Chris@349: * back. Chris@312: */ Chris@349: bool loadClipData(QString clipFilePath, float clipF0, float level); Chris@305: Chris@308: void reset(); // discarding any playing notes Chris@308: Chris@305: struct NoteStart { Chris@310: int frameOffset; // within current processing block Chris@305: float frequency; // Hz Chris@305: float level; // volume in range (0,1] Chris@305: float pan; // range [-1,1] Chris@305: }; Chris@305: Chris@305: struct NoteEnd { Chris@310: int frameOffset; // in current processing block Chris@308: float frequency; // matching note start Chris@305: }; Chris@305: Chris@305: void mix(float **toBuffers, Chris@308: float gain, Chris@305: std::vector newNotes, Chris@305: std::vector endingNotes); Chris@305: Chris@305: private: Chris@305: int m_channels; Chris@305: int m_sampleRate; Chris@305: int m_blockSize; Chris@305: Chris@305: QString m_clipPath; Chris@305: Chris@305: float *m_clipData; Chris@305: int m_clipLength; Chris@305: float m_clipF0; Chris@305: float m_clipRate; Chris@310: Chris@310: std::vector m_playing; Chris@310: Chris@310: float getResampleRatioFor(float frequency); Chris@310: int getResampledClipDuration(float frequency); Chris@310: Chris@310: void mixNote(float **toBuffers, Chris@310: float *levels, Chris@310: float frequency, Chris@310: int sourceOffset, // within resampled note Chris@310: int targetOffset, // within target buffer matthiasm@320: int sampleCount, matthiasm@320: bool isEnd); Chris@305: }; Chris@305: Chris@305: Chris@305: #endif