Last active
July 2, 2018 05:39
-
-
Save GOROman/b4b8f780f5a0fcbc78c46aaeaae2c648 to your computer and use it in GitHub Desktop.
なつかしのデータレコーダ(カセット)のピーーーーギャーーー音をつくる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! ruby -Ks | |
# -*- mode:ruby; coding:shift_jis -*- | |
# 2400ボーのデータレコーダっぽいWAVをつくる | |
# 出力ファイル | |
FILENAME = "test.wav" | |
BAUD = 2400 | |
CHANNELS = 1 # チャンネル | |
BITSWIDTH = 8 # 量子化ビット | |
SAMPLERATE = 9600 # サンプリンレート(Hz) | |
FORMAT = 0x0001 # WAVE_FORMAT_PCM | |
# ブロックサイズを得る | |
BLOCKSIZE = (BITSWIDTH / 8) * CHANNELS | |
# 1秒あたりのバイト数 | |
BYTEPERSEC = BLOCKSIZE * SAMPLERATE | |
STEP = (SAMPLERATE/BAUD)/2 | |
# データ作成 | |
OFF = [0x00].pack("C") * STEP | |
ON = [0x80].pack("C") * STEP | |
data = "" | |
# ピー | |
(2*BAUD/4).times { | |
data += OFF | |
data += ON | |
data += OFF | |
data += ON | |
} | |
# ガー(乱数で適当) | |
(8*BAUD/4).times { | |
bit = rand(2) == 0 ? OFF : ON | |
data += OFF | |
data += ON | |
data += OFF | |
data += bit | |
} | |
# WAVファイル作成(RIFFフォーマット) | |
buf = ["RIFF",36+data.size,"WAVE"].pack("a4VA4") | |
buf += ["fmt",16,FORMAT,CHANNELS,SAMPLERATE,BYTEPERSEC,BLOCKSIZE,BITSWIDTH].pack("A4VvvVVvv") | |
buf += ["data"].pack("a4") + data | |
# ファイルに保存 | |
File.open(FILENAME,"wb").write buf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment