The resources thread is getting a bit long so I thought I would start one with more dedicated focus.

Over the last few weeks I have been working on a programmable sound generator and this is where I am at so far: Synth4KThe 7-bit instructions for the generator are encoded in utf-8 strings. Sound effects use around 6 bytes each, a full song can be 1K (uncompressed). The sound generator itself is around 1K although I think I can shrink that considerably if I get a chance to work on it some more (really need to start on a game).

I have a few sound effects and a couple of songs (Mario/Zelda) in the demo but will add some more generic ones later on.

I'm not much into sound either. But here's some food for thought: Markov Chains. Don't use training data, just some small hardcoded data tables and perhaps bop between different chain orders. This could be coupled with some small grammar.

I don't suppose you could add comments to the code to help those of us (namely me) who are total noobes to java sound api and sound generation in general?

I really should clean it up a bit more and add some comments. Basically it has 8 channels of an oscillator combined with noise. Each character of the string is an instruction to either set the frequency of the oscillator, the amplitude of the channel or a delay. Combining the commands and coupled with a bunch of static settings for each channel allows you to create sound effects or music.

Quote

It stuttered badly on my machine until I reduced the sample rate to a quarter to the value.

Yeah, it is reasonably processor intensive and many Java implementations have sound that simply will wont work real time. The main culprit of this is the Java Sound Mixer, I'm not sure what it is doing but you can't get it to work without a massive buffer which equals seconds of delay.

In addition to lowering the sample rate I suggest you increase the buffer size of the line by changing this line:dest.open(format, 2048);If you just want it for music and don't care about latency you can set this to huge amount for glitch free playback.

Also you can print out AudioSystem.getMixer(null).getMixerInfo().getName() and check it isn't the Java Sound mixer.

Since Java 1.5 is the minimum target, I wonder whether it is worth re-considering the use of MIDI (without a soundbank). This drops through to using underlying hardware MIDI support when no soundbank is present and was hidiously broken in Java 1.4. However I wonder whether this works now. Any thoughts?

I think it fair to assume that on a reasonable number of systems MidiSystem.getSynthesizer() will return a valid synth. Going forward a rule could be added that the Java Midi is not forbidden but is use at your own risk and the judges aren't required to get it working. However as the entire javax.sound.midi package is on notice as likely being removed from future JRE's it seems that we would be be back to roll your own fairly soon anyway.

New version up. This one is a bit better at making sound effects and also cuts down the size of songs dramatically (1 byte per note rather than 2). I have also cleaned up the source a bit and included some comments.

I am not happy with the explosion effects but the others are sounding ok. There are 8 songs in this version (more to come). The synthesizer has a fair amount of static settings and could be set up differently for different types of effects/music. At the moment the 8 channels are programmed for a balance of FX/music:

This is pretty cool indeed!!... The version you had a couple of weeks ago was very stuttery, but this seems to play stutter free!!

Actually 2 of the sounds would fit perfectly well into my next 4K shootem-up game.. Is the source for general use? I wouldn't want to use any of it without you getting the credit.. I did investigate sound a few weeks ago but seemed to complicated/unstable to use.. but slowly changing my mind, partly due to this post..

Go right ahead, hereby placed in the public domain. Probably should have made that clear in the initial post. I would be very pleased if an entry or two this year used it, I think sound/music is much more important than many people realize.

Thats great news.. Thanks alot. Will try and use this in my next game. This may be a daft question, but I guess will need to play Sounds/Music in a new Thread? (i.e. I guess playing them directly from my main loop will cause stutter?)..

ps. New version still doesn't compile.. I think you need to change your cast from (int) to (Integer). Am using 1.5 btw. With the (Integer) cast this version seems to work ok in Eclipse for me. Thanks again

This may be a daft question, but I guess will need to play Sounds/Music in a new Thread?

You can use it in the main thread and in fact it can take the place of timing code. The main loop runs at almost exactly 64 times per second (you can easily modify but the music tempo will change).If you do too much processing the sound may stutter and it will need it's own thread or it's buffer size increased.

It is pretty damn cool indeed!! Last night I inserted the code into my new 4K Shootem up game (player fire, enemy explosion and 1 music track).. Although not perfect (needs some fine-tuning) it certainly did give the game an extra 'cool' factor. Am gonna do my extra best to try and keep this in (size permitting)..

I found a few issues (none of which I have had the time to investigate yet)..* After running Moogies Tool (Obfuscator) the sound didn't work... (No idea why)! * The game was a more stuttery (but as you said, this may be just a case of finetuning some buffer sizing)..* It ran ok in Appletviewer but in Browser it was approx 10times a slow (not sure why).. Changing the Thread.sleep(16) to 8 made it quicker in the Browser.

Ok, have to admit, at first glance, have no idea how you get the UTF strings into music.. Is there an explanation anywhere,, Ideally would like to create my own track, if only I knew how..

I reckon I need another week to program in some gameplay and will post a demo..

Also only a very small subset of midi is compatible with my synth. It is monophonic, not velocity sensitive, doesn't support pitch bending, has no note-off capability. More complicated midi files will come out sounding rubbish.

That code is from Alan_W's not mine. Frequency/8 would give you sub-harmonics (below the fundamental). Also I wouldn't recommend recalculating the phase of the oscillator every sample from the time, it means you have to use doubles and things tend to drift.

Writing good sounding synthesizers is actually a very involved topic and takes a lot of effort. If you want a slightly richer sound than a sine wave there is a simple technique that yields some nice sounds. I used it in my synth, basically you frequency modulate a sine oscillator with itself. This produces something very close to a saw wave.

Here is how:

1 2 3 4 5 6 7 8 9 10 11 12 13

floatphase = 0.0f; // the phase of the oscillator (goes from 0 -> 1)floatlastOut = 0.0f; // the last output of the oscillator so we can feed back in for some nice sounds.floatfeedbackAmount = 2.0f; // the amount of feedback, controls the buzzyness of the oscillator.floatfrequency = 440.0f; // the frequency of our tone.

Would be really cool if you could add some info on how to tune the code for own setup. For example how to create different sounds and how the tunes are composed. Update the wiki as you see right.

@Allan_W: I just copied you code to the wiki, since it was here in the open. Just remove it from the wiki if you have anything against it. I hope that eventually people will move it to the wiki by them selves, but helping along for right now.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org