Sine wave "phase" in makecode

@richard, music.playInstructions’s Buffer is arranged something like this: (copied from Gemini)

Offset  Size   Type      Parameter Name       Value Range
----------------------------------------------------------------------
0       2      UInt16LE  Sound Waveform       Waveform ID
2       2      UInt16LE  Frequency Start      0 to 65,535
4       2      UInt16LE  Duration             Milliseconds
6       2      UInt16LE  Volume Start         0 to 1023
8       2      UInt16LE  Volume End           0 to 1023
10      2      UInt16LE  Frequency End        0 to 65,535 Hz

But there is no place I can enter phases, which is when the waveform starts. for example, is a sine wave had a phase of 0, the vallue at 0 would be 0. Having a phases of pi/2 would make it 1. This could be helpful for additive reconstructive synthesis, and we could in theory play any audio file in makecode. The issue is, right now playInstructions has no phase value to do it. that means the audio would be up to 50% worse! Can you add it some how, or is it supported somewhere?

3 Likes

i don’t think adding a phase to the instruction would get you significantly closer to playing audio files (i like to think that i’m pretty familiar with this particular subject).

if i were to change the play instructions buffer format it would break a lot of stuff so i’d need a pretty strong reason to do so.

one thing i might consider doing is adding some more sine wave options to the waveform field, similar to what we do for duty values on square waves. but i don’t think that would give you the fidelity you’re looking for and it would only actually make a difference if you were rapidly switching phase or playing multiple notes simultaneously to get some phase cancellation effects… it would be pretty niche overall. unless you can demonstrate a compelling use case i’m not sure it would be worth doing.

5 Likes

@richard, you could probably design a playSineBuffer, which would be mostly the same thing but only sine (but with phase). I want to try to use ARS (Additive Reconstructive Synthesis) to play a normal sound file. I think this could work, since playInstructions talks to AudioContext, which is a function made in the browser. That means that AudioContext plays at whatever sample rate the device is (e.g. 44,100 or 48000 hz), rather than at 1000 hz. playInstructions I believe can only be given a waveform to play at a 1000 hz rate. The are around 48 samples (depending on the system) between each frame. By using ARS, I could play a couple sine waves every frame, and I would 100% replicate some frames. But to accurately use ARS, I need access to phase. This is why I am asking for it.

3 Likes

sorry, i just don’t think it makes sense to add.

even if i surfaced something like that, just for the browser and not for hardware, the reality of javascript running in the browser means that it wouldn’t be executing at audio rate. there is an API for generating low-level audio samples, but at that point we might as well just do the work to add sample playback.

also, makecode projects aren’t meant for storing large binary files like music or video. for one thing, there isn’t enough flash space on microcontrollers to hold that and the user’s code. and our sharing system wasn’t meant to hold large files anyhow. as soon as we added support for any sort of audio playback, i guarantee you that people would start attempting to to make projects with entire songs and run into all sorts of bugs/issues.

overall, while i appreciate that people like to experiment with audio playback, i wouldn’t expect it to be something that we support or add APIs for anytime soon (or ever)

5 Likes

@richard, what’s the largest string i can paste into makecode, and the project will still be shareable? A pasted 400 KB of base64, and whenever I clicked the share link it generated, it wouldd say something like the page being missing.
Also, the first project here has 2 MB worth of characters, yet when I do 400 KB it wont work.

3 Likes

there isn’t any hard and fast limit on this. it’s possible your link got flagged for having a bad word somewhere within the base64 data you pasted; that was a problem we used to have before we started stripping the base64 out of jres files in scripts before sending them to our content moderation service

3 Likes

Are you basically saying to put it in a jres file? I tried, but it still didn’t work.


that’s the saved file if it helps, what should I do?

1 Like

i think i fixed it? i removed images.g.ts and images.g.jres from pxt.json, and it worked!

If this link doesnt work when the post is approved, then react with :snake:.

10 Likes

H U H :fearful:
is mp3 is makecode… real???

3 Likes

The question is, is it possible to copy from one project to another?

My god… A new era of makecode is upon us…

no, no, no it’s not. look, for a wide variety of reasons makecode is not built for sharing entire wav files. see my post above.

@VoxelMaster64 happened to find an API that lets you play sound files in the simulator, but i’m going to get rid of it (it’s not intended to be used like this). i don’t recommend anyone take a dependency on it since it’s not long for this world.

11 Likes

1 Like

this was an opus file containing only 300 KB (400 KB as b64)! new Audio(...) always looks at the header of the file, so new Audio("data:audio/wav;base64,...opusFileData...") is actually new Audio("data:audio/ogg;base64,...opusFileData...") (ogg is opus for some reason)

it is when it i realeased it… but the mp3 was 3 MB and I made it 10 times smaller as an OPUS that sounded the same! (in other words, use OPUS or another small format)

1 Like