Tuesday, October 16, 2012

Visual Basic Gangnam Style!

Last week I was browsing though my RSS feeds and came across a post called Python Gangnam Style! that was all about playing music with Python. Now Python is a fine language but at heart I am a Visual Basic person. Clearly converting the program to Visual basic was a must do. It turns out to be pretty easy as you might expect. My version is below (it plays in response to a button click)

I’m sure there are other ways to do this by the way. I took the path of least resistance. I think that making music is something that many students do enjoy. This sort of project involves music, and physics as well as computer science. So it gives you a lot of things to talk about and allows you to tie different disciplines together as tends to happen in the so-called real world.

Imports System.Runtime.InteropServices


 


Public Class Form1


    <DllImport("Kernel32.dll")> _


    Public Shared Function Beep(dwFreq As Int32, _


                                dwDuration As Int32) As Boolean


    End Function


 


 


    Private Sub Button1_Click(sender As Object, e As EventArgs) _


                Handles Button1.Click


        Beep(293, 200)  ' D


        Beep(293, 200)  ' D


        Beep(293, 200)  ' D


        Beep(293, 600)  ' D


        Beep(246, 600)  ' B


 


        System.Threading.Thread.Sleep(0.1)


 


        Beep(369, 200) ' F#


        Beep(369, 200) ' F#


        Beep(369, 200) ' F#


        Beep(369, 600) ' F#


        Beep(329, 600) ' E


 


        System.Threading.Thread.Sleep(0.1)


 


        Beep(329, 200)  ' E


        Beep(329, 200)  ' E


        Beep(329, 200)  ' E


        Beep(369, 500)  ' F#


 


        System.Threading.Thread.Sleep(0.9)


 


        Beep(369, 200)  ' F#


        Beep(369, 200)  ' F#


        Beep(369, 200)  ' F#


        Beep(369, 600)  ' F#


 


        System.Threading.Thread.Sleep(0.9)


        Beep(369, 200)  ' F#


        Beep(369, 200)  ' F#


        Beep(369, 200)  ' F#


 


        For i = 1 To 4


            Beep(369, 200)  ' F#


            System.Threading.Thread.Sleep(0.1)


        Next


 


        For i = 1 To 4


            Beep(369, 100)  ' F#


            System.Threading.Thread.Sleep(0.1)


 


            Beep(369, 600)  ' F#


        Next


 


 


    End Sub


End Class




BTW if you are as musically challenged as I am, you can visit this full list of the hertz frequencies to get the other octaves for higher and lower notes. I confess that page doesn’t mean all that much to me but I’d imagine it does to the more musical among us.

No comments:

Post a Comment