Page 1 of 1

Mini Keyboard Source (Piano)

Posted: Sun Dec 18, 2011 12:54 am
by smashapps
Here is a small app I have thrown together, I have no piano sounds so I couldn't finish it but I'm sure if you want to use this you could find some keyboard sound effects, I might have updated versions in the future, who knows.

The keyboard has mouse over effects so the keys change colors when you move the mouse over.

Screenshot:

Image

(Ps if you like my source please +rep :D)

Source:


(Check out SmashApps's website at http://smashapps.net for more tutorials and downloads)

Re: Mini Keyboard Source (Piano)

Posted: Sun Dec 18, 2011 3:22 pm
by mandai
You can generate keyboard sounds by using MIDI commands.

Re: Mini Keyboard Source (Piano)

Posted: Sun Dec 18, 2011 11:42 pm
by smashapps
Oh really, Could you give me a code sample by any chance?

Thanks in advance.

Re: Mini Keyboard Source (Piano)

Posted: Mon Dec 19, 2011 1:00 am
by mandai
You can use this:
Code: Select all
    <DllImport("winmm.dll")> Shared Function midiOutOpen(ByRef lphMidiOut As IntPtr, ByVal uDeviceID As UInteger, ByVal dwCallback As IntPtr, ByVal dwInstance As IntPtr, ByVal dwFlags As UInteger) As UInteger
    End Function

    <DllImport("winmm.dll")> Shared Function midiOutShortMsg(ByVal hMidiOut As IntPtr, ByVal dwMsg As UInteger) As UInteger
    End Function

    <DllImport("winmm.dll")> Shared Function midiOutReset(ByVal hMidiOut As IntPtr) As UInteger
    End Function

    <DllImport("winmm.dll")> Shared Function midiOutClose(ByVal hMidiOut As IntPtr) As UInteger
    End Function

    Dim hMidi As IntPtr

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        midiOutOpen(hMidi, 0, IntPtr.Zero, IntPtr.Zero, 0)
    End Sub

    Dim down As Boolean = False

    Private Sub Panel1_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
        down = False
        midiOutReset(hMidi) 'release any held keys
    End Sub

    Private Sub Panel1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
        down = True
    End Sub

    Private Sub Panel1_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
        If down Then

            If e.X > 0 And e.X < Panel1.Width  Then

                Dim command As Byte = &H90 'note on
                Dim note As Byte = e.X / (Panel1.Width / 127)
                Dim volume As Byte = 127

                Dim msgBytes As Byte() = {command, note, volume, 0}

                Dim msg As UInteger = BitConverter.ToUInt32(msgBytes, 0)
                midiOutShortMsg(hMidi, msg)

            End If

        End If
    End Sub

    Private Sub Form1_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        midiOutClose(hMidi)
    End Sub

Re: Mini Keyboard Source (Piano)

Posted: Mon Dec 19, 2011 1:11 am
by smashapps
I got your code to work but its hard to use for what I need, your code uses a Panel and every key is inside of that panel.

How would I use this code for each key?

Thanks.

EDIT: I'm using 29 pictureboxes.

Re: Mini Keyboard Source (Piano)

Posted: Mon Dec 19, 2011 1:25 am
by mandai
You could either code in 29 different notes per picturebox, or you could use a single panel and calculate/draw bars using the Paint event.

Re: Mini Keyboard Source (Piano)

Posted: Mon Dec 19, 2011 1:38 am
by smashapps
I would rather do it for the 29 keys, would be able to give me an example for the first key and the second key? then I work it out there

Thanks.

Re: Mini Keyboard Source (Piano)

Posted: Mon Dec 19, 2011 8:10 pm
by mandai
This would do 2 keys:
Code: Select all
    Dim command As Byte = &H90 'note on
    Dim volume As Byte = 127

    Private Sub PictureBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

        Dim note As Byte = 20

        Dim msgBytes As Byte() = {command, note, volume, 0}

        Dim msg As UInteger = BitConverter.ToUInt32(msgBytes, 0)
        midiOutShortMsg(hMidi, msg)

    End Sub

    Private Sub PictureBox2_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown

        Dim note As Byte = 40

        Dim msgBytes As Byte() = {command, note, volume, 0}

        Dim msg As UInteger = BitConverter.ToUInt32(msgBytes, 0)
        midiOutShortMsg(hMidi, msg)

    End Sub

    Private Sub PictureBox_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseUp, PictureBox1.MouseUp
        midiOutReset(hMidi)
    End Sub