◇WindowsのバッチファイルでBeepや音楽を鳴らす

# ◇Beepや音楽を鳴らす
様々な方法がありますのでまとめて紹介します。
PowerShellを使うと好きな音楽を奏でることもできます。
guitarrapc_tech様の「[PowerShellでビープ音の「ドレミの歌」を奏でてみよう][]」がイイ感じです。

<details><summary>Beepや音楽を鳴らすのサンプル</summary>

```bat:BeepMusic.bat
@echo off


:: 遅延展開宣言 バグの原因になる場合があります;
setlocal enabledelayedexpansion


:: Beepや音楽を鳴らす;
echo.
echo 方法 1
echo 制御文字をそのまま出力する
pause
echo 

echo.
echo 方法 2
echo 制御文字を作成する
pause
for /f "delims=0" %%a in ('cmd.exe /u /c echo 〇') do @echo %%a

echo.
echo 方法 3
echo 制御文字を作成し環境変数に登録して鳴らす
pause
for /f "delims=0" %%a in ('cmd.exe /u /c echo 〇') do set BEL=%%a
echo %BEL%

echo.
echo 方法 4
echo Windows標準のビープを鳴らす
pause
rundll32 user32.dll,MessageBeep

echo.
echo 方法 5
echo Powershellを使って自由な音階で鳴らす
pause
PowerShell ^&{Start-Sleep -milliseconds 100;[Console]::Beep(524,500);[Console]::Beep(660,500);[Console]::Beep(784,500);[Console]::Beep(1046,900);}


:: 終了処理（エクスプローラーから起動されていた場合はPauseする）;
:EndProcess
echo %cmdcmdline%|find /i "%~f0">NUL
if %ERRORLEVEL% equ 0 Pause
goto :end
```


</details>

### 〇音階（周波数）を指定して音を鳴らす

<details><summary>再生できる音階（周波数）をテストするサンプル</summary>

