Problem with ReadData

Hello everyone!
I’m attempting to develop a granular synthesizer for the procedural sound generation software I’m working on (i’m using FMOD Core library). I need to copy the contents of an audio file into an array to be able to extract grains and play them separately with createSound and playSound, but the example code I found in the glossary does not seem to pass the data in the buffer correctly. Could anyone help me? This is a simple test code I’m using:

#include “iostream”
#include “fmod.hpp”
#include “fmod_errors.h”

int check(FMOD_RESULT result) {
if (result != FMOD_OK)
{
std::cout << “FMOD error!\n” << result << " " << FMOD_ErrorString(result) << std::endl;
exit(-1);
}
else {
return 0;
}
}

int main() {
FMOD::System* system;
FMOD::System_Create(&system);
check(system->init(128, FMOD_INIT_NORMAL, 0));

FMOD::Sound* sound;
unsigned int length;
char* buffer;

check(system->createSound("Sounds/tinfoil.wav", FMOD_DEFAULT | FMOD_OPENONLY, nullptr, &sound));
check(sound->getLength(&length, FMOD_TIMEUNIT_RAWBYTES));

buffer = new char[length];
check(sound->readData(buffer, length, nullptr));

check(sound->release());

FMOD_CREATESOUNDEXINFO info;
info.length = length;
FMOD::Sound* copy;
check(system->createSound(buffer, FMOD_OPENMEMORY, &info, &copy));

FMOD::Channel* channel;
check(system->playSound(copy, NULL, false, &channel));

int exit = 1;

while (exit != 0) {
	std::cin >> exit;
}

check(copy->release());

delete[] buffer;

check(system->close());
check(system->release());

}

Thanks for your attention!

Hi,

What version of FMOD are you using and where did you find the example code?

Hi Connor,
i’m using version 2.02 of FMOD API, here is the example:
https://www.fmod.com/docs/2.02/api/glossary.html#reading-sound-data

Hi, thank you for sharing that.

Could you elaborate on:

What are the error messages you are seeing in your check()?

I used breakpoints for debugging and that is what the buffer gets in ReadData (sorry for using italian language on Visual Studio):


I see an error message when I use buffer in createSound:

[image]

Thanks for helping!

Hi,

Thanks for the screenshots, and absolutely no worries for the Italian!

I have made a note that there isn’t much information on this topic in our documentation.

There are a few more FMOD_MODE flags required so that FMOD makes the sound using the provided data rather than looking for a file.

system->createSound(buffer, FMOD_OPENMEMORY | FMOD_OPENRAW | FMOD_CREATESAMPLE, &info, &copy);

Some of these flags have FMOD_CREATESOUNDEXINFO components that need to be set to allow for the successful creation of the sound. A full list of the FMOD_MODE can be found under FMOD API | Core API Reference - Common

Other required parameters are:

So our FMOD_CREATESOUNDEXINFO should look like this:

FMOD_SOUND_TYPE type;
FMOD_SOUND_FORMAT format;
int channels;
sound->getFormat(&type, &format, &channels, 0);
int freq = 0;
system->getSoftwareFormat(&freq, 0, 0);

FMOD_CREATESOUNDEXINFO info = FMOD_CREATESOUNDEXINFO();
info.length = length;
info.cbsize = sizeof(FMOD_CREATESOUNDEXINFO);
info.format = format;
info.numchannels = channels;
info.defaultfrequency = freq;

Now we have all the requirements met for our flags you should be able to play the sound retrieved from readData()

Hope this helps!