# I don’t understand how to create a sound from an array of floats

**URL:** <https://qa.fmod.com/t/i-don-t-understand-how-to-create-a-sound-from-an-array-of-floats/20291>\
**Category:** FMOD Engine\
**Tags:** cpp\
**Created:** [June 6, 2023, 11:11pm UTC](https://qa.fmod.com/t/i-don-t-understand-how-to-create-a-sound-from-an-array-of-floats/20291 "2023-06-06T23:11:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pdJeeves](https://avatars.discourse-cdn.com/v4/letter/p/848f3c/32.png) [@pdJeeves](https://qa.fmod.com/u/pdJeeves)\
**Post date:** [June 6, 2023, 11:11pm UTC](https://qa.fmod.com/t/i-don-t-understand-how-to-create-a-sound-from-an-array-of-floats/20291/1 "2023-06-06T23:11:36Z")

</div>

I’ve been looking through the docs trying to figure this out; but I’m just not getting it.

I’ve made a sound with a compute shader, so i have a length, a sample rate, and floats representing amplitude, I want to create an empty sound so i can copy the samples from gpu memory into the sound’s buffer. How do i do this thing?

I’m thinking maybe i create sound with FMOD\_OPENMEMORY, but i’m unsure if that wants the samples to have a header, (it says PCM which seems to be a .wav file), or if it just wants the kind of raw data i have.

Although, last time i used FMOD\_OPENMEMORY i had a flac file from offset X to offset Y in a larger package file; and i wanted to play it, and that didn’t seem to be possible. So additonally if there’s a way to do that i would like to know.

---

<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:** [June 7, 2023, 5:18am UTC](https://qa.fmod.com/t/i-don-t-understand-how-to-create-a-sound-from-an-array-of-floats/20291/2 "2023-06-07T05:18:44Z")

</div>

Hi,

I will link to our Unity documentation that “demonstrates how to capture audio data from a VideoPlayer using Unity’s AudioSampleProvider and play it back through an FMOD.Sound” here: [Unity Integration | Scripting Examples - Video Playback](https://fmod.com/docs/2.02/unity/examples-video-playback.html).

The main focus is on the update function with is using `Sound.lock()` and `Sound.unlock()` to copy the sample data to the sound. These functions are outlined under: [FMOD API | Core API Reference - Sound](https://fmod.com/docs/2.02/api/core-api-sound.html#sound_lock).

Hope this helps!

---

<div class="post-metadata">

**Author:** ![pdJeeves](https://avatars.discourse-cdn.com/v4/letter/p/848f3c/32.png) [@pdJeeves](https://qa.fmod.com/u/pdJeeves)\
**Post date:** [June 7, 2023, 1:26pm UTC](https://qa.fmod.com/t/i-don-t-understand-how-to-create-a-sound-from-an-array-of-floats/20291/3 "2023-06-07T13:26:17Z")

</div>

I tried just copying it into a vector to avoid the lock/unlock for now; but i get:

FMOD Warning: Unsupported file or audio format.

```
struct DynamicSound
{
	FMOD::Sound * _sound{};
	std::vector<float> _samples;
};

/// elsewhere
FMOD_CREATESOUNDEXINFO info;
memset(&info, 0, sizeof(info));
info.cbsize = sizeof(info);
info.length = sound->_samples.size() * sizeof(sound->_samples[0]);
info.numchannels = 1;
info.defaultfrequency = 44100;
info.format = FMOD_SOUND_FORMAT_PCMFLOAT;
info.suggestedsoundtype = FMOD_SOUND_TYPE_RAW;

CheckFMODResult(getFMODSystem()->createSound(
	(const char*)sound->_samples.data(),
	FMOD_OPENMEMORY_POINT,
	&info,
	&sound->_sound));

```

---

<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:** [June 8, 2023, 1:46am UTC](https://qa.fmod.com/t/i-don-t-understand-how-to-create-a-sound-from-an-array-of-floats/20291/4 "2023-06-08T01:46:33Z")

</div>

Hi,

The issue is how you are initializing the `FMOD::Sound`, try

```auto
struct DynamicSound
{
	FMOD::Sound * _sound;
	std::vector<float> _samples;
};

```

There are examples included with the FMOD Engine: `"C:\Program Files (x86)\FMOD SoundSystem\FMOD Studio API Windows\api\core\examples\vs2019\examples.sln"`. They may be useful reference.

Hope this helps!
