# Questions about MakeCode C++ sound

**URL:** <https://forum.makecode.com/t/questions-about-makecode-c-sound/42721>\
**Category:** Help\
**Created:** [March 11, 2026, 4:13pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721 "2026-03-11T16:13:27Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![VoxelMaster64](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/voxelmaster64/32/32075_2.png) [@VoxelMaster64](https://forum.makecode.com/u/VoxelMaster64)\
**Post date:** [June 5, 2026, 5:15pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/21 "2026-06-05T17:15:47Z")

</div>

@richard its been 3 days, have you seen it yet? Actually if you’ve seen a post here (other than past posts), maybe just like it?Also I’m not like deeply annoyed (so don’t feel rushed), I’m just running out of things to do.

---

<div class="post-metadata">

**Author:** ![VoxelMaster64](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/voxelmaster64/32/32075_2.png) [@VoxelMaster64](https://forum.makecode.com/u/VoxelMaster64)\
**Post date:** [June 5, 2026, 5:16pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/22 "2026-06-05T17:16:27Z")

</div>

also what’s the best way to paste large binary files into makecode without crashing or lagging the editor? The only things I can think of right now are:

- hex`…`
- Buffer.fromUTF8(…)
- “…”
- […]

but there might be better ways to do so. The file size I’m expecting is 0.4 to 0.8 MB, which is _very_ huge for makecode

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [June 5, 2026, 5:44pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/23 "2026-06-05T17:44:38Z")

</div>

hex is the way to do it. just put it in a file other than the one you’re working in, that’s what i do.

if you add a //% to your C++ function, a shim should automatically be generated for it.

however, there are restrictions:

1. you can only pass primitive types (strings, booleans, numbers), arrays of primitive types, or select “special” types like buffers and images. classes are a no-go
2. there is a maximum number of arguments. i don’t remember what it is off the top of my head

to get around the second issue, you can pack the arguments into arrays. see:

> <https://github.com/microsoft/pxt-common-packages/blob/master/libs/screen/image.ts#L189>

but of course the best choice is to just not have that many arguments if possible.

if the shim generation doesn’t seem to be working, you can also manually shim your function like so:

> <https://github.com/microsoft/pxt-common-packages/blob/master/libs/screen/image.ts#L147>

just declare a function with the same signature in typescript and add the `//% shim="whatever"` annotation to it.

---

<div class="post-metadata">

**Author:** ![VoxelMaster64](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/voxelmaster64/32/32075_2.png) [@VoxelMaster64](https://forum.makecode.com/u/VoxelMaster64)\
**Post date:** [June 8, 2026, 5:20pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/24 "2026-06-08T17:20:02Z")

</div>

@richard how did i never see sim/music.ts???  
so if I want pcm sampling, is the process something like this:

```typescript
music.playPcm(myHexBuffer)

```

soundEffect.ts:

```typescript
namespace music {
	export function playPCM(buf: Buffer, format: PCMFormat) {
		quePCM(buf, format as number)
	}

	//% shim=music::quePCM
	function quePCM(buf: Buffer, format: number) { }
}

```

music.ts:

```typescript
namespace pxsim.music {
	function quePCM(buf: Buffer, format: number) {
		// somehow I que the instructions but I don't know how
	}
}

```

melody.cpp:

```typescript
namespace music {

//%
void WSynthesizer::fillsamples() {
	// do both pcm and normal wave filling in here. I think I fill some of an array with sound, I think it was called "d".
}

//%
void quePCM(buf: Buffer, format: number) /*runs on hardware*/ {
	// do something similar to pxsim.music.quePCM
}

}

```

I’m probably missing something or have a wrong idea

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [June 8, 2026, 5:33pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/25 "2026-06-08T17:33:31Z")

</div>

yup, that looks about right

---

<div class="post-metadata">

**Author:** ![VoxelMaster64](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/voxelmaster64/32/32075_2.png) [@VoxelMaster64](https://forum.makecode.com/u/VoxelMaster64)\
**Post date:** [June 8, 2026, 5:33pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/26 "2026-06-08T17:33:55Z")

</div>

> [@richard](#):
>
> hex is the way to do it. just put it in a file other than the one you’re working in, that’s what i do.

@richard I have done that before, ([https://arcade.makecode.com/S99039-28239-13469-64746](https://arcade.makecode.com/S99039-28239-13469-64746)) and it still crashed makecode even when inside main.ts. Maybe i should paste it last? Also, i have 286 KB of data from an opus file.

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [June 8, 2026, 5:34pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/27 "2026-06-08T17:34:25Z")

</div>

that’s way too big. i don’t think your goal should be to import entire songs, just short samples

---

<div class="post-metadata">

**Author:** ![VoxelMaster64](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/voxelmaster64/32/32075_2.png) [@VoxelMaster64](https://forum.makecode.com/u/VoxelMaster64)\
**Post date:** [June 9, 2026, 5:27pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/28 "2026-06-09T17:27:58Z")

</div>

@richard, is the sound in the browser ever being filled using c++? Or does it use typescript the whole way?

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [June 9, 2026, 6:34pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/29 "2026-06-09T18:34:09Z")

</div>

C++ never runs in the browser

---

<div class="post-metadata">

**Author:** ![VoxelMaster64](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/voxelmaster64/32/32075_2.png) [@VoxelMaster64](https://forum.makecode.com/u/VoxelMaster64)\
**Post date:** [June 11, 2026, 10:02pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/30 "2026-06-11T22:02:31Z")

</div>

@richard, inside `pxt-common-packages\libs\mixer\soundEffect.ts` i have:

```typescript
    //% shim=music::queuePCMSound
    export function queuePCMSound(buf: Buffer, sampleRate: number) { }

    //% shim=music::startPCMSoundPlayback
    export function startPCMSoundPlayback(volume: number) { }

```

and in `pxt-common-packages\libs\mixer\sim\music.ts` i have:

```typescript
    let pendingPCMBuffer: RefBuffer | undefined = undefined;
    let pendingSampleRate: number = 11025;

    export function queuePCMSound(buf: RefBuffer, sampleRate: number) {
        pendingPCMBuffer = buf;
        pendingSampleRate = sampleRate;
    }

    export function startPCMSoundPlayback(volume: number = 0.3) {
        // If nothing is in the queue, do nothing
        if (!pendingPCMBuffer) return;

        // Convert the raw bytes into an array of 16-bit signed integers
        const int16Samples = new Int16Array(pendingPCMBuffer.data.buffer);
        let position = 0;
        const chunkSize = 512;

        // Start streaming through the AudioContextManager you found!
        AudioContextManager.playPCMBufferStreamAsync(() => {
            // If we hit the end of the sound array, return undefined to stop the stream
            if (position >= int16Samples.length) {
                pendingPCMBuffer = undefined; // Clear the queue when done
                return undefined;
            }

            // Create a temporary chunk array for this audio frame
            const arr = new Float32Array(chunkSize);
            for (let i = 0; i < chunkSize; i++) {
                if (position < int16Samples.length) {
                    // Map 16-bit hardware signed integers (-32768 to 32767) to Web Audio Floats (-1.0 to 1.0)
                    arr[i] = int16Samples[position] / 32768.0;
                    position++;
                } else {
                    arr[i] = 0; // Silence padding if we run out mid-chunk
                }
            }
            return arr;
        }, pendingSampleRate, volume);
    }

```

For some reason, it is not finding the pxsim functions:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/2/7/27e87306556264c8ac7ddcb53ff2dbf4706e1a6f.png)

im guessing its not compiling `sim\music.ts` though i dont know how to make it

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [June 12, 2026, 5:15am UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/31 "2026-06-12T05:15:34Z")

</div>

ah, right, you can’t do sim work locally. the one closed-source part of pxt-arcade is the sim.

but you can fix this by moving the sim code into pxt/pxtsim/sound/

---

<div class="post-metadata">

**Author:** ![VoxelMaster64](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/voxelmaster64/32/32075_2.png) [@VoxelMaster64](https://forum.makecode.com/u/VoxelMaster64)\
**Post date:** [June 22, 2026, 5:27pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/32 "2026-06-22T17:27:46Z")

</div>

@richard if I make a change in`pxt`, would I have to run more commands than just pxt serve?

---

<div class="post-metadata">

**Author:** ![VoxelMaster64](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/voxelmaster64/32/32075_2.png) [@VoxelMaster64](https://forum.makecode.com/u/VoxelMaster64)\
**Post date:** [June 22, 2026, 5:28pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/33 "2026-06-22T17:28:00Z")

</div>

@richard if I _do_ import a whole song, would it work better if I imported the data into a github page, then load it like that? I think that would require to run the Sim inside github but idk how to do that

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [June 22, 2026, 5:30pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/34 "2026-06-22T17:30:34Z")

</div>

you need to rebuild pxt. run this command from within the pxt repo:

```auto
gulp -n

```

that should rebuild all the relevant parts.

as for your other question, not sure what you’re askin’. what do you mean by importing a song? import an MP3? import a midi file? import a makecode song?

---

<div class="post-metadata">

**Author:** ![VoxelMaster64](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/voxelmaster64/32/32075_2.png) [@VoxelMaster64](https://forum.makecode.com/u/VoxelMaster64)\
**Post date:** [June 22, 2026, 5:59pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/35 "2026-06-22T17:59:07Z")

</div>

well import an anything, like for example, a 3 MB buffer of an mp3 file which obviously would crash makecode. I would think that loading it in github would allow more memory to be loaded.

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [June 22, 2026, 9:55pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/36 "2026-06-22T21:55:16Z")

</div>

alright, there’s a lot to unpack here…

so yes, loading a 3MB file in our text editor would probably cause something to hang. it’s not designed to handle files that large, plus converting a 3 MB file into a text representation is going to make it significantly larger since text is an inefficient way to store binary data.

if you were to do this, i would add it to the project as a JRES file, which is a JSON file that you’ve probably seen in your projects where you can encode binary data in base64. you still won’t want to open that file in the text editor, but this at least means you can keep the binary data out of your source files.

to declare a buffer in a JRES file, you define it like this:

```json
{
    "*": {
        "mimeType": "image/x-mkcd-f4",
        "dataEncoding": "base64",
        "namespace": "custom"
    },
    "test": {
        "id": "custom.test",
        "data": ""
    }
}

```

note that the “\*” entry here sets the namespace to be “custom”. the actual item is declared in the “test” entry. the “id” field here is the namespace plus the name, so “custom.test”. in the actual file, you’d want to replace the “data” entry with a base64 encoded string that contains the binary data

to reference that data in your project, you declare a variable like this:

```ts
namespace custom {
    //% jres
    export const test = hex``
}

```

that hex buffer is intentionally left empty, when makecode compiles this program it will replace the contents of it with the decoded contents of the data field in that JRES file. note that the namespace and constant name have to match the values in the JRES file (“custom.test”).

so let’s do an end-to-end example real quick. say i want my JRES to include this data:

```auto
[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]

```

first, i need to convert that into a base64 encoded string. you can do this with a little snippet of javascript:

```js
// note this snippet is NOT makecode javascript, it's plain-old regular javascript
// you can run in a browser. makecode does not have a btoa function
function toBase64(values) {
    let binaryString = "";
    for (const value of values) {
        binaryString += String.fromCharCode(value);
    }
    return btoa(binaryString);
}

```

that data encoded as base64 looks like this:

```plaintext
SGVsbG8gV29ybGQ=

```

so my jres file would look like this:

```json
{
    "*": {
        "mimeType": "image/x-mkcd-f4",
        "dataEncoding": "base64",
        "namespace": "custom"
    },
    "myValue": {
        "id": "custom.myValue",
        "data": "SGVsbG8gV29ybGQ="
    }
}

```

and my typescript file looks like this:

```ts
namespace custom {
    //% jres
    export const myValue = hex``;
}

let res = "["
for (let i = 0; i < custom.myValue.length; i++) {
    res += custom.myValue[i] + ", "
}
res = res.slice(0, -2) + "]"
console.log(res) // prints [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]

```

if you try doing this in the editor, you might need to refresh the page after editing your jres file.

---

<div class="post-metadata">

**Author:** ![VoxelMaster64](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/voxelmaster64/32/32075_2.png) [@VoxelMaster64](https://forum.makecode.com/u/VoxelMaster64)\
**Post date:** [June 29, 2026, 6:07pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/37 "2026-06-29T18:07:17Z")

</div>

What command line do you use @richard?

Also, this might be interesting.

> **My Edits**
>
> `pxt-common-packages\libs\mixer\soundEffect.ts` (makecode javascript)
> 
> ```javascript
> //% shim=music::add
> function add(a: number, b: number) { }
> 
> export function addTwo(a: number, b: number) {
> if (!a) a = 0;
> if (!b) b = 0;
> 
> return add(a, b);
> }
> 
> ```
> 
> * * *
> 
> `pxt\pxtsim\sound\audioContextManager.ts` (actual sim file)
> 
> ```javascript
> export function add(a: number, b: number) {
> return a + b;
> }
> 
> ```
> 
> * * *
> 
> `pxt-common-packages\libs\mixer\melody.cpp` (just in case)
> 
> ```cpp
> //%
> int add(int a, int b) {
> return a + b;
> }
> 
> ```

I ran these:

```powershell
cd C:\Users\...\pxt
npx gulp
cd ..\pxt-arcade
pxt serve --local

```

but those still gave me `pxsim.music.add is not a function`. I decided to check the console in devTools, expected nothing, and foung an interesting error:

 ![Screenshot 2026-06-27 191900](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/9/b/9bf061ebeb14639833e812c8266a8775ae182cef.png)

expanding `DBG-MSG breakpoint` gave me:

 ![Screenshot 2026-06-27 192015](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/6/c/6c70acd8820c1663dd804b8d9016110a340a9f09.png)

and exceptionStack is:

```Debug
TypeError: pxsim.music.add is not a function
    at Object.music_addTwo__P1785 [as fn] (eval at Runtime (https://cdn.makecode.com/blob/d881286fc27642cf780785a76ca9229565860cc6/pxtsim.js:1:84787), <anonymous>:1620:22)
    at loop (https://cdn.makecode.com/blob/d881286fc27642cf780785a76ca9229565860cc6/pxtsim.js:1:82199)
    at https://cdn.makecode.com/blob/d881286fc27642cf780785a76ca9229565860cc6/pxtsim.js:1:79624

```

Gemini claims that the browser is trying to find pxsim.music.add off the public code, not my local build. If you need to I can try to debug more

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [June 29, 2026, 6:44pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/38 "2026-06-29T18:44:49Z")

</div>

i use the windows terminal with bash on WSL.

sorry, it might not be possible for you to easily make this happen given that the sim in arcade is closed source. it’s possible to workaround that limitation, but i’m afraid i probably won’t have time to coach you through it right now

---

<div class="post-metadata">

**Author:** ![VoxelMaster64](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/voxelmaster64/32/32075_2.png) [@VoxelMaster64](https://forum.makecode.com/u/VoxelMaster64)\
**Post date:** [June 30, 2026, 5:26pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/40 "2026-06-30T17:26:35Z")

</div>

okay. Please tell me when you are ready! (and also maybe an estimate when but you dont have to)  
Also, @richard, is it possible to shim a class (and/or its constructor), interface, or variable? Also, is there some way to include the pxsim code, using `/// <reference path="...">`  
(also dont feel hurried)

---

<div class="post-metadata">

**Author:** ![VoxelMaster64](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/voxelmaster64/32/32075_2.png) [@VoxelMaster64](https://forum.makecode.com/u/VoxelMaster64)\
**Post date:** [July 16, 2026, 10:28pm UTC](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721/44 "2026-07-16T22:28:01Z")

</div>

@richard is it possible to make code run line by line? When makecode compiles code, it makes a switch loop (I guess I’ll call it), which makes code run really slow. I’m wondering, is it possible to make a function and/or code that gets ran without that?

[Previous page](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721.md?page=1)

[Next page](https://forum.makecode.com/t/questions-about-makecode-c-sound/42721.md?page=3)
