Home Man
Search
Today's Posts
Register

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

OSX Sierra transparent shell audio sampler.

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 11-13-2016
OSX Sierra transparent shell audio sampler.

Well after the transparent QuickTime Player means of capture for OSX 10.7.x became broken in at least Yosemite I decided to persevere with a version of command line capture for OSX 10.12.x.

A derivative of this DEMO is now inside AudioScope.sh to give my MBP and iMac a means of capturing an audio signal using a virgin Sierra install.

As I run my machines as a standard non-admin user then the following occurs...
On the very first run only you will get Untitled1.jpg followed by an admin and admin password window finally followed Untitled2.jpg.

It is obvious what you need to do but you will not need it again once set.

The DEMO code has NO error correction so beware. If you want to test it then use the internal mic as the source. Make sure you set up the mic levels to suit your needs.
Code:
#!/bin/sh
# ./sample.sh <number_of_seconds>
#
# This DEMO shell script uses a virgin OSX Sierra install only, nothing else is required.
# The conversion for this DEMO is mono, 8 bit, unsigned integer at 48000 sps but it is
# easy to convert to just about anything, 'man afconvert'...
#
# This saves a sample '/tmp/Untitled.m4a' and is converted to '/tmp/Untitled.wav' file.
#
# './sample.sh 6' allows for 1 second startup and 5 seconds of recording.
#
# Use the internal microphone as a test.
seconds="$1"
osascript << AppleSampler
tell application "QuickTime Player"
	activate
	close (every document whose name contains "Untitled") saving no
	set savePath to "Macintosh HD:tmp:" & "Untitled.m4a"
	set recording to new audio recording
	set visible of front window to false
	start recording
	delay "$seconds"
	stop recording
	export document "Untitled" in file savePath using settings preset "Audio Only"
	close (every document whose name contains "Untitled") saving no
	tell application "System Events" to click menu item "Hide Export Progress" of menu "Window" of menu bar 1 of process "QuickTime Player"
	quit
end tell
AppleSampler
# Example of conversion to WAVE file.
afconvert -f 'WAVE' -c 1 -d UI8@48000 /tmp/Untitled.m4a /tmp/Untitled.wav
ls -l /tmp/Untitled.*
# Playback both files.
afplay /tmp/Untitled.m4a
sleep 1
afplay /tmp/Untitled.wav
exit 0

OSX Sierra transparent shell audio sampler.-untitled1-jpg  
OSX Sierra transparent shell audio sampler.-untitled2-jpg  

The Following User Says Thank You to wisecracker For This Useful Post:
Neo (01-01-2017)
# 2  
Old 12-31-2016
Well as all of regulars on here now know the code in the OP is broken as of OSX Sierra 10.12.2. It works fine on 10.12.0 and 10.12.1 but the latest version of Sierra broke the code.
See here...
https://www.unix.com/os-x-apple-/2699...10-12-2-a.html
...for more details.
This code is a workaround for the error generated in the above URL.
The important part is this...
Code:
> /"$mypath"/Untitled.m4a
chmod 666 /"$mypath"/Untitled.m4a

...inside the function. It can be outside the function but fails on AudioScope.sh if it is. I have absolutely no idea why! AudioScope.sh now captures again using QT.
Here is the revised code to counteract the error in the above URL.
It works for me and I hope for others too.
The Applescript section has been altered to allow for time delays in program startup and initial setup before taking a recording. There is a very small amount of error checking that defaults to roughly a 1 second capture.
This fudge has taken me ages to solve and has taught me a lot about Apple and their attitude towards security. I can't blame them but it is a RPITA having to do workarounds.
Code:
#!/bin/sh
# Usage:- [./]AUDIO_CAPTURE.sh <number_of_seconds_from_1_upwards>
#
# This DEMO shell script uses a virgin OSX Sierra install only, nothing else is required.
# The conversion for this DEMO is mono, 8 bit, unsigned integer at 48000 sps but it is
# easy to convert to just about anything, 'man afconvert'...
#
# This creates a sample which is then saved to '/"$mypath"/Untitled.m4a' which is then converted
# to '/"$mypath"/Untitled.wav' file. Both formats are then played using 'afplay'.
#
# From the current directory, './AUDIO_CAPTURE.sh 5' gives about 5 seconds of recording.
#
# Use the internal built in microphone as a test.

# Time for the recording.
seconds="$1"
# Some very basic error checking.
if [ "$seconds" = "" ] || [ "$seconds" -le "1" ]
then
	seconds=1
fi

# 'mypath' should be in Applescript format, NOT POSIX format.
# E.G. 'Users:myhome:Desktop'. There is no need for leading and trailing colons - see below.
# This could be a command line second argument but is not given '$2' here.
mypath='tmp'

SAMPLE()
{
> /"$mypath"/Untitled.m4a
chmod 666 /"$mypath"/Untitled.m4a
osascript << AppleSampler
tell application "QuickTime Player"
	activate
	set savePath to "Macintosh HD:" & "$mypath" & ":Untitled.m4a"
	set recording to new audio recording
	set visible of front window to false
	delay 3
	start recording
	delay "$1"
	stop recording
	export document "Untitled" in file savePath using settings preset "Audio Only"
	close (every document whose name contains "Untitled") saving no
	tell application "System Events" to click menu item "Hide Export Progress" of menu "Window" of menu bar 1 of process "QuickTime Player"
	-- quit
end tell
AppleSampler
# 'wait' added for fullness only.
wait
}

# Use the function to create a _background_ recording.
SAMPLE "$seconds"

