# Programmer sound c++ examples

**URL:** https://qa.fmod.com/t/programmer-sound-c-examples/17403
**Category:** FMOD Engine
**Tags:** cpp
**Created:** [July 4, 2021, 6:30pm UTC](https://qa.fmod.com/t/programmer-sound-c-examples/17403 "2021-07-04T18:30:35Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![rontt](https://avatars.discourse-cdn.com/v4/letter/r/779978/32.png) [@rontt](https://qa.fmod.com/u/rontt)
#### Post date: [July 4, 2021, 6:30pm UTC](https://qa.fmod.com/t/programmer-sound-c-examples/17403/1 "2021-07-04T18:30:35Z")

</div>

I’m implementing vo for our game (9000+ lines) and and setting up programmer sounds. I have it all set up in FMOD studio to build vo banks, etc. I keep reading about some example code in the docs but am unable to actually find it. I am using the c++ API (not Unity/UE). Can someone point me to some c++ sample code. I’m not sure how to trigger a sound for a given line of dialog and how to then play the sound in the vo bank.

---

<div class="post-metadata">

### Author: ![richard\_simms](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/richard_simms/32/261_2.png) [@richard\_simms](https://qa.fmod.com/u/richard_simms)
#### Post date: [July 4, 2021, 11:13pm UTC](https://qa.fmod.com/t/programmer-sound-c-examples/17403/2 "2021-07-04T23:13:13Z")

</div>

There aren’t any examples for C++ in out online documents, which I will put a note in our tracker to look into for a future release. There is a C++ example included in the FMOD Studio API installation folders, which I will upload here for your convenience.

If you have any questions please don’t hesitate to ask.

[programmer\_sound.txt](https://qa.fmod.com/uploads/short-url/b3oo2K5S0Eryf6ioq205hS6Wdbh.txt) (6.8 KB)

---

<div class="post-metadata">

### Author: ![rontt](https://avatars.discourse-cdn.com/v4/letter/r/779978/32.png) [@rontt](https://qa.fmod.com/u/rontt)
#### Post date: [July 5, 2021, 3:47pm UTC](https://qa.fmod.com/t/programmer-sound-c-examples/17403/3 "2021-07-05T15:47:43Z")

</div>

This should be enough for me to get it going. Thanks.

---

<div class="post-metadata">

### Author: ![rontt](https://avatars.discourse-cdn.com/v4/letter/r/779978/32.png) [@rontt](https://qa.fmod.com/u/rontt)
#### Post date: [July 24, 2021, 7:04pm UTC](https://qa.fmod.com/t/programmer-sound-c-examples/17403/4 "2021-07-24T19:04:43Z")

</div>

Having some problems getting programmer sounds working using the C api. I trigger the sound and I don’t get the callback (but there are no errors). If I trigger it again, I get the callback but the userInfo() doesn’t contain a valid ProgrammerSoundContext \*context (it’s all null).

To start the sound, I have this code:

```auto
int playDialog(const char *dialog_id) {
    ProgrammerSoundContext programmerSoundContext;
    programmerSoundContext.system = _fmod_studio_system;
    programmerSoundContext.coreSystem = _fmod_system;
    programmerSoundContext.dialogueString = dialog_id;

	FMOD_STUDIO_EVENTDESCRIPTION* event_desc = NULL;
	FMOD_STUDIO_EVENTINSTANCE* event_instance = NULL;

	if (FMOD_ERROR(FMOD_Studio_System_GetEvent(_fmod_studio_system, "event:/vo", &event_desc), dialog_id)) {
		return 0;
	}
	if (FMOD_ERROR(FMOD_Studio_EventDescription_CreateInstance(event_desc, &event_instance))) {
		return 0;
	}
	if (FMOD_ERROR(FMOD_Studio_EventInstance_SetUserData(event_instance, &programmerSoundContext))) {
		return 0;
	}
	if (FMOD_ERROR(FMOD_Studio_EventDescription_SetCallback(event_desc, _programmer_sound_callback, FMOD_STUDIO_EVENT_CALLBACK_CREATE_PROGRAMMER_SOUND | FMOD_STUDIO_EVENT_CALLBACK_DESTROY_PROGRAMMER_SOUND))) {
		return 0;
	}
	if (FMOD_ERROR(FMOD_Studio_EventInstance_Start(event_instance))) {
		return 0;
	}
	if (FMOD_ERROR(FMOD_Studio_EventInstance_Release(event_instance))) {
		return 0;
	}
    return 1;
}

```

The programmer sound `event:/vo` is in the dialog\_en.bank, I assume this is correct. It wasn’t clear where to put it.

---

<div class="post-metadata">

### Author: ![richard\_simms](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/richard_simms/32/261_2.png) [@richard\_simms](https://qa.fmod.com/u/richard_simms)
#### Post date: [July 26, 2021, 1:39am UTC](https://qa.fmod.com/t/programmer-sound-c-examples/17403/5 "2021-07-26T01:39:08Z")

</div>

It looks like you are setting the callback on the `EventDescription`, which will only affect instances created in the future. To fix this you will either need to set the callback before the event instance is created, or you can change this to `EventInstance_SetCallback()`.

Also, I’m not sure about the `userInfo()` being null? Are you expecting the `void *parameters` argument of the callback function to contain the userdata? To get the userdata, you’ll need to call `FMOD_Studio_EventInstance_GetUserData()`.

---

<div class="post-metadata">

### Author: ![rontt](https://avatars.discourse-cdn.com/v4/letter/r/779978/32.png) [@rontt](https://qa.fmod.com/u/rontt)
#### Post date: [July 26, 2021, 8:52pm UTC](https://qa.fmod.com/t/programmer-sound-c-examples/17403/6 "2021-07-26T20:52:22Z")

</div>

I figure out the issue with the callback. If seems you call FMOD\_Studio\_EventInstance\_SetUserData the user data is not copied and since the programmerSoundContext was a local var it lost scope for the call back. Easy fix and now it’s working.

---

<div class="post-metadata">

### Author: ![rontt](https://avatars.discourse-cdn.com/v4/letter/r/779978/32.png) [@rontt](https://qa.fmod.com/u/rontt)
#### Post date: [July 27, 2021, 1:51am UTC](https://qa.fmod.com/t/programmer-sound-c-examples/17403/7 "2021-07-27T01:51:02Z")

</div>

One more question:

When I get a callback for `FMOD_STUDIO_EVENT_CALLBACK_DESTROY_PROGRAMMER_SOUND` I need to know the event instance but the event passed in is not the original event instance so it doesn’t have the userInfo anymore. Is there a way to attach useInfo to the sound created with `FMOD_System_CreateSound`? I need to track some information through to the destroy.

---

<div class="post-metadata">

### Author: ![rontt](https://avatars.discourse-cdn.com/v4/letter/r/779978/32.png) [@rontt](https://qa.fmod.com/u/rontt)
#### Post date: [July 27, 2021, 12:39pm UTC](https://qa.fmod.com/t/programmer-sound-c-examples/17403/8 "2021-07-27T12:39:21Z")

</div>

Never mind, I figured it out. Error on my part.
