Python script to do simple audio capture...

 
Thread Tools Search this Thread
Operating Systems OS X (Apple) Python script to do simple audio capture...
# 1  
Old 10-26-2014
Python script to do simple audio capture...

This site is the first to get this snippet.

It will capture an audio recording of any time length within the limits of OSX's QuickTime Player's capablility...

A shell script derivative of this will be used as a further capture for CygWin's AudioScope.sh.

Thoroughly read ALL the comments in the code before even attempting to use.

It works from Python 2.5.6 to 3.4.1...
Code:
# QT_Capture.py
# (C)2014, B.Walker, G0LCU.
# Issued under MIT licence.
#
# A DEMO to show how to capture an audio snippet for use in simple AudioScopes, etc...
# Designed around OSX 10.7.x and graeter using a _virgin_ OSX install and requires NO dependences.
# NOTE:- Python 2.7.1 is the default OSX 10.7.x Python install.
#
# Tested on Python 2.5.6 to 3.4.1 and saves to the default $HOME/Movies directory\folder\drawer.
# The file is a(n) *.aiff type of file and can easily be converted using the default /usr/bin/afconvert shell command
# to most filetypes, including the *.WAV format used here...
# NOTE:- afplay and afconvert are part of the default install on OSX 10.7.x and can be found inside the /usr/bin folder...
#
# Before running, startup QuickTime Player and from the menu select File -> New Audio Recording.
# From the Audio Recording display select the down pointing arrow and select Maximum Quality. Also select the input
# you want to test with; on this MacBook Pro I used the default internal microphone.
# Now close the Audio Recording window down, there is no need to do a recording. Nothing else is needed to do.
#
# Enjoy finding simple solutions to often very difficult problems.
# Bazza...

import os
import sys

# Create a function to do the task. 
def osxcapture(seconds="2"):
	sys.stdout = open("/tmp/OSXCapture", "w")
	# Generate a simple AppleScript file to launch QuickTime Player _silently_.
	# IMPORTANT!!! DO NOT change the indenting for the AppleScript file!
	print('''tell application "QuickTime Player"
set sample to (new audio recording)
set visible of front window to false
tell sample
delay 1.5
start''')
	print("delay " + seconds)
	print('''stop
end tell
quit
end tell''')
	# Reset stdout back to the default.
	sys.stdout = sys.__stdout__
	# Now capture the audio as '$HOME/Movies/Audio Recording.aifc'...
	os.system("osascript /tmp/OSXCapture")

# The OSX 10.7.x and greater default shell audio file converter.
# This DEMO CAN convert an 'aifc' file to an unsigned 8 bit, mono, at the _equivalent_ of 48000Hz sampling speed...
# Use 'afconvert -f 'WAVE' -c 1 -d UI8@48000 "$HOME/Movies/Audio Recording.aifc" $HOME/Movies/sample.wav'
# to change to 8 bit unsigned integer, mono at 48000Hz _sampling_ rate.
# This is a very, very basic function to convert the file into a more usable CD standard format WAV file...
# It is easy to convert to a .RAW data file from a .WAV file for AudioScope/Audio_Oscilloscope displaying purposes,
# this is a task to solve for yourselves. This is the difficult part... 
def audio_convert():
	sys.stdout = open("/tmp/OSXConvert", "w")
	print('''afconvert -f 'WAVE' -c 2 -d I16@44100 "$HOME/Movies/Audio Recording.aifc" $HOME/Movies/sample.wav''')
	sys.stdout = sys.__stdout__
	# This piece of DEMO code DELETES ALL *.aifc files inside $HOME/Movies drawer\folder\directory so be VERY AWARE!
	os.system("chmod 755 /tmp/OSXConvert; /tmp/OSXConvert; rm $HOME/Movies/*.aifc")

print("Start the DEMO capture using a _hidden_ Quicktime Player as the source...")

# A basic 10 second test. The file created is '$HOME/Movies/Audio Recording.aifc'...
osxcapture("10")

# Now convert to a *.WAV file...
audio_convert()

# A TEST using the OSX 10.7.x default shell command afplay and play the file as a simple listening test...
os.system("afplay $HOME/Movies/sample.wav")

print("End of DEMO program...")
# End of QT_Capture.py DEMO...

Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capture run time of python script executed inside shell script

I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.Please note i need to know execution time of python script which is getting executed inside shell .I need to store execution... (2 Replies)
Discussion started by: Adfire
2 Replies

2. Shell Programming and Scripting

The Start Of A Simple Audio Scope Shell Script...

This is a DEMO shell script to generate a simple graticule and plot inside it... Apologies for any typos... it is another building block along with my other two shell uploads recently to start a semi_serious project of an Terminal_AudioScope... The fist upload I posted recently was to show... (83 Replies)
Discussion started by: wisecracker
83 Replies

3. UNIX for Dummies Questions & Answers

ffmpeg: capture audio that's playing (command line)

I'd like to capture the audio playing in itunes (OSX) to a file (from the command line). I currently use Wiretap Pro, but i'd like a non-gui. I tried installing sox, but after installing about 40-50 dependencies it crashed out. I do have ffmpeg working and I am pretty sure it can capture what's... (0 Replies)
Discussion started by: sentinel
0 Replies

4. Homework & Coursework Questions

Help with Simple Perl/Python Script

I have the following problem, which I need done in Perl/ or Python using Unix/linux filters... 1. You have a very large file, named 'ColCheckMe', tab-delmited, that you are asked to process. You are told that each line in 'ColCheckMe' has 7 columns, and that the values... (1 Reply)
Discussion started by: Swapnilsagarwal
1 Replies

5. Shell Programming and Scripting

Help with Simple Perl/Python Script

I have the following problem, which I need done in Perl/ or Python using Unix/linux filters... 1. You have a very large file, named 'ColCheckMe', tab-delmited, that you are asked to process. You are told that each line in 'ColCheckMe' has 7 columns, and that the values in the... (1 Reply)
Discussion started by: Swapnilsagarwal
1 Replies
Login or Register to Ask a Question