# Problem with ReadData

**URL:** <https://qa.fmod.com/t/problem-with-readdata/20031>\
**Category:** FMOD Engine\
**Tags:** cpp\
**Created:** [March 27, 2023, 11:43pm UTC](https://qa.fmod.com/t/problem-with-readdata/20031 "2023-03-27T23:43:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndreaGiaccone](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@AndreaGiaccone](https://qa.fmod.com/u/AndreaGiaccone)\
**Post date:** [March 27, 2023, 11:43pm UTC](https://qa.fmod.com/t/problem-with-readdata/20031/1 "2023-03-27T23:43:10Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [March 28, 2023, 2:32am UTC](https://qa.fmod.com/t/problem-with-readdata/20031/2 "2023-03-28T02:32:41Z")

</div>

Hi,

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

---

<div class="post-metadata">

**Author:** ![AndreaGiaccone](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@AndreaGiaccone](https://qa.fmod.com/u/AndreaGiaccone)\
**Post date:** [March 28, 2023, 8:11am UTC](https://qa.fmod.com/t/problem-with-readdata/20031/3 "2023-03-28T08:11:07Z")

</div>

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](https://www.fmod.com/docs/2.02/api/glossary.html#reading-sound-data)

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [March 28, 2023, 10:33pm UTC](https://qa.fmod.com/t/problem-with-readdata/20031/4 "2023-03-28T22:33:09Z")

</div>

Hi, thank you for sharing that.

Could you elaborate on:

> [@AndreaGiaccone](#):
>
> does not seem to pass the data in the buffer correctly

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

---

<div class="post-metadata">

**Author:** ![AndreaGiaccone](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@AndreaGiaccone](https://qa.fmod.com/u/AndreaGiaccone)\
**Post date:** [March 28, 2023, 11:13pm UTC](https://qa.fmod.com/t/problem-with-readdata/20031/5 "2023-03-28T23:13:33Z")

</div>

> [@Connor\_FMOD](#):
>
> Could you elaborate on:
> 
> > [@AndreaGiaccone](#):
> >
> > does not seem to pass the data in the buffer correctly

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

 ![Buffer 2](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/f/ff76f2eea64ebb5b2839da835d43718372eee34f.jpeg)  
 ![Buffer](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/c/c21e7e2db99ab54b97c0ee3b2fa8fdf86bcb2252.jpeg)

> [@Connor\_FMOD](#):
>
> What are the error messages you are seeing in your `check()` ?

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

 ![Error message](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/b/b95ae364c7c90b7ac68d6faf2935faa03c653fbe.jpeg)

[image]

Thanks for helping!

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [March 31, 2023, 3:09am UTC](https://qa.fmod.com/t/problem-with-readdata/20031/6 "2023-03-31T03:09:38Z")

</div>

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.

```auto
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](https://fmod.com/docs/2.02/api/core-api-common.html#fmod_mode)

- `FMOD_OPENMEMORY` - needs `length`, retrieved using [sound-\>getLength()](https://fmod.com/docs/2.02/api/core-api-system.html#system_getsoftwareformat).
- `FMOD_OPENRAW` - needs `format`, retrieved using [sound-\>getFormat()](https://fmod.com/docs/2.02/api/core-api-sound.html#sound_getformat).

Other required parameters are:

- `cbszie`, which is `sizeof(FMOD_CREATESOUNDEXINFO);`.
- `numchannels`, retrieved from [sound-\>getFormat()](https://fmod.com/docs/2.02/api/core-api-sound.html#sound_getformat).
- `defaultfrequency`, retrieved from [system-\>getSoftwareFormat()](https://fmod.com/docs/2.02/api/core-api-system.html#system_getsoftwareformat).

So our `FMOD_CREATESOUNDEXINFO` should look like this:

```cpp
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!
