Questions about MakeCode C++ sound

is there a way to use the C++ layer of makecode or whatever its called? i feel like some extensions have cpp files in them, though i dont remember any. Im tryin to play partials, a type of sound, but playInstructions() or something else is making some of it sound like junk (likely makecode struggling to play hundreds at once).

4 Likes

@Richard

4 Likes

did a bit of researching, @richard is it true that c++ only compiles for the hardware and not the website? can this be changed, assuming it’s possible? There is also an app for makecode I believe, does that support c++ on computers? if I remember right (probably don’t), makecode can also use visual studio code (I have visual studio, would that work), does that work?

3 Likes

c++ is never compiled to run in the simulator, regardless of platform. all versions of makecode (browser, vs code, offline app) work exactly the same in this regard.

your project can contain c++, it just won’t work in the simulator.

the way we ā€œsimulateā€ c++ code in the browser is by having completely separate browser implementations. in most of our built-in extensions, this code can be found inside a /sim/ directory, for example here is where all the code for dealing with images/drawing in arcade lives: https://github.com/microsoft/pxt-common-packages/tree/master/libs/screen/sim

these browser implementations don’t go through the makecode compiler. as a result, they also have full access to the browser APIs. if we let extensions or projects run code that had access to browser APIs, it would be possible to, say, make a share link where it runs some simulator code that hacks your computer or starts mining bitcoin or something. for that reason, no extension or project is allowed to contribute code that runs in the simulator. it’s purely a security concern.

i always recommend that you try to keep all code in TS if possible

4 Likes

i guess i should also mention that we do accept pull requests! if you want to add new native c++ code along with browser implementations, we would be delighted!

for example, here are some example prs from our very own forum user @kwx adding new sound waveforms

4 Likes

Is there a way to upload files to makecode? I have an SDIF file that is 247KB, my output file is 145KB. I could probably compress the output file into a binary file (using every UTF-8 character (0x00 - 0xFF)) and parse it with makecode, but from what I’m aware, makecode can’t do anything like that. the output file is a number[] that can be copy pasted into makecode, utf-8 encoding cannot. the utf-8 encoded file would also be around 72KB which is a huge difference, so I would appreciate it if it was added! Also, when i was trying to comporess a huge number array (base 10) into my own character set (base 85), makecode crashed so bad, that instead of just crashing the simulator it closed every single chrome window (i was running it on chrome) on my comeputer, even windows from other accounts. Please answer this:

Add support for binary files
  • yes

  • no

  • need more info

  • no thoughts on it

  • other (respond with your own post)

0 voters

I guess I could probably expand on what I said earlier: I am taking a wav file, running it through SPEAR to generate an sdif file (an SDIF file contains ā€œpartialsā€ of a sound file, if you use SPEAR it is all the lines you see after uploading). I then use some c++ code to parse it (NOT what I want to add to makecode) and generate a number[]. Makecode can play it, using a loop and music.playInstructions(). this is the sound project (2 seconds of megalovania):

it is very glitchy, and sometimes makecode will crash because it’s playing so many partials. I was thinking that if I could access the c++ it would be possible to somehow create my own music.playInstructions(), and it would sound better. reducing the partials would make it sound worse, so I’m not doing that

3 Likes

@miles I’ve moved these posts into their own thread so that they don’t get lost in the just chatting milieu.

no, there is no way to upload data files into makecode but there is a much more efficient way to include them in your program. see this post where i gave a sample script for converting number arrays into hex buffers:

as for what you want to do, i’d say that if you wanted to edit the C++ then you might be better off adding straight up sample playback rather than doing some waveform trickery to better approximate the sound. then you could write a script to convert your sample to PCM data and have the C++ code play that back.

we do have sample playback implemented in some of our other targets (micro:bit) it just hasn’t made its way to arcade yet.

I was totally not expecting this to become another post.

If I fork the github repo(s) that make makecode run, if I get them into visual studio and have node.js, can I run makecode on vs and edit the code? Im a little confused, but would I then show you the changes I made to add sound playing?

I would like a little help if that’s possible, and if so, what github repo(s) I need to fork

1 Like

alright, fair warning: makecode is a very complex project and i would not recommend doing this unless you have some experience working with microcontrollers and webapps. with that out of the way, i don’t think i’ll be able to walk you through the entire process, but in general here is what i recommend:

  1. Install VS Code
  2. Install node.js (or node version manager)
  3. Install git

open up a terminal (or command prompt on windows) and run these commands to set up your dev environment (note, this will probably take a while)

npm install -g pxt
mkdir makecode
cd makecode

git clone https://github.com/microsoft/pxt.git
cd pxt
npm install
npx gulp

cd ..
git clone https://github.com/microsoft/pxt-common-packages.git
cd pxt-common-packages
npm install
npm link ../pxt

cd ..
git clone https://github.com/microsoft/pxt-arcade.git
cd pxt-arcade
npm install
npm link ../pxt ../pxt-common-packages

once you’ve done that, you can run makecode arcade locally by running pxt serve from within pxt-arcade.

2 Likes