1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
/**
bespoke synth, a software modular synthesizer
Copyright (C) 2021 Ryan Challinor (contact: awwbees@gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
//
// Sample.h
// modularSynth
//
// Created by Ryan Challinor on 11/25/12.
//
//
#pragma once
#include "OpenFrameworksPort.h"
#include "ChannelBuffer.h"
#include <limits>
#include "juce_events/juce_events.h"
class FileStreamOut;
class FileStreamIn;
namespace juce
{
class AudioFormatReader;
template <typename T>
class AudioBuffer;
using AudioSampleBuffer = AudioBuffer<float>;
}
class Sample : public juce::Timer
{
public:
enum class ReadType
{
Sync,
Async
};
Sample();
~Sample();
bool Read(const char* path, bool mono = false, ReadType readType = ReadType::Sync);
bool Write(const char* path = nullptr); //no path = use read filename
bool ConsumeData(double time, ChannelBuffer* out, int size, bool replace);
void Play(double time, float rate, int offset, int stopPoint = -1);
void SetRate(float rate) { mRate = rate; }
std::string Name() const { return mName; }
void SetName(std::string name) { mName = name; }
int LengthInSamples() const { return mNumSamples; }
int NumChannels() const { return mData.NumActiveChannels(); }
ChannelBuffer* Data() { return &mData; }
double GetPlayPosition() const { return mOffset; }
void SetPlayPosition(double sample) { mOffset = sample; }
float GetSampleRateRatio() const { return mSampleRateRatio; }
void Reset() { mOffset = mNumSamples; }
void SetStopPoint(int stopPoint) { mStopPoint = stopPoint; }
void ClearStopPoint() { mStopPoint = -1; }
void PadBack(int amount);
void ClipTo(int start, int end);
void ShiftWrap(int numSamples);
std::string GetReadPath() const { return mReadPath; }
static bool WriteDataToFile(const std::string& path, float** data, int numSamples, int channels = 1);
static bool WriteDataToFile(const std::string& path, ChannelBuffer* data, int numSamples);
bool IsPlaying() const { return mOffset < mNumSamples; }
void LockDataMutex(bool lock) { lock ? mDataMutex.lock() : mDataMutex.unlock(); }
void Create(int length);
void Create(ChannelBuffer* data);
void SetLooping(bool looping) { mLooping = looping; }
void SetNumBars(int numBars) { mNumBars = numBars; }
int GetNumBars() const { return mNumBars; }
void SetVolume(float vol) { mVolume = vol; }
void CopyFrom(Sample* sample);
bool IsSampleLoading() { return mSamplesLeftToRead > 0; }
float GetSampleLoadProgress() { return (mNumSamples > 0) ? (1 - (float(mSamplesLeftToRead) / mNumSamples)) : 1; }
void SaveState(FileStreamOut& out);
void LoadState(FileStreamIn& in);
private:
void Setup(int length);
void FinishRead();
//juce::Timer
void timerCallback();
ChannelBuffer mData{ 0 };
int mNumSamples{ 0 };
double mStartTime{ 0 };
double mOffset{ std::numeric_limits<double>::max() };
float mRate{ 1 };
int mOriginalSampleRate{ gSampleRate };
float mSampleRateRatio{ 1 };
int mStopPoint{ -1 };
std::string mName{ "" };
std::string mReadPath{ "" };
ofMutex mDataMutex;
ofMutex mPlayMutex;
bool mLooping{ false };
int mNumBars{ -1 };
float mVolume{ 1 };
juce::AudioFormatReader* mReader{};
std::unique_ptr<juce::AudioSampleBuffer> mReadBuffer;
int mSamplesLeftToRead{ 0 };
};
|