An Audio Function Generator...

Ok guys, gals and geeks...

As from today I am starting to learn awk in earnest doing something totally different.

I am going to create a pseudo-Audio_Function Generator centred around OSX 10.11.x minimum. The code below is a tester to see what the possibilities are.

All waveforms will be 6+ bit depth using ASCII characters decimal 32 - space to decimal 126 - tilde.

It requires SOX and I am not going to use any other source.

The code below is my starting point. All criticisms welcome...

I am now getting my head around making it fully programmable using awk.
The shell is SOOOO easy but this is new to me and I want to learn...

If you try it out the simple sinewave.raw file generated will only go from 500Hz to 6KHz...

Have fun... ;o)

#!/bin/sh
# FG.sh
awk '
# Clear the terminal window.
function cls()
{
	printf "\x1B[2J\x1B[H";
}

# User keyboard interrupt.
function keyboard( COMMAND )
{
	printf "Press <CR> to continue or QUIT<CR>:- ";
	getline COMMAND;
	if ( COMMAND == "QUIT" ) { exit 0 };
}
 
# Adjust the sample rate for SOX WRT its frequency.
function setrate( RATE )
{
	cls();
	FREQ=500;
	BYTES=8;
	RATE=16000;
	printf "Enter frequency required:- ";
	getline FREQ;
	RATE=(BYTES*FREQ);
	if ( RATE <= 4000 ) { RATE=4000 };
	if ( RATE >= 48000 ) { RATE=48000 };
	return RATE;
}

# Main loop...
function main()
{
	RATE=setrate();
	LOOPS=2;
	for ( LOOP = 1; LOOP <= LOOPS; ++LOOP ) { system ( "~/sox-14.4.2/sox -q -b 8 -r "RATE" -e unsigned-integer /tmp/sinewave8.raw -d trim 0 00:01 > /dev/null 2>&1" ) };
	keyboard( COMMAND );
}

# Now generate a simple sinewave and run the _main_ code.
BEGIN \
{
	system( ">" "/tmp/sinewave8.raw" );
	SINEWAVE8="Op}pN- -";
	for ( LOOP = 1; LOOP <= 16; ++LOOP ) { SINEWAVE8=SINEWAVE8 SINEWAVE8 };
	printf SINEWAVE8 > "/tmp/sinewave8.raw";
	cls();
	while( 1 ) { main() };
}'

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:

function keyboard() {
	printf "Press <CR> to continue, or ctl-D or QUIT<CR> to exit:- ";
	if ( getline != 1 || $1 == "QUIT" ) { exit 0 }
}

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:

	keyboard();

instead of:

	keyboard( COMMAND );

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:

function setrate( RATE )
{
	cls();
	BYTES=8;
	printf("Enter frequency required (%d <= frequency <= %d):- ",
	    int((4000 + BYTES - 1) / BYTES), ((48000 + BYTES - 1) / BYTES))
	getline FREQ;
	RATE=(BYTES*FREQ);
	if ( RATE <= 4000 ) { RATE=4000 };
	if ( RATE >= 48000 ) { RATE=48000 };
	return RATE;
}

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 ):

function setrate(	FREQ, RATE )
{
	cls()
	BYTES = 8
	printf("Enter frequency to continue or ctl-D to exit.\n")
	printf("Valid frequency range: %d <= frequency <= %d:- ",
	    int((4000 + BYTES - 1) / BYTES), ((48000 + BYTES - 1) / BYTES))
	if(getline FREQ != 1 || (FREQ + 0) == 0) {
		print "Good bye."
		exit 0
	}
	RATE = (BYTES * FREQ)
	if(RATE <= 4000) RATE=4000
	if(RATE >= 48000) RATE=48000
	return RATE
}

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:

#!/bin/sh
awk '
# Now generate a simple sinewave and run the _main_ code.
BEGIN {	# Initialize variables.
	BYTES = 8
	LOOPS = 2;
	MaxRATE = 48000
	MinRATE = 4000
	SINEWAVE8 = "Op}pN- -";
	TMPFILE = "/tmp/sinewave8.raw"

	COMMANDpt1 = "~/sox-14.4.2/sox -q -b " BYTES " -e unsigned-integer -r "
	COMMANDpt2 = " " TMPFILE " -d trim 0 00:01 > /dev/null 2>&1"

	# Create waveform file.
	for(LOOP = 1; LOOP <= 16; ++LOOP) {
		SINEWAVE8 = SINEWAVE8 SINEWAVE8
	}
	printf("%s", SINEWAVE8) > TMPFILE

	# Prompt user for first input.
	prompt()
}

