A more conventional way to terminate a UNIX program is to have it terminate when it hits EOF. In addition to being more conventional, it also allows you to feed a set of frequencies into a pipe or to read them from a file without the danger or going into an infinite read loop if the input to your script is redirected from a file that doesn't contain QUIT on an even numbered line:
Furthermore, note that the keyboard() function does not take any input parameters (neither in your original code where it declares a local variable in the function declaration nor in the above which uses NR, NF, $0, and $1 through $NF as local variables instead of COMMAND), so the call in main() to keyboard() should be:
When looking for a restricted range of numbers, it would also be nice if you told your users what the range of allowable numbers is (or at least print a warning if you set RATE in setrate() because FREQ was out of range. And, there is no need to define hard-coded values for FREQ and RATE in your function before reading a value for FREQ and calculating the value for RATE based on that:
If I was writing this, I would probably get rid of the keyboard() function completely, and have setrate() exit if EOF is found or 0 is entered for the frequency. That way you can just feed your program a file containing the frequencies you want it to process if you'd like to feed it data non-interactively (without having to worry about putting the frequencies you want on odd lines and empty even numbered lines (except for the last even numbered line that must contain only QUIT):
Note that when I write functions in awk I use the convention that parameters immediately follow the open parenthesis in the declaration and local variables declared in the function definition are separated from parameters (if there are any) by a tab.
Note also that since BYTES is not declared as a variable in the function definition line, it is a global variable. Therefore, it would be better if it were defined once in your BEGIN clause instead of redefining its constant value every time you call the function. (The same applies to LOOPS in function main().)
And note that the syntax for assigning values to variables in awk does not require that there be no spaces surrounding the equal sign operator (although that is a requirement in the shell programming language). So, if you're going to surround all of the other awk operators with spaces, you should be consistent and do it that way in assignments as well.
But, if you are trying to learn how to write an awk program, take advantage of the inherent looping awk structure instead of trying to use the awk command language to write something that looks like a shell script or a C program. Consider something more like:
Note that I do not have sox installed on my system, so when I run this, it immediately clears the screen and prints a new prompt after system() reports that it can't find sox. If sox doesn't include a delay after producing its output, you might want to add a sleep command after the sox command in the command string you pass to system().
Last edited by Don Cragun; 04-01-2016 at 08:11 PM..
Reason: Fix typo pointed out in post #9: s/Commandpt2/COMMANDpt2/
This User Gave Thanks to Don Cragun For This Post:
Many thanks for your comments, it will keep me occupied for a while. ;o)
As for the keyboard() this will be a COMMAND function to change many factors of the code as it evolves much like AudioScope, (I am still slowly working on AudioScope BTW).
I will steal your code once I have gotten my head around it as this is my first reall attempt at awk to create a working app' that interests me. I have only been doing it since yesterday although I have used awk for very small code snippets inside my shell scripts. Again as with UNIX shell scripting this is a steep learning curve for me...
The script size could again approach that of AudioScope who knows. At the moment my attempt is proof of concept. I need to create SINE, SQUARE, SAWTOOTH+, SAWTOOTH-, TRIANGLE, PULSE+, PULSE-, ARBTRARY, and NOISE waveforms first and test. So this will be the next upload...
One good thing is that in uncalibrated output level format no external HW needs to be built except a test lead...
However many thanks, as usual you always respond with useful info along with others on here.
Well I have not been inactive but working out how to make OSX 10.14.x command line audio player have a variable sample rate.
This is a back door as afplay does not have a sample rate flag unlike aplay for ALSA, in Linux flavours.
This is a DEMO only but a derivative of it will... (2 Replies)
I intend to do an Audio Function Generator using Awk, (already started thanks to Don), but the biggest thing I have struggled with was variable frequency.
I was going to generate differing sized waveforms on the fly but that would that would mean the frequencies are dependent on any... (2 Replies)
This is a small program as a tester for a basic sweep generator for bandwidth testing of AudioScope.sh.
This DEMO is only capable of 4KHz down to about 85Hz and back due to the low bit rate, but it is proof of concept for a much wider variant using a much higher bit rate.
The file generated... (4 Replies)
I'm trying to record audio using Audacity 2.0.5 installed from SlackBuilds. My system is 64-bit Slackware 14.1 and a sound card is Intel HD Audio. I didn't change my sound system to OSS. (Default sound system in Slackware 14.1 is ALSA, isn't it?) First, I set Internal Microphone slider in KMix... (2 Replies)
A very simple crude sinewave generator.
The file required is generated inside the code, is linear interpolated and requires /dev/audio to work. Ensure you have this device, if not the download oss-compat from your OS's repository...
It lasts for about 8 seconds before exiting and saves a... (5 Replies)
A DEMO mono _pure_ sinewave generator using standard text mode Python 2.6.7 to at least 2.7.3.
This code is EASILY modifyable to Python version 3.x.x...
This DEMO kids level 1KHz generator is mainly for a MacBook Pro, (13 inch in my case), OSX 10.7.5 and above. See below...... (0 Replies)