diff --git a/plugins/Bidoo-workaround/waves.cpp b/plugins/Bidoo-workaround/waves.cpp new file mode 100644 index 0000000..03d2305 --- /dev/null +++ b/plugins/Bidoo-workaround/waves.cpp @@ -0,0 +1,248 @@ +#include "../Bidoo/src/dep/waves.hpp" +#include "../Bidoo/src/dep/AudioFile/AudioFile.h" +// #define DR_WAV_IMPLEMENTATION +#include "../cf/src/dr_wav.h" +#include + +#ifndef DRWAV_ASSERT +#include +#define DRWAV_ASSERT(expression) assert(expression) +#endif +#ifndef DRWAV_MALLOC +#define DRWAV_MALLOC(sz) malloc((sz)) +#endif +#ifndef DRWAV_FREE +#define DRWAV_FREE(p) free((p)) +#endif + +#define drwav_assert DRWAV_ASSERT + +#if defined(SIZE_MAX) + #define DRWAV_SIZE_MAX SIZE_MAX +#else + #if defined(_WIN64) || defined(_LP64) || defined(__LP64__) + #define DRWAV_SIZE_MAX ((drwav_uint64)0xFFFFFFFFFFFFFFFF) + #else + #define DRWAV_SIZE_MAX 0xFFFFFFFF + #endif +#endif + +extern "C" { +float* drwav_open_file_and_read_f32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drwav_uint64* totalSampleCount); +} + +static float* drwav__read_and_close_f32(drwav* pWav, unsigned int* channels, unsigned int* sampleRate, drwav_uint64* totalSampleCount) +{ + drwav_uint64 sampleDataSize; + float* pSampleData; + drwav_uint64 samplesRead; + + drwav_assert(pWav != NULL); + + sampleDataSize = pWav->totalSampleCount * sizeof(float); + if (sampleDataSize > DRWAV_SIZE_MAX) { + drwav_uninit(pWav); + return NULL; /* File's too big. */ + } + + pSampleData = (float*)DRWAV_MALLOC((size_t)sampleDataSize); /* <-- Safe cast due to the check above. */ + if (pSampleData == NULL) { + drwav_uninit(pWav); + return NULL; /* Failed to allocate memory. */ + } + + samplesRead = drwav_read_f32(pWav, (size_t)pWav->totalSampleCount, pSampleData); + if (samplesRead != pWav->totalSampleCount) { + DRWAV_FREE(pSampleData); + drwav_uninit(pWav); + return NULL; /* There was an error reading the samples. */ + } + + drwav_uninit(pWav); + + if (sampleRate) { + *sampleRate = pWav->sampleRate; + } + if (channels) { + *channels = pWav->channels; + } + if (totalSampleCount) { + *totalSampleCount = pWav->totalSampleCount; + } + + return pSampleData; +} + +float* drwav_open_file_and_read_f32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drwav_uint64* totalSampleCount) +{ + drwav wav; + + if (sampleRate) { + *sampleRate = 0; + } + if (channels) { + *channels = 0; + } + if (totalSampleCount) { + *totalSampleCount = 0; + } + + if (!drwav_init_file(&wav, filename)) { + return NULL; + } + + return drwav__read_and_close_f32(&wav, channels, sampleRate, totalSampleCount); +} + +namespace waves { + + std::vector> getMonoWav(const std::string path, const float currentSampleRate, std::string &waveFileName, std::string &waveExtension, int &sampleChannels, int &sampleRate, int &sampleCount) { + waveFileName = rack::system::getFilename(path); + waveExtension = rack::system::getExtension(waveFileName); + std::vector> result; + if (waveExtension == ".wav") { + unsigned int c; + unsigned int sr; + drwav_uint64 sc; + float* pSampleData; + pSampleData = drwav_open_file_and_read_f32(path.c_str(), &c, &sr, &sc); + if (pSampleData != NULL) { + sampleChannels = c; + sampleRate = sr; + for (long long unsigned int i=0; i < sc; i = i + c) { + rack::dsp::Frame<1> frame; + if (sampleChannels == 2) { + frame.samples[0] = (pSampleData[i] + pSampleData[i+1])/2.0f; + } + else { + frame.samples[0] = pSampleData[i]; + } + result.push_back(frame); + } + sampleCount = sc/c; + drwav_free(pSampleData); + } + } + else if (waveExtension == ".aiff") { + AudioFile audioFile; + if (audioFile.load (path.c_str())) { + sampleChannels = audioFile.getNumChannels(); + sampleRate = audioFile.getSampleRate(); + sampleCount = audioFile.getNumSamplesPerChannel(); + for (int i=0; i < sampleCount; i++) { + rack::dsp::Frame<1> frame; + if (sampleChannels == 2) { + frame.samples[0] = (audioFile.samples[0][i] + audioFile.samples[1][i])/2.0f; + } + else { + frame.samples[0] = audioFile.samples[0][i]; + } + result.push_back(frame); + } + } + } + + if ((sampleRate != currentSampleRate) && (sampleCount>0)) { + rack::dsp::SampleRateConverter<1> conv; + conv.setRates(currentSampleRate, sampleRate); + int outCount = sampleCount; + std::vector> subResult; + for (int i=0;i frame; + frame.samples[0]=0.0f; + subResult.push_back(frame); + } + conv.process(&result[0], &sampleCount, &subResult[0], &outCount); + sampleCount = outCount; + return subResult; + } + + return result; + } + + std::vector> getStereoWav(const std::string path, const float currentSampleRate, std::string &waveFileName, std::string &waveExtension, int &sampleChannels, int &sampleRate, int &sampleCount) { + waveFileName = rack::system::getFilename(path); + waveExtension = rack::system::getExtension(waveFileName); + std::vector> result; + if (waveExtension == ".wav") { + unsigned int c; + unsigned int sr; + drwav_uint64 sc; + float* pSampleData; + pSampleData = drwav_open_file_and_read_f32(path.c_str(), &c, &sr, &sc); + if (pSampleData != NULL) { + sampleChannels = c; + sampleRate = sr; + for (long long unsigned int i=0; i < sc; i = i + c) { + rack::dsp::Frame<2> frame; + frame.samples[0] = pSampleData[i]; + if (sampleChannels == 2) + frame.samples[1] = (float)pSampleData[i+1]; + else + frame.samples[1] = (float)pSampleData[i]; + result.push_back(frame); + } + sampleCount = sc/c; + drwav_free(pSampleData); + } + } + else if (waveExtension == ".aiff") { + AudioFile audioFile; + if (audioFile.load (path.c_str())) { + sampleChannels = audioFile.getNumChannels(); + sampleRate = audioFile.getSampleRate(); + sampleCount = audioFile.getNumSamplesPerChannel(); + for (int i=0; i < sampleCount; i++) { + rack::dsp::Frame<2> frame; + frame.samples[0] = audioFile.samples[0][i]; + if (sampleChannels == 2) + frame.samples[1] = audioFile.samples[1][i]; + else + frame.samples[1] = audioFile.samples[0][i]; + result.push_back(frame); + } + } + } + + if ((sampleRate != currentSampleRate) && (sampleCount>0)) { + rack::dsp::SampleRateConverter<2> conv; + conv.setRates(sampleRate, currentSampleRate); + conv.setQuality(SPEEX_RESAMPLER_QUALITY_DESKTOP); + int outCount = 16*sampleCount; + std::vector> subResult; + for (int i=0;i frame; + frame.samples[0]=0.0f; + frame.samples[1]=0.0f; + subResult.push_back(frame); + } + conv.process(&result[0], &sampleCount, &subResult[0], &outCount); + sampleCount = outCount; + return subResult; + } + + return result; + } + + void saveWave(std::vector> &sample, int sampleRate, std::string path) { + drwav_data_format format; + format.container = drwav_container_riff; + format.format = DR_WAVE_FORMAT_PCM; + format.channels = 2; + format.sampleRate = sampleRate; + format.bitsPerSample = 32; + + int *pSamples = (int*)calloc(2*sample.size(),sizeof(int)); + memset(pSamples, 0, 2*sample.size()*sizeof(int)); + for (unsigned int i = 0; i < sample.size(); i++) { + *(pSamples+2*i)= floor(sample[i].samples[0]*2147483647); + *(pSamples+2*i+1)= floor(sample[i].samples[1]*2147483647); + } + + drwav* pWav = drwav_open_file_write(path.c_str(), &format); + drwav_write(pWav, 2*sample.size(), pSamples); + drwav_close(pWav); + free(pSamples); + } + +} diff --git a/plugins/Makefile b/plugins/Makefile index 988452a..cad8731 100644 --- a/plugins/Makefile +++ b/plugins/Makefile @@ -150,10 +150,11 @@ PLUGIN_BINARIES += Befaco/src/SpringReverbIR.pcm # Bidoo PLUGIN_FILES += $(filter-out Bidoo/src/plugin.cpp Bidoo/src/ANTN.cpp,$(wildcard Bidoo/src/*.cpp)) -PLUGIN_FILES += $(wildcard Bidoo/src/dep/*.cpp) +PLUGIN_FILES += $(filter-out Bidoo/src/dep/waves.cpp,$(wildcard Bidoo/src/dep/*.cpp)) PLUGIN_FILES += $(wildcard Bidoo/src/dep/filters/*.cpp) PLUGIN_FILES += $(wildcard Bidoo/src/dep/freeverb/*.cpp) PLUGIN_FILES += $(wildcard Bidoo/src/dep/lodepng/*.cpp) +PLUGIN_FILES += Bidoo-workaround/waves.cpp # modules/types which are present in other plugins BIDOO_CUSTOM = LadderFilter @@ -179,7 +180,7 @@ PLUGIN_FILES += $(wildcard Cardinal/src/*.cpp) # -------------------------------------------------------------- # cf -PLUGIN_FILES += $(filter-out cf/src/plugin.cpp cf/src/PLAY.cpp,$(wildcard cf/src/*.cpp)) +PLUGIN_FILES += $(filter-out cf/src/plugin.cpp,$(wildcard cf/src/*.cpp)) # -------------------------------------------------------------- # ESeries @@ -245,9 +246,6 @@ PLUGIN_FILES += $(filter-out ZetaCarinaeModules/src/plugin.cpp,$(wildcard ZetaCa BUILD_DIR = ../build/plugins -PLUGIN_OBJS = $(PLUGIN_FILES:%=$(BUILD_DIR)/%.o) -PLUGIN_OBJS += $(PLUGIN_BINARIES:%=$(BUILD_DIR)/%.bin.o) - ifeq ($(MACOS),true) BASE_FLAGS += -DARCH_MAC else ifeq ($(WINDOWS),true) @@ -323,6 +321,11 @@ clean: # -------------------------------------------------------------- # Build commands +PLUGIN_OBJS = $(PLUGIN_FILES:%=$(BUILD_DIR)/%.o) +PLUGIN_OBJS += $(PLUGIN_BINARIES:%=$(BUILD_DIR)/%.bin.o) + +.PRECIOUS: $(PLUGIN_BINARIES:%=$(BUILD_DIR)/%.bin) + # function for custom module names macro custom_module_names = -D${1}=${2}${1} -Dmodel${1}=model${2}${1} -D${1}Widget=${2}${1}Widget custom_per_file_names = -D${1}=${2}_${1} @@ -386,6 +389,15 @@ $(BUILD_DIR)/Bidoo/%.cpp.o: Bidoo/%.cpp -DpluginInstance=pluginInstance__Bidoo \ -IBidoo/src/dep/gverb/include +$(BUILD_DIR)/Bidoo-workaround/%.cpp.o: Bidoo-workaround/%.cpp + -@mkdir -p "$(shell dirname $(BUILD_DIR)/$<)" + @echo "Compiling $<" + $(SILENT)$(CXX) $< $(BUILD_CXX_FLAGS) -c -o $@ \ + $(foreach m,$(BIDOO_CUSTOM),$(call custom_module_names,$(m),Bidoo)) \ + $(foreach m,$(BIDOO_CUSTOM_PER_FILE),$(call custom_per_file_names,$(m),Bidoo_$(shell basename $*))) \ + -DpluginInstance=pluginInstance__Bidoo \ + -IBidoo/src/dep/gverb/include + $(BUILD_DIR)/BogaudioModules/%.cpp.o: BogaudioModules/%.cpp -@mkdir -p "$(shell dirname $(BUILD_DIR)/$<)" @echo "Compiling $<" diff --git a/plugins/plugins.cpp b/plugins/plugins.cpp index f57a8f7..df7bf86 100644 --- a/plugins/plugins.cpp +++ b/plugins/plugins.cpp @@ -790,8 +790,7 @@ static void initStatic__cf() p->addModel(modelL3DS3Q); p->addModel(modelSLIDERSEQ); p->addModel(modelPLAYER); - // TODO deal with drwav in multiple projects - // p->addModel(modelPLAY); + p->addModel(modelPLAY); p->addModel(modelMONO); p->addModel(modelSTEREO); p->addModel(modelSUB);