Solved

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

Posted on 2003-12-09
7
276 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

760 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

20 Experts available now in Live!

Get 1:1 Help Now