# Clear the terminal window.
function cls() {
	printf "\x1B[2J\x1B[H";
}

# Prompt user for input.
function prompt() {
	# Clear the screen.
	cls()

	# Issue the prompt.
	printf("Enter frequency to continue or ctl-D to exit.\n")
	printf("Valid frequency range: %d <= frequency <= %d:- ",
	    int((MinRATE + BYTES - 1) / BYTES), ((MaxRATE + BYTES - 1) / BYTES))
}

# Process a line of user supplied input.
{	# Exit if input frequency is 0 or non-numeric.
	if($1 + 0 == 0) exit
	
	# Convert input frequency to rate.
	RATE = BYTES * $1
	if(RATE <= MinRATE) {
		RATE = MinRATE
	}
	if(RATE >= MaxRATE) {
		RATE = MaxRATE
	}

	# Print the waveform.
	for(LOOP = 1; LOOP <= LOOPS; ++LOOP) {
		system(COMMANDpt1 RATE COMMANDpt2)
	}

	# Prompt for the next input frequency.
	prompt()
}

END {	# Clear screen and print exit message.
	cls()
	print "Goodbye."

	# Remove waveform file.
	exit system("rm -rf " TMPFILE)
}'

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() .

1 Like

Hi Don...

(Apologies for any typos.)

Cool.

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.

Why use shell magic cookie instead of awk one as interpreter ?

Should one use #!/bin/ksh .. awk '.. or just !#/usr/bin/awk -f or even /usr/bin/env awk ?

What should be most portable, preferred and/or standards compliant?

Regards
Peasant.

1 Like

Hi Peasant...

I had not even thought about my first line except that I don't know how to create a zero length file > somefile in awk yet.

I use the system() function to do this task at the moment...

Try:

awk 'BEGIN{printf "" >"somefile"}'
1 Like

Hi Scrutinizer...

Thanks that is what I needed...

I hadn't researched that yet as it was unimportant at this point in time... :slight_smile:

You might have noticed that I removed that system() call in the code I suggested. Scrutinizer has already shown you how to create a zero length file entirely using awk using printf with a redirection. But, since your code always creates this file with data, there is no need to do that. You can create a file (and remove any pre-existing contents, if there were any) in awk just like you can in the shell by redirecting an output command, such as the way I did with:

	printf("%s", SINEWAVE8) > TMPFILE

I should, however, have added:

	close(TMPFILE)

after that line to flush the output buffer and release the file descriptor.

You could certainly change the script from:

#!/bin/sh
awk '
most of awk commands
}'

to:

#!/usr/bin/awk -f
most of awk commands
}

and avoid invoking an extra shell to run your awk script.

But, if you want your script to process command-line options, using getopts in the shell is much easier than trying to duplicate its capabilities inside awk . And, if you have a configuration file that should always be given as the first file operand to your script (and you don't want users to have to type that configuration file's name every time they invoke your script), it is easier to process if you use a shell script to invoke awk instead of just having a awk only script:

#!/bin/sh
awk '
FNR == NR {
	process config file data
	next
}
{	process user input
}
' config -

versus:

#!/usr/bin/awk -f
BEGIN {
	while((getline < "config") == 1) {
		process config file data
	}
	close("config")
}
{	process user input
}

And, if you're creating temp files in your script that you want to remove when your script is done, the following two scripts are equally efficient as to the number of processes invoked:

#!/bin/sh
TMPFILE="/tmp/sinewave8.raw.$$"
trap 'rm -rf "$TMPFILE"' EXIT
awk -v TMPFILE="$TMPFILE" '
BEGIN {
	SINEWAVE8 = "Op}pN- -";
	for(LOOP = 1; LOOP <= 16; ++LOOP) {
		SINEWAVE8 = SINEWAVE8 SINEWAVE8
	}
	printf("%s", SINEWAVE8) > TMPFILE
	close(TMPFILE)
}'

versus:

#!/usr/bin/awk -f
BEGIN {
	SINEWAVE8 = "Op}pN- -";
	TMPFILE = "/tmp/sinewave8.raw"
	for(LOOP = 1; LOOP <= 16; ++LOOP) {
		SINEWAVE8 = SINEWAVE8 SINEWAVE8
	}
	printf("%s", SINEWAVE8) > TMPFILE
	close(TMPFILE)
}
END {
	exit system("rm -rf " TMPFILE)
}