```bat:ScaleFreqTest.bat
@echo off


:: 遅延展開 バグの原因になる場合があるので注意;
setlocal enabledelayedexpansion


:: BPMを元に４分音符（単位ミリ秒）を計算;
set BPM=120
set /a QuarterNote=60 * 1000 / %BPM%
set REST=Start-Sleep -milliseconds
:: 音階の国際表記と周波数（小数点以下を活かすため1000倍にしておく）;
set Cn0=16352
set Cs0=17324
set Dn0=18354
set Ds0=19446
set En0=20602
set Fn0=21827
set Fs0=23125
set Gn0=24500
set Gs0=25957
set An0=27500
set As0=29135
set Bn0=30868
:: 音階毎に周波数を設定;
for /l %%i in (0, 1, 9) do (
  set /a RATE=1
  for /l %%x in (1, 1, %%i) do (set /a RATE=!RATE!*2)
  set /a Cn%%i=%Cn0% * !RATE! / 1000
  set /a Cs%%i=%Cs0% * !RATE! / 1000
  set /a Dn%%i=%Dn0% * !RATE! / 1000
  set /a Ds%%i=%Ds0% * !RATE! / 1000
  set /a En%%i=%En0% * !RATE! / 1000
  set /a Fn%%i=%Fn0% * !RATE! / 1000
  set /a Fs%%i=%Fs0% * !RATE! / 1000
  set /a Gn%%i=%Gn0% * !RATE! / 1000
  set /a Gs%%i=%Gs0% * !RATE! / 1000
  set /a An%%i=%An0% * !RATE! / 1000
  set /a As%%i=%As0% * !RATE! / 1000
  set /a Bn%%i=%Bn0% * !RATE! / 1000
)


echo.
echo BPM=%BPM%
echo QuarterNote=%QuarterNote%ms
echo.
echo ♪再生テスト（ドミソド）
echo.
PowerShell ^&{^
  (%REST% %QuarterNote%);^
  Write-Output "Cn5=%Cn5%Hz";[Console]::Beep(%Cn5%, %QuarterNote%);^
  Write-Output "En5=%En5%Hz";[Console]::Beep(%En5%, %QuarterNote%);^
  Write-Output "Gn5=%Gn5%Hz";[Console]::Beep(%Gn5%, %QuarterNote%);^
  Write-Output "Cn6=%Cn6%Hz";[Console]::Beep(%Cn6%, %QuarterNote%*2)^
}

pause

echo.
echo ♪周波数の再生範囲テストします。
echo.
echo プログラム上で指定できる数値の範囲は整数値37～32767となっています。
echo ここではDs1(38Hz)からBn9(15804Hz)まで再生します。
echo.
echo 88鍵のピアノは一番低い音がAn0(27.5Hz)、一番高い音はCn8(4186Hz)とされてます。
echo 実際、音楽でメインの旋律にそこまで低音、高音が使われることはほとんどありません。
echo.
echo 環境によって再生できる周波数は違うと思われます。
echo 私の環境では低音はDS1(38Hz)から再生されていますがブツブツとノイズ音の方が大きく
echo 高音はGn9(12544Hz)から音が聞こえなくなりました。
echo.

pause
echo.

PowerShell ^&{^
  (%REST% %QuarterNote%);^
  Write-Output "Ds1=%Ds1%Hz";[Console]::Beep(%Ds1%, %QuarterNote%);^
  Write-Output "En1=%En1%Hz";[Console]::Beep(%En1%, %QuarterNote%);^
  Write-Output "Fn1=%Fn1%Hz";[Console]::Beep(%Fn1%, %QuarterNote%);^
  Write-Output "Fs1=%Fs1%Hz";[Console]::Beep(%Fs1%, %QuarterNote%);^
  Write-Output "Gn1=%Gn1%Hz";[Console]::Beep(%Gn1%, %QuarterNote%);^
  Write-Output "Gs1=%Gs1%Hz";[Console]::Beep(%Gs1%, %QuarterNote%);^
  Write-Output "An1=%An1%Hz";[Console]::Beep(%An1%, %QuarterNote%);^
  Write-Output "As1=%As1%Hz";[Console]::Beep(%As1%, %QuarterNote%);^
  Write-Output "Bn1=%Bn1%Hz";[Console]::Beep(%Bn1%, %QuarterNote%);^
  Write-Output "Cn2=%Cn2%Hz";[Console]::Beep(%Cn2%, %QuarterNote%);^
  Write-Output "Cs2=%Cs2%Hz";[Console]::Beep(%Cs2%, %QuarterNote%);^
  Write-Output "Dn2=%Dn2%Hz";[Console]::Beep(%Dn2%, %QuarterNote%);^
  Write-Output "Ds2=%Ds2%Hz";[Console]::Beep(%Ds2%, %QuarterNote%);^
  Write-Output "En2=%En2%Hz";[Console]::Beep(%En2%, %QuarterNote%);^
  Write-Output "Fn2=%Fn2%Hz";[Console]::Beep(%Fn2%, %QuarterNote%);^
  Write-Output "Fs2=%Fs2%Hz";[Console]::Beep(%Fs2%, %QuarterNote%);^
  Write-Output "Gn2=%Gn2%Hz";[Console]::Beep(%Gn2%, %QuarterNote%);^
  Write-Output "Gs2=%Gs2%Hz";[Console]::Beep(%Gs2%, %QuarterNote%);^
  Write-Output "An2=%An2%Hz";[Console]::Beep(%An2%, %QuarterNote%);^
  Write-Output "As2=%As2%Hz";[Console]::Beep(%As2%, %QuarterNote%);^
  Write-Output "Bn2=%Bn2%Hz";[Console]::Beep(%Bn2%, %QuarterNote%);^
  Write-Output "Cn3=%Cn3%Hz";[Console]::Beep(%Cn3%, %QuarterNote%);^
  Write-Output "Cs3=%Cs3%Hz";[Console]::Beep(%Cs3%, %QuarterNote%);^
  Write-Output "Dn3=%Dn3%Hz";[Console]::Beep(%Dn3%, %QuarterNote%);^
  Write-Output "Ds3=%Ds3%Hz";[Console]::Beep(%Ds3%, %QuarterNote%);^
  Write-Output "En3=%En3%Hz";[Console]::Beep(%En3%, %QuarterNote%);^
  Write-Output "Fn3=%Fn3%Hz";[Console]::Beep(%Fn3%, %QuarterNote%);^
  Write-Output "Fs3=%Fs3%Hz";[Console]::Beep(%Fs3%, %QuarterNote%);^
  Write-Output "Gn3=%Gn3%Hz";[Console]::Beep(%Gn3%, %QuarterNote%);^
  Write-Output "Gs3=%Gs3%Hz";[Console]::Beep(%Gs3%, %QuarterNote%);^
  Write-Output "An3=%An3%Hz";[Console]::Beep(%An3%, %QuarterNote%);^
  Write-Output "As3=%As3%Hz";[Console]::Beep(%As3%, %QuarterNote%);^
  Write-Output "Bn3=%Bn3%Hz";[Console]::Beep(%Bn3%, %QuarterNote%);^
  Write-Output "Cn4=%Cn4%Hz";[Console]::Beep(%Cn4%, %QuarterNote%);^
  Write-Output "Cs4=%Cs4%Hz";[Console]::Beep(%Cs4%, %QuarterNote%);^
  Write-Output "Dn4=%Dn4%Hz";[Console]::Beep(%Dn4%, %QuarterNote%);^
  Write-Output "Ds4=%Ds4%Hz";[Console]::Beep(%Ds4%, %QuarterNote%);^
  Write-Output "En4=%En4%Hz";[Console]::Beep(%En4%, %QuarterNote%);^
  Write-Output "Fn4=%Fn4%Hz";[Console]::Beep(%Fn4%, %QuarterNote%);^
  Write-Output "Fs4=%Fs4%Hz";[Console]::Beep(%Fs4%, %QuarterNote%);^
  Write-Output "Gn4=%Gn4%Hz";[Console]::Beep(%Gn4%, %QuarterNote%);^
  Write-Output "Gs4=%Gs4%Hz";[Console]::Beep(%Gs4%, %QuarterNote%);^
  Write-Output "An4=%An4%Hz";[Console]::Beep(%An4%, %QuarterNote%);^
  Write-Output "As4=%As4%Hz";[Console]::Beep(%As4%, %QuarterNote%);^
  Write-Output "Bn4=%Bn4%Hz";[Console]::Beep(%Bn4%, %QuarterNote%);^
  Write-Output "Cn5=%Cn5%Hz";[Console]::Beep(%Cn5%, %QuarterNote%);^
  Write-Output "Cs5=%Cs5%Hz";[Console]::Beep(%Cs5%, %QuarterNote%);^
  Write-Output "Dn5=%Dn5%Hz";[Console]::Beep(%Dn5%, %QuarterNote%);^
  Write-Output "Ds5=%Ds5%Hz";[Console]::Beep(%Ds5%, %QuarterNote%);^
  Write-Output "En5=%En5%Hz";[Console]::Beep(%En5%, %QuarterNote%);^
  Write-Output "Fn5=%Fn5%Hz";[Console]::Beep(%Fn5%, %QuarterNote%);^
  Write-Output "Fs5=%Fs5%Hz";[Console]::Beep(%Fs5%, %QuarterNote%);^
  Write-Output "Gn5=%Gn5%Hz";[Console]::Beep(%Gn5%, %QuarterNote%);^
  Write-Output "Gs5=%Gs5%Hz";[Console]::Beep(%Gs5%, %QuarterNote%);^
  Write-Output "An5=%An5%Hz";[Console]::Beep(%An5%, %QuarterNote%);^
  Write-Output "As5=%As5%Hz";[Console]::Beep(%As5%, %QuarterNote%);^
  Write-Output "Bn5=%Bn5%Hz";[Console]::Beep(%Bn5%, %QuarterNote%);^
  Write-Output "Cn6=%Cn6%Hz";[Console]::Beep(%Cn6%, %QuarterNote%);^
  Write-Output "Cs6=%Cs6%Hz";[Console]::Beep(%Cs6%, %QuarterNote%);^
  Write-Output "Dn6=%Dn6%Hz";[Console]::Beep(%Dn6%, %QuarterNote%);^
  Write-Output "Ds6=%Ds6%Hz";[Console]::Beep(%Ds6%, %QuarterNote%);^
  Write-Output "En6=%En6%Hz";[Console]::Beep(%En6%, %QuarterNote%);^
  Write-Output "Fn6=%Fn6%Hz";[Console]::Beep(%Fn6%, %QuarterNote%);^
  Write-Output "Fs6=%Fs6%Hz";[Console]::Beep(%Fs6%, %QuarterNote%);^
  Write-Output "Gn6=%Gn6%Hz";[Console]::Beep(%Gn6%, %QuarterNote%);^
  Write-Output "Gs6=%Gs6%Hz";[Console]::Beep(%Gs6%, %QuarterNote%);^
  Write-Output "An6=%An6%Hz";[Console]::Beep(%An6%, %QuarterNote%);^
  Write-Output "As6=%As6%Hz";[Console]::Beep(%As6%, %QuarterNote%);^
  Write-Output "Bn6=%Bn6%Hz";[Console]::Beep(%Bn6%, %QuarterNote%);^
  Write-Output "Cn7=%Cn7%Hz";[Console]::Beep(%Cn7%, %QuarterNote%);^
  Write-Output "Cs7=%Cs7%Hz";[Console]::Beep(%Cs7%, %QuarterNote%);^
  Write-Output "Dn7=%Dn7%Hz";[Console]::Beep(%Dn7%, %QuarterNote%);^
  Write-Output "Ds7=%Ds7%Hz";[Console]::Beep(%Ds7%, %QuarterNote%);^
  Write-Output "En7=%En7%Hz";[Console]::Beep(%En7%, %QuarterNote%);^
  Write-Output "Fn7=%Fn7%Hz";[Console]::Beep(%Fn7%, %QuarterNote%);^
  Write-Output "Fs7=%Fs7%Hz";[Console]::Beep(%Fs7%, %QuarterNote%);^
  Write-Output "Gn7=%Gn7%Hz";[Console]::Beep(%Gn7%, %QuarterNote%);^
  Write-Output "Gs7=%Gs7%Hz";[Console]::Beep(%Gs7%, %QuarterNote%);^
  Write-Output "An7=%An7%Hz";[Console]::Beep(%An7%, %QuarterNote%);^
  Write-Output "As7=%As7%Hz";[Console]::Beep(%As7%, %QuarterNote%);^
  Write-Output "Bn7=%Bn7%Hz";[Console]::Beep(%Bn7%, %QuarterNote%);^
  Write-Output "Cn8=%Cn8%Hz";[Console]::Beep(%Cn8%, %QuarterNote%);^
  Write-Output "Cs8=%Cs8%Hz";[Console]::Beep(%Cs8%, %QuarterNote%);^
  Write-Output "Dn8=%Dn8%Hz";[Console]::Beep(%Dn8%, %QuarterNote%);^
  Write-Output "Ds8=%Ds8%Hz";[Console]::Beep(%Ds8%, %QuarterNote%);^
  Write-Output "En8=%En8%Hz";[Console]::Beep(%En8%, %QuarterNote%);^
  Write-Output "Fn8=%Fn8%Hz";[Console]::Beep(%Fn8%, %QuarterNote%);^
  Write-Output "Fs8=%Fs8%Hz";[Console]::Beep(%Fs8%, %QuarterNote%);^
  Write-Output "Gn8=%Gn8%Hz";[Console]::Beep(%Gn8%, %QuarterNote%);^
  Write-Output "Gs8=%Gs8%Hz";[Console]::Beep(%Gs8%, %QuarterNote%);^
  Write-Output "An8=%An8%Hz";[Console]::Beep(%An8%, %QuarterNote%);^
  Write-Output "As8=%As8%Hz";[Console]::Beep(%As8%, %QuarterNote%);^
  Write-Output "Bn8=%Bn8%Hz";[Console]::Beep(%Bn8%, %QuarterNote%);^
  Write-Output "Cn9=%Cn9%Hz";[Console]::Beep(%Cn9%, %QuarterNote%);^
  Write-Output "Cs9=%Cs9%Hz";[Console]::Beep(%Cs9%, %QuarterNote%);^
  Write-Output "Dn9=%Dn9%Hz";[Console]::Beep(%Dn9%, %QuarterNote%);^
  Write-Output "Ds9=%Ds9%Hz";[Console]::Beep(%Ds9%, %QuarterNote%);^
  Write-Output "En9=%En9%Hz";[Console]::Beep(%En9%, %QuarterNote%);^
  Write-Output "Fn9=%Fn9%Hz";[Console]::Beep(%Fn9%, %QuarterNote%);^
  Write-Output "Fs9=%Fs9%Hz";[Console]::Beep(%Fs9%, %QuarterNote%);^
  Write-Output "Gn9=%Gn9%Hz";[Console]::Beep(%Gn9%, %QuarterNote%);^
  Write-Output "Gs9=%Gs9%Hz";[Console]::Beep(%Gs9%, %QuarterNote%);^
  Write-Output "An9=%An9%Hz";[Console]::Beep(%An9%, %QuarterNote%);^
  Write-Output "As9=%As9%Hz";[Console]::Beep(%As9%, %QuarterNote%);^
  Write-Output "Bn9=%Bn9%Hz";[Console]::Beep(%Bn9%, %QuarterNote%);^
}

echo.
echo 再生を終了しました。
echo.


:: 終了処理（エクスプローラーから起動されていた場合はPauseする）;
:EndProcess
echo %cmdcmdline%|find /i "%~f0">NUL
if %ERRORLEVEL% equ 0 (pause)
goto :eof
```
</details>

:::note info
ドレミによる音階（周波数）を指定して、ＢＰＭを指定した音符の長さで音を鳴らせします。
音楽が好きな方は、それなりに遊べる仕様になるように作りました。
:::

[PowerShellでビープ音の「ドレミの歌」を奏でてみよう]: https://tech.guitarrapc.com/entry/2013/02/05/000226


# おわりに

他にも「[⧉Windowsのバッチファイルのテクニックをご紹介します][]」にてご紹介させて頂いております。
お時間ありましたら覗いてみてください
お粗末様でした。

https://github.com/Aromatibus/

[⧉Windowsのバッチファイルのテクニックをご紹介します]: https://qiita.com/aromatibus/items/12fa4775f38c9889a686