Reading a .wav file in Java

Although in Java it is possible to get data directly from an audio device, there are no standard libraries for reading and parsing a wave (.wav) audio file. If you just want to make some simple experiments with wave files, instead of searching for a full-featured library you could use the tip proposed in this post. The wave format is rather simple: a part that states the format of the samples (e.g.: stereo 16bit 44100Hz) and a part with the samples themselves.

00 "RIFF"
04 File size in bytes - 8
08 "WAVE"

[Format chunk]
12 "fmt " (format chunk mark)
16  Format chunk size - 8 (16 for PCM)
20  Format tag (1 for PCM)
22  Number of Channels (e.g.: 2)
24  SamplesPerSec (e.g.: 8000Hz)
28  AvgBytesPerSec = (Sample Rate * BytesPerFrame).
32  BlockAlign = BytesPerFrame = (BitsPerSample * Channels)/8 
34  BitsPerSample (e.g.: 8)

[Data chunk]
36 "data"
40 Data chunk size - 8
44 Frame_0: sample_channel0[0], sample_channel1[0], ..
   Frame_1: sample_channel0[1], sample_channel1[1], ..
   Frame_n: sample_channel0[n], sample_channel1[n], ..

If you don't believe me you can check it by yourself:

$ arecord -f U8 -c 1 -r 8000 -d 5 pepe.wav
Recording WAVE 'pepe.wav' : Unsigned 8 bit, Rate 8000 Hz, Mono
$ ls -l pepe.wav 
-rw-r--r-- 1 dsl dsl 40044 Mar  2 20:14 pepe.wav
$ od -A d -N 44 -w4 -v -t a pepe.wav 
0000000   R   I   F   F
0000008   W   A   V   E
0000012   f   m   t  sp
0000036   d   a   t   a
$ od -A d -N 44 -w4 -v -t u2 pepe.wav 
0000000 18770 17990   RIFF
0000004 40036     0   file size (40044) - 8)
0000008 16727 17750   WAVE
0000012 28006  8308   fmt
0000016    16     0   16 bytes of format data
0000020     1     1   1 channel
0000024  8000     0   8000 samples/sec
0000028  8000     0   8000 bytes/sec
0000032     1     8   8 bits/sample (1 byte/frame)
0000036 24932 24948   ata
0000040 40000     0   4000 samples

Now here is the point. Suppose that we just want to play with a wave file in a specific format. Then, we can skip the first 44 format bytes and go straight to the data. In particular, if the format of the wave file is "mono 8bit" things get as simple as this:

