Skip to content

Commit

Permalink
Improve sound loading
Browse files Browse the repository at this point in the history
  • Loading branch information
izzyreal committed Nov 22, 2022
1 parent 8c0748e commit af2c8d9
Show file tree
Hide file tree
Showing 25 changed files with 162 additions and 270 deletions.
10 changes: 4 additions & 6 deletions src/main/disk/AbstractDisk.cpp
Expand Up @@ -428,10 +428,9 @@ wav_or_error AbstractDisk::readWavMeta(std::shared_ptr<MpcFile> f)
return tl::make_unexpected(mpc_io_error{"Could not read WAV file: " + msg});
}

sound_or_error AbstractDisk::readWav2(std::shared_ptr<MpcFile> f, bool shouldBeConverted)
sound_or_error AbstractDisk::readWav2(std::shared_ptr<MpcFile> f, std::shared_ptr<mpc::sampler::Sound> sound, bool shouldBeConverted)
{
auto sampler = mpc.getSampler();
auto sound = sampler->addSound();
std::string msg;

try {
Expand Down Expand Up @@ -526,17 +525,16 @@ sound_or_error AbstractDisk::readWav2(std::shared_ptr<MpcFile> f, bool shouldBeC
return tl::make_unexpected(mpc_io_error{"Could not read WAV file: " + msg});
}

sound_or_error AbstractDisk::readSnd2(std::shared_ptr<MpcFile> f)
sound_or_error AbstractDisk::readSnd2(std::shared_ptr<MpcFile> f, std::shared_ptr<mpc::sampler::Sound> sound)
{
auto sound = mpc.getSampler()->addSound();
std::string msg;

try {
try
{
SndReader sndReader(f.get());

if (sndReader.isHeaderValid())
{

sndReader.readData(*sound->getSampleData());
sound->setMono(sndReader.isMono());
sound->setStart(sndReader.getStart());
Expand Down
4 changes: 2 additions & 2 deletions src/main/disk/AbstractDisk.hpp
Expand Up @@ -99,8 +99,8 @@ class AbstractDisk
friend class mpc::lcdgui::screens::window::Mpc2000XlAllFileScreen;

wav_or_error readWavMeta(std::shared_ptr<MpcFile>);
sound_or_error readWav2(std::shared_ptr<MpcFile>, bool shouldBeConverted);
sound_or_error readSnd2(std::shared_ptr<MpcFile>);
sound_or_error readWav2(std::shared_ptr<MpcFile>, std::shared_ptr<mpc::sampler::Sound>, bool shouldBeConverted);
sound_or_error readSnd2(std::shared_ptr<MpcFile>, std::shared_ptr<mpc::sampler::Sound>);
sequence_or_error readMid2(std::shared_ptr<MpcFile>);
void readPgm2(std::shared_ptr<MpcFile>);
void readAps2(std::shared_ptr<MpcFile>, std::function<void()> on_success);
Expand Down
27 changes: 18 additions & 9 deletions src/main/disk/ApsLoader.cpp
Expand Up @@ -111,14 +111,14 @@ void ApsLoader::loadFromParsedAps(ApsParser& apsParser, mpc::Mpc& mpc, bool head
skipCount++;

if (!headless)
ApsLoader::handleSoundNotFound(mpc, soundFileName, ext);
ApsLoader::handleSoundNotFound(mpc, soundFileName);

continue;
}

finalSoundIndices[i] = i - skipCount;

ApsLoader::loadSound(mpc, soundFileName, ext, soundFile, false, i, headless);
ApsLoader::loadSound(mpc, soundFileName, ext, soundFile, headless);
}
}

Expand All @@ -133,7 +133,6 @@ void ApsLoader::loadFromParsedAps(ApsParser& apsParser, mpc::Mpc& mpc, bool head

for (int noteIndex = 0; noteIndex < 64; noteIndex++)
{
int padnn = assignTable[noteIndex];
newProgram->getPad(noteIndex)->setNote(assignTable[noteIndex]);

auto sourceStereoMixerChannel = apsProgram->getStereoMixerChannel(noteIndex);
Expand All @@ -152,13 +151,17 @@ void ApsLoader::loadFromParsedAps(ApsParser& apsParser, mpc::Mpc& mpc, bool head

auto srcNoteParams = apsProgram->getNoteParameters(noteIndex);

auto soundIndex = srcNoteParams->getSoundNumber();
auto soundIndex = srcNoteParams->getSoundIndex();

if (soundIndex != -1 && finalSoundIndices.find(soundIndex) != end(finalSoundIndices))
{
soundIndex = finalSoundIndices[soundIndex];
}

if (find(begin(unavailableSoundIndices), end(unavailableSoundIndices), srcNoteParams->getSoundNumber()) != end(unavailableSoundIndices))
if (find(begin(unavailableSoundIndices), end(unavailableSoundIndices), soundIndex) != end(unavailableSoundIndices))
{
soundIndex = -1;
}

destNoteParams->setSoundIndex(soundIndex);
destNoteParams->setTune(srcNoteParams->getTune());
Expand Down Expand Up @@ -243,11 +246,10 @@ void ApsLoader::loadSound(mpc::Mpc& mpc,
std::string soundFileName,
std::string ext,
std::weak_ptr<MpcFile> _soundFile,
bool replace,
int loadSoundIndex,
bool headless)
{
auto soundFile = _soundFile.lock();
const auto replace = false;
SoundLoader soundLoader(mpc, mpc.getSampler()->getSounds(), replace);
soundLoader.setPartOfProgram(true);

Expand All @@ -256,7 +258,14 @@ void ApsLoader::loadSound(mpc::Mpc& mpc,

SoundLoaderResult result;
bool shouldBeConverted = false;
soundLoader.loadSound(soundFile, result, shouldBeConverted);
auto sound = mpc.getSampler()->addSound();

soundLoader.loadSound(soundFile, result, sound, shouldBeConverted);

if (!result.success)
{
mpc.getSampler()->deleteSound(sound);
}
}

void ApsLoader::showPopup(mpc::Mpc& mpc, std::string name, std::string ext, int sampleSize)
Expand All @@ -276,7 +285,7 @@ void ApsLoader::showPopup(mpc::Mpc& mpc, std::string name, std::string ext, int
}
}

void ApsLoader::handleSoundNotFound(mpc::Mpc& mpc, std::string soundFileName, std::string ext)
void ApsLoader::handleSoundNotFound(mpc::Mpc &mpc, std::string soundFileName)
{
auto cantFindFileScreen = mpc.screens->get<CantFindFileScreen>("cant-find-file");
auto skipAll = cantFindFileScreen->skipAll;
Expand Down
4 changes: 2 additions & 2 deletions src/main/disk/ApsLoader.hpp
Expand Up @@ -17,8 +17,8 @@ class ApsLoader
static void loadFromParsedAps(mpc::file::aps::ApsParser&, mpc::Mpc&, bool withoutSounds, bool headless = false);

private:
static void handleSoundNotFound(mpc::Mpc&, std::string soundFileName, std::string ext);
static void loadSound(mpc::Mpc&, std::string soundFileName, std::string ext, std::weak_ptr<MpcFile> soundFile, bool replace, int loadSoundIndex, bool headless = false);
static void handleSoundNotFound(mpc::Mpc &, std::string soundFileName);
static void loadSound(mpc::Mpc&, std::string soundFileName, std::string ext, std::weak_ptr<MpcFile> soundFile, bool headless = false);
static void showPopup(mpc::Mpc&, std::string name, std::string ext, int sampleSize);

};
Expand Down
69 changes: 0 additions & 69 deletions src/main/disk/ProgramImportAdapter.cpp

This file was deleted.

39 changes: 0 additions & 39 deletions src/main/disk/ProgramImportAdapter.hpp

This file was deleted.

64 changes: 33 additions & 31 deletions src/main/disk/ProgramLoader.cpp
Expand Up @@ -6,7 +6,6 @@
#include <disk/StdDisk.hpp>
#include <disk/MpcFile.hpp>
#include <disk/PgmToProgramConverter.hpp>
#include <disk/ProgramImportAdapter.hpp>
#include <disk/SoundLoader.hpp>
#include <sampler/Program.hpp>
#include <sequencer/Track.hpp>
Expand Down Expand Up @@ -86,14 +85,13 @@ std::shared_ptr<mpc::sampler::Program> ProgramLoader::loadProgram(mpc::Mpc& mpc,
{
unavailableSoundIndices.push_back(i);
skipCount++;
notFound(mpc, soundFileName, ext);
notFound(mpc, soundFileName);
continue;
}

finalSoundIndices[i] = i - skipCount;

loadSound(mpc,
soundFileName,
pgmSoundNames[i],
ext,
soundFile,
Expand All @@ -102,24 +100,30 @@ std::shared_ptr<mpc::sampler::Program> ProgramLoader::loadProgram(mpc::Mpc& mpc,
i);
}

ProgramImportAdapter adapter(mpc.getSampler(), p, soundsDestIndex, unavailableSoundIndices);

for (auto& noteParameters : p->getNotesParameters()) {

if (noteParameters->getSoundIndex() == -1) continue;

if (finalSoundIndices.find(noteParameters->getSoundIndex()) == end(finalSoundIndices)) continue;

auto finalSoundIndex = finalSoundIndices[noteParameters->getSoundIndex()];

noteParameters->setSoundIndex(finalSoundIndex);
for (auto& srcNoteParams : p->getNotesParameters())
{
auto soundIndex = srcNoteParams->getSoundIndex();

if (soundIndex != -1 && finalSoundIndices.find(soundIndex) != end(finalSoundIndices))
{
soundIndex = finalSoundIndices[soundIndex];
}

if (find(begin(unavailableSoundIndices), end(unavailableSoundIndices), soundIndex) != end(unavailableSoundIndices))
{
soundIndex = -1;
}

srcNoteParams->setSoundIndex(soundIndex);
}

auto track = mpc.getSequencer()->getActiveTrack();
auto loadAProgramScreen = mpc.screens->get<LoadAProgramScreen>("load-a-program");

if (!loadAProgramScreen->clearProgramWhenLoading)
{
mpc.getDrum(track->getBus() - 1)->setProgram(mpc.getSampler()->getProgramCount() - 1);
}

mpc.getLayeredScreen()->openScreen("load");
}
Expand All @@ -135,30 +139,28 @@ std::shared_ptr<mpc::sampler::Program> ProgramLoader::loadProgram(mpc::Mpc& mpc,
return p;
}

void ProgramLoader::loadSound(mpc::Mpc& mpc, const std::string& soundFileName, const std::string& soundName,
const std::string& ext, std::shared_ptr<MpcFile> soundFile, std::vector<int>* soundsDestIndex,
const bool replace, const int loadSoundIndex)
void ProgramLoader::loadSound(mpc::Mpc &mpc, const std::string &soundName, const std::string &ext,
std::shared_ptr<MpcFile> soundFile, std::vector<int> *soundsDestIndex,
const bool replace, const int loadSoundIndex)
{
SoundLoader soundLoader(mpc, mpc.getSampler()->getSounds(), replace);
soundLoader.setPartOfProgram(true);
showPopup(mpc, soundName, ext, soundFile->length());
SoundLoaderResult soundLoaderResult;
bool shouldBeConverted = false;

try
{
soundLoader.loadSound(soundFile, soundLoaderResult, shouldBeConverted);

if (soundLoaderResult.existingIndex != -1)
(*soundsDestIndex)[loadSoundIndex] = soundLoaderResult.existingIndex;
}
catch (const std::exception& e)
auto sound = mpc.getSampler()->addSound();

soundLoader.loadSound(soundFile, soundLoaderResult, sound, shouldBeConverted);

if (!soundLoaderResult.success)
{
auto msg = std::string(e.what());
MLOG("Exception occurred in ProgramLoader::loadSound(...) -- " + msg);
MLOG(soundLoaderResult.errorMessage);
throw;
mpc.getSampler()->deleteSound(sound);
return;
}

if (soundLoaderResult.existingIndex != -1)
(*soundsDestIndex)[loadSoundIndex] = soundLoaderResult.existingIndex;
}

void ProgramLoader::showPopup(mpc::Mpc& mpc, std::string name, std::string ext, int sampleSize)
Expand All @@ -175,7 +177,7 @@ void ProgramLoader::showPopup(mpc::Mpc& mpc, std::string name, std::string ext,
std::this_thread::sleep_for(std::chrono::milliseconds((int)(sleepTime * 0.2)));
}

void ProgramLoader::notFound(mpc::Mpc& mpc, std::string soundFileName, std::string ext)
void ProgramLoader::notFound(mpc::Mpc &mpc, std::string soundFileName)
{
auto cantFindFileScreen = mpc.screens->get<CantFindFileScreen>("cant-find-file");
auto skipAll = cantFindFileScreen->skipAll;
Expand Down
6 changes: 4 additions & 2 deletions src/main/disk/ProgramLoader.hpp
Expand Up @@ -24,9 +24,11 @@ class ProgramLoader
static std::shared_ptr<mpc::sampler::Program> loadProgram(mpc::Mpc&, std::shared_ptr<mpc::disk::MpcFile>, const int replaceIndex);

private:
static void loadSound(mpc::Mpc&, const std::string& soundFileName, const std::string& soundName, const std::string& ext, std::shared_ptr<MpcFile> soundFile, std::vector<int>* soundsDestIndex, const bool replace, const int loadSoundIndex);
static void loadSound(mpc::Mpc &, const std::string &soundName, const std::string &ext,
std::shared_ptr<MpcFile> soundFile, std::vector<int> *soundsDestIndex, const bool replace,
const int loadSoundIndex);
static void showPopup(mpc::Mpc&, std::string name, std::string ext, int sampleSize);
static void notFound(mpc::Mpc&, std::string soundFileName, std::string ext);
static void notFound(mpc::Mpc &, std::string soundFileName);

};
}

0 comments on commit af2c8d9

Please sign in to comment.