Add oscillator=on and wavetables from file
This commit is contained in:
parent
25ca3eeb0b
commit
5f564d749a
8 changed files with 140 additions and 17 deletions
|
|
@ -274,7 +274,7 @@ void sfz::FilePool::tryToClearPromises()
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||||
|
|
||||||
for (auto& promise: promisesToClear) {
|
for (auto& promise: promisesToClear) {
|
||||||
if (promise->dataReady)
|
if (promise->dataStatus != FilePromise::DataStatus::Wait)
|
||||||
promise->reset();
|
promise->reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -313,11 +313,12 @@ void sfz::FilePool::loadingThread() noexcept
|
||||||
SndfileHandle sndFile(file.string().c_str());
|
SndfileHandle sndFile(file.string().c_str());
|
||||||
if (sndFile.error() != 0) {
|
if (sndFile.error() != 0) {
|
||||||
DBG("[sfizz] libsndfile errored for " << promise->filename << " with message " << sndFile.strError());
|
DBG("[sfizz] libsndfile errored for " << promise->filename << " with message " << sndFile.strError());
|
||||||
|
promise->dataStatus = FilePromise::DataStatus::Error;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const auto frames = static_cast<uint32_t>(sndFile.frames());
|
const auto frames = static_cast<uint32_t>(sndFile.frames());
|
||||||
streamFromFile<float>(sndFile, frames, oversamplingFactor, promise->fileData, &promise->availableFrames);
|
streamFromFile<float>(sndFile, frames, oversamplingFactor, promise->fileData, &promise->availableFrames);
|
||||||
promise->dataReady = true;
|
promise->dataStatus = FilePromise::DataStatus::Ready;
|
||||||
const auto loadDuration = std::chrono::high_resolution_clock::now() - loadStartTime;
|
const auto loadDuration = std::chrono::high_resolution_clock::now() - loadStartTime;
|
||||||
logger.logFileTime(waitDuration, loadDuration, frames, promise->filename);
|
logger.logFileTime(waitDuration, loadDuration, frames, promise->filename);
|
||||||
|
|
||||||
|
|
@ -352,7 +353,7 @@ void sfz::FilePool::cleanupPromises() noexcept
|
||||||
auto clearedIterator = promisesToClear.begin();
|
auto clearedIterator = promisesToClear.begin();
|
||||||
auto clearedSentinel = promisesToClear.rbegin();
|
auto clearedSentinel = promisesToClear.rbegin();
|
||||||
while (clearedIterator < clearedSentinel.base()) {
|
while (clearedIterator < clearedSentinel.base()) {
|
||||||
if (clearedIterator->get()->dataReady == false) {
|
if (clearedIterator->get()->dataStatus == FilePromise::DataStatus::Wait) {
|
||||||
emptyPromises.push_back(*clearedIterator);
|
emptyPromises.push_back(*clearedIterator);
|
||||||
std::iter_swap(clearedIterator, clearedSentinel);
|
std::iter_swap(clearedIterator, clearedSentinel);
|
||||||
++clearedSentinel;
|
++clearedSentinel;
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,7 @@ struct FilePromise
|
||||||
{
|
{
|
||||||
AudioSpan<const float> getData()
|
AudioSpan<const float> getData()
|
||||||
{
|
{
|
||||||
if (dataReady)
|
if (dataStatus == DataStatus::Ready)
|
||||||
return AudioSpan<const float>(fileData);
|
return AudioSpan<const float>(fileData);
|
||||||
else if (availableFrames > preloadedData->getNumFrames())
|
else if (availableFrames > preloadedData->getNumFrames())
|
||||||
return AudioSpan<const float>(fileData).first(availableFrames);
|
return AudioSpan<const float>(fileData).first(availableFrames);
|
||||||
|
|
@ -69,18 +69,30 @@ struct FilePromise
|
||||||
preloadedData.reset();
|
preloadedData.reset();
|
||||||
filename = "";
|
filename = "";
|
||||||
availableFrames = 0;
|
availableFrames = 0;
|
||||||
dataReady = false;
|
dataStatus = DataStatus::Wait;
|
||||||
oversamplingFactor = config::defaultOversamplingFactor;
|
oversamplingFactor = config::defaultOversamplingFactor;
|
||||||
sampleRate = config::defaultSampleRate;
|
sampleRate = config::defaultSampleRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void waitCompletion()
|
||||||
|
{
|
||||||
|
while (dataStatus == DataStatus::Wait)
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class DataStatus {
|
||||||
|
Wait = 0,
|
||||||
|
Ready,
|
||||||
|
Error,
|
||||||
|
};
|
||||||
|
|
||||||
absl::string_view filename {};
|
absl::string_view filename {};
|
||||||
AudioBufferPtr preloadedData {};
|
AudioBufferPtr preloadedData {};
|
||||||
AudioBuffer<float> fileData {};
|
AudioBuffer<float> fileData {};
|
||||||
float sampleRate { config::defaultSampleRate };
|
float sampleRate { config::defaultSampleRate };
|
||||||
Oversampling oversamplingFactor { config::defaultOversamplingFactor };
|
Oversampling oversamplingFactor { config::defaultOversamplingFactor };
|
||||||
std::atomic<size_t> availableFrames { 0 };
|
std::atomic<size_t> availableFrames { 0 };
|
||||||
std::atomic<bool> dataReady { false };
|
std::atomic<DataStatus> dataStatus { DataStatus::Wait };
|
||||||
std::chrono::time_point<std::chrono::high_resolution_clock> creationTime;
|
std::chrono::time_point<std::chrono::high_resolution_clock> creationTime;
|
||||||
|
|
||||||
LEAK_DETECTOR(FilePromise);
|
LEAK_DETECTOR(FilePromise);
|
||||||
|
|
|
||||||
|
|
@ -98,6 +98,9 @@ bool sfz::Region::parseOpcode(const Opcode& opcode)
|
||||||
// Wavetable oscillator
|
// Wavetable oscillator
|
||||||
case hash("oscillator_phase"):
|
case hash("oscillator_phase"):
|
||||||
setValueFromOpcode(opcode, oscillatorPhase, Default::oscillatorPhaseRange);
|
setValueFromOpcode(opcode, oscillatorPhase, Default::oscillatorPhaseRange);
|
||||||
|
case hash("oscillator"):
|
||||||
|
if (auto value = readBooleanFromOpcode(opcode))
|
||||||
|
oscillator = *value;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Instrument settings: voice lifecycle
|
// Instrument settings: voice lifecycle
|
||||||
|
|
|
||||||
|
|
@ -228,6 +228,7 @@ struct Region {
|
||||||
|
|
||||||
// Wavetable oscillator
|
// Wavetable oscillator
|
||||||
float oscillatorPhase { Default::oscillatorPhase };
|
float oscillatorPhase { Default::oscillatorPhase };
|
||||||
|
bool oscillator = false;
|
||||||
|
|
||||||
// Instrument settings: voice lifecycle
|
// Instrument settings: voice lifecycle
|
||||||
uint32_t group { Default::group }; // group
|
uint32_t group { Default::group }; // group
|
||||||
|
|
|
||||||
|
|
@ -306,6 +306,7 @@ bool sfz::Synth::loadSfzFile(const fs::path& file)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
resources.filePool.setRootDirectory(parser.originalDirectory());
|
resources.filePool.setRootDirectory(parser.originalDirectory());
|
||||||
|
resources.wavePool.clearFileWaves();
|
||||||
|
|
||||||
auto currentRegion = regions.begin();
|
auto currentRegion = regions.begin();
|
||||||
auto lastRegion = regions.rbegin();
|
auto lastRegion = regions.rbegin();
|
||||||
|
|
@ -324,7 +325,7 @@ bool sfz::Synth::loadSfzFile(const fs::path& file)
|
||||||
while (currentRegion < lastRegion.base()) {
|
while (currentRegion < lastRegion.base()) {
|
||||||
auto region = currentRegion->get();
|
auto region = currentRegion->get();
|
||||||
|
|
||||||
if (!region->isGenerator()) {
|
if (!region->oscillator && !region->isGenerator()) {
|
||||||
if (!resources.filePool.checkSample(region->sample)) {
|
if (!resources.filePool.checkSample(region->sample)) {
|
||||||
removeCurrentRegion();
|
removeCurrentRegion();
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -360,6 +361,9 @@ bool sfz::Synth::loadSfzFile(const fs::path& file)
|
||||||
if (!resources.filePool.preloadFile(region->sample, maxOffset))
|
if (!resources.filePool.preloadFile(region->sample, maxOffset))
|
||||||
removeCurrentRegion();
|
removeCurrentRegion();
|
||||||
}
|
}
|
||||||
|
else if (region->oscillator && !region->isGenerator()) {
|
||||||
|
resources.wavePool.createFileWave(resources.filePool, region->sample);
|
||||||
|
}
|
||||||
|
|
||||||
for (auto note = 0; note < 128; note++) {
|
for (auto note = 0; note < 128; note++) {
|
||||||
if (region->keyRange.containsWithEnd(note) ||
|
if (region->keyRange.containsWithEnd(note) ||
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,19 @@ void sfz::Voice::startVoice(Region* region, int delay, int number, uint8_t value
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
waveOscillator.setWavetable(wave);
|
waveOscillator.setWavetable(wave);
|
||||||
|
} else if (region->oscillator) {
|
||||||
|
const WavetableMulti* wave = resources.wavePool.getFileWave(region->sample);
|
||||||
|
waveOscillator.setWavetable(wave);
|
||||||
|
} else {
|
||||||
|
currentPromise = resources.filePool.getFilePromise(region->sample);
|
||||||
|
if (currentPromise == nullptr) {
|
||||||
|
reset();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
speedRatio = static_cast<float>(currentPromise->sampleRate / this->sampleRate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (region->oscillator || region->isGenerator()) {
|
||||||
float phase;
|
float phase;
|
||||||
const float phaseParam = region->oscillatorPhase;
|
const float phaseParam = region->oscillatorPhase;
|
||||||
if (phaseParam >= 0) {
|
if (phaseParam >= 0) {
|
||||||
|
|
@ -69,14 +81,8 @@ void sfz::Voice::startVoice(Region* region, int delay, int number, uint8_t value
|
||||||
phase = phaseDist(Random::randomGenerator);
|
phase = phaseDist(Random::randomGenerator);
|
||||||
}
|
}
|
||||||
waveOscillator.setPhase(phase);
|
waveOscillator.setPhase(phase);
|
||||||
} else {
|
|
||||||
currentPromise = resources.filePool.getFilePromise(region->sample);
|
|
||||||
if (currentPromise == nullptr) {
|
|
||||||
reset();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
speedRatio = static_cast<float>(currentPromise->sampleRate / this->sampleRate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pitchRatio = region->getBasePitchVariation(number, value);
|
pitchRatio = region->getBasePitchVariation(number, value);
|
||||||
|
|
||||||
baseVolumedB = region->getBaseVolumedB(number);
|
baseVolumedB = region->getBaseVolumedB(number);
|
||||||
|
|
@ -294,7 +300,7 @@ void sfz::Voice::renderBlock(AudioSpan<float> buffer) noexcept
|
||||||
|
|
||||||
{ // Fill buffer with raw data
|
{ // Fill buffer with raw data
|
||||||
ScopedTiming logger { dataDuration };
|
ScopedTiming logger { dataDuration };
|
||||||
if (region->isGenerator())
|
if (region->isGenerator() || region->oscillator)
|
||||||
fillWithGenerator(delayed_buffer);
|
fillWithGenerator(delayed_buffer);
|
||||||
else
|
else
|
||||||
fillWithData(delayed_buffer);
|
fillWithData(delayed_buffer);
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@
|
||||||
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
|
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
|
||||||
|
|
||||||
#include "Wavetables.h"
|
#include "Wavetables.h"
|
||||||
|
#include "FilePool.h"
|
||||||
#include "MathHelpers.h"
|
#include "MathHelpers.h"
|
||||||
#include <kiss_fftr.h>
|
#include <kiss_fftr.h>
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
namespace sfz {
|
namespace sfz {
|
||||||
|
|
||||||
|
|
@ -279,6 +279,31 @@ void WavetableMulti::fillExtra()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Harmonic profile which takes its values from a table.
|
||||||
|
*/
|
||||||
|
class TabulatedHarmonicProfile : public HarmonicProfile {
|
||||||
|
public:
|
||||||
|
explicit TabulatedHarmonicProfile(absl::Span<const std::complex<float>> harmonics)
|
||||||
|
: _harmonics(harmonics)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
std::complex<double> getHarmonic(size_t index) const override
|
||||||
|
{
|
||||||
|
if (index >= _harmonics.size())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
return _harmonics[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
absl::Span<const std::complex<float>> _harmonics;
|
||||||
|
};
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
WavetablePool::WavetablePool()
|
WavetablePool::WavetablePool()
|
||||||
{
|
{
|
||||||
|
|
@ -313,4 +338,65 @@ const WavetableMulti* WavetablePool::getWaveSquare()
|
||||||
return &wave;
|
return &wave;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const WavetableMulti* WavetablePool::getFileWave(const std::string& filename)
|
||||||
|
{
|
||||||
|
auto it = _fileWaves.find(filename);
|
||||||
|
if (it == _fileWaves.end())
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
return it->second.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
void WavetablePool::clearFileWaves()
|
||||||
|
{
|
||||||
|
_fileWaves.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
const WavetableMulti* WavetablePool::createFileWave(FilePool& filePool, const std::string& filename)
|
||||||
|
{
|
||||||
|
if (const WavetableMulti* wave = getFileWave(filename))
|
||||||
|
return wave;
|
||||||
|
|
||||||
|
if (!filePool.preloadFile(filename, 0))
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
FilePromisePtr fp = filePool.getFilePromise(filename);
|
||||||
|
if (!fp)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
fp->waitCompletion();
|
||||||
|
if (fp->dataStatus == FilePromise::DataStatus::Error)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
// use 1 channel only, maybe warn if file has more channels
|
||||||
|
auto audioData = fp->fileData.getSpan(0);
|
||||||
|
size_t fftSize = audioData.size();
|
||||||
|
size_t specSize = fftSize / 2 + 1;
|
||||||
|
|
||||||
|
typedef std::complex<kiss_fft_scalar> cpx;
|
||||||
|
std::unique_ptr<cpx[]> spec { new cpx[specSize] };
|
||||||
|
|
||||||
|
kiss_fftr_cfg cfg = kiss_fftr_alloc(fftSize, false, nullptr, nullptr);
|
||||||
|
if (!cfg)
|
||||||
|
throw std::bad_alloc();
|
||||||
|
|
||||||
|
kiss_fftr(cfg, audioData.data(), reinterpret_cast<kiss_fft_cpx*>(spec.get()));
|
||||||
|
kiss_fftr_free(cfg);
|
||||||
|
|
||||||
|
// scale transform, and normalize amplitude and phase
|
||||||
|
const std::complex<double> k = std::polar(2.0 / fftSize, -M_PI / 2);
|
||||||
|
for (size_t i = 0; i < specSize; ++i)
|
||||||
|
spec[i] *= k;
|
||||||
|
|
||||||
|
TabulatedHarmonicProfile hp {
|
||||||
|
absl::Span<const std::complex<float>> { spec.get(), specSize }
|
||||||
|
};
|
||||||
|
|
||||||
|
auto wave = std::make_shared<WavetableMulti>(
|
||||||
|
WavetableMulti::createForHarmonicProfile(hp, 1.0));
|
||||||
|
|
||||||
|
_fileWaves[filename] = wave;
|
||||||
|
return wave.get();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace sfz
|
} // namespace sfz
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,12 @@
|
||||||
#include "LeakDetector.h"
|
#include "LeakDetector.h"
|
||||||
#include "Buffer.h"
|
#include "Buffer.h"
|
||||||
#include <absl/types/span.h>
|
#include <absl/types/span.h>
|
||||||
|
#include <absl/container/flat_hash_map.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <complex>
|
#include <complex>
|
||||||
|
|
||||||
namespace sfz {
|
namespace sfz {
|
||||||
|
class FilePool;
|
||||||
|
|
||||||
class WavetableMulti;
|
class WavetableMulti;
|
||||||
|
|
||||||
|
|
@ -185,15 +187,23 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Holds predefined wavetables.
|
* @brief Holds predefined and loaded wavetables.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
struct WavetablePool {
|
struct WavetablePool {
|
||||||
WavetablePool();
|
WavetablePool();
|
||||||
|
|
||||||
|
const WavetableMulti* getFileWave(const std::string& filename);
|
||||||
|
const WavetableMulti* createFileWave(FilePool& filePool, const std::string& filename);
|
||||||
|
void clearFileWaves();
|
||||||
|
|
||||||
static const WavetableMulti* getWaveSin();
|
static const WavetableMulti* getWaveSin();
|
||||||
static const WavetableMulti* getWaveTriangle();
|
static const WavetableMulti* getWaveTriangle();
|
||||||
static const WavetableMulti* getWaveSaw();
|
static const WavetableMulti* getWaveSaw();
|
||||||
static const WavetableMulti* getWaveSquare();
|
static const WavetableMulti* getWaveSquare();
|
||||||
|
|
||||||
|
private:
|
||||||
|
absl::flat_hash_map<std::string, std::shared_ptr<WavetableMulti>> _fileWaves;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace sfz
|
} // namespace sfz
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue