nesdev.com
http://forums.nesdev.com/

A constant-cycle music engine
http://forums.nesdev.com/viewtopic.php?f=6&t=16111
Page 1 of 2

Author:  pubby [ Sun Jun 18, 2017 5:15 pm ]
Post subject:  A constant-cycle music engine

Over the weekend I wrote a music engine that runs in a constant number of cycles per frame. The intention was to have an engine that could be hidden in timing-critical code such as parallax effects, and also to avoid the dreaded lag spikes. Currently, the cycle count is 766, which makes it far faster than other open-source engines. For comparison, Famitone peaks at 3000+ cycles and GGSound peaks at 5000+. I think Pently peaks at 2500+, but I'm not sure.

The converter converts from Famitracker and supports instruments with Pitch/Volume/Arpeggio/Duty. Nothing else is supported; no effects nor volume columns. DMC is not supported, nor are sound effects or PAL pitches.

It uses 12 bytes of zeropage and $100-$156 in stack space. The converted songs are not as space efficient as other engines, but they're small enough for my purposes.

Anyway, you can find the code here: https://github.com/pubby/penguin Keep in mind this is not a library; I wrote the code for myself.

I'll attach a ROM that plays Ralph 4's music.

Attachments:
penguin_demo.nes [24.02 KiB]
Downloaded 94 times

Author:  tokumaru [ Sun Jun 18, 2017 7:40 pm ]
Post subject:  Re: A constant-cycle music engine

Cool, the idea of running the sound code at times you'd otherwise not do anything (e.g. while waiting for raster effects) is very interesting. And 766 cycles is pretty fast, about 7 scanlines only. Will take a better look at this when I get to my PC.

Author:  Bregalad [ Sun Jun 18, 2017 11:49 pm ]
Post subject:  Re: A constant-cycle music engine

Quote:
nor are sound effects or PAL pitches.

PAL pitches are usually as simple as adapting the pitch table to PAL values.

As for sound effects, the lack of them would really make your engine unsuitable for a game, although the concept of a constance-cycle music engine still apply.

Personally I was wondering how feasible was a fully constant-cycle game engine feasible. This could ease raster split a lot, perhaps impressive parallax scrolling could be done in-game without even needed neither sprite-zero hits nor IRQs.

Author:  Memblers [ Mon Jun 19, 2017 8:20 pm ]
Post subject:  Re: A constant-cycle music engine

Very cool, and very impressive too! Less than 7 scanlines of time, that fits well within a row of background tiles.

Author:  pubby [ Sun Jun 25, 2017 1:02 pm ]
Post subject:  Re: A constant-cycle music engine

Thanks.

The newest update adds sound effects and PAL pitches.

Sound effects were implemented as one-off instruments that play single notes. This makes them zero-cost; memory and cycles were not increased. PAL pitches on the other hand added 24 cycles and 2 bytes zeropage. The current cycle count is now 790 per frame.

Author:  Bregalad [ Sun Jun 25, 2017 11:31 pm ]
Post subject:  Re: A constant-cycle music engine

pubby wrote:
PAL pitches on the other hand added 24 cycles and 2 bytes zeropage. The current cycle count is now 790 per frame.

They should come at zero costs, really. All you need is to change the pitch table's values. Sound engines needs not supporting PAL or NTSC pitches at runtimes - they should just be configured at compile time to run either version.

Yes, technically you *could* have a runtime check and this comes with some advantages (only one version of the ROM), however that's not how it's typically done - most of the time you'd want to have one NTSC version and one PAL version of the ROM, and all the difference is made compile-time only.

Author:  rainwarrior [ Mon Jun 26, 2017 12:20 am ]
Post subject:  Re: A constant-cycle music engine

This is an interesting idea. Though it's not something I think I would ever want to do, it's always interesting to see a different approach to the problem. Thanks for sharing the source code.

Author:  pubby [ Sun Jan 28, 2018 12:51 am ]
Post subject:  Re: A constant-cycle music engine

I finally got around to using this, and found a few bugs along the way. The repo has been updated.

I forgot to mention, but it does support the full range of pitches and duty cycles, which Famitone does not. The ROM usage is pretty bad though; about 2x that of Famitone. Maybe one day I'll optimize it better.

Author:  FrankenGraphics [ Sun Jan 28, 2018 6:57 am ]
Post subject:  Re: A constant-cycle music engine

this piece of BGM was written with the intention to comply with your driver, just thought i'd let you know. :) . Sadly, the mini game got shelved indefinitely as elseyf needed to tend to other things. It is meant to use the cycle constancy to reliably time a few scroll splits without irq support.

Author:  GradualGames [ Sun Jan 28, 2018 8:43 am ]
Post subject:  Re: A constant-cycle music engine

:shock: Well, this is humbling! By the time ggsound burns 760 cycles, it has just had its coffee and is still waking up in the morning. Ah, time to actually make sounds now! :lol: I can really learn something from this code. Thanks for sharing.

Author:  tepples [ Sun Jan 28, 2018 9:33 am ]
Post subject:  Re: A constant-cycle music engine

Feel free to mention anything pertinent in the engine's entry in the wiki's list of music engines.

Author:  pubby [ Sun Jan 28, 2018 5:35 pm ]
Post subject:  Re: A constant-cycle music engine

Quote:
this piece of BGM was written with the intention to comply with your driver, just thought i'd let you know.

Cool! That sounds really, really good.

GradualGames wrote:
:shock: Well, this is humbling! By the time ggsound burns 760 cycles, it has just had its coffee and is still waking up in the morning. Ah, time to actually make sounds now! :lol: I can really learn something from this code. Thanks for sharing.

The biggest optimization comes from dividing the work out across multiple frames. I divided it into 4, and well, that's why it's 4x faster than famitone.

Author:  tepples [ Sun Jan 28, 2018 6:02 pm ]
Post subject:  Re: A constant-cycle music engine

pubby wrote:
The biggest optimization comes from dividing the work out across multiple frames. I divided it into 4, and well, that's why it's 4x faster than famitone.

I've considered doing some of that in my own engine, checking for loop commands during downtime on frames that are not the start of a row. But to compensate for the lack of Sxx and Gxx, someone might need to use speed 3 to get (say) thirty-second-note resolution at 150 BPM. How would an engine that splits work over four frames cope with speed 3?

Also throw std::runtime_error("pattern size not multiple of 8"); would blow up on the 36-row patterns of the 2 AM theme in the FamiTracker version of the Thwaite soundtrack, which is in 9/8 time.

Author:  FrankenGraphics [ Sun Jan 28, 2018 6:05 pm ]
Post subject:  Re: A constant-cycle music engine

Division of workload across frames - that ought to be a good technique for any future driver aspiring to drive INL:s expansion audio project. More channels = more burden (and potentially more data, depending on how the extra channels are facilitated).

Author:  pubby [ Sun Jan 28, 2018 6:24 pm ]
Post subject:  Re: A constant-cycle music engine

If I ever rewrote it I would probably use 3 frame resolution instead of 4. I didn't know much about FT music back then and it turns out that 3 speed is common and useful.

You can sometimes fake faster speed with arpeggio sequences, but that's a pain.

Page 1 of 2 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/