Home
Man
Search
Today's Posts
Register

OS X is a line of Unix-based graphical operating systems developed, marketed, and sold by Apple.

An Audio Function Generator...

Login to Reply

 
Thread Tools Search this Thread
# 8  
Old 03-31-2016
Quote:
Originally Posted by wisecracker
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...
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:
Code:
	printf("%s", SINEWAVE8) > TMPFILE

I should, however, have added:
Code:
	close(TMPFILE)

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

You could certainly change the script from:
Code:
#!/bin/sh
awk '
most of awk commands
}'

to:
Code:
#!/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:
Code:
#!/bin/sh
awk '
FNR == NR {
	process config file data
	next
}
{	process user input
}
' config -

versus:
Code:
#!/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:
Code:
#!/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:
Code:
#!/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.
The Following 2 Users Say Thank You to Don Cragun For This Useful Post:
MadeInGermany (04-01-2016), wisecracker (04-01-2016)
# 9  
Old 04-01-2016
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:-
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.
# 10  
Old 04-03-2016
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:-
https://www.unix.com/unix-for-advance...le-limits.html
Code:
#!/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...
# 11  
Old 04-03-2016
Quote:
Originally Posted by wisecracker
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:-
https://www.unix.com/unix-for-advance...le-limits.html
Code:
Code removed to save space.

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):
Code:
#!/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")
}

The Following User Says Thank You to Don Cragun For This Useful Post:
wisecracker (04-04-2016)
# 12  
Old 04-04-2016
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.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Variable frequency audio generator... wisecracker OS X (Apple) 2 08-19-2016 06:32 AM
A Bash Audio Sweep Generator... wisecracker OS X (Apple) 4 08-08-2014 05:43 PM
Problems with audio recording in Audacity 2.0.5. Slackware64 14.1; Intel HD Audio. qzxcvbnm Slackware 2 07-23-2014 01:34 PM
A Crude 1KHz Audio Sinewave Generator Demo... wisecracker Shell Programming and Scripting 5 01-25-2013 12:31 PM
Python, Platform Independent, Pure Audio Sinewave Generator... wisecracker Programming 0 01-14-2013 12:07 PM
time generator hernand Shell Programming and Scripting 7 06-02-2011 06:23 AM
Load Generator for Web momo UNIX for Advanced & Expert Users 1 05-15-2002 06:40 PM


All times are GMT -4. The time now is 09:25 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password