Skip to content

Instantly share code, notes, and snippets.

@Simon-L
Last active December 22, 2023 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Simon-L/aea8bbcb6cca34093f58d9fbba1f323d to your computer and use it in GitHub Desktop.
Save Simon-L/aea8bbcb6cca34093f58d9fbba1f323d to your computer and use it in GitHub Desktop.
ReloadSoundUI soundinterface;
soundinterface.printSoundfilesInfos();
auto sf = soundinterface.createNewSoundfile("/path/to/newfile.wav");
soundinterface.updateSoundfile("sflabel", sf);
soundinterface.printSoundfilesInfos();
// ReloadSoundUI keeps a map that associates labels used in dsp code
// with pointers to Soundfile zones so that they can be reloaded
// while the DSP is running
class ReloadSoundUI : public SoundUI
{
std::map<std::string, Soundfile**> fSfZoneMap;
public:
ReloadSoundUI(const std::string& sound_directory = "", int sample_rate = -1, SoundfileReader* reader = nullptr, bool is_double = false)
: SoundUI(sound_directory, sample_rate, reader, is_double)
{
}
~ReloadSoundUI() {}
virtual void addSoundfile(const char* label, const char* url, Soundfile** sf_zone)
{
const char* saved_url = url; // 'url' is consumed by parseMenuList2
std::vector<std::string> file_name_list;
bool menu = parseMenuList2(url, file_name_list, true);
// If not a list, we have as single file
if (!menu) { file_name_list.push_back(saved_url); }
// Parse the possible list
std::string saved_url_real = std::string(saved_url) + "_" + std::to_string(fIsDouble); // fIsDouble is used in the key
if (fSoundfileMap.find(saved_url_real) == fSoundfileMap.end()) {
// Check all files and get their complete path
std::vector<std::string> path_name_list = fSoundReader->checkFiles(fSoundfileDir, file_name_list);
if (path_name_list.size() > 1) {
std::cerr << "addSoundfile : ReloadSoundUI only supports one file! " << path_name_list.size() << " files provided." << std::endl;
*sf_zone = defaultsound;
return;
}
// Read them and create the Soundfile
Soundfile* sound_file = fSoundReader->createSoundfile(path_name_list, MAX_CHAN, fIsDouble);
if (sound_file) {
fSoundfileMap[saved_url_real] = std::shared_ptr<Soundfile>(sound_file);
} else {
// If failure, use 'defaultsound'
std::cerr << "addSoundfile : soundfile for " << saved_url << " cannot be created !" << std::endl;
*sf_zone = defaultsound;
return;
}
}
// Keep a map of label to sf_zone to access it later
fSfZoneMap[label] = sf_zone;
// Get the soundfile pointer
*sf_zone = fSoundfileMap[saved_url_real].get();
}
void printSoundfilesInfos()
{
for (const auto &item : fSfZoneMap) {
auto sf = *(item.second);
std::cout << item.first << ": Length: " << sf->fLength[0] << " SR: " << sf->fSR[0] << " Channels: " << sf->fChannels << '\n';
}
}
Soundfile* createNewSoundfile(std::string path)
{
std::vector<std::string> path_name_list;
path_name_list.push_back(path);
std::string saved_url_real = std::string(path) + "_" + std::to_string(fIsDouble); // fIsDouble is used in the key
Soundfile* sound_file = fSoundReader->createSoundfile(path_name_list, MAX_CHAN, fIsDouble);
if (sound_file) {
fSoundfileMap[saved_url_real] = std::shared_ptr<Soundfile>(sound_file);
} else {
// If failure, use 'defaultsound'
std::cerr << "addSoundfile : soundfile for " << path << " cannot be created !" << std::endl;
return defaultsound;
}
return sound_file;
}
Soundfile* updateSoundfile(std::string label, Soundfile* newSoundfile)
{
if (fSfZoneMap.find(label) == fSfZoneMap.end()) {
return nullptr;
}
auto oldSoundfile = fSfZoneMap[label];
*(fSfZoneMap[label]) = newSoundfile;
return *oldSoundfile;
}
};
import("stdfaust.lib");
mysf(s) = 0, s : soundfile("sflabel[url:/path/to/file.wav]", 2);
sf_l(s) = mysf(s) : !,!,_,!;
sf_r(s) = mysf(s) : !,!,!,_;
sf_len = mysf(0) : _,!,!,!;
sf_sr = mysf(0) : !,_,!,!;
process = it.raise_modulo(0, 1, sf_len) : _,! <: sf_l(_), sf_r(_);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment