|
-
Feb 27th, 2010, 02:26 PM
#1
Thread Starter
Member
Musicprogram
Hi. I'm having problem with a program that generates a random melody.
Code:
Label5.Text = (Int(Rnd() * 6)) Generate the first tone.
If Label5.Text = 0 Then
Label1.Text = "A"
ElseIf Label5.Text = 1 Then
Label1.Text = "B"
ElseIf Label5.Text = 2 Then
Label1.Text = "C"
ElseIf Label5.Text = 3 Then
Label1.Text = "D"
ElseIf Label5.Text = 4 Then
Label1.Text = "E"
ElseIf Label5.Text = 5 Then
Label1.Text = "F"
ElseIf Label5.Text = 6 Then
Label1.Text = "G"
End If
'Label 2
Label5.Text = (Int(Rnd() * 6)) Generate the second tone
If Label5.Text = 0 Then
Label2.Text = "A"
ElseIf Label5.Text = 1 Then
Label2.Text = "B"
ElseIf Label5.Text = 2 Then
Label2.Text = "C"
ElseIf Label5.Text = 3 Then
Label2.Text = "D"
ElseIf Label5.Text = 4 Then
Label2.Text = "E"
ElseIf Label5.Text = 5 Then
Label2.Text = "F"
ElseIf Label5.Text = 6 Then
Label2.Text = "G"
End If
'Label 3
Label5.Text = (Int(Rnd() * 6)) Generate the third tone
If Label5.Text = 0 Then
Label3.Text = "A"
ElseIf Label5.Text = 1 Then
Label3.Text = "B"
ElseIf Label5.Text = 2 Then
Label3.Text = "C"
ElseIf Label5.Text = 3 Then
Label3.Text = "D"
ElseIf Label5.Text = 4 Then
Label3.Text = "E"
ElseIf Label5.Text = 5 Then
Label3.Text = "F"
ElseIf Label5.Text = 6 Then
Label3.Text = "G"
End If
'Label 4
Label5.Text = (Int(Rnd() * 6)) Generate the forth tone
If Label5.Text = 0 Then
Label4.Text = "A"
ElseIf Label5.Text = 1 Then
Label4.Text = "B"
ElseIf Label5.Text = 2 Then
Label4.Text = "C"
ElseIf Label5.Text = 3 Then
Label4.Text = "D"
ElseIf Label5.Text = 4 Then
Label4.Text = "E"
ElseIf Label5.Text = 5 Then
Label4.Text = "F"
ElseIf Label5.Text = 6 Then
Label4.Text = "G"
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Randomize()
End Sub
Here is the problem
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If the user clicks "play melody"
If Label1.Text = "A" Then Play the first tone
My.Computer.Audio.Play("C:\Tunes\A.wav")
ElseIf Label1.Text = "B" Then
My.Computer.Audio.Play("C:\Tunes\B.wav")
ElseIf Label1.Text = "C" Then
My.Computer.Audio.Play("C:\Tunes\C.wav")
ElseIf Label1.Text = "D" Then
My.Computer.Audio.Play("C:\Tunes\D.wav")
ElseIf Label1.Text = "E" Then
My.Computer.Audio.Play("C:\Tunes\E.wav")
ElseIf Label1.Text = "F" Then
My.Computer.Audio.Play("C:\Tunes\F.wav")
ElseIf Label1.Text = "G" Then
My.Computer.Audio.Play("C:\Tunes\G.wav")
End If
If Label1.Text = "A" Then Play the second tone
My.Computer.Audio.Play("C:\Tunes\A.wav")
ElseIf Label2.Text = "B" Then
My.Computer.Audio.Play("C:\Tunes\B.wav")
ElseIf Label2.Text = "C" Then
My.Computer.Audio.Play("C:\Tunes\C.wav")
ElseIf Label2.Text = "D" Then
My.Computer.Audio.Play("C:\Tunes\D.wav")
ElseIf Label2.Text = "E" Then
My.Computer.Audio.Play("C:\Tunes\E.wav")
ElseIf Label2.Text = "F" Then
My.Computer.Audio.Play("C:\Tunes\F.wav")
ElseIf Label2.Text = "G" Then
My.Computer.Audio.Play("C:\Tunes\G.wav")
End If
If Label3.Text = "A" Then Play the third tone
My.Computer.Audio.Play("C:\Tunes\A.wav")
ElseIf Label3.Text = "B" Then
My.Computer.Audio.Play("C:\Tunes\B.wav")
ElseIf Label3.Text = "C" Then
My.Computer.Audio.Play("C:\Tunes\C.wav")
ElseIf Label3.Text = "D" Then
My.Computer.Audio.Play("C:\Tunes\D.wav")
ElseIf Label3.Text = "E" Then
My.Computer.Audio.Play("C:\Tunes\E.wav")
ElseIf Label3.Text = "F" Then
My.Computer.Audio.Play("C:\Tunes\F.wav")
ElseIf Label3.Text = "G" Then
My.Computer.Audio.Play("C:\Tunes\G.wav")
End If
If Label3.Text = "A" Then Play the forth tone
My.Computer.Audio.Play("C:\Tunes\A.wav")
ElseIf Label3.Text = "B" Then
My.Computer.Audio.Play("C:\Tunes\B.wav")
ElseIf Label3.Text = "C" Then
My.Computer.Audio.Play("C:\Tunes\C.wav")
ElseIf Label3.Text = "D" Then
My.Computer.Audio.Play("C:\Tunes\D.wav")
ElseIf Label3.Text = "E" Then
My.Computer.Audio.Play("C:\Tunes\E.wav")
ElseIf Label3.Text = "F" Then
My.Computer.Audio.Play("C:\Tunes\F.wav")
ElseIf Label3.Text = "G" Then
My.Computer.Audio.Play("C:\Tunes\G.wav")
End If
End Sub
But fore some reason, when the program has played the first tone, it won't play the rest. Why?
-
Feb 27th, 2010, 02:38 PM
#2
Re: Musicprogram
Wow, sorry to say but that's some ugly code. You could reduce that to a few lines max.
For the first part, generating a tone could be done much more easily. You could put the notes in a List(Of String), and use the random number as the index to that list.
Also, instead of using Rnd, use the Random class.
Code:
Dim notes As New List(Of String)
notes.AddRange(New String() {"A", "B", "C", "D", "E", "F", "G"})
Dim r As New Random
Dim index As Integer = r.Next(0, notes.Count) 'generate index between 0 and number of items in list
' take the note at the random index
Dim note As String = notes(index)
' and set the label text
Label1.Text = note
The second part is even easier
Code:
Dim note As String = Label1.Text 'get the note name
Dim fileName As String = "C:\Tunes\" & note & ".wav" 'generate filename from note name
My.Computer.Audio.Play(fileName)
As for your actual problem, I can't follow your code so I've no idea what you're trying to do. I think your problem is that the notes will be played simulatenously (which will probably result in only playing the last file), because you are doing something like this:
Code:
If Label1.Text = "A" then play A
If Label2.Text = "B" then play B
If Label3.Text = "D" then play D
If Label4.Text = "A" then play A
That code will not pause anywhere in between (at least, I don't think the Play method will halt execution, or does it?), so it will get executed almost at the same time!
-
Feb 27th, 2010, 02:46 PM
#3
Thread Starter
Member
Re: Musicprogram
The problem is like this code
My.Computer.Audio.Play ("Blabla")
My.Computer.Audio.Play ("Blabla2")
My.Computer.Audio.Play ("Blabla3")
My.Computer.Audio.Play ("Blabla4")
The program plays Blabla, but skips blabla 2-4
-
Feb 27th, 2010, 03:00 PM
#4
Addicted Member
Re: Musicprogram
First off, Nick is right, you really need to learn a little more about list and arrays
secondly, it looks like it is stopping because your if statement
I'm not sure your follow through, but i do know from personal experiance, when dealing with wav files, unless you're going to monitor ur thread and control it optimally, you're going to have trouble repeatedly, Select Case, and even For Each statements have brought me much better results with minimal thread control, i'm know expert and not completely sur why this is, but id imagine it has to do with relativity of namespaces within th dotnet libraries, just a guess.
Anyway, I'd really suggest, before you try somthing this somewhat complex, you check google for tutorials on Arrays, and Collections, and List
just google search
"Arrays vb.net" and you'll get a ton of tutorials to check out
-
Feb 27th, 2010, 03:19 PM
#5
Addicted Member
Re: Musicprogram
Let me add, don't let any of nick's or mine comments discourage you, dotnet is an easy language and only takes a little time and personal research to figure out,
I'll keep on this thread, post when you have some updated info, maybe a better example of what is going on.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|