but the shell version will remove the temp file even if the script is killed (except by kill -9 ) while the awk version will leave the temp file laying around if it is killed. And the shell version can have multiple instances running without them interfering with each other because it creates a unique temp file for each running script while the awk version has a single temp file that will be removed by the 1st instance that exits leaving other running instances with no temp file to process (producing errors when sox tries to process the non-existent temp file).

As frequently happens, this is another instance where UNIX systems give you several ways to do things and you need to decide which best suits your needs. :wink:

2 Likes

Hi all...

Steady on guys I ain't that quick... ;oD

Don...
Your code did not work initially I had to do some minor changes.
BYTES is removed as the number '8' is bit depth per sample and will never change.
The reason is I want everything in this project to be pure ASCII including waveform generation; therefore 16 bit depth is a none starter and has two problems:-
1) You have to tell sox whether the two bytes per sample are big or little endian on raw data.
This is not necessary on 8 bit data.
2) Because of 1) there will ALWAYS be binary values and this defeats text mode waveform generation.
This also means I can create an arbitrary waveform in any text editor...
LBNL. I have no intention at this point of deleting the TMPFILEs so I have commented 'rm' out...

Mods to your code:-

	#### Change these two lines...
	COMMANDpt1 = "~/sox-14.4.0/sox -q -b 8 -r "
	COMMANDpt2 = " -e unsigned-integer " TMPFILE " -d trim 0 00:01 > /dev/null 2>&1"

		#### Change here correct lowercase Commandpt2...
		system(COMMANDpt1 RATE COMMANDpt2)

	exit # system("rm -rf " TMPFILE)

It now works...
A derivative of your code will be my starting point many thanks.
BTW your code answered my next question about global variables thanks for that too.

Hi all...
This is the very start of all of the waveforms...

SINEWAVE16, SINEWAVE64, TRIANGLE16 and TRIANGLE64 have not yet been worked out WRT to correct values but this is the finished waveform generation variable and file section.
All file lengths are 524288 bytes in size.
During doing this I got the error:-
"""
/usr/bin/awk: /tmp/pulseplus64.raw makes too many open files
source line number <somenumber>
"""
And www.unix.com came to the rescue yet again:-
nawk - file limits

