Bad Apple on the micro:bit¶
Introduction¶
If you're in the Touhou community, or really anything with a demoscene community, you've probably heard of this one black-and-white music video called "Bad Apple". It can basically be represented by anything resembling a screen, which is why people have gotten Bad Apple to run on anything you could think of.
But when people have gotten around to making Bad Apple run on so many devices, what's there left to run it on? A calculator? Done. A fucking pregnancy test? Done.
After much thinking however, I thought about a slightly niche microcontroller device, called the micro:bit. I wouldn't have expected BBC to be the one who made this damn thing, but I guess it makes sense considering they also made the BBC Micro back in the 80s.
I mean, it's got a 5x5 screen. That could certainly display Bad Apple...
Now you might be asking "Wait. Hasn't someone done this?", and you'd be right, but the implementations I saw (two of them) were janky, one of them required 37 scripts to be spliced together, and the other didn't really run on real hardware. Also, at this point, I kind of gave up trying to make something unique. Oh well.
Reinventing the Wheel¶
First off, I had to figure out how the hell I was going to get full motion video on this. The micro:bit obviously wasn't going to play a 480p H.264 video in full resolution at 30fps, so first, I used my handy media knife (ffmpeg) to change the video to a "square" resolution of 360x360. Then I wrote some OpenCV code to finally resize the video down to the desired 5x5 resolution, converting it to black-and-white in the process to save data...
Note
While the micro:bit could control the LEDs in grayscale, I decided not to due to the amount of data that would be required. I would like this thing to fit in one script, after all!
Then, in that same script, I encode the video in a series of 25-bit numbers (5x5 screens ARE tiny!), where 1 number represents one bit, like this:
00000
01010 █ █
01110 ███
01010 █ █
00000
0000001010011100101000000 -> 53940
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,420,20,0,108421,108421,318c21,318c21,318c21,318c21,318421,308420,108400,108400,188400,108400,1108400,118c400,118c400,118c600,118c600,118c610,118c610,118c610,198e610,19ce610,19ce210,19ce210,19ce210,19ce210,19ce310,19ce710,19ce730,19ce730,19ce731,19ce733,19ce633,19ce633,19ce631,19ce631,19ce631,19de631,19de631,19de631,1bdce31 ...
Gradual Steps¶
The first thing I thought to do, was to, well, not reencode every single frame. I implemented a basic run-length system, where each frame had a number that indicated how many frames that frame ran for, like this...
0:47,420:1,20:1,0:1,108421:2,318c21:4,318421:1,308420:1,108400:2,188400:1,108400:1,1108400:1,118c400:2,118c600:2,118c610:3,198e610:1,19ce610:1,19ce210:4,19ce310:1,19ce710:1,19ce730:2,19ce731:1,19ce733:1,19ce633:2,19ce631:3,19de631:3,1bdce31:6 ...
The next thing was reducing the information required. I
- reduced the framerate to 15fps (25 KiB -> 17 KiB, ~68% of previous step)
- removed the run length, for frames that only ran for 1 frame (17 KiB -> 14 KiB, ~82.3% of previous step)
Now, the file sizes were getting pretty small, but they weren't small enough for my liking. I mean, the micro:bit (v1) only has 16KB of SRAM, and a lot of the MakeCode runtime I was using was going to gobble that up. So I found another way to reduce the sizes further...
Previously, we had been encoding the entire frame for every frame. But what if we didn't? What if we just took the changes for the next frame and applied them to the previous frame? Well, we can!
Take our previous glorious H...
00000
01010 █ █
01110 ███
01010 █ █
00000
0000001010011100101000000 - H
0000000100001000010000000 - Delta
0000001110010100111000000 - ???
00000
01110 ███
01010 █ █
01110 ███
00000
And while we're at it, since TypeScript (on MakeCode) supports base 36 natively, let's encode our video and run length with it! Now our file looks like this...
0:q,tc,tc,n76p,1aebk:2,1g5d,18y74:2,ab6kg,e8,g:2,55la8,sg:2,zk,w,3,74,2,1ekg:2,1962o:3,1s,4zsoy:5,4zz0g,2,8,1s:2,74:9,1s,2:2,8:2,74:7,74,8:5,1u:7,1s:2,2:3,8:2,2,20,18y68:2,1eki:2,1kz:2,2hzlw,2t58,sh:2,so,74:4,18y68:2,2t4w:a,18y68,74:3,1 ...
There are ways to improve on this even further, like removing the deltas that are simply just repeated from the previous frame and only stating the runtime, but I am very satisfied. This takes just ~20.5% of the space the uncompressed version took! (Or ~31.8% if you consider the 30fps version)
To the micro:bit!¶
Our video is now ready. Now we just have to make some code on the micro:bit to play it. Here's what it took:
let vs = "0:q,tc,tc,n76p,1aebk:2,1g5d,18y74:2,ab6kg,e8,g:2,55la8,sg:2,zk,w,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,6io,2hwcg:2,cnl,23up,2hxxc,2t4w,b8pc,bc5e,5zbc,6bqkg,1dawy,4zz2c,d1iq0,ckkd4,h85w,505c0,b8jk,eebk,b8jk:2,2t4w,b8jk,47pg,1erk,7q96o,2u14,740,6bk,1g5c,775u,3wgsw,d1vo,2t4w,1w85d,h3qy,1ssg,vx2c,3t,1ekg,2gy,5078,23uo,sw,blkw,a1qv7,9zy20,h1c0,4zsow,ec,4zsow,9b1c,39e,2hwjk,zl,2:2,2hwjk:2,74,8,1o8w,348w,2,32tg,1bri8,74x,1w656,1eq2,n78i,w,1t,e,dt,369:3,35s:2,35s:3,1kw,2zgo,1erk,2nima,6fzr8,68rnl,ay3hc,693wg,740g:2,70u8:3,5pfk,1bxmw,6io,2i2o4,5srm,w,fml9c,bl7l,dv4,duo,fl,h:2,4qo,6bs,2tc0,70w4,55n0i,93j8h,bg9og,o8ow,6frps,1kw,2dc,24n4,5648w,5m9u0,1zrpc,3r0u8,1ekg,6bk:3,1ekg,1kw:2,1s,2hwjk,74,2t4w,2,1kw,35s,1l0,1qa,1eki,2,1hs0,35u:2,w:4,sh,1ls,1,16o,b8jk,ab6yo,bf9c,ezksg,2zgg,e1vk,5yww,2t4w,58hog:2,2t4w,74,2t50:2,1kw,75u,cn4:2,w,1s,2t4x,w,mh35:2,2:2,5rw1s,2hwcg,4zsp4,55f60,1eku8,mvb4,1feo,8,9zmzk,18y6m,18y7e,24oi,1f29,b9q8,16o,co0,3kw,e8,e8:2,4:6,4,4:3,35s,35s,3k,4:2,4,4,3o,2t4w:2,2xvm,1hvo:6,2t8g:2,4,4,2t8g:4,3k:6,2t4w,5slc,4,6bk,5mh4,e8:2,bocg,9zle8,aatxc,505cg,8lq8:2,8,8fes,55fd4,2ns3s,1em8,1mq:3,a,94,1s,6,2jax2,2t4w,1kw:2,18zr8,25fl,1adk0,nvpe,qw0,n75s,pb5,x,x:3,36o:2,3k:5,8,74:2,6bk,5m9s:7,1,1:8,2,2:5,2,1s,1kw:12,5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9zy2s,5b18g,65n4,aatz4,4zsow,8w,2t52:2,20,5m9s,6bk:2,6,35s,35s,35s,5m9s,7hp34,194q3,azua8:s,azua8"
let lastChar = 0;
for (let char = 0; char < vs.length; char++) {
if (vs[char] !== ",") continue;
const frameString = vs.substr(lastChar, char - lastChar);
lastChar = char + 1;
let now = input.runningTime();
const fSSplit = frameString.split(":");
let count = 1;
let frame = parseInt(fSSplit.get(0), 36);
if (fSSplit.length > 1) {
count = parseInt(fSSplit.get(1), 36);
}
for (let bit = 0; bit < 25; bit++) {
const x = bit % 5;
const y = Math.floor(bit / 5);
if (frame & 1) {
led.toggle(4 - x, 4 - y);
}
frame = frame >> 1;
}
basic.pause(((1000 / 16) * count) - (input.runningTime() - now));
}
And this is what it looks like on the real thing! (contrast increased, out of sync because framerate matching is hard)
So, all in all, this was a nice way to get to mess around with encoding video. I'll probably have more stupid ideas I could experiment with in the future... but wait. Aren't we missing something?
Where's my audio?¶
If I had muted the audio for one of our earlier samples, you would've heard no sound. Well, because there was no sound. It doesn't come neatly prepackaged like the video, you see. So where do we go from here? I'm thinking some MIDI files...
MIDI, the ultimate audio format¶
If you've read the article this far, you probably know what a MIDI file is. But if you don't, MIDI is the standard way for keyboards, synthesizers (and the like) to communicate with each other with "messages". With this comes MIDI files, which are files that contain messages to be played. Note how they're just data, and they don't contain any actual audio. They're just... instructions for musical instruments, like sheet music. This is very useful for us, since we need none of the actual audio samples that are there, we just need to know what the notes are so we can synthesize them on the micro:bit.
I'm not very musically inclined, which is why I decided to have other people do the hard work of transcribing Bad Apple into MIDI. Thanks to this kind soul from SMWCentral, I now had a relatively accurate representation of Bad Apple in MIDI.
However, as they usually say, "you can't synthesize MIDI directly on a micro:bit", which is why I will reinvent the wheel again...
Reinventing the Wheel. Again.¶
Now, there are several ways to make noise on the micro:bit, like music.playTone()
which can play a singular tone, but I figured the easiest way to do this was to use music.startMelody()
and its associated functions, since they function (heh) without any work required to integrate it into the video system, which would be a nightmare to do.
While MIDI files are a bunch of binary messages, what the micro:bit really wants is something like:
['g4:1', 'c5', 'e', 'g:2', 'e:1', 'g:3']
So, I started my second journey. First, I used a library called mido
to read the MIDI file and parse all the messages. They all looked like this in the console...
>>> mid.tracks[7][17:50]
MidiTrack([
Message('note_on', channel=8, note=46, velocity=95, time=92160),
Message('note_on', channel=8, note=39, velocity=95, time=0),
Message('note_on', channel=8, note=42, velocity=95, time=0),
Message('note_off', channel=8, note=46, velocity=95, time=7680),
Message('note_off', channel=8, note=39, velocity=95, time=0),
Message('note_off', channel=8, note=42, velocity=95, time=0),
Message('note_on', channel=8, note=35, velocity=95, time=0),
Message('note_on', channel=8, note=42, velocity=95, time=0),
Message('note_on', channel=8, note=39, velocity=95, time=0),
Message('note_off', channel=8, note=35, velocity=95, time=3840),
Message('note_off', channel=8, note=42, velocity=95, time=0),
Message('note_off', channel=8, note=39, velocity=95, time=0),
Message('note_on', channel=8, note=37, velocity=95, time=0),
Message('note_on', channel=8, note=41, velocity=95, time=0),
Message('note_on', channel=8, note=44, velocity=95, time=0),
Message('note_off', channel=8, note=37, velocity=95, time=3840),
Message('note_off', channel=8, note=41, velocity=95, time=0),
Message('note_off', channel=8, note=44, velocity=95, time=0),
Message('note_on', channel=8, note=46, velocity=95, time=0),
Message('note_on', channel=8, note=39, velocity=95, time=0),
Message('note_on', channel=8, note=42, velocity=95, time=0),
Message('note_off', channel=8, note=46, velocity=95, time=7680),
Message('note_off', channel=8, note=39, velocity=95, time=0),
Message('note_off', channel=8, note=42, velocity=95, time=0),
Message('note_on', channel=8, note=35, velocity=95, time=0),
Message('note_on', channel=8, note=42, velocity=95, time=0),
Message('note_on', channel=8, note=39, velocity=95, time=0),
Message('note_off', channel=8, note=35, velocity=95, time=3840),
Message('note_off', channel=8, note=42, velocity=95, time=0),
Message('note_off', channel=8, note=39, velocity=95, time=0),
Message('note_on', channel=8, note=37, velocity=95, time=0),
Message('note_on', channel=8, note=41, velocity=95, time=0),
Message('note_off', channel=8, note=37, velocity=95, time=960)])
note_on
and note_off
for the same note, and the duration between note_off
and note_on
of two different notes when there are no notes playing. The latter is so we can measure rests in the music correctly. I converted these durations based on some MIDI tick values and I set the BPM on the micro:bit correspondingly.
Since the micro:bit only natively supports playing from 1 channel at a time, that is what we're going to stick with. I'm sure I could do some simple signal processing to mix two manually-synthesized channels, but that is not something I wanted to go through. For this reason, I also spliced the audio from different channels and combined them so there's less silence.
I decided to store the MIDI note value of the note, along with the note's duration in quarter beats. And now, I had the music in a more digestable format:
R:128,39:2,39:1,39:1,R:1,39:1,37:1,39:1,39:2,39:1,39:1,R:1,39:1,37:1,39:1,39:2,39:1,39:1,R:1,39:1,37:1,39:1,39:2,42:1,39:1,44:2,42:1,44:1,39:2,39:1,39:1,R:1,39:1,37:1,39:1, ...
27:2,R,27,R,27,R,27:1,27,27,27,27:2,R,27,R,27,R,27,27,27,R,27,R,27,R,27:1,27,27,27,27:2,R,27,R,27,R, ...
The Finale¶
With a working Bad Apple video in one hand, and a converted Bad Apple MIDI in another, this is what we get:
let vs = "0:q,tc,tc,n76p,1aebk:2,1g5d,18y74:2,ab6kg,e8,g:2,55la8,sg:2,zk,w,3,74,2,1ekg:2,1962o:3,1s,4zsoy:5,4zz0g,2,8,1s:2,74:9,1s,2:2,8:2,74:7,74,8:5,1u:7,1s:2,2:3,8:2,2,20,18y68:2,1eki:2,1kz:2,2hzlw,2t58,sh:2,so,74:4,18y68:2,2t4w:a,18y68,74:3,1,1:2,w0:2,w,pa8,w:3,35s:2,w:6,35s:4,pa8:2,w,sg,3k,35s,74,35s,2hwcg,8,3d0:2,3t:3,2hwcg,18z2c,3lly:2,mh4w,1g5c:2,mh5a,5xoi,3imk,8mli,4w7:3,2hwcg:2,2t4w,2hwcg,35s,2t4w:2,3k:2,35s:5,35s:3,3k:2,2t4w:8,2ups:2,3k:5,1kw,1kw,1vgu8,2img0,jwctb:7,586tc,abq4g,1vpxc,cmfhc,55eyx,t7,9zojp,bdag,5gni8,ckav4,1x8g0,5b2c0,9ea4,41w,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:8,1,18y68,68qv4,35s:a,35s:2,4zssg,4zsp0,2t4w,50hz4,pa8,7j4so,cnc,1ls,coao,18zr4,2o,3k,9zlj4,by82,am39w,1l3k,qvb8,8feq,gydk,paa:2,9zmkk,20,18zrc,1eo0,3r4,2kphc,6bk,6ozl,w:4,pa8:3,1en4,si,w,1lt:j,35s:2,35s:4,1ekg:i,1ekg:9,35s:4,35s:4,1ekg,1s,1:2,1ls,q2q,5m9s:2,pa8,tc,1,2hwcg,e1og:2,9zlds,18y68,5m9s,1ekg,4zsow,mh34:2,3if4,2i9e8,bl6o,b8y8,1l3c,1ekg0,5sso,39g:3,2t4w:f,pa8:4,mh34:c,35s,2,1s:3,35s:4,1s:4,8,1g75,7k,lc,1l3l,5ob4,g,36aa,35s,5m9w,6bs,74,cn6,es,5c,1s,4qy,1g5c,47pc,pa80,3k,sn8,194i8,1gy0,2kwgw,55im9,6xi,ab6ko,g:g,4:5,h:2,4,k:4,1,1:2,3k,h:3,w,g,35t,10,e9:2,1kw,ec,1kw,4,3d0,4,8,g,ao,5yww,5m9s,6bl,q2o,6bs,6bl,g:2,cn5,2wht,3k,sg,6io,2hwcg:2,cnl,23up,2hxxc,2t4w,b8pc,bc5e,5zbc,6bqkg,1dawy,4zz2c,d1iq0,ckkd4,h85w,505c0,b8jk,eebk,b8jk:2,2t4w,b8jk,47pg,1erk,7q96o,2u14,740,6bk,1g5c,775u,3wgsw,d1vo,2t4w,1w85d,h3qy,1ssg,vx2c,3t,1ekg,2gy,5078,23uo,sw,blkw,a1qv7,9zy20,h1c0,4zsow,ec,4zsow,9b1c,39e,2hwjk,zl,2:2,2hwjk:2,74,8,1o8w,348w,2,32tg,1bri8,74x,1w656,1eq2,n78i,w,1t,e,dt,369:3,35s:2,35s:3,1kw,2zgo,1erk,2nima,6fzr8,68rnl,ay3hc,693wg,740g:2,70u8:3,5pfk,1bxmw,6io,2i2o4,5srm,w,fml9c,bl7l,dv4,duo,fl,h:2,4qo,6bs,2tc0,70w4,55n0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aye5t:3,18y68:3,1hq8,1s0:2,3k,18ydc:2,3k,35s,1ekg,74,nf35,4zz7k,2qi2o,35s,6bk,bev4,55la8,1aeeb,b8jk:2,9zlds:4,b8jk:4,2ksqs:2,2kphc:3,2kphc,2t4w:3,2t4w,2kphc:3,2t4w:d,9zlds,1,36o,2i8zk,2wao0,1,w,5m9s,5mao,9zlds,4zsow,3qulk,1ex2,1ad1c,1enns,pacw,a0e9t,1enls:2,3y8,5m9s,658g:2,3czk,564oy,35s,2nio4,9zldt,7c,1acqo,7wg,24g0,tx74,bew0,58g5e,cn41x,bwf89,74,otg0,7ebk,bgo0,f81j7,2nio4,3s9ak,2i490,oyz4,ib8g,f28sj,2niro,5mbk,3s9ak,36psk,7ebk,bf36,hpwat,5mbk,3s9ak,35b10,1s74,i9nk,f27b7,2nio4,5mbk,3s9ak,miog,3rqj0,dmo,1r7mw:2,f0sub,3k,2t4w,2hwck:3,2kphc,e80:3,fsw,3y8,55lhk,u8,w,2o,1kw:7,1s,w,1kw:3,1s:2,1hq8,57414,f0g,n6dc,38i2s:2,1anuq,2t4w,q0bl,2t5o,lg,2t8g,35s,9zy2s,5b18g,65n4,aatz4,4zsow,8w,2t52:2,20,5m9s,6bk:2,6,35s,35s,35s,5m9s,7hp34,194q3,azua8:s,"
let as = "R:12,27:2,R,27,R,27,R,27:1,27,27,27,27:2,R,27,R,27,R,27,27,27,R,27,R,27,R,27:1,27,27,27,27:2,R,27,R,27,R,27,27,27,R,27,R,27,R,27:1,27,27,27,27:2,R,27,R,27,R,27,27,27,R,27,R,27,R,27:1,27,27,27,27:2,R,27,R,27:1,27,27,27,27:2,R,39,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,39:2,42:1,39,44:2,42:1,44,39:2,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,44:2,42:1,44,42:2,39:1,42,39:2,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,39:2,42:1,39,44:2,42:1,44,39:2,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,44:2,42:1,44,42:2,39:1,42,75:2,77,78,80,82:4,87:2,85,82:4,75,82:2,80,78,77,75,77,78,80,82:4,80:2,78,77,75,77,78,77,75,74,77,75,77,78,80,82:4,87:2,85,82:4,75,82:2,80,78,77,75,77,78,80,82:4,80:2,78,77:4,78,80,82,75:2,77,78,80,82:4,87:2,85,82:4,75,82:2,80,78,77,75,77,78,80,82:4,80:2,78,77,75,77,78,77,75,74,77,75,77,78,80,82:4,87:2,85,82:4,75,82:2,80,78,77,75,77,78,80,82:4,80:2,78,77:4,78,80,82,85:2,87,82,80,82:4,80:2,82,85,87,82,80,82:4,80:2,82,80,78,77,73,75:4,73:2,75,77,78,80,82,75:4,80:2,82,85,87,82,80,82:4,80:2,82,85,87,82,80,82:4,80:2,82,80,78,77,73,75:4,73:2,75,77,78,80,82,75:4,80:2,82,85,87,82,80,82:4,80:2,82,85,87,82,80,82:4,80:2,82,80,78,77,73,75:4,73:2,75,77,78,80,82,75:4,80:2,82,85,87,82,80,82:4,80:2,82,85,87,82,80,82:4,87:2,89,90,89,87,85,82:4,80:2,82,80,78,77,73,75:4,80:2,82,85,87,82,80,82:4,80:2,82,85,87,82,80,82:4,80:2,82,80,78,77,73,75:4,73:2,75,77,78,80,82,75:4,80:2,82,85,87,82,80,82:4,80:2,82,85,87,82,80,82:4,80:2,82,80,78,77,73,75:4,73:2,75,77,78,80,82,75:4,80:2,82,85,87,82,80,82:4,80:2,82,85,87,82,80,82:4,80:2,82,80,78,77,73,75:4,73:2,75,77,78,80,82,75:4,80:2,82,85,87,82,80,82:4,80:2,82,85,87,82,80,82:4,87:2,89,90,89,87,85,82:4,80:2,82,80,78,77,73,44:4,42,39:2,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,39:2,42:1,39,44:2,42:1,44,39:2,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,44:2,42:1,44,42:2,39:1,42,39:2,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,39:2,42:1,39,44:2,42:1,44,39:2,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,39:2,39:1,39,R,39,37,39,44:2,42:1,44,42:2,39:1,42,75:2,77,78,80,82:4,87:2,85,82:4,75,82:2,80,78,77,75,77,78,80,82:4,80:2,78,77,75,77,78,77,75,74,77,75,77,78,80,82:4,87:2,85,82:4,75,82:2,80,78,77,75,77,78,80,82:4,80:2,78,77:4,78,80,82,75:2,77,78,80,82:4,87:2,85,82:4,75,82:2,80,78,77,75,77,78,80,82:4,80:2,78,77,75,77,78,77,75,74,77,75,77,78,80,82:4,87:2,85,82:4,75,82:2,80,78,77,75,77,78,80,82:4,80:2,78,77:4,78,80,82,85:2,87,82,80,82:4,80:2,82,85,87,82,80,82:4,80:2,82,80,78,77,73,75:4,73:2,75,77,78,80,82,75:4,80:2,82,85,87,82,80,82:4,80:2,82,85,87,82,80,82:4,80:2,82,80,78,77,73,75:4,73:2,75,77,78,80,82,75:4,80:2,82,85,87,82,80,82:4,80:2,82,85,87,82,80,82:4,80:2,82,80,78,77,73,75:4,73:2,75,77,78,80,82,75:4,80:2,82,85,87,82,80,82:4,80:2,82,85,87,82,80,82:4,87:2,89,90,89,87,85,82:4,80:2,82,80,78,77,73,75:4,80:2,82,86,88,83,81,83:4,81:2,83,86,88,83,81,83:4,81:2,83,81,79,78,74,76:4,74:2,76,78,79,81,83,76:4,81:2,83,86,88,83,81,83:4,81:2,83,86,88,83,81,83:4,81:2,83,81,79,78,74,76:4,74:2,76,78,79,81,83,76:4,81:2,83,86,88,83,81,83:4,81:2,83,86,88,83,81,83:4,81:2,83,81,79,78,74,76:4,74:2,76,78,79,81,83,76:4,81:2,83,86,88,83,81,83:4,81:2,83,86,88,83,81,83:4,88:2,90,91,90,88,86,83:4,81:2,83,81,79,78,74,76:6,R:2,83:3,R:1,83,R,83,R,83:3,R:1,83,R,83,R,83:3,R:1,83,R,83,R,83:3,R:1,83,R,83,R,83:3,R:1,83,R,83,R,83:3,R:1,83,R,83,R,83:3,R:1,83,R,83,R,83:3,R:1,83,R,83,R".split(",");
// music
function midiToNote(note: number): string {
let octave = Math.floor(note / 12);
if (octave > 3) octave -= 2; // making sure it doesn't bleed people's ears
return ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'][note % 12] + octave;
}
let lastLength = 0;
as = as.map((v, i) => {
const split = v.split(":");
let note = parseInt(split[0]);
if (split.length > 1) lastLength = parseInt(split[1])
if (isNaN(note)) return v;
return midiToNote(note) + ":" + (split.length > 1 ? split[1] : lastLength);
});
music.setTempo(138);
music.startMelody(as, MelodyOptions.OnceInBackground)
// video
let lastChar = 0;
for (let char = 0; char < vs.length; char++) {
let now = input.runningTime();
if (vs[char] !== ",") continue;
const frameString = vs.substr(lastChar, char - lastChar);
lastChar = char + 1;
const fSSplit = frameString.split(":");
let count = 1;
let frame = parseInt(fSSplit.get(0), 36);
if (fSSplit.length > 1) {
count = parseInt(fSSplit.get(1), 36);
}
for (let bit = 0; bit < 25; bit++) {
const x = bit % 5;
const y = Math.floor(bit / 5);
if (frame & 1) {
led.toggle(4 - x, 4 - y);
}
frame = frame >> 1;
}
// cursed fps count
basic.pause(((1000 / 15.97) * count) - (input.runningTime() - now));
}
And now, the Bad Apple on micro:bit saga shall end... (real device footage soon:tm:)
Conclusion¶
Honestly? I didn't even expect myself to take it this far. I mean, I start a billion projects and this is one of maybe three that I'll finish. It was a fun experience looking for ways to encode video in text, after all. I'm sure I'll find more dumb ways to reinvent the wheel after this...
I might publish the source code to the script that generated all of this, but it's not certain. Oh well.