Ogg Vorbis is a red herring

…turns out that just putting code in to decode Oggs into ByteBuffers cost about 4k, and then, the samples themselves are sort of massive really compared to anything else. So sound effects from Oggs don’t appear to be such a great idea after all :slight_smile: Back to the drawing board on that one.

In other news, just to show you how completely rubbish I am at all this, so far I’ve got a black screen up in about 6800 bytes. Admittedly it’s a very nice black screen which resizes, remembers its position between runs, can be toggled between fullscreen and windowed, paused, fades volume out when inactive and back in again, is perfectly rate locked at 60Hz, and automatically adjusts its aspect ratio correctly for any size of display. But, er, you get the idea. Hm…

Cas :slight_smile:

Sounds like a perfect powerful black screen to me ;D

Would you mind to release the source? I mean - that sounds like it’s basically one of the best startup codes you could get for any LWJGL game, right? A commented version would be a jewel in the LWJGL wiki for example 8)

I got discouraged about using Ogg/Vorbis as well.

I’m currently pursuing two ideas. One is to see what can be done generating SF/X with FM synthesis (frequency modulation). The overhead for FM is remarkably light. Biggest cost is maybe two K for a good sized sine wave table. It’s just a matter of figuring out how to program the various needed SF/X. I’m hopeful as I have a good decade of DX7 programming behind me, including a fair number of SF/X in my library that can potentially be plundered. Hoping to make some more progress later in the week. (It COULD also be a way to encode and play back musical ditties.)

What kind of SF/X exactly are you hoping for?

The second idea is to work off of samples, but in a more efficient fashion. The ClipTrack is the most recent thing I wrote. It works like a Clip, but allows one to replay with overlap, including playback at different speeds. I am thinking, also, the source data doesn’t have to be stereo, and there may be other ways to compact things. For example, using a form of granular synthesis, make continuous effects (rain, running water, fire, wind) from random assembly of a few fragments rather than multi-second samples. Or even just use a lower resolution in the storage. It can be totally arbitrary, as long as the operation to convert to the correct resolution is no more than a multiply. Maybe even A-law or mu-law quantization of the audio would work, as these are cheap in terms of cpu to decode (compared to mp3 or ogg/vorbis).

With the ClipTrack, for example, one can take a single sample of a bell, and play it back at several different speeds, making a melody. The gun shot effect I demo on this site
http://www.hexara.com/VSL/AudioMixerDemoWarOfWorlds.htm
kind of works as an explosion as well (again, different speeds). But slowed down effects are better if they originate in the distance, due to frequency rolloff.

I am happy to share any of this code (would be honored).

I got a pretty decent FM synth to fit in ~2k intended for the 4K contest.

http://www.angryoctopus.co.nz/java4k/Synth4K

I’m re-writing it for this years contest, trying to get it under 1K.

That 4k synth sounds ideal.

All my source code ends up completely open generally - I’ll release my brilliant Black Screen Game on the first milestone deadline :slight_smile:

Cas :slight_smile:

I totally overlooked that this topic was a “limited space” contest. :clue:

The AngryOctapus FM code has been fun to look through! Thanks for posting that ShannonSmith! Impressive how long some of those musical cues are, given the size of the code.

Maybe too limited :smiley: We’ll see how it goes. If I had my way it’d be LWJGK64k but I have a feeling someone would indulge in a massive techno-wank and do some shader based extravaganza and demoralize everyone with a stunning graphical demo. I’d still give it 0/10 for shit gameplay though :slight_smile:

Cas :slight_smile:

Ogg/Vorbis files got about 3.5k of header overhead. This overhead collapses inside a solid archive, because that whole thing is essentially the same for all of your samples (except for 12 bytes somewhere at the beginning).

Zip (or jar for that matter) doesn’t allow you to create solid archives.

(Solid archive = everything is compressed in one go, not individually.)

I pack my wavs into a single OGG file normally. Unfortunately all of my OGGs are huge anyway and I can’t find a way of compressing them further.
Not to worry though, I’ve got an FM soft synth working in about 5kb so far. Though I can’t get it to produce satisfactory noise. Any tips on producing noise that actually varies with pitch? (Just using Math.random() produces uniform white noise at any pitch!)

