## Sunday, December 16, 2012

### Extracting audio from a .mp4 video

I've been using my Android phone to record snippets of audio, usually some fragment of music. In the past, I would extract the audio by recording playback, using a tool like Total Recorder. However, a better way to isolate the audio is to extract the audio data (demux) from the video, so that I won't lose any quality to re-encoding. The following works well:

Download MP4Box, which comes as part of GPAC. The only part of GPAC needed is MP4Box.

I wrote a little python script. If I drag and drop any number of .mp4 videos to it, _track1.mp4 audio files will be written out. (Adjust as necessary to point to MP4Box or change which track id to export).

import os,sys,subprocess

def go():
for arg in sys.argv[1:]:
print arg
s = 'full/path/to/MP4Box.exe'
if not arg.endswith('.mp4'):
print 'must end with .mp4'
return

outfile = arg.replace('.mp4','_track1.mp4')
assert not os.path.exists(outfile)
scmd = '"%s" -single 1 "%s"'%(s,arg)
# -single 1 means to extract track id 1 in an mp4 container.
subprocess.call(scmd)
go()
os.system('pause')



If you want a full GUI (at the expense of bulk operations), Yamb looks like a candidate. It depends on MP4Box.

## Thursday, October 18, 2012

### Network Connections

On my old laptop, when I was on the bus and wanted to conserve battery life, I'd turn off the wireless adapter. Although this could be done with a hardware switch on the side of the laptop, I found it was more convenient to do in software. In Windows, there's a Control Panel page called 'Network Connections' that is quite useful for this:

It seems kind of difficult to find in the Control Panel these days, I didn't see it. So, you can open the Run dialog (from the Start Menu, or press Win+R), type
::{7007ACC7-3202-11D1-AAD2-00805FC1270E}

and press Enter. This will open Network Connections.

From here, you can create a shortcut on the desktop by finding the little icon to the left of the path ( see the red circle in the figure above), and dragging the icon onto the desktop.

(I found this GUID by opening a Network Connections .lnk shortcut in a code editor to see what was in it).

## Monday, July 30, 2012

An acquaintance posted a fun little snippet of C code that prints 1 - 1000 and then exits.

Looked like fun, so I came up with my own:

You can even compile and run this code right now at codepad. Modern web apps are truly amazing (see jsLinux and Game Boy emulation).

Reader: what does d refer to? Will it change depending on the compiler/architecture? There might be a difference between char *s="foo"; and char s[]="foo"; ...

## Wednesday, February 29, 2012

### SsipScite

SsipScite is a fork of the SciTE code editor with added support for Python extensions and source indexing. If one is working in a C or C++ codebase (even a large one with many thousands of files), one can click on a function or method and press F12 to go to its definition, Shift- F12 to go to the definition of a function/method/class, or Ctrl-F12 to list references to a function/method/class.

## Example:

Let's say I'm working on the 7z project.
First I create a file ssipscite/plugins/searchsip_userprefs/7z_example.cfg with the contents
[main]
srcdir1=C:\delldev\dev_here\ssipscite\example

This tells SsipScite that it can create an index of all source code in this directory. (Update this path based on where the files are).

Then, I can start ssipscite/SciTE.exe, and open 7zIn.c.
What does CSzCoderInfo refer to? I can press F12 and go to the definition:
What other files refer to this struct? I can press Ctrl-F12 and list references:
One can then click on any of the results to open that file and line.

After the index is created, these searches are noticeably faster than full-text search, and they intelligently point you to just the information you needed. Pretty useful, huh?

SsipScite works great for large codebases that cause Visual Studio to feel slow; it indexes hundreds of files a second (on my machine) and subsequent updates are fast and incremental. The code analysis is based on simple heuristics; SsipScite won't work for all coding styles.

