Smallest possible audio file to make a 440hz tone?
January 23, 2010 5:45 PM   Subscribe

I want to create the smallest (file size) possible audio file that, when repeated, will sound like a given note (e.g. 440hz A). I think this needs to be just the length of one wavelength, but I'm not sure how I translate that to a length of time, or if the smallest audio format is one in which note duration doesn't matter. I don't care if this tone sounds artificial, but it needs to play natively in Firefox, Safari, and Chrome browsers. Is MIDI the best (most compressed) format for this or should I use something else? And what (ideally free) software might I use to create the files, given access to OSX and Windows XP?
posted by scottreynen to Technology (11 answers total) 2 users marked this as a favorite
 
You can use Audacity (free, open source) to create a tone (Generate > tone and then enter the waveform, frequency, and length in seconds). Audacity will generate many file types, although to write to Mp3 you will need to download and install the LAME Mp3 encoder. I am not sure if Mp3 is the format that you want, but wanted to give you that heads-up.

It can be difficult to get sounds to play in a variety of browsers. Someone else will have better suggestions than I will on that topic, probably.
posted by k8lin at 6:02 PM on January 23, 2010


wavelength = 1 / frequency

MIDI is not an audio format. A Midi file is like a musical score - it doesn't sound like anything unless it is rendered by synthesizers.

Start with wav or aiff, the resulting files may be small enough that converting to mp3 is unnecessary.
posted by b1tr0t at 6:12 PM on January 23, 2010


After a few minutes of experimenting with Audacity, I can confirm that it is possible to create a sound file consisting of just one wavelength. The problem with that is that the file makes no sound when you play it (it's too short for the computer to get the idea of playing it before it's over), and trying to play it in VLC on loop just makes the player flicker madly while not creating any sound. Doing a very short snatch of tone (0.05s) has the same effect.

Even making a 1-second-long file creates a noticeable "blip" when the file restarts, and although I can't be sure I'd expect browsers to do much the same. Essentially, any way of doing a repeated note is likely to make it pulse to some extent, so I'd recommend going down the route of compressing a long sound as much as possible. For reference, a ten-second A note produces a 160KB MP3 file on my machine (128kbps).
posted by ZsigE at 6:22 PM on January 23, 2010


It's not going to work worth crap unless the note frequency evenly divides the file's recording frequency (typically 48K 45K 30K 22.5K 20K,16K,11.25K,8K etc). The mismatch will always cause a pop when the file loops.
440K doesn't divide any of these very well, but an uncompressed mono 8K file in most any format is (wait for it) 8000 bytes/second. So you can send a 1-second sample in 8K bytes.
posted by hexatron at 7:01 PM on January 23, 2010


The smallest possible file would be a square wave consisting of a one unit peak and a one unit valley. The actual frequency will depend on the sample rate.
posted by Sys Rq at 7:16 PM on January 23, 2010


MIDI is not an audio format

I should have done more research before asking this. MIDI plays sound in all the browsers I care about, but when I went back, prompted by the comment above, to confirm that it plays in the context I care about (the new HTML5 audio element), I found it doesn't. And further research revealed that the only format that does is MP3. So that's really the only format I can use. Which makes the format part of question irrelevant.

It's not going to work worth crap unless the note frequency evenly divides the file's recording frequency

Aren't those two different kinds of frequencies (waves per second vs. kilobits per second)? Is there a direct correlation between waves and kilobits? I'm somewhat flexible on the base frequency I use, though I would like to do a full chromatic scale somewhere near middle C. If starting at 480hz would compress better than starting at 440hz, that's fine. I'll do some testing in Audacity.
posted by scottreynen at 8:08 PM on January 23, 2010


And further research revealed that the only format that does is MP3.

I'm pretty sure this is false, and that Firefox doesn't do MP3, only Ogg Vorbis and WAV. You're going to need to have multiple sources with different codecs to support all of the browsers, because some don't do WAV, some don't do Ogg, some don't do MP3.
posted by floam at 8:11 PM on January 23, 2010


If you really want a continuous tone without gaps I highly doubt you're ever going to achieve it by telling the browser to loop a clip of any format. There will always be a small gap, because they just aren't designed to be able to do sample-precise looping.

Until you mentioned HTML5, I was going to suggest writing a tone generator in flash. There would be no audio file, just code that fills a buffer with x=A*sin(f*t) and then plays it. The advantage of that approach is that you can take care of making sure that the buffers are sized to exact multiples of the wavelength. But if flash is out then that idea isn't going to fly.
posted by Rhomboid at 12:50 AM on January 24, 2010


Let's take a few steps back.

1. What are you actually trying to accomplish here? Describe your end result, not how you think you can accomplish it.

2. Why are you so worried about file size?

Your best bet might be to cook up a MIDI file that plays notes on the GM piano, or one of the other standard GM (General MIDI) instruments, but it is very hard to tell if this will work without knowing what you area really trying to accomplish.
posted by b1tr0t at 12:54 AM on January 24, 2010


b1tr0t:
No, period = 1 / frequency.
Wavelength = v / frequency

where v is the speed of the wave: for sound traveling through air, this is about 330 m/s (though it varies with temp, humidity etc)
posted by James Scott-Brown at 3:16 AM on January 24, 2010


I'm pretty sure this is false, and that Firefox doesn't do MP3

Yep, you're right. I was sure I had an MP3 file playing in all three browsers, but I just re-tested and MP3 didn't work in Firefox.

Until you mentioned HTML5, I was going to suggest writing a tone generator in flash

I actually started with a tone generator with HTML5 audio element and data: URIs, based on Steven Wittens' work. But it only worked in Safari and Firefox, not Chrome. I spent a while trying to track down why Chrome would treat this different from Safari, but came up with nothing. Looking at it now, the reason is obvious: Chrome doesn't support wav files, so of course it doesn't work with wav-based data: URIs. Now I just need to figure out how to generate ogg tones in minimal JavaScript.

1. What are you actually trying to accomplish here? Describe your end result, not how you think you can accomplish it.

2. Why are you so worried about file size?


My end result is a web application that has very little to do with audio and needs to fit in a fixed memory size. Everything but the audio is done, though I could cut other things out if they seem less compelling than the audio. But before I can make that assessment, I have to know how small the audio can get, and then figure out what I might cut out to save that space. It's kind of complicated, which is why I tried to avoid going into it.

On to play with ogg tone generation. If anyone has any help with that, it would be much appreciated, but you've already collectively pushed me in the right direction despite my poorly researched question. Thanks!
posted by scottreynen at 4:52 PM on January 24, 2010


« Older What would make me happy in Honduras?   |   Is he a good big brother, or a bad big brother? Newer »
This thread is closed to new comments.