Geeks making the world a bit better.

Entries Tagged Programming

Running Python from within Emacs

Emacs python mode supports running scripts in an inferior python process that is running in an emacs buffer. I really like this idea but have never been able to reliably use it because so much of my work depends on external libraries that may not do a complete job of cleaning up their state. Attempting to reuse them in the same process is, in my experience, a recipe for confusion. Also, emacs seems to be hung waiting on the python subprocess while my GUI app is running. As a result, I always edit in emacs and then switch to a command prompt to run.

It occurred to me that if I my program doesn’t need input (almost all mine have a GUI) I can run them using the emacs compile command. The compile command in emacs is intended for running make and other command-line build tools. The command runs in a separate process and its output is directed to an emacs buffer. I tried it out and it works great!

In my .emacs file I defined this function

(defun my-compile ()
  "Use compile to run python programs"
  (interactive)
  (compile (concat "python " (buffer-name))))
(setq compilation-scroll-output t)

This function invokes compile with python foo.py when I’m editing a buffer named foo.py. The setq makes the compilation buffer scroll to follow the output.

And then in my python-mode hook function I added:

  (local-set-key "\C-c\C-c" 'my-compile)

This binds Ctrl-C Ctrl-C to compile instead of the usual eval-buffer which I never use anyway.

Now I can edit and type Ctrl-C Ctrl-C to run. If my program raises an exception, I get a traceback in the emacs buffer. emacs parses the buffer looking for error messages. It notices the File lines in the trace back and sets them up so I can click on a filename to visit the file and line associated with the traceback.

I’m loving this! No more directing my program output into a file when I’m printing tons of debug messages. No more constant Alt-Tab to switch between emacs and the command prompt.

I’m using it on Ubuntu, but a quick test indicates this works on Windows also.

Building aubio for windows

What I tried, so I can reconstruct it later.

Install cygwin. Change the Devel category from Default to Install in their setup tool.

Download libsndfile, extract, configure, make make install.
Download libsamplerate, extract, configure, make, make install.

Download aubio and extract it.
cd into its folder

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
./configure --disable-shared
make
make install

Raw Input API

Meg pointed out the Raw Input API for Windows. This would allow us to distinguish among multiple mice and keyboards. It might be interesting to use multiple numeric keyboards as specialized input devices, or multiple mice to provide 2 or 3 switches per hand.

Making CVtypes work on the Mac

I’m writing this down because I don’t see complete working (for me at least) directions anywhere else.

  1. Install xcode from the Mac OS X install CD
  2. MacPorts (everyone calls it DarwinPorts)
  3. Get and unpack the opencv source tarball
  4. cd opencv; mkdir build; cd build
  5. ../configure CPPFLAGS=”-I/opt/local/include” LDFLAGS=”-L/opt/local/lib” –without-python
  6. make
  7. sudo make install

I also needed the macam webcam driver to use my Logitech webcam. I believe the notebooks have a camera built in so you should be good to go on one of those.

I’m getting tons of error messages like these as my app is running and I’m only getting about 5 frames per second. What’s up with that?

2008-02-14 09:00:23.983 Python[85901:6d37] *** Break on _NSLockError() to debug.
2008-02-14 09:00:24.179 Python[85901:880f] *** -[NSLock unlock]: lock ( '(null)') unlocked from thread which did not lock it

Also, I note that video doesn’t start until I move the mouse. Are these macam bugs?

Bringing OS X compatibility to our apps

I regularly get requests from teachers at schools that are Mac based for versions of our software that will work for them. I want every kid to have access to our stuff so I bought a Mac mini for development. I’d rather be running OS X in a virtual machine but Apple won’t let me do that. So I’ve got the mini sitting here with its video out connected to one of the inputs on my right projector and I hacked a python wrapper for synergy server to switch the projector source when I move the mouse off my Ubuntu desktop onto the mini. It works great. Combine that with MacFusion for file sharing and routing the mini’s audio output into the line-in on my Ubuntu machine and it is just about as good as a VM would be.

Pygame seems to work fine on the mini so most of what I need is there. Good deal.

But it sure is frustrating to try to get anything else going. First there is hardly any information online. What I find is often out of date and useless because so much seems to change from release to release. Then when something doesn’t work I don’t know who to ask. In contrast on Windows or Ubuntu there are huge and active communities of hackers like me who 1) figure stuff out and 2) write it down where Google can find it. The problem is usually culling through millions of results. On queries related to OS X the problem is I get 5 hits, 3 of which are asking the same question with no answer. The documentation at Apple is horrible and either not indexed by Google or not indexed well. I seriously don’t know why software developers put up with it. I guess they like being pioneers. I just want to get the job done…

