Questions about MakeCode C++ sound

@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.

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

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:

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:

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

3 Likes

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

music.playPcm(myHexBuffer)

soundEffect.ts:

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:

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

melody.cpp:

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

yup, that looks about right

@richard I have done that before, (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.

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

1 Like

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

C++ never runs in the browser

2 Likes