Generating tones of different frequencies and running batch commands

keyboard1333

Member
Joined
Nov 18, 2012
Messages
11
Programming Experience
1-3
Greetings all :)
I have three seperate questions, but they're related to the same project, so I decided to post them in the same thread.

#1 -
Within a vb.net console application, how can you generate tones of different frequencies? I know of the command Console.Beep(), but how can you change the length of the tone, and its pitch?

#2 -
How can I run a variety of batch commands within a vb.net console application?

#3 -
How can I temporarily empty the enviromental variable path, just untill the console is closed?

Thanks everyone :)
keebs
 
1. If you know about the Console.Beep method then I don't really know why you need to ask that question. Just read the documentation for that method and it will tell you.
2. I'm not quite sure what you mean by "batch commands" but whatever you want to do I imagine that you will want to use Process.Start. You can use that method to do basically the equivalent of typing into a command prompt.
3. You can call the Environment.GetEnvironmentVariable and .SetEnvironmentVariable methods but I can't see a scenario where clearing the environment path would be a good idea, even temporarily. Can you explain why you want to do that? We could probably suggest a better alternative.
 
Beep(frequency, duration)

Here is some sample code:

VB.NET:
    Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
        Dim freq() As Integer = {600, 600, 650, 600, 800, 750}
        Dim dur() As Integer = {250, 250, 500, 500, 500, 1000}
        Dim words() As String = {"Happy", " ", "Birthday", " ", "to ", "you!"}
        'plays Happy Birthday
        txtSong.Clear()
        For x As Integer = 0 To 5
            txtSong.Text &= words(x)  'TextBox1.Text = TextBox1.Text & words(x)
            txtSong.Update()
            Console.Beep(freq(x), dur(x))
        Next
    End Sub

    Private Sub btnBeep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBeep.Click
        Console.Beep()
        Console.Beep(32767, 1000)   'upper range of frequency, 1 second (inaudible)
        Console.Beep(800, 250)      'same as beep w/no arguments
        Console.Beep(10000, 2000)   '2 seconds of silence
        Console.Beep(37, 500)       'lower range of frequency, 1/2 second
    End Sub
 
Thanks for your responses!
I'll try that code out in a minute solitare. Thanks :)

As to your questions -
I already tried Process.Start, but it just gave me errors... I'll try it again and see if I'm doing something syntactically wrong.
This application is called from the cmd or a batch file.
I want to disable all other batch commands after this application has been called.
Emptying the path variable should do that yes?

Edit -
I didn't realise that the beep command could take duration and frequency as parameters.... Thanks :)
 
Last edited:
I already tried Process.Start, but it just gave me errors... I'll try it again and see if I'm doing something syntactically wrong.
You must be doing something wrong. We might be able to help fix it but we'd have to know what you did and what happened.
This application is called from the cmd or a batch file.
I want to disable all other batch commands after this application has been called.
Emptying the path variable should do that yes?
The path environment variable is simply a list of folders for which you don't have to qualify the names of files they contain in order to execute them. For example, "C:\Windows\system32" is in the path so you can simply type the name of a file in that folder instead of qualifying it with the folder path. Utilities in that folder can still be executed by qualifying them even if the folder is not included in the path but if they are not qualified then an error will occur. What you're suggesting will probably give you the result you want but it's extremely crude. If someone gave me software that did that I would bin it immediately.
I didn't realise that the beep command could take duration and frequency as parameters
That's why you should always read the documentation.
 
You must be doing something wrong. We might be able to help fix it but we'd have to know what you did and what happened.

I'll try to work out what is going wrong...

The path environment variable is simply a list of folders for which you don't have to qualify the names of files they contain in order to execute them. For example, "C:\Windows\system32" is in the path so you can simply type the name of a file in that folder instead of qualifying it with the folder path. Utilities in that folder can still be executed by qualifying them even if the folder is not included in the path but if they are not qualified then an error will occur. What you're suggesting will probably give you the result you want but it's extremely crude. If someone gave me software that did that I would bin it immediately.

Well, sorry to tell you mate, but that's the point of the software.
It might seem stupid to you, but in the case of what else it does, it makes perfect sense.

All the same, is there a less crude way you could achieve my goal?
Also, instead of the "x is not recognisable as an internal

That's why you should always read the documentation.
Point taken.
 
For #1 -

Code -
VB.NET:
Module Module1
    Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs
    Sub Main()
        If CommandLineArgs.Count() = 0 Then
            Console.WriteLine("Error: No type parameter passed.")
            Exit Sub
        End If
        If CommandLineArgs(0).ToLower() = "off" Or CommandLineArgs(0).ToLower() = "on" Then
            Process.Start("echo test")
        Else
            Console.WriteLine("Error: Invalid type parameter.")
        End If
    End Sub

End Module


Also, you complained because that method of stopping batch commands is so crude... is there another way you can do it that would allow better errors messages?

The error message -
 

Attachments

  • Capture.PNG
    Capture.PNG
    16.3 KB · Views: 51
The others have mostly said it all, it's just Console.Beep([Frequency/Pitch],[Duration]) with the first value in Hertz and the second in Milliseconds. Also, this article might be useful to you:

Piano key frequencies - Wikipedia, the free encyclopedia

This tells you all the frequencies for keys on a piano - you can use these to create Console Beeps for every key if you so wished. Or you could program it so when you pressed a key on your keyboard, it activated a certain frequency beep. You'd have your own Console Piano. Though I have to tell you having made one, that they can be really annoying ;).
 
Thanks for your reply Mynotoar.
I already figured out the Console.Beep(), but that link is usefull. Thanks :)
However, I still need help with the other two... Does anyone have any suggestions?
keebs
 
Back
Top