Results 1 to 9 of 9

Thread: Morse Code Translator - Play Audio & Image Sequentially Issue

  1. #1

    Thread Starter
    New Member Clooder's Avatar
    Join Date
    Apr 2016
    Posts
    5

    Morse Code Translator - Play Audio & Image Sequentially Issue

    The Morse Code Translator is for a school project. I'm fairly inexperienced with coding. I'll try to explain the program the best I can.

    There are 2 Textboxes and 1 Button. Input a character, word or sentence into TextBox1, Click the Translate Button, and then TextBox2 will show those characters in Morse Code.

    The button also plays Audio from my resources. (If I type A and click the button, a 'dit + dah' (.-) will play)
    The button also flashes a picturebox with an image of a light that corresponds to the dits and dahs.

    The Problem


    When there are 2 or more characters entered, only the audio for the last letter plays. (Type CAT, only audio for T plays)

    I countered this by using WaitToComplete instead of Background

    My.Computer.Audio.Play(My.Resources.MorseA, _
    AudioPlayMode.WaitToComplete)


    And it worked, it played the audio for C, A then T. BUT WaitToComplete stops executing code, and now my pictureboxes won't flash. I've tried using timers and progressbars, but I'm really struggling to get it right.

    Is there a way to play the audio files sequentially, without stopping the code from executing, allowing the pictureboxes to flash with the audio?

    This is probably not a great explanation, but please do ask questions if you don't understand something. And thanks in advance for the help!

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Morse Code Translator - Play Audio & Image Sequentially Issue

    try a BackgroundWorker for playing the audio. put the code for it in the DoWork event. that will run it on a secondary thread. leave the rest of the code that deals with images where it is at behind the button click.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member Clooder's Avatar
    Join Date
    Apr 2016
    Posts
    5

    Re: Morse Code Translator - Play Audio & Image Sequentially Issue

    I tried doing a bit of research on background workers, but I still don't understand how to use them although it possibly could be a solution.
    The audio is in the DoWork event but I'm not sure how to use it.


    Here's The Code before I tried using BackgroundWorker ---- For Morse A and B... etc.

    Code:
    Function EngToMorse(ByVal ToMorse As String) As Boolean
            Select Case ToMorse
    
                Case "A"
                    TextBox2.Text = TextBox2.Text & " " & A
    
                    My.Computer.Audio.Play(My.Resources.MorseA, _
                    AudioPlayMode.Background)
    
    
                Case "B"
                    TextBox2.Text = TextBox2.Text & " " & B
    
                    My.Computer.Audio.Play(My.Resources.MorseB, _
            AudioPlayMode.Background)
    
    Dim A As String = ".-"
    Dim B As String = "-..."
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim text As String = TextBox1.Text.ToUpper()            'Convert the text entered to upper case        
            TextBox2.Text = ""                                                 'Refresh the Output Textbox
    
            For i As Integer = 0 To text.Length - 1
                EngToMorse(text.Chars(i).ToString())                     'Grabs letters sequentially
    
            Next
        End Sub
    Definitely not the most efficient way.
    Also, I'm not worrying about the images right now. Just getting audio to play for each character in the textbox without WaitToComplete.
    But where and how do I get the BackgroundWorker to Play Audio?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Morse Code Translator - Play Audio & Image Sequentially Issue

    something like this:

    Code:
    Imports System.ComponentModel
    
    Public Class Form1
    
        Private WithEvents bgw As New BackgroundWorker
    
        Private Delegate Sub appendTextCallBack(ByVal text As String)
        Private Sub appendText(ByVal text As String)
            If TextBox2.InvokeRequired Then
                TextBox2.Invoke(New appendTextCallBack(AddressOf appendText), text)
            Else
                TextBox2.Text &= " " & text
            End If
        End Sub
    
        Private Delegate Sub showImageCallBack(ByVal img As Bitmap)
        Private Sub showImage(ByVal img As Bitmap)
            If PictureBox1.InvokeRequired Then
                PictureBox1.Invoke(New showImageCallBack(AddressOf showImage), img)
            Else
                PictureBox1.Image = img
                PictureBox1.Refresh()
            End If
        End Sub
    
        Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
            Dim text As String = e.Argument.ToString
    
            For i As Integer = 0 To text.Length - 1
                appendText(text.Chars(i).ToString())
                showImage([yourImageFile])
                My.Computer.Audio.Play(DirectCast(My.Resources.ResourceManager.GetObject("Morse" & text.Chars(i).ToString()), IO.Stream), _
                AudioPlayMode.WaitToComplete)
            Next
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim text As String = TextBox1.Text.ToUpper()            'Convert the text entered to upper case        
            TextBox2.Text = ""                                                 'Refresh the Output Textbox
    
            bgw.RunWorkerAsync(text)
        End Sub
    
    End Class
    Last edited by .paul.; Apr 17th, 2016 at 10:55 PM.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Morse Code Translator - Play Audio & Image Sequentially Issue

    Almost... I figured the image handling could remain in the main thread, and only handle the audio portion from the BGW. I'm guessing that there's more than one image involved, as far as I know PictureBoxes don't show animated images (I could be wrong) and so in order to show even a simple "dat" it would have to swap in the "on" image, then swap in the "off" image. But the playing of the audio files interferes with that, which is why I suggested offloading the audio to the alternate thread.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Morse Code Translator - Play Audio & Image Sequentially Issue

    Pictureboxes can show animated gifs but they tend to lag...

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Morse Code Translator - Play Audio & Image Sequentially Issue

    That's cool, I didn't know that... it's been sometimes since I've done anything "that fancy pants". There isn't usually much call for animation in enterprise software.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    New Member Clooder's Avatar
    Join Date
    Apr 2016
    Posts
    5

    Re: Morse Code Translator - Play Audio & Image Sequentially Issue

    I've taken some time to understand your code .paul. and it's working great! Just had to adapt it a bit to fit the project. But there were a few problems, and some I could luckily solve on my own. But there are 2 big issues I don't know how to get around.

    1. When I use a space " " or any other character that is not a letter "," or " ' " in the textbox, the program crashes. I think it's because there isn't a file audio file for those characters. Is there a way to make exceptions for every other character, so it does not search for an audio file?

    (Crashes at the audio line below)

    Code:
            For i As Integer = 0 To text.Length - 1
                appendText(text.Chars(i).ToString())
                showImage(My.Resources.YellowLight)
                My.Computer.Audio.Play(DirectCast(My.Resources.ResourceManager.GetObject("Morse" & text.Chars(i).ToString()), IO.Stream), _
                    AudioPlayMode.WaitToComplete)
            Next
    2. If you click the button to translate again, while bgw is doing its work, it crashes and says the bgw is busy. Is there a way to cancel the bgw with the same button or another button.

    Thanks for your patience everyone!

  9. #9
    Addicted Member
    Join Date
    Nov 2011
    Posts
    223

    Re: Morse Code Translator - Play Audio & Image Sequentially Issue

    Each of your problems can be dealt with using a conditional statement.

    In the first case if you only have audio for the upper case letters A through Z the condition could be something like the following which skips anything outside the limits.


    Code:
     Dim mychar As Integer = Asc(text.Chars(i).ToString)
    
            If mychar > 64 AndAlso mychar < 91 Then  'decimal values A through Z
               
                    My.Computer.Audio.Play(DirectCast(My.Resources.ResourceManager.GetObject("Morse" & text.Chars(i).ToString()), IO.Stream), _
                    AudioPlayMode.WaitToComplete)
    
            End If
    The second condition can be used to cancel the BGW or wait until it has finished.

    Code:
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If BackgroundWorker1.IsBusy Then
    
            Else
    
            End If
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width