$ vi
public class pepe {
  public static void main(String[] args) {
    int bytes, cursor, unsigned;
    try {
      FileInputStream s = new FileInputStream("./pepe.wav");
      BufferedInputStream b = new BufferedInputStream(s);
      byte[] data = new byte[128];
      cursor = 0;
      while ((bytes = > 0) {
        // do something
        for(int i=0; i<bytes; i++) {
                unsigned = data[i] & 0xFF; // Java..
                System.out.println(cursor + " " + unsigned);
    } catch(Exception e) {
$ javac
$ java pepe > data.txt
$ gnuplot
gnuplot> set size ratio 0.3
gnuplot> plot "data.txt" with lines

Hope that's useful for you.

Ubuntu Spanish accents | Tildes en Ubuntu.

This is a method for writing Spanish accents on Ubuntu when you don't have a Spanish keyboard. I tested it on Ubuntu 12.0.4 LTS (classic desktop). I am using a Japanese keyboard with an English environment.

System tools -> System settings -> Keyboard layouts -> Layout ->
Options -> Compose key option -> Right Alt

Note: if you can't find "System tools" menu, execute the command 'gnome-control-center'

Then you can write Spanish accents as follows:

RightAlt-a + ' = á

RightAlt-e + ' = é
RightAlt-i + ' = í
RightAlt-o + ' = ó
RightAlt-u + ' = ú
RightAlt-n + - = ñ
RightAlt-? + ? = ¿

Note: RigthAlt is the Alt key on the right side of the keyboard.

Recycling an IR LED for timelapse movies with Arduino

In this post, we are going to reuse the infrared LED inside a recycled remote controller for periodically activating the shutter of a digital camera in order to create a timelapse video like the following one.

In Japan there are many recycling shops where you can get old electronic junk for a cheap price. It's a very ecological way to learn about electronics, so I went ahead and bought a DVD remote controller at hard off.

Most remote controllers are based on infrared communication. Although humans can't see infrared light, I found out that the CCDs inside most cameras do. This can be useful for making your own infrared night vision camera (maybe a future post), or just for quickly checking whether your remote control is working or not.

Now let's go inside the device: remove the batteries and screws, and open it up by pressure. A remote controller typically consists of the following elements:
  • A case made of ABS plastic.
  • A PCB containing button switches and the control circuitry.
  • A button panel made of silicone rubber. Each button has a layer of conductive paint underneath. When pressed it connects button switches on the PCB board.

Here is a sketch of the remote controller I got from hard off. It's a very simple circuit consisting of:
  • Electrolytic Capacitor (47uF 10V): removes high frequency noise or peaks in the power supply. A general rule is to use one that supports at least double the normal voltage (10V > 2x3V).
  • Ceramic resonator (4MHz): it works like quartz crystals but has bigger tolerance, it's cheaper and smaller (includes built-in capacitors). Used to create the carrier wave for infrared communication.
  • Resistor (2.2 Ohm): not sure why such a low value.
  • Transistor (C3203 NPN): for switching the LED on and off.
  • IR LED: a LED that emits light in the infrared range (e.g., 950nm).
  • IC: the chip where all the logic takes place. I couldn't identify which IC was used here, but I'm guessing it's something similar to the PCA8521. Button switches are wired to the IC, which (I guess) performs internally a loop (it could be done with flip-flops too) and polls the status of each button switch. Next, the IC selects the corresponding code from an internal ROM (customized during fabrication) and sends a modulated pulse wave through the IR LED.
For more information about remote controllers check this article.

In this post we are going to recycle the IR LED and save the remaining electronic components for a future circuit. An easy way to desolder components is to use a desoldering braid.

Once you desolder the LED, connect it to an Arduino digital pin using a resistor to limit the current running through (Arduino can handle up to 40mA). I'm using a 5V power supply and the LED's voltage drop is 1V so a 220 Ohm resistor will give me 18mA which is good enough to power the LED.
The final step wildly depends on your camera model. I have a Sony NEX-5 and fortunately there was a nice person that made the corresponding Arduino code public here. For different models you have to google a bit, and maybe modify existing arduino code, but it should not be too difficult (here you have code for Nikon cameras). Once you have taken interval pictures, put them together in a video with a command such:
mencoder -idx -nosound -noskip -ovc lavc -lavcopts vcodec=mjpeg -o output-rec.avi -mf fps=15 'mf://@files.txt'

Mt. Takao (高尾山)

Mt. Takao (高尾山) is a 599m tall mountain very popular among people living in Tokyo because of its location, about 50min from Shinjuku.

On a good day, you can see Mt Fuji from there. Unfortunately, I didn't have luck this time so I can only show you some sprouts of the imminent momiji season.

Apart from nature, Mt Takao (in particular route 1) has an interesting collection of tengu sculptures worth the attention. Now let's go for the practical details:
  • Access: use hyperdia to find the best route from your home to Takaosanguchi station.
  • Hiking trails: there are several trails. My advice is to go up using route 6 (nature) and come down using route 1 (sculptures and temples).
  • Onsen: after the walk you can have a bath at furoppi (ふろっぴぃ). There is a free bus from Takasanguchi every 30min (1h on weekdays). When I was there last time, the price was either 800yen for having a bath (2h limit) or 2000yen for towel, yukata, relax room, manga library, restaurant etc.