Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

play musical notes in vb, e.g. A, B, B#

Posted on 2003-12-09
7
Medium Priority
?
373 Views
Last Modified: 2011-10-03
someone i knew had qbasic long ago and he was able to do it

he assigned a key to a tone e.g.
COMPUTER KEY A = (musical)"A"
COMPUTER KEY B = (musical)"B"



etc.

is something like this possible in vb?

0
Comment
Question by:jxharding
  • 2
  • 2
  • 2
7 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 9911966
You could use a multimedia control to play one of a set of sound (e.g.) ".wav" files according to which key is pressed.

Programmatic sound synthesis is not as fashionable (or easy) as it once was (e.g. on the Amstrad CPC464 built-in Basic). It's mostly done with sampling which gives a better sound, but of course requires the samples. DirectX would probably be necesssary, but it's an horrendous learning curve.
0
 
LVL 1

Expert Comment

by:Moglor
ID: 9912364
ok here is some code.

Private Declare Function midiOutClose Lib "winmm.dll" (ByVal hMidiOut As Long) As Long
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 midiOutShortMsg Lib "winmm.dll" (ByVal hMidiOut As Long, ByVal dwMsg As Long) As Long
Dim hMidiOut As Long

Private Sub Form_KeyPress(KeyAscii As Integer)
    SendMidiOut 144, KeyAscii, 100
End Sub

Private Sub Form_Load()
    midiOutOpen hMidiOut, 0, 0, 0, 0
End Sub

Private Sub SendMidiOut(MidiEventOut, MidiNoteOut, MidiVelOut)
    lowint = (MidiNoteOut * 256) + MidiEventOut
    VelOut = MidiVelOut * 256
    highint = VelOut * 256
    MidiMessage = lowint + highint
    midiOutShortMsg hMidiOut, MidiMessage
End Sub

Private Sub Form_Unload(Cancel As Integer)
    midiOutClose hMidiOut
End Sub
0
 

Author Comment

by:jxharding
ID: 9912556
thanks for the input!

modlor , i just cant figure out how your sub
sendmidiout works. when do i call it?

i forgot to mention the qbasic version worked on pc speaker, but who wants that these days?
thanks!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Accepted Solution

by:
Moglor earned 500 total points
ID: 9912652
just start a new project, copy paste my code, then run it. when you press a key on the keyboard it will play piano notes.
i guess if you wanted to customize it further you could remove the Form_KeyPress sub and replace it with this...

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim i As Long
    Select Case KeyCode
    Case vbKeyA
        i = 55
    Case vbKeyB
        i = 88
    Case vbKeyC
        i = 44
    Case Else
        i = KeyCode
    End Select
   
    SendMidiOut 144, i, 100
End Sub
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 10144573
I think Moglor's comments are worth preserving.
0
 

Author Comment

by:jxharding
ID: 10144841
thanks for the input
i cant get it to work, but im accepting it
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

926 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