NokiaTune on obniz Board 1Y
2021/3/21公開
2021/4/17更新
概要
圧電スピーカーでメロディーを鳴らしてみたくなったので、obnizのIO-Animation機能を使ってひと昔前の海外で着信音として定番だったNokiaTuneを鳴らしてみました。
たまにテンポがモタることがあるので、Youtubeに上げてあるのはいい感じに撮れたテイクです。
サンプルコード
<script src="https://unpkg.com/obniz@3.x.0/obniz.js"></script>
<script>
// 音階と周波数の定義
const FREQS = {
'C4' : 261.626,
'C#4' : 277.183,
'D4' : 293.665,
'D#4' : 311.127,
'E4' : 329.628,
'F4' : 349.228,
'F#4' : 369.994,
'G4' : 391.995,
'G#4' : 415.305,
'A4' : 440.000,
'A#4' : 466.164,
'B4' : 493.883,
'C5' : 523.251,
'C#5' : 554.365,
'D5' : 587.330,
'D#5' : 622.254,
'E5' : 659.255,
'F5' : 698.456,
'F#5' : 739.989,
'G5' : 783.991,
'G#5' : 830.609,
'A5' : 880.000,
'A#5' : 932.328,
'B5' : 987.767,
'C6' : 1046.502,
'C#6' : 1108.731,
'D6' : 1174.659,
'D#6' : 1244.508,
'E6' : 1318.510,
'F6' : 1396.913,
'F#6' : 1479.978,
'G6' : 1567.982,
'G#6' : 1661.219,
'A6' : 1760.000,
'A#6' : 1864.655,
'B6' : 1975.533
}
var obniz = new Obniz("OBNIZ_ID_HERE");
var spk = null;
obniz.onconnect = async function () {
spk = obniz.wired("Speaker", {signal:0, gnd:1});
// canvas準備
const canvas = document.createElement('canvas');
canvas.width = 128;
canvas.height = 64;
const ctx = canvas.getContext("2d");
// 画像読み込み
const img = new Image();
img.src = "data:image/png;base64,【ここにBASE64エンコードした画像データ(省略)】";
img.onload = function() {
ctx.drawImage(img, 0, 0);
obniz.display.draw(ctx);
};
// 音を出して接続確認
spk.play(1000);
obniz.wait(100);
spk.stop();
obniz.switch.onchange = function(state) {
// 左上のスイッチを押し込むと再生
if (state === "push") {
play();
}
}
}
function play() {
obniz.io.repeatWait([
{
duration: 125,
state: function(index){
spk.play(FREQS['E5'])
}
},{
duration: 125,
state: function(index){
spk.play(FREQS['D5'])
}
},{
duration: 250,
state: function(index){
spk.play(FREQS['F#4'])
}
},{
duration: 250,
state: function(index){
spk.play(FREQS['G#4'])
}
},{
duration: 125,
state: function(index){
spk.play(FREQS['C#5'])
}
},{
duration: 125,
state: function(index){
spk.play(FREQS['B4'])
}
},{
duration: 250,
state: function(index){
spk.play(FREQS['D4'])
}
},{
duration: 250,
state: function(index){
spk.play(FREQS['E4'])
}
},{
duration: 125,
state: function(index){
spk.play(FREQS['B4'])
}
},{
duration: 125,
state: function(index){
spk.play(FREQS['A4'])
}
},{
duration: 250,
state: function(index){
spk.play(FREQS['C#4'])
}
},{
duration: 250,
state: function(index){
spk.play(FREQS['E4'])
}
},{
duration: 500,
state: function(index){
spk.play(FREQS['A4'])
}
},{
duration: 500,
state: function(index){
spk.stop()
}
}
],14);
}
</script>
戻る