# Frequencies of musical notes and tempo are all wrong when played on a micro.bit

**URL:** https://forum.makecode.com/t/frequencies-of-musical-notes-and-tempo-are-all-wrong-when-played-on-a-micro-bit/31535
**Category:** micro:bit
**Tags:** make
**Created:** [November 5, 2024, 10:04pm UTC](https://forum.makecode.com/t/frequencies-of-musical-notes-and-tempo-are-all-wrong-when-played-on-a-micro-bit/31535 "2024-11-05T22:04:18Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Roo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/roo/32/17913_2.png) [@Roo](https://forum.makecode.com/u/Roo)
#### Post date: [November 5, 2024, 10:04pm UTC](https://forum.makecode.com/t/frequencies-of-musical-notes-and-tempo-are-all-wrong-when-played-on-a-micro-bit/31535/1 "2024-11-05T22:04:18Z")

</div>

I received the latest V2 micro.bit yesterday and the simple tutorial exercises work well, except selecting notes like Middle C from “Music” followed by Middle G does not play as the notes and is not in harmony. Also the temp setting seems to be all wrong. Below is the full program I copied from a website [Behind the MakeCode Hardware - Speaker on the Circuit Playground Express](https://www.youtube.com/watch?v=JjJ-KGwKh_4). The tune played is supposed to be “Mary Had a Little Lamb”

```auto
on start
    set volume (200)
    change tempo by (bpm) (60)

on button (A) pressed
   play (tone (Middle C) for 1/4 beat) until done
    rest for (1/8 beat)
   play (tone (Middle C) for 1/4 beat) until done
    rest for (1/8 beat)
   play (tone (Middle G) for 1/4 beat) until done
    rest for (1/8 beat)
   play (tone (Middle G) for 1/4 beat) until done
    rest for (1/8 beat)
   play (tone (Middle A) for 1/4 beat) until done
    rest for (1/8 beat)
   play (tone (Middle A) for 1/4 beat) until done
    rest for (1/8 beat)
   play (tone (Middle G) for 1/2 beat) until done
    rest for (1/8 beat)

```

I do not understand what “until done” does"? the original code did not have it, but I don not seem to be able to select “Block Code” without that extension.

Any Help would be appreciated.

I am a retired Software Engineer, who has developed the language [immdiate C](https://www.immediatec.net/2016/04/16/immediate-c/#more-1), an “Event Driven” extension of the C language - suitable for Machine Control, Internet of Things (IOT), GUi’s and Games.  
Since micro.bit supports both SPI and I2C, I want to use micro.bit as a simple I/O device. I have drivers for both SPI and I2C for immediate C.

---

<div class="post-metadata">

### Author: ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)
#### Post date: [November 7, 2024, 5:01pm UTC](https://forum.makecode.com/t/frequencies-of-musical-notes-and-tempo-are-all-wrong-when-played-on-a-micro-bit/31535/2 "2024-11-07T17:01:37Z")

</div>

Hi @Roo,

I can’t speak to your questions on tempo or tone. You may want to look at the source code for `music.ts`, `music.cpp`, and `playable.ts`.

I can give some direction on `until done` — it just means that it’s blocking within that scope. When programming in the “code blocks”, one usually _wants_ blocking (until done) behavior for notes so they can hear each note for an appropriate duration. If non-blocking you’d only hear the last note… On the other hand, sometimes you want to play a sound nearly forever. Here’s an example that you can pick the three options via the dropdown in the `play` block and get a sense of the differences in behavior: [https://makecode.microbit.org/S83895-76743-27699-85070](https://makecode.microbit.org/S83895-76743-27699-85070) (Clicking `A` in the simulator and seeing the images on screen and tone durations).

There are maybe two different reasons for the difference in the block in Circuit Express: 1) It may have been an intentional choice to keep it simpler for Circuit Express (no background option) or, in my opinion the more likely reason, 2) they just based it on an older version of MakeCode before background music was supported. You’re using `play` on the micro:bit currently includes a [`PlaybackMode`](https://github.com/microsoft/pxt-microbit/blob/5641d516979e7f7de486ffef2bceb828131a8e4a/libs/core/playable.ts#L91) option, but the history of the API only goes [back to 2023](https://github.com/microsoft/pxt-microbit/commits/5641d516979e7f7de486ffef2bceb828131a8e4a/libs/core/playable.ts). MakeCode for the micro:bit still includes a simple [`playTone()`](https://github.com/microsoft/pxt-microbit/blob/3ff8de4353bfeb7bc21054a66e842f4bca583e36/libs/core/music.ts#L203), but it’s [deprecated](https://github.com/microsoft/pxt-microbit/blob/3ff8de4353bfeb7bc21054a66e842f4bca583e36/libs/core/music.ts#L202). The [`playTone`](https://github.com/microsoft/pxt-adafruit/blob/f045710530898f6f2f4f197bfaea7957daafcd14/libs/music/shims.d.ts#L39) you see in CircuitExpress appears to be based on the older MakeCode API.

You can access SPI and I2C from the MakeCode level APIs (TypeScript, Python, blocks), but sometimes it’s easier to do hardware from a C++ extension.

Hope that helps a bit!