I mainly wrote this for myself to use (and it's been very helpful over the past month), but you can Download if you'd like to try (win32, Python source).

## Friday, January 20, 2012

### Periodic Table (updated)

I wrote this in 2005. Back then, Internet Explorer was the only browser that could render text upside down without a browser plugin. A few years later, other browsers added their own extensions like -webkit-transform. Today I updated this old html page for cross-browser support.

Try it!

## Sunday, January 15, 2012

### SSIP

This tool will process source code and generates an index of terms. After the index is built, searches can be run very quickly, even on a large codebase. It is intended for c and c++ code, but has partial support for other languages like java and c#. The index is only for whole-string matching.

### Usage

First, make a copy of ssipexample.cfg and name it ssip.cfg. Edit the file so that it points to a directory containing source code.
[main]  srcdir1=C:\path\to\my\source

Then, from a command line, one can do the following: Type sssip –s main, and you should see a result for your main() function. Full options are:
sssip -s word  Search for a word
sssip -start  Re-build index
sssip -noindex word  Search for a word without using index
sssip -noindexnowhole word  Search for a partial word without using index

If you update one of your source files, the index will notice the change and keep itself up to date.

This program is designed for speed, so it should be able to make a complete index quickly.

Source and win32 binary
Source only

### Measurements

I've claimed this is 'fast'. What do I mean? I downloaded the source for the Audacity audio editor, and the entire source for php 5.3.9. My test machine is a 2.67 Ghz machine I bought a year and a half ago. The test involves creating a db and indexing every file.
Test: audacity-src-1.2.6
987 source files.
9.351Mb of data indexed.
Execution time: 3.753s
Speed: 263 files/s

Test: php-5.3.9
1584 source files.
41.677Mb of data indexed.
Execution time: 6.297s
Speed: 251 files/s

Looks pretty good to me. :)

### Description

I use IDEs like Eclipse and Visual Studio for debugging, but find them far too bulky for everyday programming. They are slow to open and less customizable than my code editor of choice, SciTE. However, IDEs do have some useful features, in particular Go to Definition (which takes the currently-selected symbol and opens the file and line on which it is defined). SSIP is my first step in adding features like this to code editors like SciTE.

My primary goal is speed. I want this to be usable on large codebases. I want even the initial run, creating the index from scratch, to take less than 10 seconds. Otherwise, there’s no point in generating an index, as a full-file search would be sufficient. I also found it enjoyable to optimize and tune a program, eschewing all C++ features (classes, exceptions) in favor of fast, raw, procedural code.

I struck upon a way to quickly create an index that actually does not store any text at all. I used what is known as a hash function to map every word into a number. The function always maps the same word to the same number. For example, the word ‘hello’ maps to the number 1634674. The word ‘hello2’, though, maps to a completely different number, 26453545. When the user searches for the term ‘hello’, I use the same hash function to find its corresponding number and search only for the number. This may seem too good to be true: I’ve transformed the 6 byte string ‘hello2’ into a 4 byte integer. The catch is that several inputs can map to the same number. ‘zsesdvf’, ‘rtyrqtrdg’, and ‘hello2’ could all map to the same number, 26453545. This is perfectly fine for my search index, though, because before displaying the results to the user, I’ll do a second check to make sure that the result matches what the user typed. In other words, this type of search could return false positives, but the false positives can be easily filtered out, and in the end the results are always correct.

A drawback of this method is that only full words can be searched.

The initial search creates a database from scratch. The name of each file encountered is also stored along with its last-modified time. Subsequent searches will traverse through the files again and check each file’s last-modified time. If the file is new, or its update time is more recent than what we have, we process the file and update the database.

### More Implementation Details

I chose to use Sqlite to store data. By using prepared queries, the sql only had to be parsed once per session, leading to performance improvements.

I also adjusted the hash function so that it didn’t require the length of the string up front. This allowed me to stream each character into the hash, and avoid any second buffer of the current word. (In general, I’ve noticed that avoiding memory allocations and having fewer buffers can lead to significant gains in performance).

At first, I wrote a threadpool system where a main thread would hand out files to be processed by the workers. This worked, but I was surprised to see that the number of worker threads barely affected the speed of execution. Unfortunately, writing to the database was significantly slower than reading from the text file, and since sqlite only allows one writer at a time, this was a clear bottleneck. It would have been possible to create separate databases to be later merged, but this was too much of a hack even for me. I returned to single threaded execution, which did simplify the code.

