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@436: #include "base/BaseTypes.h" Chris@436: 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@436: ClipMixer(int channels, sv_samplerate_t sampleRate, sv_frame_t 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@436: bool loadClipData(QString clipFilePath, double clipF0, double level); Chris@305: Chris@308: void reset(); // discarding any playing notes Chris@308: Chris@305: struct NoteStart { Chris@595: sv_frame_t frameOffset; // within current processing block Chris@595: float frequency; // Hz Chris@595: float level; // volume in range (0,1] Chris@595: float pan; // range [-1,1] Chris@305: }; Chris@305: Chris@305: struct NoteEnd { Chris@595: sv_frame_t 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@595: std::vector newNotes, Chris@595: std::vector endingNotes); Chris@305: Chris@305: private: Chris@305: int m_channels; Chris@436: sv_samplerate_t m_sampleRate; Chris@436: sv_frame_t m_blockSize; Chris@305: Chris@305: QString m_clipPath; Chris@305: Chris@305: float *m_clipData; Chris@436: sv_frame_t m_clipLength; Chris@436: double m_clipF0; Chris@436: sv_samplerate_t m_clipRate; Chris@310: Chris@310: std::vector m_playing; Chris@310: Chris@436: double getResampleRatioFor(double frequency); Chris@436: sv_frame_t getResampledClipDuration(double frequency); Chris@310: Chris@310: void mixNote(float **toBuffers, Chris@310: float *levels, Chris@310: float frequency, Chris@436: sv_frame_t sourceOffset, // within resampled note Chris@436: sv_frame_t targetOffset, // within target buffer Chris@436: sv_frame_t sampleCount, matthiasm@320: bool isEnd); Chris@305: }; Chris@305: Chris@305: Chris@305: #endif