Solved

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

Posted on 2003-12-09
7
289 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Accepted Solution

by:
Moglor earned 125 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

815 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

7 Experts available now in Live!

Get 1:1 Help Now