Following a blog post I saw last night about dynamically generating tones in Love, I decided to run with the idea and make a monophonic song player. Anyone familiar with Megazeux or ZZT would be immediately familiar with these songs (used as sound effects).
Here's the code: http://pastebin.com/5nHqknCQ
As a quick explanation of the code, there's a string parser that will run through the song in two passes, first to figure out the number of sample required to store the song in. Second pass to generate the tones for each note.
I decided to tune to 440, though megazeux tuned to something slightly lower. Also, I guessed at the BPM being 200. Even though I have the source code of megazeux, and can talk to the devs, I was more interested in getting code working than waiting for them to respond on IRC.
So, play around with it (consider it licensed under the WTFPL https://secure.wikimedia.org/wikipedia/en/wiki/WTFPL). Tell me if there are any bugs or things that I should fix.
Megazeux style sound effect generator
Re: Megazeux style sound effect generator
I like it, good 8-bits effects. I've always used sfxr ( http://www.drpetter.se/project_sfxr.html ) for this kind of effects but this requires almost no memory so it's much better. With a bit of work it could be turned into an incredibly lightweight 8-bit sound library. Anyway, awesome job
Re: Megazeux style sound effect generator
Thanks, I'm glad you like it.Ellohir wrote:I like it, good 8-bits effects. I've always used sfxr ( http://www.drpetter.se/project_sfxr.html ) for this kind of effects but this requires almost no memory so it's much better. With a bit of work it could be turned into an incredibly lightweight 8-bit sound library. Anyway, awesome job
Just a correction though, it does use memory, lots of it. 16-bit single channel 44100hz uses 88k per second of audio, plus whatever overhead Love and Lua have on top. The GC Memory metric there wasn't the only metric I was using, I was also using top in a terminal window. When the garbage collector does a cycle, thats when Love gets a chance to clean up some of the audio buffers. So, this code is just a demo, it's not production ready yet.
But yeah, I totally plan to adopt this code into a project I'm doing. I'll share then when it's ready.
Re: Megazeux style sound effect generator
Interesting! Would you mind to share a link to this blog post?Inny wrote:Following a blog post I saw last night about dynamically generating tones in Love, I decided to run with the idea and make a monophonic song player. Anyone familiar with Megazeux or ZZT would be immediately familiar with these songs (used as sound effects).
What a great license! Thanks! And keep working on your project. I can think of karaoke-like system based on this.Inny wrote: So, play around with it (consider it licensed under the WTFPL https://secure.wikimedia.org/wikipedia/en/wiki/WTFPL). Tell me if there are any bugs or things that I should fix.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Re: Megazeux style sound effect generator
You could also check out my BFXR-like program called LFXR: http://love2d.org/forums/viewtopic.php? ... fxr#p28641
Re: Megazeux style sound effect generator
I meant ".love size". Not having the sound files makes a huge difference. Any computer capable of running löve should have enough RAM to manage a few simultaneous channels. Having strings instead of OGG or WAV makes completely worth it.Inny wrote:Just a correction though, it does use memory, lots of it. 16-bit single channel 44100hz uses 88k per second of audio, plus whatever overhead Love and Lua have on top.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Megazeux style sound effect generator
Do note that static sounds are always fully decompressed, so I doubt it uses any(/much) more memory than sound files.
Re: Megazeux style sound effect generator
The blog post in question that gave me the inspiration is here: http://www.headchant.com/2011/11/01/sou ... ine-waves/
I couldn't find an online tutorial on how to use mzx's music system, so I'll do a quick writeup here (if it wasn't evident from the code)
You use it like so: playsong("5c-gec-gec")
The song strings are parsed like so
0, 1, 2, 3, 4, 5, 6: Sets the Octave of the following notes, the default is 3.
+, -: Increases or decreases the current octave by one for the following notes
W: Sets the following notes to be whole notes
H: Sets the following notes to be half notes
Q: Sets the following notes to be quarter notes
I: Sets the following notes to be eighth notes
S: Sets the following notes to be sixteenth notes
T: Sets the following notes to be thirty-second notes (the default)
Z: Sets the following notes to be sixty-fourth notes
!: Sets the following notes to be triplets, i.e. divides the note length by 3 (so Q! creates Quarter Triplet notes)
.: Sets the following notes to be dotted, i.e. multiplies the note length by 1.5 (so Q. creates Dotted Quarter notes)
#: Makes the previous note a Sharp
$: Makes the previous note a Flat
C, D, E, F, G, A, B: The note scale arrange in their octave. So, starting from Middle C, A would be to the right.
X: Rest
All other letters and symbols are currently ignored, but should probably be considered reserved. Spaces will always be ignored.
The following song, "1c+c0d#+d#-g+g-c#", could be spaced out like this for clarity: "1 c + c 0 d# + d# - g + g - c#"
Note: B-Sharp, C-Flat, E-Sharp, and F-Flat, play as B, C, E, and F, and not at C, B, F and E respectively.
So, have fun!
Right, that makes this good for general sound effects, but not really as a streamed music replacement.bartbes wrote:Do note that static sounds are always fully decompressed, so I doubt it uses any(/much) more memory than sound files.
I couldn't find an online tutorial on how to use mzx's music system, so I'll do a quick writeup here (if it wasn't evident from the code)
You use it like so: playsong("5c-gec-gec")
The song strings are parsed like so
0, 1, 2, 3, 4, 5, 6: Sets the Octave of the following notes, the default is 3.
+, -: Increases or decreases the current octave by one for the following notes
W: Sets the following notes to be whole notes
H: Sets the following notes to be half notes
Q: Sets the following notes to be quarter notes
I: Sets the following notes to be eighth notes
S: Sets the following notes to be sixteenth notes
T: Sets the following notes to be thirty-second notes (the default)
Z: Sets the following notes to be sixty-fourth notes
!: Sets the following notes to be triplets, i.e. divides the note length by 3 (so Q! creates Quarter Triplet notes)
.: Sets the following notes to be dotted, i.e. multiplies the note length by 1.5 (so Q. creates Dotted Quarter notes)
#: Makes the previous note a Sharp
$: Makes the previous note a Flat
C, D, E, F, G, A, B: The note scale arrange in their octave. So, starting from Middle C, A would be to the right.
X: Rest
All other letters and symbols are currently ignored, but should probably be considered reserved. Spaces will always be ignored.
The following song, "1c+c0d#+d#-g+g-c#", could be spaced out like this for clarity: "1 c + c 0 d# + d# - g + g - c#"
Note: B-Sharp, C-Flat, E-Sharp, and F-Flat, play as B, C, E, and F, and not at C, B, F and E respectively.
So, have fun!
Who is online
Users browsing this forum: Ahrefs [Bot] and 2 guests