Cas :slight_smile:


                int n = (int)(RATE/(2.0*frequency));
                if (j%n==0) {
                    sample = 2.0*Math.random()-1.0;

Only a code fragment from a 3 channel + noise synth I was playing with. The noise channel worked by only taking a new random number at the frequency specified by the note, effectively giving a noise low pass filtered at the note frequency. It’s a bit rough and ready, but takes very little code.

@Alan_W: cool idea! I have to try it out, myself.

Would varying amounts of lp filtering do the job? There’s a “classic” cheap and easy low pass filter.

For an audio stream of random values:

outputValue[N] = outputValue[N-1] + filterAmount * (audioStream[N] - outputValue[N-1])

filterAmount ranges from 0 to 1.


from “Algorithmic Implementation” near the end of the article.

I haven’t tried it on noise, but it worked pretty well on a sawtooth wave I generated via a wavetable, yesterday. (Just starting to learn about filters.) I found I had to make the values pretty close to 0 to hear any effect, so some scaling other than linear will probably be needed.

As has been suggested, you can easily generate 1 bit pitched noise by generating a new sample (randomly 1 or -1) every period. This is how old consoles like the NES work and it sounds pretty good. Alternatively you can low pass filter white noise and there are a bunch of different filters with varying characteristics. State variable filters are about as simple you can get, have resonance to play with and low/high/bandpass simultaneously available:


// z1 z2 are member variables for filter memory
// f = sin(2*PI*cut)  cut = normalized cutoff frequency (0-1)
// q = 1/res            res = resonance (0 - ~10)
public float process(float in){
  float lowPass = (z2 + f*z1);
  float highPass = in - lowPass - q*z1;
  float bandPass = z1 + f*highPass;
  z1 = bandPass;
  z2 = lowPass;
  return lowPass; // could return high or bandpass outputs for different sounds
}

This filter becomes a bit unstable at extremes so you probably want to constrain the cutoff to 0.01 - 0.99.

@philfrei

Looks good to me. I hadn’t realised a LPF could be done in so little code. It would be interesting to compare the two results. I imagine the roll-off could be different.

Edit: @ShannonSmith. The HPF and BPF look good too. I’m thinking about reworking my synth. The main drawback is that it doesn’t support tempo changes. Once that’s fixed, the next thing on the list is programmable channels. I was considering a program definition that specified the amount of each harmonic used in the note generation to see what variety that would give me. Also easy is programmable attack/decay (currently fixed attack/decay is used to avoid glitches). I was wondering about some sort of pitch bend capability, perhaps as simple as specifying a rate of increase/decrease in frequency from the starting note. Other nice to have features are hall effect and modulation. Of course this all adds more code, so perhaps a music based game.

Regards, Alan

I reckon once optimised, proguarded, and compressed the whole synth will be under about 3kb and will provide a huge wealth of sounds for a game.

Sound is critical to most games. I wish more people realised this!

Cas :slight_smile:

@ShannonSmith - love that filter code - just excellent! My synth is now doing noise, triangle, sine and square waves, FM, AM, polyphony, and has envelopes for ADSR, pitch, resonance, and cutoff. Combined with reverb and echo from OpenALSoft I think I should be able to get a whole load of cool sounds out of it.

Cas :slight_smile:

Be careful, I wanted a to write a simple soft-synth for use in a game engine a few years ago and since then I’ve finished 15 Synths and 0 Games.

Mine is now finished thanks to you and Alan_w :slight_smile: It’s still a little large for my liking - though with Pack200, I’ve got this entire awesome synth and the aforementioned Nice Professional Windowing Code all in 4kb so far. I think it’s time to move on to font rendering. I’m budgeting 2k for font rendering.

Cas :slight_smile:

You really do have a super-human ability to stay on target and should write a tutorial on how you do that. I literally have 50 game projects that are all about 10% complete. Whoever said getting started is the hardest part needs a swift kick in the bollocks.

You only get to see the public results :slight_smile: I have a pile of unfinished broken projects lying around too, you just don’t see so much of them!
Besides, I’ve been at it for a few days now and still don’t have a game to show for it. I’m thinking a vector asteroids game would be a good start so that’s what I’m going to aim for, and vector graphics need very little art or memory to do really well. And there should be some awesome blooping and zapping noises.

Cas :slight_smile:

On a related note:

Crazy stuff. :smiley: