Sponsored Content
Operating Systems OS X (Apple) An Audio Function Generator... Post 302970037 by Don Cragun on Thursday 31st of March 2016 03:42:33 PM
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. Smilie
These 2 Users Gave Thanks to Don Cragun For This Post:
 

8 More Discussions You Might Find Interesting

1. Programming

Python, Platform Independent, Pure Audio Sinewave Generator...

IKHz_SW_OSX.py 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)
Discussion started by: wisecracker
0 Replies

2. Shell Programming and Scripting

A Crude 1KHz Audio Sinewave Generator Demo...

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)
Discussion started by: wisecracker
5 Replies

3. Shell Programming and Scripting

Sequence generator

Thanks Guys This really helped (5 Replies)
Discussion started by: robert89
5 Replies

4. Slackware

Problems with audio recording in Audacity 2.0.5. Slackware64 14.1; Intel HD Audio.

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)
Discussion started by: qzxcvbnm
2 Replies

5. OS X (Apple)

A Bash Audio Sweep Generator...

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)
Discussion started by: wisecracker
4 Replies

6. OS X (Apple)

Variable frequency audio generator...

Hi all... 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)
Discussion started by: wisecracker
2 Replies

7. OS X (Apple)

A simple variable frequency sinewave audio generator.

Hi all... 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)
Discussion started by: wisecracker
2 Replies

8. Shell Programming and Scripting

Hostsfile generator

Hello I use a bash script to creating the hosts file /etc/hosts But there is a bug inside my output and I want to fix this. My Array looks like this: 205,IP 111.122.133.20 205,HOST2 unas 205,HOST1 unas15533 205,COMMENT # UNAS 775,IP ... (9 Replies)
Discussion started by: Marti95
9 Replies
All times are GMT -4. The time now is 05:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy