0
2

More than 1 year has passed since last update.

JScriptでサウンド再生

Last updated at Posted at 2021-11-05

wavファイルの再生

wmp.js
wmp = new ActiveXObject("WMPlayer.OCX");
wmp.URL = "C:\\Windows\\Media\\tada.wav";
wmp.Controls.play();
WScript.Sleep(2000);

wavファイルを作成して再生

wavファイルのイメージをメモリに作っても直接再生する手段は無さそうなので、wavファイルを作成して再生する。

pipo

pipo.js
adTypeBinary = 1
adTypeText = 2
adSaveCreateNotExist = 1
adSaveCreateOverWrite = 2

function word(x) { return num_le(x, 2); }
function dword(x) { return num_le(x, 4); }
function num_le(x, n) {
    b = "";
    for (var i = 0; i < n; i++) {
        b += String.fromCharCode(x & 0xff);
        x >>>= 8;
    }
    return b;
}

function main() {
    b = "";
    b += "RIFF";
    b += dword(36 + 4000);
    b += "WAVE";
    b += "fmt ";
    b += dword(16);
    b += word(1);
    b += word(1);
    b += dword(8000);
    b += dword(8000);
    b += word(1);
    b += word(8);
    b += "data";
    b += dword(4000);
    for (var i = 0; i < 500; i++) b += "\x90\x90\x70\x70";
    for (var i = 0; i < 250; i++) b += "\x90\x90\x90\x90\x70\x70\x70\x70";

    st = new ActiveXObject("ADODB.Stream");
    st.Charset = "iso-8859-1";
    st.Type = adTypeText;
    st.Open();
    st.WriteText(b);
    st.SaveToFile("pipo.wav", adSaveCreateOverWrite);
    st.Close();

    wmp = new ActiveXObject("WMPlayer.OCX");
    wmp.URL = "pipo.wav";
    wmp.Controls.play();
    WScript.Sleep(1000);
}

main();

coin

coin.js
adTypeBinary = 1;
adTypeText = 2;
adSaveCreateNotExist = 1;
adSaveCreateOverWrite = 2;

SAMPLE_RATE = 48000;
DATA_LEN = Math.floor(SAMPLE_RATE * 1.1);

function main() {
    b = "";
    b += "RIFF";
    b += dword(36 + DATA_LEN);
    b += "WAVE";
    b += "fmt ";
    b += dword(16);
    b += word(1);
    b += word(1);
    b += dword(SAMPLE_RATE);
    b += dword(SAMPLE_RATE);
    b += word(1);
    b += word(8);
    b += "data";
    b += dword(DATA_LEN);
    b += tone(83, .1);
    b += tone(88, 1);

    st = new ActiveXObject("ADODB.Stream");
    st.Charset = "iso-8859-1";
    st.Type = adTypeText;
    st.Open();
    st.WriteText(b);
    st.SaveToFile("coin.wav", adSaveCreateOverWrite);
    st.Close();

    wmp = new ActiveXObject("WMPlayer.OCX");
    wmp.URL = "coin.wav";
    wmp.Controls.play();
    WScript.Sleep(1500);
}

function word(x) { return num_le(x, 2); }
function dword(x) { return num_le(x, 4); }
function num_le(x, n) {
    b = "";
    for (i = 0; i < n; i++) {
        b += String.fromCharCode(x & 0xff);
        x >>>= 8;
    }
    return b;
}

function tone(note, sec) {
    freq = 440 * Math.pow(2, (note - 69) / 12);
    len = SAMPLE_RATE * sec;
    t = 0;
    lv = SAMPLE_RATE * 16;
    b = "";
    for (i = 0; i < len; i++) {
        h = Math.floor(lv / SAMPLE_RATE);
        lv -= 16;
        c = 0x80 + ((t < SAMPLE_RATE/2) ? h : -h);
        b += String.fromCharCode(c);
        t = (t + freq) % SAMPLE_RATE;
    }
    return b;
}

main();
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2