Solved

Sending MIDI notes to built-in soundcard

Posted on 2014-11-15
4
319 Views
Last Modified: 2014-11-19
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
0
Comment
Question by:hindersaliva
  • 2
4 Comments
 
LVL 39

Expert Comment

by:als315
ID: 40445409
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
0
 

Author Comment

by:hindersaliva
ID: 40445584
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

0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40445679
The issue is that the code is too simple, and the device calls to slow, to get something useful. You can try out by multiplying the Duration in line 29 with 10 - you should here sounds (if MIDI works at all). The original Duelling Banjos sample does produce sound only in a reasonable way if I double the duration. This even applies for playing one single note.

You might also have to set up the volume for Excel (or MS VBA) in your Mixer application first.

Please try with this slightly improved code, which gives better results:
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
    midiOutShortMsg hMidiOut, RGB(192, voiceNum - 1, 127)
    lanote = 12 + CLng(noteNum)
    midiOutShortMsg hMidiOut, RGB(144, lanote, 127)
    Sleep (Duration)
    midiOutShortMsg hMidiOut, RGB(128, lanote, 127)
 End Sub

Sub PlayWorksheetNotes()
    Dim r As Long
    'ActiveSheet.Calculate
    midiOutOpen hMidiOut, 0, 0, 0, 0
    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
    midiOutClose hMidiOut
 End Sub
 

Open in new window

The improvement is that the MIDI device is not reopened and closed which each note, but only once. And the duration is implemented by Note On, Wait, Note Off now.
0
 

Author Comment

by:hindersaliva
ID: 40451812
Qlemo, yes it now works as it should. Thank you.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now