#!/usr/bin/awk -f
BEGIN \
{
	# General variables...
	LOOP = 1
	LOOPS = 16
	INTEGER = 32

	# Waveform variables...
	SINEWAVE4 = "O~O "
	SINEWAVE16 = "Op}pN- -Op}pN- -"
	SINEWAVE64 = "Op}pN- -Op}pN- -Op}pN- -Op}pN- -Op}pN- -Op}pN- -Op}pN- -Op}pN- -"
	SQUAREWAVE4 = "~~  "
	SQUAREWAVE16 = "~~~~~~~~        "
	SQUAREWAVE64 = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                "
	TRIANGLEWAVE4 = "O~O "
	TRIANGLEWAVE16 = "________--------"
	TRIANGLEWAVE64 = "________________________________--------------------------------"
	SAWTOOTHPLUS4 = " ?^~"
	SAWTOOTHPLUS16 = " &,29?EKRX^dkqw~"
	SAWTOOTHPLUS64 = " !#$&')*,-/0235689:<>?ABDEGHJKMNPQSTVWYZ\\^_abdeghjkmnpqstvwyz|}~"
	SAWTOOTHMINUS4 = "~^? "
	SAWTOOTHMINUS16 = "~wqkd^XRKE?92,& "
	SAWTOOTHMINUS64 = "~}|zywvtsqpnmkjhgedba_^\\ZYWVTSQPNMKJHGEDBA?><:9865320/-,*)'&$#! "
	PULSEPLUS4 = "~   "
	PULSEPLUS16 = "~               "
	PULSEPLUS64 = "~                                                               "
	PULSEMINUS4 = " ~~~"
	PULSEMINUS16 = " ~~~~~~~~~~~~~~~"
	PULSEMINUS64 = " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
	# ARBITRARY can be any length from 2 to 1024 bytes, sampling rate will be set to 96000 later...
	ARBITRARY = "Op}pN- -"
	NOISE = "$VER freq.awk_Version_0.00.01_B.Walker_G0LCU."

	# 4 byte waveforms, the sinewave and triangle wave are identical...
	for(LOOP = 1; LOOP <= 17; ++LOOP)
	{
		SINEWAVE4 = SINEWAVE4 SINEWAVE4
		SQUAREWAVE4 = SQUAREWAVE4 SQUAREWAVE4
		TRIANGLEWAVE4 = TRIANGLEWAVE4 TRIANGLEWAVE4
		SAWTOOTHPLUS4 = SAWTOOTHPLUS4 SAWTOOTHPLUS4
		SAWTOOTHMINUS4 = SAWTOOTHMINUS4 SAWTOOTHMINUS4
		PULSEPLUS4 = PULSEPLUS4 PULSEPLUS4
		PULSEMINUS4 = PULSEMINUS4 PULSEMINUS4
	}
	printf("%s", SINEWAVE4) > "/tmp/sinewave4.raw"
	close("/tmp/sinewave4.raw")
	printf("%s", SQUAREWAVE4) > "/tmp/squarewave4.raw"
	close("/tmp/squarewave4.raw")
	printf("%s", TRIANGLEWAVE4) > "/tmp/trianglewave4.raw"
	close("/tmp/trianglewave4.raw")
	printf("%s", SAWTOOTHPLUS4) > "/tmp/sawtoothplus4.raw"
	close("/tmp/sawtoothplus4.raw")
	printf("%s", SAWTOOTHMINUS4) > "/tmp/sawtoothminus4.raw"
	close("/tmp/sawtoothminus4.raw")
	printf("%s", PULSEPLUS4) > "/tmp/pulseplus4.raw"
	close("/tmp/pulseplus4.raw")
	printf("%s", PULSEMINUS4) > "/tmp/pulseminus4.raw"
	close("/tmp/pulseminus4.raw")

	# 16 byte waveforms...
	for(LOOP = 1; LOOP <= 15; ++LOOP)
	{
		SINEWAVE16 = SINEWAVE16 SINEWAVE16
		SQUAREWAVE16 = SQUAREWAVE16 SQUAREWAVE16
		TRIANGLEWAVE16 = TRIANGLEWAVE16 TRIANGLEWAVE16
		SAWTOOTHPLUS16 = SAWTOOTHPLUS16 SAWTOOTHPLUS16
		SAWTOOTHMINUS16 = SAWTOOTHMINUS16 SAWTOOTHMINUS16
		PULSEPLUS16 = PULSEPLUS16 PULSEPLUS16
		PULSEMINUS16 = PULSEMINUS16 PULSEMINUS16
	}
	printf("%s", SINEWAVE16) > "/tmp/sinewave16.raw"
	close("/tmp/sinewave16.raw")
	printf("%s", SQUAREWAVE16) > "/tmp/squarewave16.raw"
	close("/tmp/squarewave16.raw")
	printf("%s", TRIANGLEWAVE16) > "/tmp/trianglewave16.raw"
	close("/tmp/trianglewave16.raw")
	printf("%s", SAWTOOTHPLUS16) > "/tmp/sawtoothplus16.raw"
	close("/tmp/sawtoothplus16.raw")
	printf("%s", SAWTOOTHMINUS16) > "/tmp/sawtoothminus16.raw"
	close("/tmp/sawtoothminus16.raw")
	printf("%s", PULSEPLUS16) > "/tmp/pulseplus16.raw"
	close("/tmp/pulseplus16.raw")
	printf("%s", PULSEMINUS16) > "/tmp/pulseminus16.raw"
	close("/tmp/pulseminus16.raw")

	# 64 byte waveforms...
	for(LOOP = 1; LOOP <= 13; ++LOOP)
	{
		SINEWAVE64 = SINEWAVE64 SINEWAVE64
		SQUAREWAVE64 = SQUAREWAVE64 SQUAREWAVE64
		TRIANGLEWAVE64 = TRIANGLEWAVE64 TRIANGLEWAVE64
		SAWTOOTHPLUS64 = SAWTOOTHPLUS64 SAWTOOTHPLUS64
		SAWTOOTHMINUS64 = SAWTOOTHMINUS64 SAWTOOTHMINUS64
		PULSEPLUS64 = PULSEPLUS64 PULSEPLUS64
		PULSEMINUS64 = PULSEMINUS64 PULSEMINUS64
	}
	printf("%s", SINEWAVE64) > "/tmp/sinewave64.raw"
	close("/tmp/sinewave64.raw")
	printf("%s", SQUAREWAVE64) > "/tmp/squarewave64.raw"
	close("/tmp/squarewave64.raw")
	printf("%s", TRIANGLEWAVE64) > "/tmp/trianglewave64.raw"
	close("/tmp/trianglewave64.raw")
	printf("%s", SAWTOOTHPLUS64) > "/tmp/sawtoothplus64.raw"
	close("/tmp/sawtoothplus64.raw")
	printf("%s", SAWTOOTHMINUS64) > "/tmp/sawtoothminus64.raw"
	close("/tmp/sawtoothminus64.raw")
	printf("%s", PULSEPLUS64) > "/tmp/pulseplus64.raw"
	close("/tmp/pulseplus64.raw")
	printf("%s", PULSEMINUS64) > "/tmp/pulseminus64.raw"
	close("/tmp/pulseminus64.raw")

	# Generate the noise waveform...
	srand()
	for(LOOP = 1; LOOP <= 524288; ++LOOP)
	{
		INTEGER = 32 + rand() * 95
		printf("%c", INTEGER) > "/tmp/noise.raw"
	}
	close("/tmp/noise.raw")

	# Create a temporary ARBITRARY file...
	for(LOOP = 1; LOOP <= LOOPS; ++LOOP)
	{
		ARBITRARY = ARBITRARY ARBITRARY
	}
	printf("%s", ARBITRARY) > "/tmp/arbitrary.raw"
	close("/tmp/arbitrary.raw")
}

