add ability to save samples
This commit is contained in:
parent
a2357df8bb
commit
4de343eea7
|
@ -1,4 +1,48 @@
|
||||||
#include "sample.h"
|
#include "sample.h"
|
||||||
|
#include "../ta-log.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <sndfile.h>
|
||||||
|
|
||||||
|
const int sampleRates[6]={
|
||||||
|
4000, 8000, 11025, 16000, 22050, 32000
|
||||||
|
};
|
||||||
|
|
||||||
|
bool DivSample::save(const char* path) {
|
||||||
|
SNDFILE* f;
|
||||||
|
SF_INFO si;
|
||||||
|
memset(&si,0,sizeof(SF_INFO));
|
||||||
|
|
||||||
|
if (length<1) return false;
|
||||||
|
|
||||||
|
si.channels=1;
|
||||||
|
si.samplerate=sampleRates[rate];
|
||||||
|
if (depth==16) {
|
||||||
|
si.format=SF_FORMAT_PCM_16|SF_FORMAT_WAV;
|
||||||
|
} else {
|
||||||
|
si.format=SF_FORMAT_PCM_U8|SF_FORMAT_WAV;
|
||||||
|
}
|
||||||
|
|
||||||
|
f=sf_open(path,SFM_WRITE,&si);
|
||||||
|
|
||||||
|
if (f==NULL) {
|
||||||
|
logE("could not open wave file for saving! %s\n",sf_error_number(sf_error(f)));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (depth==16) {
|
||||||
|
sf_writef_short(f,data,length);
|
||||||
|
} else {
|
||||||
|
short* cbuf=new short[length];
|
||||||
|
for (int i=0; i<length; i++) {
|
||||||
|
cbuf[i]=(data[i]<<8)^0x8000;
|
||||||
|
}
|
||||||
|
sf_writef_short(f,cbuf,length);
|
||||||
|
delete[] cbuf;
|
||||||
|
}
|
||||||
|
sf_close(f);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
DivSample::~DivSample() {
|
DivSample::~DivSample() {
|
||||||
if (data) delete[] data;
|
if (data) delete[] data;
|
||||||
|
|
|
@ -10,6 +10,7 @@ struct DivSample {
|
||||||
short* rendData;
|
short* rendData;
|
||||||
unsigned char* adpcmRendData;
|
unsigned char* adpcmRendData;
|
||||||
|
|
||||||
|
bool save(const char* path);
|
||||||
DivSample():
|
DivSample():
|
||||||
name(""),
|
name(""),
|
||||||
length(0),
|
length(0),
|
||||||
|
|
|
@ -676,7 +676,7 @@ void FurnaceGUI::drawSampleList() {
|
||||||
}
|
}
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
if (ImGui::Button("Save##SampleSave")) {
|
if (ImGui::Button("Save##SampleSave")) {
|
||||||
openFileDialog(GUI_FILE_SAMPLE_SAVE);
|
if (curSample>=0 && curSample<(int)e->song.sample.size()) openFileDialog(GUI_FILE_SAMPLE_SAVE);
|
||||||
}
|
}
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
if (ImGui::ArrowButton("SampleUp",ImGuiDir_Up)) {
|
if (ImGui::ArrowButton("SampleUp",ImGuiDir_Up)) {
|
||||||
|
@ -1459,6 +1459,9 @@ bool FurnaceGUI::loop() {
|
||||||
e->addSampleFromFile(copyOfName.c_str());
|
e->addSampleFromFile(copyOfName.c_str());
|
||||||
break;
|
break;
|
||||||
case GUI_FILE_SAMPLE_SAVE:
|
case GUI_FILE_SAMPLE_SAVE:
|
||||||
|
if (curSample>=0 && curSample<(int)e->song.sample.size()) {
|
||||||
|
e->song.sample[curSample]->save(copyOfName.c_str());
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
curFileDialog=GUI_FILE_OPEN;
|
curFileDialog=GUI_FILE_OPEN;
|
||||||
|
|
Loading…
Reference in a new issue