Sorry to (probably) ask a stupid question, but I was trying to use a file as a sample to replace the binary stream stored (by what software btw ?) in BinaryData.cpp as a copy of a cello.wav file in the JuceDemo project.
I thus replaced the original:
ScopedPointer<AudioFormatReader> audioReader (wavFormat.createReaderFor (new MemoryInputStream (BinaryData::cello_wav, BinaryData::cello_wavSize, false),true));
with something like (don't worry this is not a final code, just testing...):
FILE *wavsmpl1;
void *buffer;
size_t fileSize;
wavsmpl1 = fopen("cello.wav","r"); // cello.wav is in JuceDemo.app folder for testing purpose
fileSize = ftell(wavsmpl1);
buffer = malloc(fileSize);
fread(buffer,1,fileSize,wavsmpl1);
ScopedPointer<AudioFormatReader> audioReader (wavFormat.createReaderFor (new MemoryInputStream (buffer,fileSize,true),true));
but I get 0 in audioReader.
Any hint ? This is done on a Mac for the MacOS platform but I am assuming that does not make a difference.
Call me stupid indeed... Forgot to move pointer to end of file before calling ftell. I was too much focusing on the JUCE API as I thought I just misunderstood something there.
So the working code (can't say it's "correct" as it is just for testing purpose) is:
FILE *wavsmpl1; void *buffer;
size_t fileSize;
wavsmpl1 = fopen("cello.wav","r"); // cello.wav is in JuceDemo.app folder for testing purpose
fseek(wavsmpl1, 0, SEEK_END);
fileSize = ftell(wavsmpl1);
buffer = malloc(fileSize);
rewind(wavsmpl1);
fread(buffer,1,fileSize,wavsmpl1);
ScopedPointer<AudioFormatReader> audioReader (wavFormat.createReaderFor (new MemoryInputStream (buffer,fileSize,true),true));
The correct way to do it probably is to use a structure with sample size info read and stored once for all. Or alternatively to set keepInternalCopyOfData to true (as I did in code above).
// ScopedPointer<AudioFormatReader> audioReader (wavFormat.createReaderFor(new MemoryInputStream (BinaryData::cello_wav,
// BinaryData::cello_wavSize,
// false),true));
FileInputStream* cello = new FileInputStream (File ("./cello.wav"));
if (cello->openedOk()) {
ScopedPointer<AudioFormatReader> audioReader (wavFormat.createReaderFor (cello,true));
BigInteger allNotes;
allNotes.setRange (0, 128, true);
synth.clearSounds();
synth.addSound (new SamplerSound ("demo sound",
*audioReader,
allNotes,
74, // root midi note
0.1, // attack time
0.1, // release time
10.0 // maximum sample length
));
}
Hi I’m trying to also use a file as a synth sound sample and I tried implementing what you talked about, but it doesn’t recognize the file.
I also referred to this link: File streaming FileInputStream/FileOutputStream
File only works with absolute paths. In a GUI there are too many places, where the file could be relative to.
Best is to start with File::getSpecialLocation() and a sequence of File::getChildFile() to reach the file…