At this point, the program was logging string hash, line number found, id, file id, and other flags for each word. Because the id was incrementing, I could search for two consecutive terms with a bit of sql. The other flags included whether the line was indented or not, and the surrounding characters. Surrounding characters could among other things distinguish a method implementation, like ::foo. The size of the database on disk was rather large, though.

Because writing to the database was my bottleneck, if I could store less data, the program would be faster.

I realized that my prior experience showed me that reading through a file was unexpectedly fast. So, I decided to not even store the line number in my index at all. Results saying ‘the string “hello” is somewhere in the file “foo.cpp”’ is good enough, because it takes hardly any time to run through foo.cpp to list the true results.

This also meant that I only had to record a particular string once per file. I came up with a very effective trick to rapidly detect duplicates. I was already hashing each string to a number. I reduced the number of bits in the hash to 24, probably reasonable because most words in source code are short. (Remember that in my design collisions might cause false positives that take time to be filtered out, but won’t affect correctness). This means that the number of distinct strings I was tracking was 2 to the 24th power, or 16,777,216. I then allocated a chunk of memory, 16,777,216 bytes worth to be exact. For each file I cleared all of the memory to 0, and as each string appeared I would find its hash and ‘check off’ the corresponding place in memory. I could then quickly see if a string had already been encountered in a file. (I could have also done this at the bit level, but bit operations take time, and a 24-bit hash has been sufficient). One might worry that hash collisions could cause us to incorrectly skip over a word because its brother has already been added to the db for that file, but this is not the case, because I consistently use the same hash algorithm. When searching, the program must still look through the file, because that hash did occur in that file.

The next step is for me to write a Python wrapper around Ssip that lets me ask questions like ‘find the constructor of the CFoo class’, ‘find the implementation of the bar() method’, and ‘find the type of the baz global variable’. I’ll then plug this into my code editor and be well on my way to having my own lightweight productivity toolkit.

## Tuesday, January 10, 2012

### Wavcut, Find Silence

A few posts ago I wrote about wavcuthhh, the tool I wrote to split .wav files into separate tracks. I recently added the feature to look for periods of silence within a .wav file, and to cut the file based on those points. This might be useful for splitting up dj sets that consist of one huge mp3 file, or for isolating the 'hidden track' that some bands will include in the final track of an album after a minute of silence.

I use an amplitude threshold to specify what is 'silent'. The transition is not always clean, though, because songs can have a beat and the amplitude will change frequently. First, I made a large low pass filter and basically averaged the amplitude about every 0.2 seconds. To check the results, I could have written a GUI, or printed to a text file and plotted the data, but instead, went with a quick and dirty solution: writing data into another .wav file and using Audacity itself to visualize data.

The upper channel approximates the current amplitude after lpf, the lower channel indicates whether the amplitude is above or below the cutoff.

In a few cases, this was not enough, and there was still bouncing. I don't think there's a general way to solve this problem, because the silence between tracks can be the same as the silence within a track. I came up with a heuristic that has worked well so far. I assume that the first song is fading out, and the second song is fading in. Both of these might have temporary dips where the amplitude goes below the threshold, but then rises again. My algorithm goes from left to right. When encountering a drop below the threshold, I examine the next 10 seconds of audio. I look for the longest string of consecutive samples where the amplitude is below the threshold, and choose the cut location as the middle of this string. This tends to work because the pause between tracks tends to be longer than the temporary drops below the threshold during a fade-in or fade-out.

Here's what it looks like (these are ~800kb because of an example .wav):
C# source
Win32 binary

## Sunday, January 8, 2012

### Clipcircle32

Clipcircle is a Windows tool that lets you store multiple values in the clipboard at the same time and cycle between them.

### Example

Run clipcircle32, and it will appear in the system tray.

Copy a word of text from any program.
Copy a different word of text.
Press Windows-V. (The windows key has a flag icon and is typically between Ctrl and Alt)
The word you copied appears, but is selected!
Press Windows-V again.
Now, the first word that was copied appears.

Because Clipcircle selects as well as pastes, you can quickly cycle through the contents of the circle. By default, Clipcircle stores 8 items. It intentionally only stores this many items, so that you can repeatedly press Windows-V to cycle through everything that is stored.

To close Clipcircle, press Windows-Escape.