VBAで楽曲を演奏 ~Play music with VBA~

ExcelマクロのBeep音を用いて曲を演奏してみたいと思います。

I would like to play a song using the Beep sound of an Excel macro.

ExcelではBeepAPIというものを用いて音を鳴らすことができます。取り敢えず長いですがソースコードを全文掲載します。

In Excel, you can make sounds using something called Beep API. Although the amount of sentences is large for the time being, the full source code will be posted.

Option Explicit

#If Win64 Then
  ' 64Bit
  Declare PtrSafe Function BeepAPI Lib "kernel32.dll" Alias "Beep" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
#Else
  ' 32Bit
  Declare Function BeepAPI Lib "kernel32.dll" Alias "Beep" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
#End If

Dim play_book As Workbook
Dim play_sheet As Worksheet
Dim music_scale_sheet As Worksheet

Dim score_book As Workbook
Dim score_sheet As Worksheet

Dim song_name As String

Dim i As Long
Dim j As Long
Dim k As Long
Dim temp_num As Long
Dim debug_num As Long

Dim pitch_num As Long
Dim pitch As Long
Dim pitch_time As Double

Dim temp_position As Long
Dim start_position As Long
Dim end_position As Long

Dim start_time As Double
Dim end_time As Double
Dim elapsed_time As Double
Dim interval_time As Double
Dim cumulative_time As Double

Sub set_position()

Set play_book = Workbooks("play_music.xlsm") ' ブック「play_music.xlsm」を取得
Set play_sheet = play_book.Worksheets("play")  ' ブック「play_music.xlsm」のシート「play」を取得
Set music_scale_sheet = Workbooks("play_music.xlsm").Worksheets("music_scale")  ' ブック「play_music.xlsm」のシート「music_scale」を取得

song_name = play_sheet.Cells(4, 6).Value

Set score_sheet = play_book.Worksheets(song_name)  ' ブック「play_music.xlsm」のシート「song_name」を取得

'開始位置の設定
start_position = play_sheet.Cells(5, 6).Value
If start_position <= 1 Then
    start_position = 2      '何もなければ最初から
End If
'終了位置の設定
end_position = play_sheet.Cells(6, 6).Value
If end_position <= 1 Or end_position <= start_position Then
    For i = 2 To 88 Step 1
        If score_sheet.Cells(i, 2).Value <> 0 Then
            end_position = i    '何もなければ最後まで
        End If
    Next
End If

End Sub
Sub set_note()

Set play_book = Workbooks("play_music.xlsm") ' ブック「play_music.xlsm」を取得
Set play_sheet = play_book.Worksheets("play")  ' ブック「play_music.xlsm」のシート「play」を取得
Set music_scale_sheet = Workbooks("play_music.xlsm").Worksheets("music_scale")  ' ブック「play_music.xlsm」のシート「music_scale」を取得

song_name = play_sheet.Cells(4, 6).Value

Set score_sheet = play_book.Worksheets(song_name)  ' ブック「play_music.xlsm」のシート「song_name」を取得

Call set_position                               '開始位置と終了位置の設定

For i = start_position To end_position Step 1
    pitch_num = score_sheet.Cells(i, 2)      'ドレミの音(鍵盤番号)を取得
    For j = 2 To 88 Step 1
        temp_num = music_scale_sheet.Cells(j, 1).Value                          '設定情報のドレミの音(鍵盤番号)を取得
        If pitch_num = temp_num Then
            score_sheet.Cells(i, 4).Value = music_scale_sheet.Cells(j, 3).Value                        'ドレミの音符(音階)を取得
            score_sheet.Cells(i, 5).Value = music_scale_sheet.Cells(j, 4).Value                        'ドレミの音符(音階)を取得
        End If
    Next
Next

End Sub
Sub music_test()

Set play_book = Workbooks("play_music.xlsm") ' ブック「play_music.xlsm」を取得
Set play_sheet = Workbooks("play_music.xlsm").Worksheets("play")  ' ブック「play_music.xlsm」のシート「play」を取得
Set music_scale_sheet = Workbooks("play_music.xlsm").Worksheets("music_scale")  ' ブック「play_music.xlsm」のシート「music_scale」を取得

pitch_time = 300

Call set_position                           '開始位置と終了位置の設定

If play_sheet.Cells(3, 6).Value = 1 Then
    For i = start_position To end_position Step 1
        pitch = music_scale_sheet.Cells(i + 1, 2)
        Call BeepAPI(pitch, pitch_time)
    Next
End If

End Sub
Sub play_music()

Set play_book = Workbooks("play_music.xlsm") ' ブック「play_music.xlsm」を取得
Set play_sheet = play_book.Worksheets("play")  ' ブック「play_music.xlsm」のシート「play」を取得
Set music_scale_sheet = Workbooks("play_music.xlsm").Worksheets("music_scale")  ' ブック「play_music.xlsm」のシート「music_scale」を取得

song_name = play_sheet.Cells(4, 6).Value

Set score_sheet = play_book.Worksheets(song_name)  ' ブック「play_music.xlsm」のシート「song_name」を取得

Call set_position                           '開始位置と終了位置の設定

'ループ処理の間隔の設定
interval_time = 0.01
cumulative_time = 0
temp_position = 0

If play_sheet.Cells(3, 6).Value = 2 Then
    start_time = Timer                                                               '開始時刻を取得
    
    Call make_sound                                                                  '楽曲を再生
    
End If

End Sub
Sub make_sound()

Set play_book = Workbooks("play_music.xlsm") ' ブック「play_music.xlsm」を取得
Set play_sheet = play_book.Worksheets("play")  ' ブック「play_music.xlsm」のシート「play」を取得
Set music_scale_sheet = Workbooks("play_music.xlsm").Worksheets("music_scale")  ' ブック「play_music.xlsm」のシート「music_scale」を取得

song_name = play_sheet.Cells(4, 6).Value

Set score_sheet = play_book.Worksheets(song_name)  ' ブック「play_music.xlsm」のシート「song_name」を取得

For i = start_position To end_position Step 1
    pitch_num = score_sheet.Cells(i, 2)                                         'ドレミの音(鍵盤番号)を取得
    For j = 2 To 88 Step 1
        temp_num = music_scale_sheet.Cells(j, 1).Value                          '設定情報のドレミの音(鍵盤番号)を取得
        If pitch_num = temp_num Then
            pitch = music_scale_sheet.Cells(j, 2).Value                         'ドレミの音(周波数)を取得
        End If
    Next

    pitch_time = score_sheet.Cells(i, 3)                        '鳴らす時間を取得
    Call BeepAPI(pitch, pitch_time)                             '音を鳴らす
Next

End Sub

いきなりこんな長いソースコードを見せられても困惑してしまうと思います。どちらかと言うとExcelでこんなこともできるんだという例を示してみた感じです。一応Lemonという曲の冒頭を演奏するマクロブックのファイルを作成しましたのでリンクを貼っておきます。

Even if you suddenly see such a long source code, you will be confused. If anything, it’s like showing an example that Excel can do this. I created a macro book file to play the beginning of the song called Lemon, so I will put a link here.

Excelで楽曲を演奏するマクロファイルのダウンロード

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA