Link to home
Start Free TrialLog in
Avatar of hindersaliva
hindersalivaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Sending MIDI notes to built-in soundcard

Newbie question:
I'm trying to get Excel 2013 to send a MIDI note (say 64) to play on the built-in device on my Windows 8 laptop.

Can someone please give me a 101 ? (as a starting point for a little more challenging project)
Thanks
Avatar of als315
als315
Flag of Russian Federation image

It is relatively simple to send midi file. Look at sample (place sample midi file to any folder and correct path to file in sub)
PlayMidi.xlsm
Avatar of hindersaliva

ASKER

als315, I need to play ONE note, for example when a shape is clicked, rather than triggering the the playing of a MIDI file.

There's an example here - Duelling Banjos by John Walkenbach.
http://j-walkblog.com/index.php?/weblog/posts/dueling_banjos_in_excel/ 

But it doesn't work on my Excel 2013/Win8.

Here's his code

Option Explicit
Private Declare Function midiOutOpen Lib "winmm.dll" _
    (lphMidiOut As Long, _
    ByVal uDeviceID As Long, _
    ByVal dwCallback As Long, _
    ByVal dwInstance As Long, _
    ByVal dwFlags As Long) As Long
 
Private Declare Function midiOutClose Lib "winmm.dll" _
    (ByVal hMidiOut As Long) As Long

Private Declare Function midiOutShortMsg Lib "winmm.dll" _
    (ByVal hMidiOut As Long, _
    ByVal dwMsg As Long) As Long
 
Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
Dim hMidiOut As Long
Public lanote As Long
 
Sub PlayMIDI(voiceNum, noteNum, Duration)
    Dim Note As Long
    On Error Resume Next
    midiOutClose hMidiOut
    midiOutOpen hMidiOut, 0, 0, 0, 0
    midiOutShortMsg hMidiOut, RGB(192, voiceNum - 1, 127)
    lanote = 12 + CLng(noteNum)
    Note = RGB(144, lanote, 127)
    midiOutShortMsg hMidiOut, Note
    Sleep (Duration)
    midiOutClose hMidiOut
 End Sub

Sub PlayWorksheetNotes()
    Dim r As Long
    'ActiveSheet.Calculate
    For r = 2 To Application.CountA(Range("A:A"))
        Cells(r, 4).Select
        Call PlayMIDI(Cells(r, 1), Cells(r, 2), Cells(r, 3))
    Next r
 End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Qlemo, yes it now works as it should. Thank you.