All criticisms welcome...

Hi wisecracker,
You didn't have to go searching for the too many open files problem; it was already discussed in this thread. (Take a look at post #8 in this thread again.)

Your comments say that ARBITRARY can be any length from 2 to 1024 bytes, but that isn't really true. Any change to the length of that string would also require a change tot he LOOPS variable. And, with the way you are creating the strings you're writing to your files, ARBITRARY (and all of the waveforms have to have lengths that are a power of two.

Any time you are doing the same thing more than two or three times, I strongly suggest that you consider using a function. The following gets rid of your hard coded loop counts for various input waveform lengths, runs a little faster (since space is only needed for one .5Mb string instead of growing each of your waveform strings to .5Mb), and makes it much easier if you ever want to create shorter or longer files (just change the SIZE variable to a different value that is still a power of 2):

#!/usr/bin/awk -f
function WaveFileGen(wave, file,	l, w) {
	# Generate a raw waveform file (file) of size SIZE by repeatedly
	# doubling the given wave string (wave).
	for(l = length(w = wave); l < SIZE; l *= 2)
		w = w w
	printf("%s", w) > file
	close(file)
}
BEGIN {
	# General variables...
#	LOOPS = 16	# This variable is no longer used.
#	The following two variables only used in for loops where their value is
#	set immediately before they are used.  The initial values assigned here
#	are overridden when these variables are used.
#	LOOP = 1
#	INTEGER = 32
	NOISE = "$VER freq.awk_Version_0.00.01_B.Walker_G0LCU."
	# Note that this is an awk script; not a shell script, VER is not
	# defined as an awk variable in this awk script, and even if it was
	# defined, $VER will not be expanded in an awk string constant.

	# Waveform variables...
	SIZE = 2**19	# Number of bytes to be placed in waveform files.
			# Note that since 524288 is 2**19 and WaveFileGen()
			# repeatedly doubles waveforms, the following waveforms
			# must have a length (not including the terminating null
			# byte) that is a power of 2 single-byte characters
			# long.
	SINEWAVE4 = "O~O "
	SINEWAVE16 = "Op}pN- -Op}pN- -"
	SINEWAVE64 = "Op}pN- -Op}pN- -Op}pN- -Op}pN- -Op}pN- -Op}pN- -Op}pN- -Op}pN- -"
	SQUAREWAVE4 = "~~  "
	SQUAREWAVE16 = "~~~~~~~~        "
	SQUAREWAVE64 = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                "
	TRIANGLEWAVE4 = "O~O "
	TRIANGLEWAVE16 = "________--------"
	TRIANGLEWAVE64 = "________________________________--------------------------------"
	SAWTOOTHPLUS4 = " ?^~"
	SAWTOOTHPLUS16 = " &,29?EKRX^dkqw~"
	SAWTOOTHPLUS64 = " !#$&')*,-/0235689:<>?ABDEGHJKMNPQSTVWYZ\\^_abdeghjkmnpqstvwyz|}~"
	SAWTOOTHMINUS4 = "~^? "
	SAWTOOTHMINUS16 = "~wqkd^XRKE?92,& "
	SAWTOOTHMINUS64 = "~}|zywvtsqpnmkjhgedba_^\\ZYWVTSQPNMKJHGEDBA?><:9865320/-,*)'&$#! "
	PULSEPLUS4 = "~   "
	PULSEPLUS16 = "~               "
	PULSEPLUS64 = "~                                                               "
	PULSEMINUS4 = " ~~~"
	PULSEMINUS16 = " ~~~~~~~~~~~~~~~"
	PULSEMINUS64 = " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
	# ARBITRARY can be any length from 2 to 1024 bytes (as long as the
	# length is a power of 2), sampling rate will be set to 96000 later...
	ARBITRARY = "Op}pN- -"

	# 4 byte waveforms, the sinewave and triangle wave are identical...
	WaveFileGen(SINEWAVE4, "/tmp/sinewave4.raw")
	WaveFileGen(SQUAREWAVE4, "/tmp/squarewave4.raw")
	WaveFileGen(TRIANGLEWAVE4, "/tmp/trianglewave4.raw")
	WaveFileGen(SAWTOOTHPLUS4, "/tmp/sawtoothplus4.raw")
	WaveFileGen(SAWTOOTHMINUS4, "/tmp/sawtoothminus4.raw")
	WaveFileGen(PULSEPLUS4, "/tmp/pulseplus4.raw")
	WaveFileGen(PULSEMINUS4, "/tmp/pulseminus4.raw")

	# 16 byte waveforms...
	WaveFileGen(SINEWAVE16, "/tmp/sinewave16.raw")
	WaveFileGen(SQUAREWAVE16, "/tmp/squarewave16.raw")
	WaveFileGen(TRIANGLEWAVE16, "/tmp/trianglewave16.raw")
	WaveFileGen(SAWTOOTHPLUS16, "/tmp/sawtoothplus16.raw")
	WaveFileGen(SAWTOOTHMINUS16, "/tmp/sawtoothminus16.raw")
	WaveFileGen(PULSEPLUS16, "/tmp/pulseplus16.raw")
	WaveFileGen(PULSEMINUS16, "/tmp/pulseminus16.raw")

	# 64 byte waveforms...
	WaveFileGen(SINEWAVE64, "/tmp/sinewave64.raw")
	WaveFileGen(SQUAREWAVE64, "/tmp/squarewave64.raw")
	WaveFileGen(TRIANGLEWAVE64, "/tmp/trianglewave64.raw")
	WaveFileGen(SAWTOOTHPLUS64, "/tmp/sawtoothplus64.raw")
	WaveFileGen(SAWTOOTHMINUS64, "/tmp/sawtoothminus64.raw")
	WaveFileGen(PULSEPLUS64, "/tmp/pulseplus64.raw")
	WaveFileGen(PULSEMINUS64, "/tmp/pulseminus64.raw")

	# Generate the noise waveform...
	srand()
	for(LOOP = 1; LOOP <= SIZE; ++LOOP)
	{
		INTEGER = 32 + rand() * 95
		printf("%c", INTEGER) > "/tmp/noise.raw"
	}
	close("/tmp/noise.raw")

	# Create a temporary ARBITRARY file...
	WaveFileGen(ARBITRARY, "/tmp/arbitrary.raw")
}
1 Like

Hi Don...

# defined, $VER will not be expanded in an awk string constant.

This made me smile...

It is not meant to be a variable at all.

As I am still an ancient AMIGA _fanatic_ this is how any executable file in the AMIGA directory tree displays what the program and its version is when _asked_.
$VER is always the first 4 characters followed by a space and after that is pure text. I use underscores instead of other spaces.
So if you see anything of mine it will always have $VER in it. In AudioScope the '$' is escaped so as not to be confused with a variable in 'sh'...
It is a quirk of mine...

All of the files are now created but I forgot about the 'close()' you mentioned in post 8, my bad.

Anyhow it looks as though this creation will be yours too... ;oD

Once again you have added to my learning and I had thought about a function but was not sure at this stage where to start.

As for the ARBITRARY waveform size anything greater that 480,000 bytes is OK and that was my next job...

2 questions:-

Eventually for the ARBITRARY generation I was going to call the shell command ed as this is difinitve on all versions of Linux and UNIX flavours.

1) Which editor would you use to call?
2) Is it possible to save the file without a newline?

I have checked three editors, 'Gedit' is my GUI editor of choice, 'ed' and 'vi(m)'. I might be missing something with 'ed' and 'vi(m)' as I am no expert by any stretch of the imagination...

TIA.