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

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.

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.

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.

Hope this helps!

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));

Hi,

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

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!