# Example of conversion to a WAVE file.
afconvert -f 'WAVE' -c 1 -d UI8@48000 /"$mypath"/Untitled.m4a /"$mypath"/Untitled.wav
ls -l /"$mypath"/Untitled.*

# Playback both files.
afplay /"$mypath"/Untitled.m4a
sleep 1
afplay /"$mypath"/Untitled.wav
exit 0

EDIT:-

Just comment out the 'quit' part in the Applescript section to remove the annoying opening and closing of QT on the dock...

Last edited by wisecracker; 12-31-2016 at 09:43 AM.. Reason: See above.
The Following User Says Thank You to wisecracker For This Useful Post:
Neo (01-01-2017)
# 3  
Old 01-01-2017
As an addendum to this thread I enabled 'csrutil' again and it has NOT affected this capture mode so all is looking good.

I am going to consider it solved for the time being but not enable the 'solved' _tag_ at this point.

Bazza.
# 4  
Old 02-05-2017
Now tested on the latest OSX 10.12.3 Sierra on a(n) MBP and iMac.
And it is still working - WOOHOO.
The code now has some error checking and commented out code for a "path" option instead of just "tmp" alone.
It is bash centric but should work fine on ksh(93)...
Code:
#!/bin/bash
# Usage:- [./]AUDIO_CAPTURE.sh <number_of_seconds_from_1_upwards><CR>
#
# This DEMO shell script uses a virgin OSX Sierra install only, nothing else is required.
# The conversion for this DEMO is mono, 8 bit, unsigned integer at 48000 sps but it is
# easy to convert to just about anything, 'man afconvert'...
#
# This creates a sample which is then saved to '/"$POSIXpath"/Untitled.m4a' which is then converted
# to '/"$POSIXpath"/Untitled.wav' file. Both formats are then played using 'afplay'.
#
# From the current directory, './AUDIO_CAPTURE.sh 5<CR>' gives about 5 seconds of recording.
#
# Use the internal built in microphone as a test.

# Time for the recording.
seconds="$1"
# Some very basic error checking.
case $seconds in
	''|*[!0-9]*)	seconds=1 ;;
esac
if [ "$seconds" = "" ] || [ "$seconds" -le "1" ]
then
	seconds=1
fi
POSIXpath="tmp"

# 'POSIXpath' should be in POSIX format.
# E.G. 'Users/myhome/Desktop'. There is no need for leading and trailing backslashes - see below.
# This could be a command line second argument but is NOT given '$2' in this DEMO.
# When the section below is uncommented...
# Usage:- [./]AUDIO_CAPTURE.sh <number_of_seconds_from_1_upwards> [your/path/to]<CR>
# Where [your/path/to] is optional and defaults to "tmp".
#
# POSIXpath=$2
# Error check for directory existence.
# if [ ! -d "/$POSIXpath/" ]
# then
#	POSIXpath="tmp"
# fi

# Convert to AppleScript format for this code.
OSXpath="${POSIXpath////:}"

SAMPLE()
{
: > /"$POSIXpath"/Untitled.m4a
chmod 666 /"$POSIXpath"/Untitled.m4a
osascript << AppleSampler
tell application "QuickTime Player"
	activate
	set savePath to "Macintosh HD:" & "$OSXpath" & ":Untitled.m4a"
	set recording to new audio recording
	set visible of front window to false
	delay 2
	start recording
	delay "$1"
	stop recording
	export document "Untitled" in file savePath using settings preset "Audio Only"
	close (every document whose name contains "Untitled") saving no
	tell application "System Events" to click menu item "Hide Export Progress" of menu "Window" of menu bar 1 of process "QuickTime Player"
	delay 1
	quit
end tell
AppleSampler
# 'wait' added for fullness only.
wait
}

# Use the function to create a _background_ recording.
SAMPLE "$seconds"

# Example of conversion to a WAVE file.
afconvert -f 'WAVE' -c 1 -d UI8@48000 /"$POSIXpath"/Untitled.m4a /"$POSIXpath"/Untitled.wav
ls -l /"$POSIXpath"/Untitled.*

# Playback both files.
afplay /"$POSIXpath"/Untitled.m4a
sleep 1
afplay /"$POSIXpath"/Untitled.wav
exit 0

👤 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
If you run macOS High Sierra version 10.13.1, be sure to install today's update. Don Cragun OS X (Apple) 6 12-01-2017 01:05 PM
Capture power button press on MacOs High Sierra? xedge UNIX for Beginners Questions & Answers 0 10-12-2017 05:04 PM
The Start Of A Simple Audio Scope Shell Script... wisecracker Shell Programming and Scripting 83 12-04-2016 04:31 PM
Generate 10000 unique audio file of 2MB each using shell script. sushil.kumar Shell Programming and Scripting 11 09-29-2015 07:14 AM
Problems with audio recording in Audacity 2.0.5. Slackware64 14.1; Intel HD Audio. qzxcvbnm Slackware 2 07-23-2014 12:34 PM
Transparent Proxy with URL Rewriting crottyan IP Networking 0 03-05-2012 03:19 PM
Vlc transparent jtsmith90 Ubuntu 0 08-20-2011 07:26 AM
Invisible/Transparent Background in VM al0x Virtualization and Cloud Computing 0 07-30-2010 09:51 AM
Need help with a shell script:Config Transparent Proxy using Shell kaka287 Shell Programming and Scripting 0 01-10-2010 02:58 AM


All times are GMT -4. The time now is 04:43 PM.

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