C00D11B1 error in windows media player

I downloaded a book from netlibrary.com via our campus library. When I tried to play it Windows Media Player (on VMware) would complain that it couldn’t play it with the only explanation being C00D11B1 (real useful). The error apparently means something went wrong. I tried several fixes none of which helped. I finally found the suggestion to delete the content of the DRM folder (c:\Document and Settings\All Users\DRM). I had to use regedit to find it. After deleting the content of that folder, opening the book produced the login response I expected and now the file plays fine. It is incredible to me that WMP was broken in this way. I created this VM and installed Windows XP SP2 fresh on it recently. This was the very first occasion that WMP has ever been used on this machine!

Opengazer

Wow! Pete sent me this pointer to Opengazer. I’ve wondered if this was possible but never found a student to work on it. Thanks to Piotr Zieliński for developing it and making it open source. We’ll have great fun with this in accessible applications for people with impaired movement.

Open source speech recognition

I saw a story on slashdot about the simon speech recognition system available on SourceForge.net.

Using the Wiimote with PyGame

Several of my project teams this semester will (I hope) use PyGame and the Wiimote in their projects. I had it working fine on Ubuntu but struggled to find an interface that works with Python on Windows XP. Luke pointed me to wiiuse so I wrote a very simple wrapper with ctypes and then wrapped that in a module for PyGame. You can find the code in CVS at SourceForge in pywiiuse. I also uploaded a source release. You’ll need wiiuse.dll on Windows or wiiuse.so on Ubuntu.

Continue reading →

Alternatives for distributing ET tools

We want to give away software for fun and learning and we want it to run on as many machines as possible. I think these are my minimum requirements.

  • One full-screen window, with 2D graphics and text.
  • Stereo sound with panning control at least.
  • Text to speech.
  • USB game controllers like DDR pads, joysticks, etc.
  • Access to new devices like the Wiimote

Other features that would be great include:

  • 3D graphics
  • Spatial sound.
  • Run within the browser
  • Small downloads and simple install if any.

It seems to me the possibilities are Flash, Javascript for a single browser like Firefox, Jython (Python on the Java VM), and Pygame.

Flash has nice graphics, good sound, and small downloads. I hate the expensive IDE that is required for development. Flash has no access to game controllers and such without some downloaded extension. Perhaps if we could make custom “Projectors” (as they appear to call their stand along executables) we could embed the extensions. I don’t know if that is possible. Flash has no text-to-speech though we’ve done a simple web service that handles it remotely. Very slow though. The security model is always (it seems) getting in the way of doing interesting things.

Javascript in a single browser would avoid the ugliness of trying to make it work everywhere but like Flash you’ve got no access to special devices, and no text to speech. I guess we could build a custom version that is extended or have people download a little local service (a bit like Google Gears). We could use a simple fixed Flash movie to get access to sound. We’d have to worry about the browser supporting crappy old PCs.

Jython on the Java VM could be cool. If we programmed for the lowest common denominator VM (1.1) we’d have a pretty good chance of working on most machines. I’ve never had good luck with Java sound. Last time I tried (not that long ago) the demo’s on the Sun website died. That’s not encouraging. We’d have no access to special devices without some sort of bridge.

I really like Pygame. It is a nice simple game development environment. Pygame claims to run on Windows, Mac, and Linux. I can verify that it works fine on Windows and Linux. Miriam pointed me to Renpy, a visual novel system built on top of pygame. I see people over there distributing games/novels with downloads for all three platforms. We can’t run in browser and we have to test everywhere. The downloads will be big. We’ve already figured out how to do text to speech on all 3 platforms and we can have 3D graphics using OpenGL. We could get 3D sound with FMODex. I wrote a wrapper in pyrex but keeping it sync’d with their ever changing api is a major pain. But the 2D sound in pygame is good for most of what we do because you can’t really count on schools having more than stereo sound anyway.