Python read microphone without PyAudio - python

I'm trying to collect data from a Geiger counter using the microphone and process it using Python. However, I'm using a university computer so I'm not allowed to install the PyAudio module which seems necessary for this job (Python read microphone). Are there any equivalent functions in numpy, matplotlib or scipy?

Here's an outline an approach that I think might work:
The hardest part of this is getting data from the microphone, and you'll need a tool that's built for this. Since you're on Windows, you could look for a prebuilt tool to do this. You could try to run something as a subprocess, but probably better is to use ctypes and windll.kernel32 to call a Windows recording API. Googling "windll.kernel32 recording" produces some reasonable hits, like this.
If you do go the subprocess route, you'll probably end up calling something that first writes the output to a .wav file. If that's the case, you could then read the file using either the Python wave module, or scipy.io.wavefile.read. (Note wave files can be more complex than these modules can read, so when you set the parameters, don't go crazy.)
Finally, this idea of getting the data into the computer by recording the audio from the device is quite problematic, and will lead to problems as external audio noises will need to be sorted out. It would be much better to find a way to get the data into the computer without the intervening audio.

I know the question go answered and accepted, but I'd like to offer 2 other options:
python virtualenv would work around the "not allowed to install anything on the computer" which I guess is more imposed by local IT than dept policy
use ffmpeg in a wrapper. Drop the statically compiled executable in a known and acceptable location. use subprocess to start it with appropriate command line switches to output the captured audio to stdout (read as a file-like object on python's side)
both these options are free as in free beer and add a straightforward to simple cross platform support.

Related

Are there some light-weight python libraries to play music segments with poloyphony?

Looking at 4-note polyphony, where notes are sampled audio (wav/mp3/ogg files). Is there some asynchronous audio library where I can tell the library to play an audio file (to completion, or a certain length), without blocking ? While the previous audio segment is still playing, I could invoke the library again to play another note, also without blocking. Thus I achieve polyphony, and can play upto 4-note chords ?
I am looking for an approach suitable for a very low end ARM9 (260MHz) + 64MB RAM type of device, running Linux. I am trying to keep the software as lean as possible, and thus cannot imagine putting a full-scale software synth on it. Also the user interaction of my simple 4-note polyphony, "poor-man's synth", needs to be programmatically achieved.
Only decent options I've found so far, seems to be SDL, but not clear how well it might fit my needs and meets the low-eright constraints.
Edit:
Found this SO Q&A but I am hoping that there is a more elegant solution, that has emerged in the 3 years since.
There is a list if Python sound/music libraries at https://wiki.python.org/moin/PythonInMusic; several of these appear to suit your needs (in the section Music programming in Python). Those with MIDI support may be particularly useful (there is also a section MIDI Mania), but others may also meet your needs.

Working on many files with Python

The task:
I am working with 4 TB of data/files, stored on an external usb disk: images, html, videos, executables and so on.
I want to index all those files in a sqlite3 database with the following schema:
path TEXT, mimetype TEXT, filetype TEXT, size INT
So far:
I os.walk recursively through the mounted directory, execute the linux file command with python's subprocess and get the size with os.path.getsize(). Finally the results are written into the database, stored on my computer - the usb is mounted with -o ro, of course. No threading, by the way
You can see the full code here http://hub.darcs.net/ampoffcom/smtid/browse/smtid.py
The problem:
The code is really slow. I realized that the deeper the direcory structure, the slower the code. I suppose, os.walk might be a problem.
The questions:
Is there a faster alternative to os.walk?
Would threading fasten things up?
Is there a faster alternative to os.walk?
Yes. In fact, multiple.
scandir (which will be in the stdlib in 3.5) is significantly faster than walk.
The C function fts is significantly faster than scandir. I'm pretty sure there are wrappers on PyPI, although I don't know one off-hand to recommend, and it's not that hard to use via ctypes or cffi if you know any C.
The find tool uses fts, and you can always subprocess to it if you can't use fts directly.
Would threading fasten things up?
That depends on details your system that we don't have, but… You're spending all of your time waiting on the filesystem. Unless you have multiple independent drives that are only bound together at user-level (that is, not LVM or something below it like RAID) or not at all (e.g., one is just mounted under the other's filesystem), issuing multiple requests in parallel will probably not speed things up.
Still, this is pretty easy to test; why not try it and see?
One more idea: you may be spending a lot of time spawning and communicating with those file processes. There are multiple Python libraries that use the same libmagic that it does. I don't want to recommend one in particular over the others, so here's search results.
As monkut suggests, make sure you're doing bulk commits, not autocommitting each insert with sqlite. As the FAQ explains, sqlite can do ~50000 inserts per second, but only a few dozen transactions per second.
While we're at it, if you can put the sqlite file on a different filesystem than the one you're scanning (or keep it in memory until you're done, then write it to disk all at once), that might be worth trying.
Finally, but most importantly:
Profile your code to see where the hotspots are, instead of guessing.
Create small data sets and benchmark different alternatives to see how much benefit you get.

Python - How to know if Matlab is complaining about missing free network license

One of the tools I use at work is Matlab, however due to server license there is limited number of users that can use it at the same time.
I decided to write a short script that will open Matlab - simple script with a infinite loop.
Now I want to improve my code a bit, to determine if the Matlab is actually opened (otherwise Licence error pops up).
Easy way would be just to check the process in task manager - unfortunately if error occurs as Matlab.exe process (the same as I would be in case of properly opened program).
So I figured out maybe it would be possible to check the name of the window header to determine if there is error or no. I tried to find some solution on the internet, with no luck. Could You provide me with some hint? Or maybe some other solution to the problem?
You can check with
$MATLABROOT/etc/lmstat -c yourlicencefile -a
and parse its output to see if you allocated a license or not to your computer.

Audio Domain Specific Language vs Python

I want to write some code to do acoustic analysis and I'm trying to determine the proper tool(s) for the job. I would normally write something like this in Python using numpy and scipy and possibly Cython for the analysis part. I've discovered that the world of Python audio libraries is a bit chaotic, with scads of very limited packages in various states of development.
I've also come across a bunch of audio/acoustic specific languages like SuperCollider, Faust, etc. that seem to make the audio processing easy but may be limited in terms of IO and analysis capability.
I'm currently working on Linux with Alsa and PulseAudio installed by default. I would prefer not to involve and of the various and sundry other audio packages like Jack if possible, though that is not a hard requirement.
My primary interest in this question is to determine whether there is a domain specific language that will provide for quicker prototyping and testing or whether a general language like Python is more appropriate. Thanks.
I've got a lot of experience with SuperCollider and Python (with and without Numpy). I do a lot of audio analysis, and I'm afraid the answer depends on what you want to do.
If you want to create systems that will input OR output audio in real time, then Python is not a good choice. The audio I/O libraries (as you say) are a bit sketchy. There's also a fundamental issue that Python's garbage collector is not really designed for realtime stuff. You should use a system that is designed from the ground up for realtime. SuperCollider is nice for this, and as caseyanderson notes, some of the standard building-blocks for audio analysis are right there. There are other environments too.
If you want to do hardcore work such as applying various machine learning algorithms, not necessarily in real time (i.e. if you can get away with reading/writing WAV files rather than live audio), then you should use a general-purpose programming language with wide support, and an ecosystem of good libraries for the extra things you want. Using Python with libs such as numpy and scikits-learn works great for this. It's good for quick prototyping, but not only does it lack solid realtime audio, it also has far fewer of the standard audio building-blocks. Those are two important things which hold you back when prototyping audio pipelines.
So, then, you're caught between these two options. Depending on your application you may be able to combine the two by manipulating the audio I/O in a realtime environment, and using OSC messaging or shell scripts to communicate with an external Python process. The limitation there is that you can't really throw masses of data around between the two (you can't sensibly pipe all your audio across to some other process, that'd be silly).
SuperCollider has lots of support for things along these lines, both as externals/plugins or Quarks. That said, it depends exactly what you want to do. If you are simply looking to detect events, Onsets.kr would be fine. If you are looking for frequency/pitch information, Pitch or Tartini would work (I find Tartini to be more accurate). If you are trying to track amplitude, a combination of Amplitude.ar and some simple math would also work.
Similarly, there is SpecCentroid.kr (for a kind of brightness analysis), Loudness.kr, SpecFlatness.kr, etc.
The above are all pretty general, and there are lots more (the JoshUGens externals package has some interesting FFT-related acoustics stuff). So I would recommend downloading the program, joining the mailing list (if you have further questions), which lives here, and poking around in the Externals, Quarks, and Standard UGens.
Nonetheless, since I am not sure what you are trying to do, I cannot make more concrete recommendations than the above combined with my feeling that it makes the most sense to go to SC for this, rather than writing all of your own tools in Python from scratch.
I'm not 100% sure what you want to do, but as an additional suggestion I would put forth: Spear with scripting in Common Lisp. If what you are doing involves a great deal of spectral analysis, then you can do the heavy Lifting in Spear, and script all of this using Common List with Common Music. Spear has some great tools in terms of editing out very specific partials.

Check Idle Time when running as a Windows Service

Using win32api.GetLastInputInfo() is an easy way to determine a USERS's idle time. However when running as a SERVICE this does not apply (always returns 0).
Does anyone know a simple way for a WINDOWS SERVICE to determine last keypress/mouse activity? (or some other effective way to determine idle time)
Not in Python, but the approach proposed in http://www.codeproject.com/KB/DLL/trackuseridle.aspx looks interesting.
[edit]
The code it is a standard C DLL, so you should be able to use it with ctypes. The way the C code is written using SetWindowsHookEx means you could maybe rewrite it directly Python + pywin32. See stackoverflow.com/questions/6458812 and python-forum.org/pythonforum/viewtopic.php?f=2&t=11154 for more on this (the first link mentions kinds of events you can get without writing a DLL, and the other shows a python example).

Categories