<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wiki.bindingforce.net/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=DontBaguMe</id>
	<title>BindingForce Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.bindingforce.net/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=DontBaguMe"/>
	<link rel="alternate" type="text/html" href="http://wiki.bindingforce.net/index.php/Special:Contributions/DontBaguMe"/>
	<updated>2026-04-07T01:43:24Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.33.1</generator>
	<entry>
		<id>http://wiki.bindingforce.net/index.php?title=Music_Engine_Description&amp;diff=344</id>
		<title>Music Engine Description</title>
		<link rel="alternate" type="text/html" href="http://wiki.bindingforce.net/index.php?title=Music_Engine_Description&amp;diff=344"/>
		<updated>2022-04-08T20:29:07Z</updated>

		<summary type="html">&lt;p&gt;DontBaguMe: /* Title Music Engine */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There are two separate music engines in Zelda 2.  Both are in bank 6 (along&lt;br /&gt;
with the song data).  The music engine for the title screen resides at&lt;br /&gt;
&amp;lt;code&amp;gt;$8000&amp;lt;/code&amp;gt; and the engine for the rest of the game is at&lt;br /&gt;
&amp;lt;code&amp;gt;$9000&amp;lt;/code&amp;gt;.  Each of these engines is called once per frame during the&lt;br /&gt;
appropriate part of the game.&lt;br /&gt;
&lt;br /&gt;
Changes to the music engine should be very easy to make, as there is an&lt;br /&gt;
abundance of free space in bank 6.&lt;br /&gt;
&lt;br /&gt;
== Title Music Engine ==&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' properly format the following by bgt (original at https://eab.xyz/W2zplGD) and dontbagume  (on discord).&lt;br /&gt;
&lt;br /&gt;
# Title Music Engine&lt;br /&gt;
&lt;br /&gt;
The title music engine is different from the engine that plays music&lt;br /&gt;
during the rest of the game.  In particular, it has a wider range of&lt;br /&gt;
notes that can be represented.  If you want to look into the actual&lt;br /&gt;
code, the title screen music engine main loop is at `$8000` (vesus&lt;br /&gt;
`$9000` for the game music loop).&lt;br /&gt;
&lt;br /&gt;
The metadata for the songs is all the same as in the other areas,&lt;br /&gt;
although the title music is actually broken up into several songs&lt;br /&gt;
itself.  These song boundaries control the timing of the title scroll.&lt;br /&gt;
&lt;br /&gt;
The following songs exist in the title song table:&lt;br /&gt;
&lt;br /&gt;
  1. Intro&lt;br /&gt;
  2. Start&lt;br /&gt;
  3. Build up&lt;br /&gt;
  4. Main&lt;br /&gt;
  5. Breakdown&lt;br /&gt;
&lt;br /&gt;
The start of song 2 triggers the title to scroll into view.  The start&lt;br /&gt;
of song 4 triggers a countdown until the story scroll begins.  After&lt;br /&gt;
song 5 finishes, the engine loops back to song 2.&lt;br /&gt;
&lt;br /&gt;
Within the note data, a special format is used.  Rather than encoding&lt;br /&gt;
the duration and pitch together in a single byte, the title music has&lt;br /&gt;
&amp;quot;pitch&amp;quot; bytes and &amp;quot;duration&amp;quot; bytes.  Any byte with the highest bit set&lt;br /&gt;
(i.e. anything &amp;gt;= `$80`) is interpreted as a duration change, which&lt;br /&gt;
sets the duration of all future notes.  Anything else is a pitch value&lt;br /&gt;
which indicates a note of the current duration should be played.  The&lt;br /&gt;
low nibble of duration values keys into a lookup table at bank 6 `$8084` &lt;br /&gt;
and store the duration byte at $07FF.  Value durations are as follows:&lt;br /&gt;
&lt;br /&gt;
  * `$80` - 8 ticks   (sixteenth note)&lt;br /&gt;
  * `$81` - 24 ticks  (dotted eighth note)&lt;br /&gt;
  * `$82` - 16 ticks  (eighth note)&lt;br /&gt;
  * `$83` - 32 ticks  (quarter note)&lt;br /&gt;
  * `$84` - 48 ticks  (dotted quarter note)&lt;br /&gt;
  * `$85` - 64 ticks  (half note)&lt;br /&gt;
  * `$86` - 96 ticks  (dotted half note)&lt;br /&gt;
  * `$87` - 128 ticks (whole note)&lt;br /&gt;
  * `$88` - 11 ticks  (eighth note triplet)&lt;br /&gt;
  * `$89` - 10 ticks  (???)&lt;br /&gt;
  * `$8A` - 80 ticks  (half note + eighth note)&lt;br /&gt;
  * `$8B` - 256 ticks (two whole notes)&lt;br /&gt;
  * `$8F` - 6 ticks   (dotted 32nd note)&lt;br /&gt;
&lt;br /&gt;
Note: $8C, $8D and $8E are duplicates, so these could be used for&lt;br /&gt;
custom durations if desired.&lt;br /&gt;
&lt;br /&gt;
The pitch values are much more straightforward.  As far as I can tell,&lt;br /&gt;
`$4C` is A4 and every semitone away from that adds or subtracts 2 from&lt;br /&gt;
the value.  Thus, C5 (three semitones above A4) is `$52`.  The value&lt;br /&gt;
`$02` represents a rest.&lt;br /&gt;
&lt;br /&gt;
        -2-  -3-  -4-  -5-  -6-  -7-&lt;br /&gt;
   A  :      $12  $2A  $42  $5A  $72&lt;br /&gt;
   Bb :      $14  $2C  $44  $5C  $74&lt;br /&gt;
   B  :      $16  $2E  $46  $5E  $76&lt;br /&gt;
   C  :      $18  $30  $48  $60  $78&lt;br /&gt;
   C# :      $1A  $32  $4A  $62  $7A&lt;br /&gt;
   D  : $04  $1C  $34  $4C  $64&lt;br /&gt;
   Eb : $06  $1E  $36  $4E  $66&lt;br /&gt;
   E  : $08  $20  $38  $50  $68&lt;br /&gt;
   F  : $0A  $22  $3A  $52  $6A&lt;br /&gt;
   F# : $0C  $24  $3C  $54  $6C&lt;br /&gt;
   G  : $0E  $26  $3E  $56  $6E&lt;br /&gt;
   G# : $10  $28  $40  $58  $70&lt;br /&gt;
&lt;br /&gt;
By way of an example, let's look at the main section of the vanilla&lt;br /&gt;
title music.  The title metadata starts at bank 6 `$84DA`:&lt;br /&gt;
&lt;br /&gt;
    08 11 14 16 19 1E 1E 1E&lt;br /&gt;
             ^ Main section&lt;br /&gt;
&lt;br /&gt;
The main section is the fourth song, starting at `$84DA + $16 = $84F0`:&lt;br /&gt;
&lt;br /&gt;
    3F 3F 00&lt;br /&gt;
&lt;br /&gt;
This means there is a single phrase for the main section, which is&lt;br /&gt;
repeated twice.  The phrase data is at `$84DA + $3F = $8519`.&lt;br /&gt;
&lt;br /&gt;
    00 3D 86 3A 1A 5F&lt;br /&gt;
&lt;br /&gt;
From this, we see the melody note data is located at `$863D`:&lt;br /&gt;
&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    02 Rest&lt;br /&gt;
    48 Ab4&lt;br /&gt;
    82 Eighth note&lt;br /&gt;
    46 G4&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    3E Eb4&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    84 Dotted quarter notes&lt;br /&gt;
    2E G3&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    30 Ab3&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    3A Db4&lt;br /&gt;
    38 C4&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    30 Ab3&lt;br /&gt;
    82 Eighth notes&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    30 Ab3&lt;br /&gt;
    85 Half notes&lt;br /&gt;
    2E G3&lt;br /&gt;
    82 Quarter notes&lt;br /&gt;
    02 Rest&lt;br /&gt;
    00 End of Data&lt;br /&gt;
&lt;br /&gt;
== Main Music Engine ==&lt;br /&gt;
&lt;br /&gt;
=== Data Tables ===&lt;br /&gt;
&lt;br /&gt;
There are several data tables used by the music engine.&lt;br /&gt;
&lt;br /&gt;
==== Pulse Envelope ====&lt;br /&gt;
&lt;br /&gt;
This table is used to set the volume of the pulse channels over time.  The index&lt;br /&gt;
used starts at a certain value and decrements, so the table is in reverse order.&lt;br /&gt;
The index is halved before use, so each value lasts two frames.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ Pulse Envelope &amp;lt;code&amp;gt;$9135&amp;lt;/code&amp;gt;&lt;br /&gt;
|90||91||91||91||92||92||92||92&lt;br /&gt;
|-&lt;br /&gt;
|93||93||94||94||94||95||95||95&lt;br /&gt;
|-&lt;br /&gt;
|96||96||96||97||97||97||98||98&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Duration LUT ====&lt;br /&gt;
&lt;br /&gt;
This table is used to determine the duration of a note.  The note data is stored&lt;br /&gt;
with 5 bits of pitch information and 3 bits of duration, as such:&lt;br /&gt;
&lt;br /&gt;
 D1 D0 P4 P3 P2 P1 P0 D2&lt;br /&gt;
&lt;br /&gt;
The Get Note Duration routine shifts things around to get the D bits into the&lt;br /&gt;
three least significant bits and masks it before use.  This value is then added&lt;br /&gt;
to the tempo value in &amp;lt;code&amp;gt;$E5&amp;lt;/code&amp;gt; which is always a multiple of 8 to index&lt;br /&gt;
into the following table:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+Duration LUT &amp;lt;code&amp;gt;$914D&amp;lt;/code&amp;gt;&lt;br /&gt;
|'''00'''||04||0C||08||10||18||20||05||06&lt;br /&gt;
|-&lt;br /&gt;
|'''08'''||04||0F||09||12||1B||24||06||06&lt;br /&gt;
|-&lt;br /&gt;
|'''10'''||05||0F||0A||14||1E||28||07||06&lt;br /&gt;
|-&lt;br /&gt;
|'''18'''||06||12||0C||18||24||30||08||10&lt;br /&gt;
|-&lt;br /&gt;
|'''20'''||07||15||0E||1C||2A||38||13||12&lt;br /&gt;
|-&lt;br /&gt;
|'''28'''||07||15||0E||1C||2A||38||XX||XX&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The last two values in each row seem a bit strange, as they are not nice&lt;br /&gt;
multiples of the first entry which is usually thought of as the length of an&lt;br /&gt;
eighth note.  The last row has garbage values in those positions as that is the&lt;br /&gt;
start of another data table, so they are not even listed here.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' Check what values are used in vanilla.&lt;br /&gt;
&lt;br /&gt;
==== Pitch LUT ====&lt;br /&gt;
&lt;br /&gt;
This table is used to detemine the pitch of a note.  The note data is stored as&lt;br /&gt;
described above, and the fact that the least significant bit is masked out is&lt;br /&gt;
advantageous since the pulse channels can use 11 bits of timer data, so each&lt;br /&gt;
entry in this table is actually two bytes wide.&lt;br /&gt;
&lt;br /&gt;
Rather than show the raw bytes here, it's more useful to show the rough note&lt;br /&gt;
that those values represent.  If the raw values are interesting to you, check&lt;br /&gt;
the ROM for them.  It's worth noting that some of the higher notes are several&lt;br /&gt;
cents off from the listed note.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+Pitch LUT &amp;lt;code&amp;gt;$918F&amp;lt;/code&amp;gt;&lt;br /&gt;
|  || 00|| 02|| 04|| 06|| 08|| 0a|| 0c|| 0e&lt;br /&gt;
|-&lt;br /&gt;
|00|| C3||---|| E3|| G3||G#3|| A3||A#3|| B3&lt;br /&gt;
|-&lt;br /&gt;
|10|| C4||C#4|| D4||D#4|| E4|| F4||F#4|| G4&lt;br /&gt;
|-&lt;br /&gt;
|20||G#4|| A4||A#4|| B4|| C5||C#5|| D5||D#5&lt;br /&gt;
|-&lt;br /&gt;
|30|| E5|| F5||F#5|| G5|| A5||A#5|| B5||C#3&lt;br /&gt;
|-&lt;br /&gt;
|40|| D3||D#3|| F3||F#3||G#5|| C6||C#6|| D6&lt;br /&gt;
|-&lt;br /&gt;
|50||D#6|| E6|| F6||F#6|| G6||G#6|| A6||A#6&lt;br /&gt;
|-&lt;br /&gt;
|60|| B6|| C7||C#7|| D7||D#7|| E7|| F7||F#7&lt;br /&gt;
|-&lt;br /&gt;
|70|| G7||G#7|| A7||A#7|| B7|| C8&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Value &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt; represents a rest.&lt;br /&gt;
&lt;br /&gt;
Note that although the table goes through &amp;lt;code&amp;gt;$7A&amp;lt;/code&amp;gt; values above&lt;br /&gt;
&amp;lt;code&amp;gt;$3E&amp;lt;/code&amp;gt; aren't usable due to the 5 bit limit for pitch data in&lt;br /&gt;
the song data.  However, the sound effects routines make use of these&lt;br /&gt;
extra notes.  For example, the sword beam effect alternates between&lt;br /&gt;
playing C7 and F6.&lt;br /&gt;
&lt;br /&gt;
==== Noise Samples ====&lt;br /&gt;
&lt;br /&gt;
Several sound effects use the same routine to load &amp;quot;samples&amp;quot; for the noise&lt;br /&gt;
channel to play.  These values have the noise volume in the high nybble and the&lt;br /&gt;
noise period in the low nybble.&lt;br /&gt;
&lt;br /&gt;
 $90E8 - Sword slash&lt;br /&gt;
 $9123 - Enemy hurt&lt;br /&gt;
 $912C - Crumble block&lt;br /&gt;
&lt;br /&gt;
=== Variables ===&lt;br /&gt;
&lt;br /&gt;
This is just a dump of RAM addresses used by the music engine and a short&lt;br /&gt;
description of what they are.&lt;br /&gt;
&lt;br /&gt;
 $00E0 - Current phrase note data address (low byte)&lt;br /&gt;
 $00E1 - Current phrase note data address (high byte)&lt;br /&gt;
 $00E2 - Song offset, converted from song id to be an index instead of a bit field&lt;br /&gt;
 $00E3 - Current phrase index&lt;br /&gt;
 $00E4 - Drums loop start&lt;br /&gt;
 $00E5 - Tempo&lt;br /&gt;
 $00E6 - Pulse 1 low bits (used for vibrato effect)&lt;br /&gt;
 $00E7 - Pulse 2 low bits (used for vibrato effect)&lt;br /&gt;
 $00E8 - Pitch storage, temporary&lt;br /&gt;
 $00E9 - Play sound effect&lt;br /&gt;
 $00EA - Disable music&lt;br /&gt;
 $00EB - Request new song&lt;br /&gt;
 $00EC - Play sound effect&lt;br /&gt;
 $00ED - Play sound effect&lt;br /&gt;
 $00EE - Play sound effect&lt;br /&gt;
 $00EF - Play sound effect&lt;br /&gt;
&lt;br /&gt;
 $0707 - Current world&lt;br /&gt;
 $075F - Queued song, loaded after screen transitions&lt;br /&gt;
 $07DA - Ganon Laugh sample&lt;br /&gt;
 $07DB - Song to resume after fanfare&lt;br /&gt;
 $07DF - ??? Likely sound effect flag for pulse 1 channel override&lt;br /&gt;
&lt;br /&gt;
 $07E0 - ??? Likely sound effect flag for noise channel&lt;br /&gt;
 $07E2 - Pulse 2 envelope index&lt;br /&gt;
 $07E3 - Pulse 1 envelope index&lt;br /&gt;
 $07E4 - Drums current note duration&lt;br /&gt;
 $07E5 - Bass current note duration&lt;br /&gt;
 $07E6 - Harmony current note duration&lt;br /&gt;
 $07E7 - Melody current note duration&lt;br /&gt;
 $07E8 - Drums next note index&lt;br /&gt;
 $07E9 - Bass next note index&lt;br /&gt;
 $07EA - Harmony next note index&lt;br /&gt;
 $07EB - Melody next note index&lt;br /&gt;
&lt;br /&gt;
 $07EC - Ganon Laugh counter&lt;br /&gt;
 $07ED - Sound FX counter (E9)&lt;br /&gt;
 $07EE - Sound FX counter (E9)&lt;br /&gt;
 $07F5 - Sound FX counter (ED)&lt;br /&gt;
&lt;br /&gt;
 $07FA - Current SFX (E9)&lt;br /&gt;
 $07FB - Current song&lt;br /&gt;
 $07FD - Current SFX (ED)&lt;br /&gt;
 $07FE - ??? Likely sound effect flag for pulse 2 channel override&lt;br /&gt;
 $07FF - Current SFX (EF)&lt;br /&gt;
&lt;br /&gt;
=== Routines ===&lt;br /&gt;
&lt;br /&gt;
This is a non-exhaustive list of the routines in bank 6 involving the playing of&lt;br /&gt;
music and sound effects.  Each routine also lists its address so you can search&lt;br /&gt;
in a disassembly for the actual code for that routine if these descriptions&lt;br /&gt;
aren't clear.&lt;br /&gt;
&lt;br /&gt;
==== Main Music Loop &amp;lt;code&amp;gt;$9000&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Checks that the music disable flag is off, then runs several SFX routines.&lt;br /&gt;
After the SFX routines, the main music routine runs.  After all this, various&lt;br /&gt;
music related memory locations are cleared.&lt;br /&gt;
&lt;br /&gt;
==== Configure Pulse1 Channel &amp;lt;code&amp;gt;$9031&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes registers x and y and configures the APU registers &amp;lt;code&amp;gt;$4000&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$4001&amp;lt;/code&amp;gt;&lt;br /&gt;
respectively.&lt;br /&gt;
&lt;br /&gt;
==== Configure Pulse2 Channel &amp;lt;code&amp;gt;$9038&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes registers x and y and configures the APU registers &amp;lt;code&amp;gt;$4004&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$4005&amp;lt;/code&amp;gt;&lt;br /&gt;
respectively.&lt;br /&gt;
&lt;br /&gt;
==== Play Note Pulse1 &amp;lt;code&amp;gt;$9042&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a pitch index in register a and plays the note on the pulse 1 channel.&lt;br /&gt;
&lt;br /&gt;
==== Play Note &amp;lt;code&amp;gt;$9044&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Called by the various play note routines.  Takes a pitch index in register a and&lt;br /&gt;
uses register x as a channel selector.  For the pulse channels, this routine&lt;br /&gt;
also saves the lower 8 bits of the APU timer register to &amp;lt;code&amp;gt;$E6&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$E7&amp;lt;/code&amp;gt; which&lt;br /&gt;
are used for a vibrato effect.&lt;br /&gt;
&lt;br /&gt;
==== Play Note Pulse2 &amp;lt;code&amp;gt;$9067&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a pitch index in register a and plays the note on the pulse 2 channel.&lt;br /&gt;
&lt;br /&gt;
==== Play Note Triangle &amp;lt;code&amp;gt;$906B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a pitch index in register a and plays the note on the triangle channel.&lt;br /&gt;
&lt;br /&gt;
==== Get Note Duration &amp;lt;code&amp;gt;$906F&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes note data in register a and looks up the duration.   The original note&lt;br /&gt;
data is saved in register x and the result of the duration lookup is put in&lt;br /&gt;
register a.  The duration lookup is offset by the tempo stored in &amp;lt;code&amp;gt;$E5&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Vibrato Pulse1 &amp;lt;code&amp;gt;$9089&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Applies a vibrato effect on the pulse 1 channel.  Takes the duration of the note&lt;br /&gt;
left in register a and the APU timer low bits in register y.&lt;br /&gt;
&lt;br /&gt;
==== Vibrato Pulse2 &amp;lt;code&amp;gt;$9097&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Applies a vibrato effect on the pulse 2 channel.  Takes the duration of the note&lt;br /&gt;
left in register a and the APU timer low bits in register y.&lt;br /&gt;
&lt;br /&gt;
==== Vibrato &amp;lt;code&amp;gt;$909E&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes the duration of the note remaining in register a, the low bits of the APU&lt;br /&gt;
timer in register y, and the channel to use in register x.  This is called by&lt;br /&gt;
the other vibrato routines after setting x appropriately.&lt;br /&gt;
&lt;br /&gt;
Uses the &amp;lt;code&amp;gt;$04&amp;lt;/code&amp;gt; bit of the duration to either add or subtract 2 from the low&lt;br /&gt;
timer bits.  Only the low bits are used because writing to the high bits resets&lt;br /&gt;
the phase of the channel which can cause clicks.  None of the notes from the&lt;br /&gt;
pitch LUT are close enough to the boundary for the change to require the high&lt;br /&gt;
bits to change anyway, so this works out fine.&lt;br /&gt;
&lt;br /&gt;
==== Play E9 SFX &amp;lt;code&amp;gt;$920B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$E9&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play EF SFX &amp;lt;code&amp;gt;$92F4&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$EF&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play EE SFX &amp;lt;code&amp;gt;$9408&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$EE&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play Noise &amp;lt;code&amp;gt;$959D&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Configures the APU noise registers to make a noise burst.  Takes configuration&lt;br /&gt;
for registers &amp;lt;code&amp;gt;$400F&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;$400E&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;$400C&amp;lt;/code&amp;gt; in registers y, x, and a.&lt;br /&gt;
&lt;br /&gt;
==== Crumble Bridge SFX &amp;lt;code&amp;gt;$956C&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Starts playing the sound effect for a crumble bridge crumbling.  This uses both&lt;br /&gt;
the noise and the triangle channels so it sets flags to disable the music on&lt;br /&gt;
those channels.  Continues with SFX Noise Decay.&lt;br /&gt;
&lt;br /&gt;
==== SFX Noise Decay &amp;lt;code&amp;gt;$9587&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Counts down the timer in &amp;lt;code&amp;gt;$07F5&amp;lt;/code&amp;gt; and turns off the noise when it reaches 0.&lt;br /&gt;
Also clears the flag for the ED sound effect in &amp;lt;code&amp;gt;$07FD&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Play ED SFX &amp;lt;code&amp;gt;$95A7&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect request in &amp;lt;code&amp;gt;$ED&amp;lt;/code&amp;gt; or continues the sound effect saved in&lt;br /&gt;
&amp;lt;code&amp;gt;$07FD&amp;lt;/code&amp;gt;.  Existing sound effects which are higher than the current sound effect&lt;br /&gt;
will be ignored.  If the same sound effect is requested that is already playing,&lt;br /&gt;
it will restart the sound effect except for &amp;lt;code&amp;gt;$80&amp;lt;/code&amp;gt; which gives priority to&lt;br /&gt;
continuing.  When the sound is continued instead of newly played, the decay&lt;br /&gt;
routine will be called instead.&lt;br /&gt;
&lt;br /&gt;
; &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt;&lt;br /&gt;
: Crumble Bridge&lt;br /&gt;
; &amp;lt;code&amp;gt;$04&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt;&lt;br /&gt;
: Crumble Block / Enemy Hurt&lt;br /&gt;
; &amp;lt;code&amp;gt;$20&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$80&amp;lt;/code&amp;gt;&lt;br /&gt;
: Sword Strike&lt;br /&gt;
; &amp;lt;code&amp;gt;$40&amp;lt;/code&amp;gt;&lt;br /&gt;
: Very short noise burst, probably unused.  Doesn't store the effect, and doesn't have an associated decay.&lt;br /&gt;
&lt;br /&gt;
==== Crumble Block / Enemy Hurt SFX &amp;lt;code&amp;gt;$95E6&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Starts playing the sound of a block breaking or an enemy getting hurt.&lt;br /&gt;
&lt;br /&gt;
==== Crumble Block / Enemy Hurt SFX Decay &amp;lt;code&amp;gt;$95EE&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Looks up noise register values based on the timer and the sound effect value.&lt;br /&gt;
Crumble Block samples are at &amp;lt;code&amp;gt;$912C&amp;lt;/code&amp;gt; while Enemy Hurt is at &amp;lt;code&amp;gt;$9123&amp;lt;/code&amp;gt;.  Continues&lt;br /&gt;
on to Play Noise Samples.&lt;br /&gt;
&lt;br /&gt;
==== Play Noise Samples &amp;lt;code&amp;gt;$9612&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a sample value in register a.  The low four bits of this are used to set&lt;br /&gt;
the noise period via APU register &amp;lt;code&amp;gt;$400E&amp;lt;/code&amp;gt;.  The high four bits are used to set&lt;br /&gt;
the noise volume via APU register &amp;lt;code&amp;gt;$400C&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Continues on the SFX Noise Decay process.&lt;br /&gt;
&lt;br /&gt;
==== Sword Strike SFX &amp;lt;code&amp;gt;$9604&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Starts playing the sound of swinging your sword.  This looks up samples based on&lt;br /&gt;
the duration left at &amp;lt;code&amp;gt;$90E8&amp;lt;/code&amp;gt; and plays them via Play Noise Samples.&lt;br /&gt;
&lt;br /&gt;
==== Play EC SFX &amp;lt;code&amp;gt;$990B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$EC&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play Music &amp;lt;code&amp;gt;$9B18&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the music for the game.&lt;br /&gt;
&lt;br /&gt;
Runs Load Song if a new song is requested in &amp;lt;code&amp;gt;$EB&amp;lt;/code&amp;gt;.  Otherwise, runs Play Notes&lt;br /&gt;
if the current song is set in &amp;lt;code&amp;gt;$07FB&amp;lt;/code&amp;gt;.  If neither of these is true, do nothing.&lt;br /&gt;
&lt;br /&gt;
==== Next Song &amp;lt;code&amp;gt;$9B24&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Checks if the previous song should loop.  If not, calls Mute All and returns.&lt;br /&gt;
&lt;br /&gt;
If the previous song was &amp;lt;code&amp;gt;$01&amp;lt;/code&amp;gt; (the &amp;quot;intro&amp;quot; to the main theme), sets the next&lt;br /&gt;
song to &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt; (the main theme loop).&lt;br /&gt;
&lt;br /&gt;
If the previous song was &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; (the item get / level up fanfare), sets the&lt;br /&gt;
current song from &amp;lt;code&amp;gt;$07DB&amp;lt;/code&amp;gt;.  Otherwise, leaves the current song the same as the&lt;br /&gt;
previous song.&lt;br /&gt;
&lt;br /&gt;
Proceeds to Load Song.&lt;br /&gt;
&lt;br /&gt;
==== Mute All &amp;lt;code&amp;gt;$9B3B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Clears the current song at &amp;lt;code&amp;gt;$07FB&amp;lt;/code&amp;gt; and sets the APU registers to mute the volume&lt;br /&gt;
on both pulse channels, the triangle channel, and the noise channel.&lt;br /&gt;
&lt;br /&gt;
==== Load Song &amp;lt;code&amp;gt;$9B61&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a song id in register a and preps some variables for that song.&lt;br /&gt;
&lt;br /&gt;
If the song is &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; (item get / level up fanfare) then it first saves the&lt;br /&gt;
previous song to &amp;lt;code&amp;gt;$07FB&amp;lt;/code&amp;gt; if it is less than &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; so that the previous music&lt;br /&gt;
will resume after the fanfare.&lt;br /&gt;
&lt;br /&gt;
Sets the phrase counter at &amp;lt;code&amp;gt;$E3&amp;lt;/code&amp;gt; to 0 and the song offset at &amp;lt;code&amp;gt;$E2&amp;lt;/code&amp;gt; based on the&lt;br /&gt;
song id.  Song ids are a bit field, and the song offset is the position of the&lt;br /&gt;
lowest bit in the song id for indexing into the song table.&lt;br /&gt;
&lt;br /&gt;
Proceeds to Load Song Data.&lt;br /&gt;
&lt;br /&gt;
==== Load Song Data &amp;lt;code&amp;gt;$9B80&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Loads the music variables based on the current world and current song offset.&lt;br /&gt;
&lt;br /&gt;
There are four blocks of code that are basically the same, for each of the four&lt;br /&gt;
different music tables.  Each one just uses a different position for the tables&lt;br /&gt;
to load.&lt;br /&gt;
&lt;br /&gt;
Based on the current world in &amp;lt;code&amp;gt;$0707&amp;lt;/code&amp;gt; it loads data from these offsets:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt;&lt;br /&gt;
: Overworld music - &amp;lt;code&amp;gt;$9B87&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;$01&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt;&lt;br /&gt;
: Town music - &amp;lt;code&amp;gt;$9BC6&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;$03&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$04&amp;lt;/code&amp;gt;&lt;br /&gt;
: Palace music - &amp;lt;code&amp;gt;$9C05&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;$05&amp;lt;/code&amp;gt; or higher&lt;br /&gt;
: Great Palace music - &amp;lt;code&amp;gt;$9C42&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, uses the song offset in &amp;lt;code&amp;gt;$E2&amp;lt;/code&amp;gt;  to index into the song table at the base&lt;br /&gt;
offset given above.&lt;br /&gt;
&lt;br /&gt;
The phrase counter at &amp;lt;code&amp;gt;$E3&amp;lt;/code&amp;gt; is added to that and used as in index again to&lt;br /&gt;
retrieve the current phrase offset.  If the current phrase offset is &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt; that&lt;br /&gt;
means the song is over and the process goes back to the Next Song routine.&lt;br /&gt;
&lt;br /&gt;
Otherwise, the following structure is read at the base offset above + phrase&lt;br /&gt;
offset just calculated.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;$E5&amp;lt;/code&amp;gt; Tempo&lt;br /&gt;
* &amp;lt;code&amp;gt;$E0&amp;lt;/code&amp;gt; Note Data Low&lt;br /&gt;
* &amp;lt;code&amp;gt;$E1&amp;lt;/code&amp;gt; Note Data High&lt;br /&gt;
* &amp;lt;code&amp;gt;$07E9&amp;lt;/code&amp;gt; Bass Note Pointer&lt;br /&gt;
* &amp;lt;code&amp;gt;$07EA&amp;lt;/code&amp;gt; Harmony Note Pointer&lt;br /&gt;
* &amp;lt;code&amp;gt;$07E8&amp;lt;/code&amp;gt; Drum Note Pointer&lt;br /&gt;
&lt;br /&gt;
After this, the drum loop start point at &amp;lt;code&amp;gt;$E4&amp;lt;/code&amp;gt; is also set to the same value as&lt;br /&gt;
the drum note pointer.  The melody note pointer at &amp;lt;code&amp;gt;$07EB&amp;lt;/code&amp;gt; is set to &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt;, and&lt;br /&gt;
all the durations (melody at &amp;lt;code&amp;gt;$07E7&amp;lt;/code&amp;gt;, harmony at &amp;lt;code&amp;gt;$07E6&amp;lt;/code&amp;gt;, bass at &amp;lt;code&amp;gt;$07E5&amp;lt;/code&amp;gt;, and&lt;br /&gt;
drums at &amp;lt;code&amp;gt;$07E4&amp;lt;/code&amp;gt;) are set to &amp;lt;code&amp;gt;$01&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Proceeds to Play Notes.&lt;br /&gt;
&lt;br /&gt;
==== Play Notes ====&lt;br /&gt;
&lt;br /&gt;
This routine is broken into four major and similar sections: one each for&lt;br /&gt;
melody, harmony, bass, and drums.  The general process is as follows:&lt;br /&gt;
&lt;br /&gt;
 decrement duration&lt;br /&gt;
 if duration == 0 {&lt;br /&gt;
   load next note&lt;br /&gt;
   increment note pointer&lt;br /&gt;
   set duration for note&lt;br /&gt;
   if no overriding sound effect {&lt;br /&gt;
     play note on appropriate channel&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Some channels have unique processing at different points in this.  For example,&lt;br /&gt;
the melody note data is null terminated, so if the loaded note in &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt; then the&lt;br /&gt;
routine will jump to Load Song Data instead to go to the next phrase.  If the&lt;br /&gt;
drums channel gets a &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt; note it will reset the drums note pointer to the&lt;br /&gt;
drums loop start and repeat the phrase.  The bass and harmony channels don't&lt;br /&gt;
have this looping behavior.  This means that if the bass or harmony channels&lt;br /&gt;
aren't long enough, the engine will happily just play whatever data comes next&lt;br /&gt;
in the ROM.&lt;br /&gt;
&lt;br /&gt;
The melody is played on pulse 2 channel, the harmony on pulse 1 (which gets&lt;br /&gt;
subverted by sound effects that need just one pulse channel), the bass on the&lt;br /&gt;
triangle channel, and the drums on the noise channel.&lt;br /&gt;
&lt;br /&gt;
For the pulse channels, after the note is played, the envelope counter for that&lt;br /&gt;
channel (&amp;lt;code&amp;gt;$07E2&amp;lt;/code&amp;gt; for pulse 1, &amp;lt;code&amp;gt;$07E3&amp;lt;/code&amp;gt; for pulse 2) is set.  Normally the&lt;br /&gt;
envelope is set to &amp;lt;code&amp;gt;$2F&amp;lt;/code&amp;gt; but song &amp;lt;code&amp;gt;$40&amp;lt;/code&amp;gt; (crystal fanfare and final boss music)&lt;br /&gt;
sets it to &amp;lt;code&amp;gt;$18&amp;lt;/code&amp;gt; instead.  The envelope is not set if no note was played because&lt;br /&gt;
the pitch index was &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt; which indicates a rest.&lt;br /&gt;
&lt;br /&gt;
Also for the pulse channels, after the above pseudocode, the vibrato and&lt;br /&gt;
enveloping happens as long as there is no overriding sound effect.  The envelope&lt;br /&gt;
works by checking the envelope counter set previously and decrementing it.  The&lt;br /&gt;
counter value is halved and used to look up the envelope value in the LUT at&lt;br /&gt;
&amp;lt;code&amp;gt;$9135&amp;lt;/code&amp;gt;.  These values are written directly to APU register &amp;lt;code&amp;gt;$4000&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$4004&amp;lt;/code&amp;gt;&lt;br /&gt;
depending on the channel.&lt;br /&gt;
&lt;br /&gt;
The bass channel has one special handling as well.  The APU register &amp;lt;code&amp;gt;$4008&amp;lt;/code&amp;gt; is&lt;br /&gt;
set differently based on the current song id.  For song &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; (item get / level&lt;br /&gt;
up fanfare) the register is set to &amp;lt;code&amp;gt;$60&amp;lt;/code&amp;gt; but all other songs use &amp;lt;code&amp;gt;$1f&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The noise channel is not affect at all by the pitch.  Any pitch value that is&lt;br /&gt;
non zero will play a burst of noise.&lt;/div&gt;</summary>
		<author><name>DontBaguMe</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.bindingforce.net/index.php?title=Music_Engine_Description&amp;diff=343</id>
		<title>Music Engine Description</title>
		<link rel="alternate" type="text/html" href="http://wiki.bindingforce.net/index.php?title=Music_Engine_Description&amp;diff=343"/>
		<updated>2022-03-30T10:58:05Z</updated>

		<summary type="html">&lt;p&gt;DontBaguMe: /* Title Music Engine */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There are two separate music engines in Zelda 2.  Both are in bank 6 (along&lt;br /&gt;
with the song data).  The music engine for the title screen resides at&lt;br /&gt;
&amp;lt;code&amp;gt;$8000&amp;lt;/code&amp;gt; and the engine for the rest of the game is at&lt;br /&gt;
&amp;lt;code&amp;gt;$9000&amp;lt;/code&amp;gt;.  Each of these engines is called once per frame during the&lt;br /&gt;
appropriate part of the game.&lt;br /&gt;
&lt;br /&gt;
Changes to the music engine should be very easy to make, as there is an&lt;br /&gt;
abundance of free space in bank 6.&lt;br /&gt;
&lt;br /&gt;
== Title Music Engine ==&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' properly format the following by bgt (original at https://eab.xyz/W2zplGD) and dontbagume  (on discord).&lt;br /&gt;
&lt;br /&gt;
# Title Music Engine&lt;br /&gt;
&lt;br /&gt;
The title music engine is different from the engine that plays music&lt;br /&gt;
during the rest of the game.  In particular, it has a wider range of&lt;br /&gt;
notes that can be represented.  If you want to look into the actual&lt;br /&gt;
code, the title screen music engine main loop is at `$8000` (vesus&lt;br /&gt;
`$9000` for the game music loop).&lt;br /&gt;
&lt;br /&gt;
The metadata for the songs is all the same as in the other areas,&lt;br /&gt;
although the title music is actually broken up into several songs&lt;br /&gt;
itself.  These song boundaries control the timing of the title scroll.&lt;br /&gt;
&lt;br /&gt;
The following songs exist in the title song table:&lt;br /&gt;
&lt;br /&gt;
  1. Intro&lt;br /&gt;
  2. Start&lt;br /&gt;
  3. Build up&lt;br /&gt;
  4. Main&lt;br /&gt;
  5. Breakdown&lt;br /&gt;
&lt;br /&gt;
The start of song 2 triggers the title to scroll into view.  The start&lt;br /&gt;
of song 4 triggers a countdown until the story scroll begins.  After&lt;br /&gt;
song 5 finishes, the engine loops back to song 2.&lt;br /&gt;
&lt;br /&gt;
Within the note data, a special format is used.  Rather than encoding&lt;br /&gt;
the duration and pitch together in a single byte, the title music has&lt;br /&gt;
&amp;quot;pitch&amp;quot; bytes and &amp;quot;duration&amp;quot; bytes.  Any byte with the highest bit set&lt;br /&gt;
(i.e. anything &amp;gt;= `$80`) is interpreted as a duration change, which&lt;br /&gt;
sets the duration of all future notes.  Anything else is a pitch value&lt;br /&gt;
which indicates a note of the current duration should be played.  The&lt;br /&gt;
low nibble of duration values keys into a lookup table at bank 6 `$8084` &lt;br /&gt;
and store the duration byte at $07FF.  Value durations are as follows:&lt;br /&gt;
&lt;br /&gt;
  * `$80` - 8 ticks   (sixteenth note)&lt;br /&gt;
  * `$81` - 24 ticks  (dotted eighth note)&lt;br /&gt;
  * `$82` - 16 ticks  (eighth note)&lt;br /&gt;
  * `$83` - 32 ticks  (quarter note)&lt;br /&gt;
  * `$84` - 48 ticks  (dotted quarter note)&lt;br /&gt;
  * `$85` - 64 ticks  (half note)&lt;br /&gt;
  * `$86` - 96 ticks  (dotted half note)&lt;br /&gt;
  * `$87` - 128 ticks (whole note)&lt;br /&gt;
  * `$88` - 11 ticks  (eighth note triplet)&lt;br /&gt;
  * `$89` - 10 ticks  (???)&lt;br /&gt;
  * `$8A` - 80 ticks  (half note + eighth note)&lt;br /&gt;
  * `$8B` - 256 ticks (two whole notes)&lt;br /&gt;
  * `$8F` - 6 ticks   (dotted 32nd note)&lt;br /&gt;
&lt;br /&gt;
Note: $8C, $8D and $8E are duplicates, so these could be used for&lt;br /&gt;
custom durations if desired.&lt;br /&gt;
&lt;br /&gt;
The pitch values are much more straightforward.  As far as I can tell,&lt;br /&gt;
`$4C` is A4 and every semitone away from that adds or subtracts 2 from&lt;br /&gt;
the value.  Thus, C5 (three semitones above A4) is `$52`.  The value&lt;br /&gt;
`$02` represents a rest.&lt;br /&gt;
&lt;br /&gt;
        -2-  -3-  -4-  -5-  -6-  -7-&lt;br /&gt;
   A  :      $12  $2A  $42  $5A  $72&lt;br /&gt;
   Bb :      $14  $2C  $44  $5C  $74&lt;br /&gt;
   B  :      $16  $2E  $46  $5E  $76&lt;br /&gt;
   C  :      $18  $30  $48  $60  $78&lt;br /&gt;
   C# :      $1A  $32  $4A  $62  $7A&lt;br /&gt;
   D  : $04  $1C  $34  $4C  $64&lt;br /&gt;
   Eb : $06  $1E  $36  $4E  $66&lt;br /&gt;
   E  : $08  $20  $38  $50  $68&lt;br /&gt;
   F  : $0A  $22  $3A  $52  $6A&lt;br /&gt;
   F# : $0C  $24  $3C  $54  $6C&lt;br /&gt;
   G  : $0E  $26  $3E  $56  $6E&lt;br /&gt;
   G# : $10  $28  $40  $58  $70&lt;br /&gt;
&lt;br /&gt;
By way of an example, let's look at the main section of the vanilla&lt;br /&gt;
title music.  The title metadata starts at bank 6 `$84DA`:&lt;br /&gt;
&lt;br /&gt;
    08 11 14 16 19 1E 1E 1E&lt;br /&gt;
             ^ Main section&lt;br /&gt;
&lt;br /&gt;
The main section is the fourth song, starting at `$84DA + $16 = $84F0`:&lt;br /&gt;
&lt;br /&gt;
    3F 3F 00&lt;br /&gt;
&lt;br /&gt;
This means there is a single phrase for the main section, which is&lt;br /&gt;
repeated twice.  The phrase data is at `$84DA + $3F = $8519`.&lt;br /&gt;
&lt;br /&gt;
    00 3D 86 3A 1A 5F&lt;br /&gt;
&lt;br /&gt;
From this, we see the melody note data is located at `$863D`:&lt;br /&gt;
&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    02 Rest&lt;br /&gt;
    48 Ab4&lt;br /&gt;
    82 Eighth note&lt;br /&gt;
    46 G4&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    3E Eb4&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    84 Dotted quarter notes&lt;br /&gt;
    2E G3&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    30 Ab3&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    3A Db4&lt;br /&gt;
    38 C4&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    30 Ab3&lt;br /&gt;
    82 Eighth notes&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    30 Ab3&lt;br /&gt;
    85 Half notes&lt;br /&gt;
    2E G3&lt;br /&gt;
    82 Quarter notes&lt;br /&gt;
    02 Rest&lt;br /&gt;
    00 End of Data&lt;br /&gt;
&lt;br /&gt;
dontbagume additions:&lt;br /&gt;
&lt;br /&gt;
DURATION&lt;br /&gt;
  * `$80` - 6 ticks    sixteenth note&lt;br /&gt;
  * `$81` - 9 ticks    dotted eighth note&lt;br /&gt;
  * `$82` - 12 ticks    eighth note&lt;br /&gt;
  * `$83` - 24 ticks    quarter note&lt;br /&gt;
  * `$84` - 36 ticks    dotted quarter note&lt;br /&gt;
  * `$85` - 48 ticks    half note&lt;br /&gt;
  * `$86` - 72 ticks    dotted half note&lt;br /&gt;
  * `$87` - 96 ticks    whole note&lt;br /&gt;
  * `$88` - 8 ticks    eighth triplets&lt;br /&gt;
  * `$89` - 7 ticks    poor man's swung 16th (long)&lt;br /&gt;
  * `$8A` - 60 ticks    half + eighth&lt;br /&gt;
  * `$8B` - 192 ticks    two whole notes&lt;br /&gt;
  * `$8F` - 5 ticks    poor man's swung 16th (short)&lt;br /&gt;
&lt;br /&gt;
PITCH&lt;br /&gt;
         -1-    -2-    -3-    -4-    -5-&lt;br /&gt;
  A  :     $04    $1C    $34    $4C    $64&lt;br /&gt;
  Bb :     $06    $1E    $36    $4E    $66&lt;br /&gt;
  B  :     $08    $20    $38    $50    $68&lt;br /&gt;
  C  :     $0A    $22    $3A    $52    $6A&lt;br /&gt;
  C# :     $0C    $24    $3C    $54    $6C&lt;br /&gt;
  D  :     $0E    $26    $3E    $56    $6E&lt;br /&gt;
  Eb :     $10    $28    $40    $58    $70&lt;br /&gt;
  E  :     $12    $2A    $42    $5A    $72&lt;br /&gt;
  F  :     $14    $2C    $44    $5C    $74&lt;br /&gt;
  F# :     $16    $2E    $46    $5E    $76&lt;br /&gt;
  G  :     $18    $30    $48    $60    $78&lt;br /&gt;
  G# :     $1A    $32    $4A    $62    $7A&lt;br /&gt;
&lt;br /&gt;
== Main Music Engine ==&lt;br /&gt;
&lt;br /&gt;
=== Data Tables ===&lt;br /&gt;
&lt;br /&gt;
There are several data tables used by the music engine.&lt;br /&gt;
&lt;br /&gt;
==== Pulse Envelope ====&lt;br /&gt;
&lt;br /&gt;
This table is used to set the volume of the pulse channels over time.  The index&lt;br /&gt;
used starts at a certain value and decrements, so the table is in reverse order.&lt;br /&gt;
The index is halved before use, so each value lasts two frames.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ Pulse Envelope &amp;lt;code&amp;gt;$9135&amp;lt;/code&amp;gt;&lt;br /&gt;
|90||91||91||91||92||92||92||92&lt;br /&gt;
|-&lt;br /&gt;
|93||93||94||94||94||95||95||95&lt;br /&gt;
|-&lt;br /&gt;
|96||96||96||97||97||97||98||98&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Duration LUT ====&lt;br /&gt;
&lt;br /&gt;
This table is used to determine the duration of a note.  The note data is stored&lt;br /&gt;
with 5 bits of pitch information and 3 bits of duration, as such:&lt;br /&gt;
&lt;br /&gt;
 D1 D0 P4 P3 P2 P1 P0 D2&lt;br /&gt;
&lt;br /&gt;
The Get Note Duration routine shifts things around to get the D bits into the&lt;br /&gt;
three least significant bits and masks it before use.  This value is then added&lt;br /&gt;
to the tempo value in &amp;lt;code&amp;gt;$E5&amp;lt;/code&amp;gt; which is always a multiple of 8 to index&lt;br /&gt;
into the following table:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+Duration LUT &amp;lt;code&amp;gt;$914D&amp;lt;/code&amp;gt;&lt;br /&gt;
|'''00'''||04||0C||08||10||18||20||05||06&lt;br /&gt;
|-&lt;br /&gt;
|'''08'''||04||0F||09||12||1B||24||06||06&lt;br /&gt;
|-&lt;br /&gt;
|'''10'''||05||0F||0A||14||1E||28||07||06&lt;br /&gt;
|-&lt;br /&gt;
|'''18'''||06||12||0C||18||24||30||08||10&lt;br /&gt;
|-&lt;br /&gt;
|'''20'''||07||15||0E||1C||2A||38||13||12&lt;br /&gt;
|-&lt;br /&gt;
|'''28'''||07||15||0E||1C||2A||38||XX||XX&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The last two values in each row seem a bit strange, as they are not nice&lt;br /&gt;
multiples of the first entry which is usually thought of as the length of an&lt;br /&gt;
eighth note.  The last row has garbage values in those positions as that is the&lt;br /&gt;
start of another data table, so they are not even listed here.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' Check what values are used in vanilla.&lt;br /&gt;
&lt;br /&gt;
==== Pitch LUT ====&lt;br /&gt;
&lt;br /&gt;
This table is used to detemine the pitch of a note.  The note data is stored as&lt;br /&gt;
described above, and the fact that the least significant bit is masked out is&lt;br /&gt;
advantageous since the pulse channels can use 11 bits of timer data, so each&lt;br /&gt;
entry in this table is actually two bytes wide.&lt;br /&gt;
&lt;br /&gt;
Rather than show the raw bytes here, it's more useful to show the rough note&lt;br /&gt;
that those values represent.  If the raw values are interesting to you, check&lt;br /&gt;
the ROM for them.  It's worth noting that some of the higher notes are several&lt;br /&gt;
cents off from the listed note.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+Pitch LUT &amp;lt;code&amp;gt;$918F&amp;lt;/code&amp;gt;&lt;br /&gt;
|  || 00|| 02|| 04|| 06|| 08|| 0a|| 0c|| 0e&lt;br /&gt;
|-&lt;br /&gt;
|00|| C3||---|| E3|| G3||G#3|| A3||A#3|| B3&lt;br /&gt;
|-&lt;br /&gt;
|10|| C4||C#4|| D4||D#4|| E4|| F4||F#4|| G4&lt;br /&gt;
|-&lt;br /&gt;
|20||G#4|| A4||A#4|| B4|| C5||C#5|| D5||D#5&lt;br /&gt;
|-&lt;br /&gt;
|30|| E5|| F5||F#5|| G5|| A5||A#5|| B5||C#3&lt;br /&gt;
|-&lt;br /&gt;
|40|| D3||D#3|| F3||F#3||G#5|| C6||C#6|| D6&lt;br /&gt;
|-&lt;br /&gt;
|50||D#6|| E6|| F6||F#6|| G6||G#6|| A6||A#6&lt;br /&gt;
|-&lt;br /&gt;
|60|| B6|| C7||C#7|| D7||D#7|| E7|| F7||F#7&lt;br /&gt;
|-&lt;br /&gt;
|70|| G7||G#7|| A7||A#7|| B7|| C8&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Value &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt; represents a rest.&lt;br /&gt;
&lt;br /&gt;
Note that although the table goes through &amp;lt;code&amp;gt;$7A&amp;lt;/code&amp;gt; values above&lt;br /&gt;
&amp;lt;code&amp;gt;$3E&amp;lt;/code&amp;gt; aren't usable due to the 5 bit limit for pitch data in&lt;br /&gt;
the song data.  However, the sound effects routines make use of these&lt;br /&gt;
extra notes.  For example, the sword beam effect alternates between&lt;br /&gt;
playing C7 and F6.&lt;br /&gt;
&lt;br /&gt;
==== Noise Samples ====&lt;br /&gt;
&lt;br /&gt;
Several sound effects use the same routine to load &amp;quot;samples&amp;quot; for the noise&lt;br /&gt;
channel to play.  These values have the noise volume in the high nybble and the&lt;br /&gt;
noise period in the low nybble.&lt;br /&gt;
&lt;br /&gt;
 $90E8 - Sword slash&lt;br /&gt;
 $9123 - Enemy hurt&lt;br /&gt;
 $912C - Crumble block&lt;br /&gt;
&lt;br /&gt;
=== Variables ===&lt;br /&gt;
&lt;br /&gt;
This is just a dump of RAM addresses used by the music engine and a short&lt;br /&gt;
description of what they are.&lt;br /&gt;
&lt;br /&gt;
 $00E0 - Current phrase note data address (low byte)&lt;br /&gt;
 $00E1 - Current phrase note data address (high byte)&lt;br /&gt;
 $00E2 - Song offset, converted from song id to be an index instead of a bit field&lt;br /&gt;
 $00E3 - Current phrase index&lt;br /&gt;
 $00E4 - Drums loop start&lt;br /&gt;
 $00E5 - Tempo&lt;br /&gt;
 $00E6 - Pulse 1 low bits (used for vibrato effect)&lt;br /&gt;
 $00E7 - Pulse 2 low bits (used for vibrato effect)&lt;br /&gt;
 $00E8 - Pitch storage, temporary&lt;br /&gt;
 $00E9 - Play sound effect&lt;br /&gt;
 $00EA - Disable music&lt;br /&gt;
 $00EB - Request new song&lt;br /&gt;
 $00EC - Play sound effect&lt;br /&gt;
 $00ED - Play sound effect&lt;br /&gt;
 $00EE - Play sound effect&lt;br /&gt;
 $00EF - Play sound effect&lt;br /&gt;
&lt;br /&gt;
 $0707 - Current world&lt;br /&gt;
 $075F - Queued song, loaded after screen transitions&lt;br /&gt;
 $07DA - Ganon Laugh sample&lt;br /&gt;
 $07DB - Song to resume after fanfare&lt;br /&gt;
 $07DF - ??? Likely sound effect flag for pulse 1 channel override&lt;br /&gt;
&lt;br /&gt;
 $07E0 - ??? Likely sound effect flag for noise channel&lt;br /&gt;
 $07E2 - Pulse 2 envelope index&lt;br /&gt;
 $07E3 - Pulse 1 envelope index&lt;br /&gt;
 $07E4 - Drums current note duration&lt;br /&gt;
 $07E5 - Bass current note duration&lt;br /&gt;
 $07E6 - Harmony current note duration&lt;br /&gt;
 $07E7 - Melody current note duration&lt;br /&gt;
 $07E8 - Drums next note index&lt;br /&gt;
 $07E9 - Bass next note index&lt;br /&gt;
 $07EA - Harmony next note index&lt;br /&gt;
 $07EB - Melody next note index&lt;br /&gt;
&lt;br /&gt;
 $07EC - Ganon Laugh counter&lt;br /&gt;
 $07ED - Sound FX counter (E9)&lt;br /&gt;
 $07EE - Sound FX counter (E9)&lt;br /&gt;
 $07F5 - Sound FX counter (ED)&lt;br /&gt;
&lt;br /&gt;
 $07FA - Current SFX (E9)&lt;br /&gt;
 $07FB - Current song&lt;br /&gt;
 $07FD - Current SFX (ED)&lt;br /&gt;
 $07FE - ??? Likely sound effect flag for pulse 2 channel override&lt;br /&gt;
 $07FF - Current SFX (EF)&lt;br /&gt;
&lt;br /&gt;
=== Routines ===&lt;br /&gt;
&lt;br /&gt;
This is a non-exhaustive list of the routines in bank 6 involving the playing of&lt;br /&gt;
music and sound effects.  Each routine also lists its address so you can search&lt;br /&gt;
in a disassembly for the actual code for that routine if these descriptions&lt;br /&gt;
aren't clear.&lt;br /&gt;
&lt;br /&gt;
==== Main Music Loop &amp;lt;code&amp;gt;$9000&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Checks that the music disable flag is off, then runs several SFX routines.&lt;br /&gt;
After the SFX routines, the main music routine runs.  After all this, various&lt;br /&gt;
music related memory locations are cleared.&lt;br /&gt;
&lt;br /&gt;
==== Configure Pulse1 Channel &amp;lt;code&amp;gt;$9031&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes registers x and y and configures the APU registers &amp;lt;code&amp;gt;$4000&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$4001&amp;lt;/code&amp;gt;&lt;br /&gt;
respectively.&lt;br /&gt;
&lt;br /&gt;
==== Configure Pulse2 Channel &amp;lt;code&amp;gt;$9038&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes registers x and y and configures the APU registers &amp;lt;code&amp;gt;$4004&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$4005&amp;lt;/code&amp;gt;&lt;br /&gt;
respectively.&lt;br /&gt;
&lt;br /&gt;
==== Play Note Pulse1 &amp;lt;code&amp;gt;$9042&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a pitch index in register a and plays the note on the pulse 1 channel.&lt;br /&gt;
&lt;br /&gt;
==== Play Note &amp;lt;code&amp;gt;$9044&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Called by the various play note routines.  Takes a pitch index in register a and&lt;br /&gt;
uses register x as a channel selector.  For the pulse channels, this routine&lt;br /&gt;
also saves the lower 8 bits of the APU timer register to &amp;lt;code&amp;gt;$E6&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$E7&amp;lt;/code&amp;gt; which&lt;br /&gt;
are used for a vibrato effect.&lt;br /&gt;
&lt;br /&gt;
==== Play Note Pulse2 &amp;lt;code&amp;gt;$9067&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a pitch index in register a and plays the note on the pulse 2 channel.&lt;br /&gt;
&lt;br /&gt;
==== Play Note Triangle &amp;lt;code&amp;gt;$906B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a pitch index in register a and plays the note on the triangle channel.&lt;br /&gt;
&lt;br /&gt;
==== Get Note Duration &amp;lt;code&amp;gt;$906F&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes note data in register a and looks up the duration.   The original note&lt;br /&gt;
data is saved in register x and the result of the duration lookup is put in&lt;br /&gt;
register a.  The duration lookup is offset by the tempo stored in &amp;lt;code&amp;gt;$E5&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Vibrato Pulse1 &amp;lt;code&amp;gt;$9089&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Applies a vibrato effect on the pulse 1 channel.  Takes the duration of the note&lt;br /&gt;
left in register a and the APU timer low bits in register y.&lt;br /&gt;
&lt;br /&gt;
==== Vibrato Pulse2 &amp;lt;code&amp;gt;$9097&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Applies a vibrato effect on the pulse 2 channel.  Takes the duration of the note&lt;br /&gt;
left in register a and the APU timer low bits in register y.&lt;br /&gt;
&lt;br /&gt;
==== Vibrato &amp;lt;code&amp;gt;$909E&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes the duration of the note remaining in register a, the low bits of the APU&lt;br /&gt;
timer in register y, and the channel to use in register x.  This is called by&lt;br /&gt;
the other vibrato routines after setting x appropriately.&lt;br /&gt;
&lt;br /&gt;
Uses the &amp;lt;code&amp;gt;$04&amp;lt;/code&amp;gt; bit of the duration to either add or subtract 2 from the low&lt;br /&gt;
timer bits.  Only the low bits are used because writing to the high bits resets&lt;br /&gt;
the phase of the channel which can cause clicks.  None of the notes from the&lt;br /&gt;
pitch LUT are close enough to the boundary for the change to require the high&lt;br /&gt;
bits to change anyway, so this works out fine.&lt;br /&gt;
&lt;br /&gt;
==== Play E9 SFX &amp;lt;code&amp;gt;$920B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$E9&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play EF SFX &amp;lt;code&amp;gt;$92F4&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$EF&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play EE SFX &amp;lt;code&amp;gt;$9408&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$EE&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play Noise &amp;lt;code&amp;gt;$959D&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Configures the APU noise registers to make a noise burst.  Takes configuration&lt;br /&gt;
for registers &amp;lt;code&amp;gt;$400F&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;$400E&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;$400C&amp;lt;/code&amp;gt; in registers y, x, and a.&lt;br /&gt;
&lt;br /&gt;
==== Crumble Bridge SFX &amp;lt;code&amp;gt;$956C&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Starts playing the sound effect for a crumble bridge crumbling.  This uses both&lt;br /&gt;
the noise and the triangle channels so it sets flags to disable the music on&lt;br /&gt;
those channels.  Continues with SFX Noise Decay.&lt;br /&gt;
&lt;br /&gt;
==== SFX Noise Decay &amp;lt;code&amp;gt;$9587&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Counts down the timer in &amp;lt;code&amp;gt;$07F5&amp;lt;/code&amp;gt; and turns off the noise when it reaches 0.&lt;br /&gt;
Also clears the flag for the ED sound effect in &amp;lt;code&amp;gt;$07FD&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Play ED SFX &amp;lt;code&amp;gt;$95A7&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect request in &amp;lt;code&amp;gt;$ED&amp;lt;/code&amp;gt; or continues the sound effect saved in&lt;br /&gt;
&amp;lt;code&amp;gt;$07FD&amp;lt;/code&amp;gt;.  Existing sound effects which are higher than the current sound effect&lt;br /&gt;
will be ignored.  If the same sound effect is requested that is already playing,&lt;br /&gt;
it will restart the sound effect except for &amp;lt;code&amp;gt;$80&amp;lt;/code&amp;gt; which gives priority to&lt;br /&gt;
continuing.  When the sound is continued instead of newly played, the decay&lt;br /&gt;
routine will be called instead.&lt;br /&gt;
&lt;br /&gt;
; &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt;&lt;br /&gt;
: Crumble Bridge&lt;br /&gt;
; &amp;lt;code&amp;gt;$04&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt;&lt;br /&gt;
: Crumble Block / Enemy Hurt&lt;br /&gt;
; &amp;lt;code&amp;gt;$20&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$80&amp;lt;/code&amp;gt;&lt;br /&gt;
: Sword Strike&lt;br /&gt;
; &amp;lt;code&amp;gt;$40&amp;lt;/code&amp;gt;&lt;br /&gt;
: Very short noise burst, probably unused.  Doesn't store the effect, and doesn't have an associated decay.&lt;br /&gt;
&lt;br /&gt;
==== Crumble Block / Enemy Hurt SFX &amp;lt;code&amp;gt;$95E6&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Starts playing the sound of a block breaking or an enemy getting hurt.&lt;br /&gt;
&lt;br /&gt;
==== Crumble Block / Enemy Hurt SFX Decay &amp;lt;code&amp;gt;$95EE&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Looks up noise register values based on the timer and the sound effect value.&lt;br /&gt;
Crumble Block samples are at &amp;lt;code&amp;gt;$912C&amp;lt;/code&amp;gt; while Enemy Hurt is at &amp;lt;code&amp;gt;$9123&amp;lt;/code&amp;gt;.  Continues&lt;br /&gt;
on to Play Noise Samples.&lt;br /&gt;
&lt;br /&gt;
==== Play Noise Samples &amp;lt;code&amp;gt;$9612&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a sample value in register a.  The low four bits of this are used to set&lt;br /&gt;
the noise period via APU register &amp;lt;code&amp;gt;$400E&amp;lt;/code&amp;gt;.  The high four bits are used to set&lt;br /&gt;
the noise volume via APU register &amp;lt;code&amp;gt;$400C&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Continues on the SFX Noise Decay process.&lt;br /&gt;
&lt;br /&gt;
==== Sword Strike SFX &amp;lt;code&amp;gt;$9604&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Starts playing the sound of swinging your sword.  This looks up samples based on&lt;br /&gt;
the duration left at &amp;lt;code&amp;gt;$90E8&amp;lt;/code&amp;gt; and plays them via Play Noise Samples.&lt;br /&gt;
&lt;br /&gt;
==== Play EC SFX &amp;lt;code&amp;gt;$990B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$EC&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play Music &amp;lt;code&amp;gt;$9B18&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the music for the game.&lt;br /&gt;
&lt;br /&gt;
Runs Load Song if a new song is requested in &amp;lt;code&amp;gt;$EB&amp;lt;/code&amp;gt;.  Otherwise, runs Play Notes&lt;br /&gt;
if the current song is set in &amp;lt;code&amp;gt;$07FB&amp;lt;/code&amp;gt;.  If neither of these is true, do nothing.&lt;br /&gt;
&lt;br /&gt;
==== Next Song &amp;lt;code&amp;gt;$9B24&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Checks if the previous song should loop.  If not, calls Mute All and returns.&lt;br /&gt;
&lt;br /&gt;
If the previous song was &amp;lt;code&amp;gt;$01&amp;lt;/code&amp;gt; (the &amp;quot;intro&amp;quot; to the main theme), sets the next&lt;br /&gt;
song to &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt; (the main theme loop).&lt;br /&gt;
&lt;br /&gt;
If the previous song was &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; (the item get / level up fanfare), sets the&lt;br /&gt;
current song from &amp;lt;code&amp;gt;$07DB&amp;lt;/code&amp;gt;.  Otherwise, leaves the current song the same as the&lt;br /&gt;
previous song.&lt;br /&gt;
&lt;br /&gt;
Proceeds to Load Song.&lt;br /&gt;
&lt;br /&gt;
==== Mute All &amp;lt;code&amp;gt;$9B3B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Clears the current song at &amp;lt;code&amp;gt;$07FB&amp;lt;/code&amp;gt; and sets the APU registers to mute the volume&lt;br /&gt;
on both pulse channels, the triangle channel, and the noise channel.&lt;br /&gt;
&lt;br /&gt;
==== Load Song &amp;lt;code&amp;gt;$9B61&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a song id in register a and preps some variables for that song.&lt;br /&gt;
&lt;br /&gt;
If the song is &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; (item get / level up fanfare) then it first saves the&lt;br /&gt;
previous song to &amp;lt;code&amp;gt;$07FB&amp;lt;/code&amp;gt; if it is less than &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; so that the previous music&lt;br /&gt;
will resume after the fanfare.&lt;br /&gt;
&lt;br /&gt;
Sets the phrase counter at &amp;lt;code&amp;gt;$E3&amp;lt;/code&amp;gt; to 0 and the song offset at &amp;lt;code&amp;gt;$E2&amp;lt;/code&amp;gt; based on the&lt;br /&gt;
song id.  Song ids are a bit field, and the song offset is the position of the&lt;br /&gt;
lowest bit in the song id for indexing into the song table.&lt;br /&gt;
&lt;br /&gt;
Proceeds to Load Song Data.&lt;br /&gt;
&lt;br /&gt;
==== Load Song Data &amp;lt;code&amp;gt;$9B80&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Loads the music variables based on the current world and current song offset.&lt;br /&gt;
&lt;br /&gt;
There are four blocks of code that are basically the same, for each of the four&lt;br /&gt;
different music tables.  Each one just uses a different position for the tables&lt;br /&gt;
to load.&lt;br /&gt;
&lt;br /&gt;
Based on the current world in &amp;lt;code&amp;gt;$0707&amp;lt;/code&amp;gt; it loads data from these offsets:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt;&lt;br /&gt;
: Overworld music - &amp;lt;code&amp;gt;$9B87&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;$01&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt;&lt;br /&gt;
: Town music - &amp;lt;code&amp;gt;$9BC6&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;$03&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$04&amp;lt;/code&amp;gt;&lt;br /&gt;
: Palace music - &amp;lt;code&amp;gt;$9C05&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;$05&amp;lt;/code&amp;gt; or higher&lt;br /&gt;
: Great Palace music - &amp;lt;code&amp;gt;$9C42&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, uses the song offset in &amp;lt;code&amp;gt;$E2&amp;lt;/code&amp;gt;  to index into the song table at the base&lt;br /&gt;
offset given above.&lt;br /&gt;
&lt;br /&gt;
The phrase counter at &amp;lt;code&amp;gt;$E3&amp;lt;/code&amp;gt; is added to that and used as in index again to&lt;br /&gt;
retrieve the current phrase offset.  If the current phrase offset is &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt; that&lt;br /&gt;
means the song is over and the process goes back to the Next Song routine.&lt;br /&gt;
&lt;br /&gt;
Otherwise, the following structure is read at the base offset above + phrase&lt;br /&gt;
offset just calculated.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;$E5&amp;lt;/code&amp;gt; Tempo&lt;br /&gt;
* &amp;lt;code&amp;gt;$E0&amp;lt;/code&amp;gt; Note Data Low&lt;br /&gt;
* &amp;lt;code&amp;gt;$E1&amp;lt;/code&amp;gt; Note Data High&lt;br /&gt;
* &amp;lt;code&amp;gt;$07E9&amp;lt;/code&amp;gt; Bass Note Pointer&lt;br /&gt;
* &amp;lt;code&amp;gt;$07EA&amp;lt;/code&amp;gt; Harmony Note Pointer&lt;br /&gt;
* &amp;lt;code&amp;gt;$07E8&amp;lt;/code&amp;gt; Drum Note Pointer&lt;br /&gt;
&lt;br /&gt;
After this, the drum loop start point at &amp;lt;code&amp;gt;$E4&amp;lt;/code&amp;gt; is also set to the same value as&lt;br /&gt;
the drum note pointer.  The melody note pointer at &amp;lt;code&amp;gt;$07EB&amp;lt;/code&amp;gt; is set to &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt;, and&lt;br /&gt;
all the durations (melody at &amp;lt;code&amp;gt;$07E7&amp;lt;/code&amp;gt;, harmony at &amp;lt;code&amp;gt;$07E6&amp;lt;/code&amp;gt;, bass at &amp;lt;code&amp;gt;$07E5&amp;lt;/code&amp;gt;, and&lt;br /&gt;
drums at &amp;lt;code&amp;gt;$07E4&amp;lt;/code&amp;gt;) are set to &amp;lt;code&amp;gt;$01&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Proceeds to Play Notes.&lt;br /&gt;
&lt;br /&gt;
==== Play Notes ====&lt;br /&gt;
&lt;br /&gt;
This routine is broken into four major and similar sections: one each for&lt;br /&gt;
melody, harmony, bass, and drums.  The general process is as follows:&lt;br /&gt;
&lt;br /&gt;
 decrement duration&lt;br /&gt;
 if duration == 0 {&lt;br /&gt;
   load next note&lt;br /&gt;
   increment note pointer&lt;br /&gt;
   set duration for note&lt;br /&gt;
   if no overriding sound effect {&lt;br /&gt;
     play note on appropriate channel&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Some channels have unique processing at different points in this.  For example,&lt;br /&gt;
the melody note data is null terminated, so if the loaded note in &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt; then the&lt;br /&gt;
routine will jump to Load Song Data instead to go to the next phrase.  If the&lt;br /&gt;
drums channel gets a &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt; note it will reset the drums note pointer to the&lt;br /&gt;
drums loop start and repeat the phrase.  The bass and harmony channels don't&lt;br /&gt;
have this looping behavior.  This means that if the bass or harmony channels&lt;br /&gt;
aren't long enough, the engine will happily just play whatever data comes next&lt;br /&gt;
in the ROM.&lt;br /&gt;
&lt;br /&gt;
The melody is played on pulse 2 channel, the harmony on pulse 1 (which gets&lt;br /&gt;
subverted by sound effects that need just one pulse channel), the bass on the&lt;br /&gt;
triangle channel, and the drums on the noise channel.&lt;br /&gt;
&lt;br /&gt;
For the pulse channels, after the note is played, the envelope counter for that&lt;br /&gt;
channel (&amp;lt;code&amp;gt;$07E2&amp;lt;/code&amp;gt; for pulse 1, &amp;lt;code&amp;gt;$07E3&amp;lt;/code&amp;gt; for pulse 2) is set.  Normally the&lt;br /&gt;
envelope is set to &amp;lt;code&amp;gt;$2F&amp;lt;/code&amp;gt; but song &amp;lt;code&amp;gt;$40&amp;lt;/code&amp;gt; (crystal fanfare and final boss music)&lt;br /&gt;
sets it to &amp;lt;code&amp;gt;$18&amp;lt;/code&amp;gt; instead.  The envelope is not set if no note was played because&lt;br /&gt;
the pitch index was &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt; which indicates a rest.&lt;br /&gt;
&lt;br /&gt;
Also for the pulse channels, after the above pseudocode, the vibrato and&lt;br /&gt;
enveloping happens as long as there is no overriding sound effect.  The envelope&lt;br /&gt;
works by checking the envelope counter set previously and decrementing it.  The&lt;br /&gt;
counter value is halved and used to look up the envelope value in the LUT at&lt;br /&gt;
&amp;lt;code&amp;gt;$9135&amp;lt;/code&amp;gt;.  These values are written directly to APU register &amp;lt;code&amp;gt;$4000&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$4004&amp;lt;/code&amp;gt;&lt;br /&gt;
depending on the channel.&lt;br /&gt;
&lt;br /&gt;
The bass channel has one special handling as well.  The APU register &amp;lt;code&amp;gt;$4008&amp;lt;/code&amp;gt; is&lt;br /&gt;
set differently based on the current song id.  For song &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; (item get / level&lt;br /&gt;
up fanfare) the register is set to &amp;lt;code&amp;gt;$60&amp;lt;/code&amp;gt; but all other songs use &amp;lt;code&amp;gt;$1f&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The noise channel is not affect at all by the pitch.  Any pitch value that is&lt;br /&gt;
non zero will play a burst of noise.&lt;/div&gt;</summary>
		<author><name>DontBaguMe</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.bindingforce.net/index.php?title=Music_Engine_Description&amp;diff=342</id>
		<title>Music Engine Description</title>
		<link rel="alternate" type="text/html" href="http://wiki.bindingforce.net/index.php?title=Music_Engine_Description&amp;diff=342"/>
		<updated>2022-03-30T04:47:01Z</updated>

		<summary type="html">&lt;p&gt;DontBaguMe: /* Title Music Engine */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There are two separate music engines in Zelda 2.  Both are in bank 6 (along&lt;br /&gt;
with the song data).  The music engine for the title screen resides at&lt;br /&gt;
&amp;lt;code&amp;gt;$8000&amp;lt;/code&amp;gt; and the engine for the rest of the game is at&lt;br /&gt;
&amp;lt;code&amp;gt;$9000&amp;lt;/code&amp;gt;.  Each of these engines is called once per frame during the&lt;br /&gt;
appropriate part of the game.&lt;br /&gt;
&lt;br /&gt;
Changes to the music engine should be very easy to make, as there is an&lt;br /&gt;
abundance of free space in bank 6.&lt;br /&gt;
&lt;br /&gt;
== Title Music Engine ==&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' properly format the following by bgt (original at https://eab.xyz/W2zplGD) and dontbagume  (on discord).&lt;br /&gt;
&lt;br /&gt;
# Title Music Engine&lt;br /&gt;
&lt;br /&gt;
The title music engine is different from the engine that plays music&lt;br /&gt;
during the rest of the game.  In particular, it has a wider range of&lt;br /&gt;
notes that can be represented.  If you want to look into the actual&lt;br /&gt;
code, the title screen music engine main loop is at `$8000` (vesus&lt;br /&gt;
`$9000` for the game music loop).&lt;br /&gt;
&lt;br /&gt;
The metadata for the songs is all the same as in the other areas,&lt;br /&gt;
although the title music is actually broken up into several songs&lt;br /&gt;
itself.  These song boundaries control the timing of the title scroll.&lt;br /&gt;
&lt;br /&gt;
The following songs exist in the title song table:&lt;br /&gt;
&lt;br /&gt;
  1. Intro&lt;br /&gt;
  2. Start&lt;br /&gt;
  3. Build up&lt;br /&gt;
  4. Main&lt;br /&gt;
  5. Breakdown&lt;br /&gt;
&lt;br /&gt;
The start of song 2 triggers the title to scroll into view.  The start&lt;br /&gt;
of song 4 triggers a countdown until the story scroll begins.  After&lt;br /&gt;
song 5 finishes, the engine loops back to song 2.&lt;br /&gt;
&lt;br /&gt;
Within the note data, a special format is used.  Rather than encoding&lt;br /&gt;
the duration and pitch together in a single byte, the title music has&lt;br /&gt;
&amp;quot;pitch&amp;quot; bytes and &amp;quot;duration&amp;quot; bytes.  Any byte with the highest bit set&lt;br /&gt;
(i.e. anything &amp;gt;= `$80`) is interpreted as a duration change, which&lt;br /&gt;
sets the duration of all future notes.  Anything else is a pitch value&lt;br /&gt;
which indicates a note of the current duration should be played.  The&lt;br /&gt;
low nibble of duration values keys into a lookup table at bank 6 `$8084` &lt;br /&gt;
and store the duration byte at $07FF.  Value durations are as follows:&lt;br /&gt;
&lt;br /&gt;
  * `$80` - 8 ticks   (sixteenth note)&lt;br /&gt;
  * `$81` - 24 ticks  (dotted eighth note)&lt;br /&gt;
  * `$82` - 16 ticks  (eighth note)&lt;br /&gt;
  * `$83` - 32 ticks  (quarter note)&lt;br /&gt;
  * `$84` - 48 ticks  (dotted quarter note)&lt;br /&gt;
  * `$85` - 64 ticks  (half note)&lt;br /&gt;
  * `$86` - 96 ticks  (dotted half note)&lt;br /&gt;
  * `$87` - 128 ticks (whole note)&lt;br /&gt;
  * `$88` - 11 ticks  (eighth note triplet)&lt;br /&gt;
  * `$89` - 10 ticks  (???)&lt;br /&gt;
  * `$8A` - 80 ticks  (half note + eighth note)&lt;br /&gt;
  * `$8B` - 256 ticks (two whole notes)&lt;br /&gt;
  * `$8F` - 6 ticks   (dotted 32nd note)&lt;br /&gt;
&lt;br /&gt;
Note: $8C, $8D and $8E are duplicates, so these could be used for&lt;br /&gt;
custom durations if desired.&lt;br /&gt;
&lt;br /&gt;
The pitch values are much more straightforward.  As far as I can tell,&lt;br /&gt;
`$4C` is A4 and every semitone away from that adds or subtracts 2 from&lt;br /&gt;
the value.  Thus, C5 (three semitones above A4) is `$52`.  The value&lt;br /&gt;
`$02` represents a rest.&lt;br /&gt;
&lt;br /&gt;
         -1-  -2-  -3-  -4-  -5-&lt;br /&gt;
    A  : $04  $1C  $34  $4C  $64&lt;br /&gt;
    Bb : $06  $1E  $36  $4E  $66&lt;br /&gt;
    B  : $08  $20  $38  $50  $68&lt;br /&gt;
    C  : $0A  $22  $3A  $52  $6A&lt;br /&gt;
    C# : $0C  $24  $3C  $54  $6C&lt;br /&gt;
    D  : $0E  $26  $3E  $56  $6E&lt;br /&gt;
    Eb : $10  $28  $40  $58  $70&lt;br /&gt;
    E  : $12  $2A  $42  $5A  $72&lt;br /&gt;
    F  : $14  $2C  $44  $5C  $74&lt;br /&gt;
    F# : $16  $2E  $46  $5E  $76&lt;br /&gt;
    G  : $18  $30  $48  $60  $78&lt;br /&gt;
    G# : $1A  $32  $4A  $62  $7A&lt;br /&gt;
&lt;br /&gt;
By way of an example, let's look at the main section of the vanilla&lt;br /&gt;
title music.  The title metadata starts at bank 6 `$84DA`:&lt;br /&gt;
&lt;br /&gt;
    08 11 14 16 19 1E 1E 1E&lt;br /&gt;
             ^ Main section&lt;br /&gt;
&lt;br /&gt;
The main section is the fourth song, starting at `$84DA + $16 = $84F0`:&lt;br /&gt;
&lt;br /&gt;
    3F 3F 00&lt;br /&gt;
&lt;br /&gt;
This means there is a single phrase for the main section, which is&lt;br /&gt;
repeated twice.  The phrase data is at `$84DA + $3F = $8519`.&lt;br /&gt;
&lt;br /&gt;
    00 3D 86 3A 1A 5F&lt;br /&gt;
&lt;br /&gt;
From this, we see the melody note data is located at `$863D`:&lt;br /&gt;
&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    02 Rest&lt;br /&gt;
    48 Ab4&lt;br /&gt;
    82 Eighth note&lt;br /&gt;
    46 G4&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    3E Eb4&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    84 Dotted quarter notes&lt;br /&gt;
    2E G3&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    30 Ab3&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    3A Db4&lt;br /&gt;
    38 C4&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    30 Ab3&lt;br /&gt;
    82 Eighth notes&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    30 Ab3&lt;br /&gt;
    85 Half notes&lt;br /&gt;
    2E G3&lt;br /&gt;
    82 Quarter notes&lt;br /&gt;
    02 Rest&lt;br /&gt;
    00 End of Data&lt;br /&gt;
&lt;br /&gt;
dontbagume additions:&lt;br /&gt;
&lt;br /&gt;
DURATION&lt;br /&gt;
  * `$80` - 6 ticks    sixteenth note&lt;br /&gt;
  * `$81` - 9 ticks    dotted eighth note&lt;br /&gt;
  * `$82` - 12 ticks    eighth note&lt;br /&gt;
  * `$83` - 24 ticks    quarter note&lt;br /&gt;
  * `$84` - 36 ticks    dotted quarter note&lt;br /&gt;
  * `$85` - 48 ticks    half note&lt;br /&gt;
  * `$86` - 72 ticks    dotted half note&lt;br /&gt;
  * `$87` - 96 ticks    whole note&lt;br /&gt;
  * `$88` - 8 ticks    eighth triplets&lt;br /&gt;
  * `$89` - 7 ticks    poor man's swung 16th (long)&lt;br /&gt;
  * `$8A` - 60 ticks    half + eighth&lt;br /&gt;
  * `$8B` - 192 ticks    two whole notes&lt;br /&gt;
  * `$8F` - 5 ticks    poor man's swung 16th (short)&lt;br /&gt;
&lt;br /&gt;
PITCH&lt;br /&gt;
         -1-    -2-    -3-    -4-    -5-&lt;br /&gt;
  A  :     $04    $1C    $34    $4C    $64&lt;br /&gt;
  Bb :     $06    $1E    $36    $4E    $66&lt;br /&gt;
  B  :     $08    $20    $38    $50    $68&lt;br /&gt;
  C  :     $0A    $22    $3A    $52    $6A&lt;br /&gt;
  C# :     $0C    $24    $3C    $54    $6C&lt;br /&gt;
  D  :     $0E    $26    $3E    $56    $6E&lt;br /&gt;
  Eb :     $10    $28    $40    $58    $70&lt;br /&gt;
  E  :     $12    $2A    $42    $5A    $72&lt;br /&gt;
  F  :     $14    $2C    $44    $5C    $74&lt;br /&gt;
  F# :     $16    $2E    $46    $5E    $76&lt;br /&gt;
  G  :     $18    $30    $48    $60    $78&lt;br /&gt;
  G# :     $1A    $32    $4A    $62    $7A&lt;br /&gt;
&lt;br /&gt;
== Main Music Engine ==&lt;br /&gt;
&lt;br /&gt;
=== Data Tables ===&lt;br /&gt;
&lt;br /&gt;
There are several data tables used by the music engine.&lt;br /&gt;
&lt;br /&gt;
==== Pulse Envelope ====&lt;br /&gt;
&lt;br /&gt;
This table is used to set the volume of the pulse channels over time.  The index&lt;br /&gt;
used starts at a certain value and decrements, so the table is in reverse order.&lt;br /&gt;
The index is halved before use, so each value lasts two frames.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ Pulse Envelope &amp;lt;code&amp;gt;$9135&amp;lt;/code&amp;gt;&lt;br /&gt;
|90||91||91||91||92||92||92||92&lt;br /&gt;
|-&lt;br /&gt;
|93||93||94||94||94||95||95||95&lt;br /&gt;
|-&lt;br /&gt;
|96||96||96||97||97||97||98||98&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Duration LUT ====&lt;br /&gt;
&lt;br /&gt;
This table is used to determine the duration of a note.  The note data is stored&lt;br /&gt;
with 5 bits of pitch information and 3 bits of duration, as such:&lt;br /&gt;
&lt;br /&gt;
 D1 D0 P4 P3 P2 P1 P0 D2&lt;br /&gt;
&lt;br /&gt;
The Get Note Duration routine shifts things around to get the D bits into the&lt;br /&gt;
three least significant bits and masks it before use.  This value is then added&lt;br /&gt;
to the tempo value in &amp;lt;code&amp;gt;$E5&amp;lt;/code&amp;gt; which is always a multiple of 8 to index&lt;br /&gt;
into the following table:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+Duration LUT &amp;lt;code&amp;gt;$914D&amp;lt;/code&amp;gt;&lt;br /&gt;
|'''00'''||04||0C||08||10||18||20||05||06&lt;br /&gt;
|-&lt;br /&gt;
|'''08'''||04||0F||09||12||1B||24||06||06&lt;br /&gt;
|-&lt;br /&gt;
|'''10'''||05||0F||0A||14||1E||28||07||06&lt;br /&gt;
|-&lt;br /&gt;
|'''18'''||06||12||0C||18||24||30||08||10&lt;br /&gt;
|-&lt;br /&gt;
|'''20'''||07||15||0E||1C||2A||38||13||12&lt;br /&gt;
|-&lt;br /&gt;
|'''28'''||07||15||0E||1C||2A||38||XX||XX&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The last two values in each row seem a bit strange, as they are not nice&lt;br /&gt;
multiples of the first entry which is usually thought of as the length of an&lt;br /&gt;
eighth note.  The last row has garbage values in those positions as that is the&lt;br /&gt;
start of another data table, so they are not even listed here.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' Check what values are used in vanilla.&lt;br /&gt;
&lt;br /&gt;
==== Pitch LUT ====&lt;br /&gt;
&lt;br /&gt;
This table is used to detemine the pitch of a note.  The note data is stored as&lt;br /&gt;
described above, and the fact that the least significant bit is masked out is&lt;br /&gt;
advantageous since the pulse channels can use 11 bits of timer data, so each&lt;br /&gt;
entry in this table is actually two bytes wide.&lt;br /&gt;
&lt;br /&gt;
Rather than show the raw bytes here, it's more useful to show the rough note&lt;br /&gt;
that those values represent.  If the raw values are interesting to you, check&lt;br /&gt;
the ROM for them.  It's worth noting that some of the higher notes are several&lt;br /&gt;
cents off from the listed note.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+Pitch LUT &amp;lt;code&amp;gt;$918F&amp;lt;/code&amp;gt;&lt;br /&gt;
|  || 00|| 02|| 04|| 06|| 08|| 0a|| 0c|| 0e&lt;br /&gt;
|-&lt;br /&gt;
|00|| C3||---|| E3|| G3||G#3|| A3||A#3|| B3&lt;br /&gt;
|-&lt;br /&gt;
|10|| C4||C#4|| D4||D#4|| E4|| F4||F#4|| G4&lt;br /&gt;
|-&lt;br /&gt;
|20||G#4|| A4||A#4|| B4|| C5||C#5|| D5||D#5&lt;br /&gt;
|-&lt;br /&gt;
|30|| E5|| F5||F#5|| G5|| A5||A#5|| B5||C#3&lt;br /&gt;
|-&lt;br /&gt;
|40|| D3||D#3|| F3||F#3||G#5|| C6||C#6|| D6&lt;br /&gt;
|-&lt;br /&gt;
|50||D#6|| E6|| F6||F#6|| G6||G#6|| A6||A#6&lt;br /&gt;
|-&lt;br /&gt;
|60|| B6|| C7||C#7|| D7||D#7|| E7|| F7||F#7&lt;br /&gt;
|-&lt;br /&gt;
|70|| G7||G#7|| A7||A#7|| B7|| C8&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Value &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt; represents a rest.&lt;br /&gt;
&lt;br /&gt;
Note that although the table goes through &amp;lt;code&amp;gt;$7A&amp;lt;/code&amp;gt; values above&lt;br /&gt;
&amp;lt;code&amp;gt;$3E&amp;lt;/code&amp;gt; aren't usable due to the 5 bit limit for pitch data in&lt;br /&gt;
the song data.  However, the sound effects routines make use of these&lt;br /&gt;
extra notes.  For example, the sword beam effect alternates between&lt;br /&gt;
playing C7 and F6.&lt;br /&gt;
&lt;br /&gt;
==== Noise Samples ====&lt;br /&gt;
&lt;br /&gt;
Several sound effects use the same routine to load &amp;quot;samples&amp;quot; for the noise&lt;br /&gt;
channel to play.  These values have the noise volume in the high nybble and the&lt;br /&gt;
noise period in the low nybble.&lt;br /&gt;
&lt;br /&gt;
 $90E8 - Sword slash&lt;br /&gt;
 $9123 - Enemy hurt&lt;br /&gt;
 $912C - Crumble block&lt;br /&gt;
&lt;br /&gt;
=== Variables ===&lt;br /&gt;
&lt;br /&gt;
This is just a dump of RAM addresses used by the music engine and a short&lt;br /&gt;
description of what they are.&lt;br /&gt;
&lt;br /&gt;
 $00E0 - Current phrase note data address (low byte)&lt;br /&gt;
 $00E1 - Current phrase note data address (high byte)&lt;br /&gt;
 $00E2 - Song offset, converted from song id to be an index instead of a bit field&lt;br /&gt;
 $00E3 - Current phrase index&lt;br /&gt;
 $00E4 - Drums loop start&lt;br /&gt;
 $00E5 - Tempo&lt;br /&gt;
 $00E6 - Pulse 1 low bits (used for vibrato effect)&lt;br /&gt;
 $00E7 - Pulse 2 low bits (used for vibrato effect)&lt;br /&gt;
 $00E8 - Pitch storage, temporary&lt;br /&gt;
 $00E9 - Play sound effect&lt;br /&gt;
 $00EA - Disable music&lt;br /&gt;
 $00EB - Request new song&lt;br /&gt;
 $00EC - Play sound effect&lt;br /&gt;
 $00ED - Play sound effect&lt;br /&gt;
 $00EE - Play sound effect&lt;br /&gt;
 $00EF - Play sound effect&lt;br /&gt;
&lt;br /&gt;
 $0707 - Current world&lt;br /&gt;
 $075F - Queued song, loaded after screen transitions&lt;br /&gt;
 $07DA - Ganon Laugh sample&lt;br /&gt;
 $07DB - Song to resume after fanfare&lt;br /&gt;
 $07DF - ??? Likely sound effect flag for pulse 1 channel override&lt;br /&gt;
&lt;br /&gt;
 $07E0 - ??? Likely sound effect flag for noise channel&lt;br /&gt;
 $07E2 - Pulse 2 envelope index&lt;br /&gt;
 $07E3 - Pulse 1 envelope index&lt;br /&gt;
 $07E4 - Drums current note duration&lt;br /&gt;
 $07E5 - Bass current note duration&lt;br /&gt;
 $07E6 - Harmony current note duration&lt;br /&gt;
 $07E7 - Melody current note duration&lt;br /&gt;
 $07E8 - Drums next note index&lt;br /&gt;
 $07E9 - Bass next note index&lt;br /&gt;
 $07EA - Harmony next note index&lt;br /&gt;
 $07EB - Melody next note index&lt;br /&gt;
&lt;br /&gt;
 $07EC - Ganon Laugh counter&lt;br /&gt;
 $07ED - Sound FX counter (E9)&lt;br /&gt;
 $07EE - Sound FX counter (E9)&lt;br /&gt;
 $07F5 - Sound FX counter (ED)&lt;br /&gt;
&lt;br /&gt;
 $07FA - Current SFX (E9)&lt;br /&gt;
 $07FB - Current song&lt;br /&gt;
 $07FD - Current SFX (ED)&lt;br /&gt;
 $07FE - ??? Likely sound effect flag for pulse 2 channel override&lt;br /&gt;
 $07FF - Current SFX (EF)&lt;br /&gt;
&lt;br /&gt;
=== Routines ===&lt;br /&gt;
&lt;br /&gt;
This is a non-exhaustive list of the routines in bank 6 involving the playing of&lt;br /&gt;
music and sound effects.  Each routine also lists its address so you can search&lt;br /&gt;
in a disassembly for the actual code for that routine if these descriptions&lt;br /&gt;
aren't clear.&lt;br /&gt;
&lt;br /&gt;
==== Main Music Loop &amp;lt;code&amp;gt;$9000&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Checks that the music disable flag is off, then runs several SFX routines.&lt;br /&gt;
After the SFX routines, the main music routine runs.  After all this, various&lt;br /&gt;
music related memory locations are cleared.&lt;br /&gt;
&lt;br /&gt;
==== Configure Pulse1 Channel &amp;lt;code&amp;gt;$9031&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes registers x and y and configures the APU registers &amp;lt;code&amp;gt;$4000&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$4001&amp;lt;/code&amp;gt;&lt;br /&gt;
respectively.&lt;br /&gt;
&lt;br /&gt;
==== Configure Pulse2 Channel &amp;lt;code&amp;gt;$9038&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes registers x and y and configures the APU registers &amp;lt;code&amp;gt;$4004&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$4005&amp;lt;/code&amp;gt;&lt;br /&gt;
respectively.&lt;br /&gt;
&lt;br /&gt;
==== Play Note Pulse1 &amp;lt;code&amp;gt;$9042&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a pitch index in register a and plays the note on the pulse 1 channel.&lt;br /&gt;
&lt;br /&gt;
==== Play Note &amp;lt;code&amp;gt;$9044&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Called by the various play note routines.  Takes a pitch index in register a and&lt;br /&gt;
uses register x as a channel selector.  For the pulse channels, this routine&lt;br /&gt;
also saves the lower 8 bits of the APU timer register to &amp;lt;code&amp;gt;$E6&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$E7&amp;lt;/code&amp;gt; which&lt;br /&gt;
are used for a vibrato effect.&lt;br /&gt;
&lt;br /&gt;
==== Play Note Pulse2 &amp;lt;code&amp;gt;$9067&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a pitch index in register a and plays the note on the pulse 2 channel.&lt;br /&gt;
&lt;br /&gt;
==== Play Note Triangle &amp;lt;code&amp;gt;$906B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a pitch index in register a and plays the note on the triangle channel.&lt;br /&gt;
&lt;br /&gt;
==== Get Note Duration &amp;lt;code&amp;gt;$906F&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes note data in register a and looks up the duration.   The original note&lt;br /&gt;
data is saved in register x and the result of the duration lookup is put in&lt;br /&gt;
register a.  The duration lookup is offset by the tempo stored in &amp;lt;code&amp;gt;$E5&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Vibrato Pulse1 &amp;lt;code&amp;gt;$9089&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Applies a vibrato effect on the pulse 1 channel.  Takes the duration of the note&lt;br /&gt;
left in register a and the APU timer low bits in register y.&lt;br /&gt;
&lt;br /&gt;
==== Vibrato Pulse2 &amp;lt;code&amp;gt;$9097&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Applies a vibrato effect on the pulse 2 channel.  Takes the duration of the note&lt;br /&gt;
left in register a and the APU timer low bits in register y.&lt;br /&gt;
&lt;br /&gt;
==== Vibrato &amp;lt;code&amp;gt;$909E&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes the duration of the note remaining in register a, the low bits of the APU&lt;br /&gt;
timer in register y, and the channel to use in register x.  This is called by&lt;br /&gt;
the other vibrato routines after setting x appropriately.&lt;br /&gt;
&lt;br /&gt;
Uses the &amp;lt;code&amp;gt;$04&amp;lt;/code&amp;gt; bit of the duration to either add or subtract 2 from the low&lt;br /&gt;
timer bits.  Only the low bits are used because writing to the high bits resets&lt;br /&gt;
the phase of the channel which can cause clicks.  None of the notes from the&lt;br /&gt;
pitch LUT are close enough to the boundary for the change to require the high&lt;br /&gt;
bits to change anyway, so this works out fine.&lt;br /&gt;
&lt;br /&gt;
==== Play E9 SFX &amp;lt;code&amp;gt;$920B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$E9&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play EF SFX &amp;lt;code&amp;gt;$92F4&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$EF&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play EE SFX &amp;lt;code&amp;gt;$9408&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$EE&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play Noise &amp;lt;code&amp;gt;$959D&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Configures the APU noise registers to make a noise burst.  Takes configuration&lt;br /&gt;
for registers &amp;lt;code&amp;gt;$400F&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;$400E&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;$400C&amp;lt;/code&amp;gt; in registers y, x, and a.&lt;br /&gt;
&lt;br /&gt;
==== Crumble Bridge SFX &amp;lt;code&amp;gt;$956C&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Starts playing the sound effect for a crumble bridge crumbling.  This uses both&lt;br /&gt;
the noise and the triangle channels so it sets flags to disable the music on&lt;br /&gt;
those channels.  Continues with SFX Noise Decay.&lt;br /&gt;
&lt;br /&gt;
==== SFX Noise Decay &amp;lt;code&amp;gt;$9587&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Counts down the timer in &amp;lt;code&amp;gt;$07F5&amp;lt;/code&amp;gt; and turns off the noise when it reaches 0.&lt;br /&gt;
Also clears the flag for the ED sound effect in &amp;lt;code&amp;gt;$07FD&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Play ED SFX &amp;lt;code&amp;gt;$95A7&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect request in &amp;lt;code&amp;gt;$ED&amp;lt;/code&amp;gt; or continues the sound effect saved in&lt;br /&gt;
&amp;lt;code&amp;gt;$07FD&amp;lt;/code&amp;gt;.  Existing sound effects which are higher than the current sound effect&lt;br /&gt;
will be ignored.  If the same sound effect is requested that is already playing,&lt;br /&gt;
it will restart the sound effect except for &amp;lt;code&amp;gt;$80&amp;lt;/code&amp;gt; which gives priority to&lt;br /&gt;
continuing.  When the sound is continued instead of newly played, the decay&lt;br /&gt;
routine will be called instead.&lt;br /&gt;
&lt;br /&gt;
; &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt;&lt;br /&gt;
: Crumble Bridge&lt;br /&gt;
; &amp;lt;code&amp;gt;$04&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt;&lt;br /&gt;
: Crumble Block / Enemy Hurt&lt;br /&gt;
; &amp;lt;code&amp;gt;$20&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$80&amp;lt;/code&amp;gt;&lt;br /&gt;
: Sword Strike&lt;br /&gt;
; &amp;lt;code&amp;gt;$40&amp;lt;/code&amp;gt;&lt;br /&gt;
: Very short noise burst, probably unused.  Doesn't store the effect, and doesn't have an associated decay.&lt;br /&gt;
&lt;br /&gt;
==== Crumble Block / Enemy Hurt SFX &amp;lt;code&amp;gt;$95E6&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Starts playing the sound of a block breaking or an enemy getting hurt.&lt;br /&gt;
&lt;br /&gt;
==== Crumble Block / Enemy Hurt SFX Decay &amp;lt;code&amp;gt;$95EE&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Looks up noise register values based on the timer and the sound effect value.&lt;br /&gt;
Crumble Block samples are at &amp;lt;code&amp;gt;$912C&amp;lt;/code&amp;gt; while Enemy Hurt is at &amp;lt;code&amp;gt;$9123&amp;lt;/code&amp;gt;.  Continues&lt;br /&gt;
on to Play Noise Samples.&lt;br /&gt;
&lt;br /&gt;
==== Play Noise Samples &amp;lt;code&amp;gt;$9612&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a sample value in register a.  The low four bits of this are used to set&lt;br /&gt;
the noise period via APU register &amp;lt;code&amp;gt;$400E&amp;lt;/code&amp;gt;.  The high four bits are used to set&lt;br /&gt;
the noise volume via APU register &amp;lt;code&amp;gt;$400C&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Continues on the SFX Noise Decay process.&lt;br /&gt;
&lt;br /&gt;
==== Sword Strike SFX &amp;lt;code&amp;gt;$9604&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Starts playing the sound of swinging your sword.  This looks up samples based on&lt;br /&gt;
the duration left at &amp;lt;code&amp;gt;$90E8&amp;lt;/code&amp;gt; and plays them via Play Noise Samples.&lt;br /&gt;
&lt;br /&gt;
==== Play EC SFX &amp;lt;code&amp;gt;$990B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$EC&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play Music &amp;lt;code&amp;gt;$9B18&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the music for the game.&lt;br /&gt;
&lt;br /&gt;
Runs Load Song if a new song is requested in &amp;lt;code&amp;gt;$EB&amp;lt;/code&amp;gt;.  Otherwise, runs Play Notes&lt;br /&gt;
if the current song is set in &amp;lt;code&amp;gt;$07FB&amp;lt;/code&amp;gt;.  If neither of these is true, do nothing.&lt;br /&gt;
&lt;br /&gt;
==== Next Song &amp;lt;code&amp;gt;$9B24&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Checks if the previous song should loop.  If not, calls Mute All and returns.&lt;br /&gt;
&lt;br /&gt;
If the previous song was &amp;lt;code&amp;gt;$01&amp;lt;/code&amp;gt; (the &amp;quot;intro&amp;quot; to the main theme), sets the next&lt;br /&gt;
song to &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt; (the main theme loop).&lt;br /&gt;
&lt;br /&gt;
If the previous song was &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; (the item get / level up fanfare), sets the&lt;br /&gt;
current song from &amp;lt;code&amp;gt;$07DB&amp;lt;/code&amp;gt;.  Otherwise, leaves the current song the same as the&lt;br /&gt;
previous song.&lt;br /&gt;
&lt;br /&gt;
Proceeds to Load Song.&lt;br /&gt;
&lt;br /&gt;
==== Mute All &amp;lt;code&amp;gt;$9B3B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Clears the current song at &amp;lt;code&amp;gt;$07FB&amp;lt;/code&amp;gt; and sets the APU registers to mute the volume&lt;br /&gt;
on both pulse channels, the triangle channel, and the noise channel.&lt;br /&gt;
&lt;br /&gt;
==== Load Song &amp;lt;code&amp;gt;$9B61&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a song id in register a and preps some variables for that song.&lt;br /&gt;
&lt;br /&gt;
If the song is &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; (item get / level up fanfare) then it first saves the&lt;br /&gt;
previous song to &amp;lt;code&amp;gt;$07FB&amp;lt;/code&amp;gt; if it is less than &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; so that the previous music&lt;br /&gt;
will resume after the fanfare.&lt;br /&gt;
&lt;br /&gt;
Sets the phrase counter at &amp;lt;code&amp;gt;$E3&amp;lt;/code&amp;gt; to 0 and the song offset at &amp;lt;code&amp;gt;$E2&amp;lt;/code&amp;gt; based on the&lt;br /&gt;
song id.  Song ids are a bit field, and the song offset is the position of the&lt;br /&gt;
lowest bit in the song id for indexing into the song table.&lt;br /&gt;
&lt;br /&gt;
Proceeds to Load Song Data.&lt;br /&gt;
&lt;br /&gt;
==== Load Song Data &amp;lt;code&amp;gt;$9B80&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Loads the music variables based on the current world and current song offset.&lt;br /&gt;
&lt;br /&gt;
There are four blocks of code that are basically the same, for each of the four&lt;br /&gt;
different music tables.  Each one just uses a different position for the tables&lt;br /&gt;
to load.&lt;br /&gt;
&lt;br /&gt;
Based on the current world in &amp;lt;code&amp;gt;$0707&amp;lt;/code&amp;gt; it loads data from these offsets:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt;&lt;br /&gt;
: Overworld music - &amp;lt;code&amp;gt;$9B87&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;$01&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt;&lt;br /&gt;
: Town music - &amp;lt;code&amp;gt;$9BC6&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;$03&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$04&amp;lt;/code&amp;gt;&lt;br /&gt;
: Palace music - &amp;lt;code&amp;gt;$9C05&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;$05&amp;lt;/code&amp;gt; or higher&lt;br /&gt;
: Great Palace music - &amp;lt;code&amp;gt;$9C42&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, uses the song offset in &amp;lt;code&amp;gt;$E2&amp;lt;/code&amp;gt;  to index into the song table at the base&lt;br /&gt;
offset given above.&lt;br /&gt;
&lt;br /&gt;
The phrase counter at &amp;lt;code&amp;gt;$E3&amp;lt;/code&amp;gt; is added to that and used as in index again to&lt;br /&gt;
retrieve the current phrase offset.  If the current phrase offset is &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt; that&lt;br /&gt;
means the song is over and the process goes back to the Next Song routine.&lt;br /&gt;
&lt;br /&gt;
Otherwise, the following structure is read at the base offset above + phrase&lt;br /&gt;
offset just calculated.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;$E5&amp;lt;/code&amp;gt; Tempo&lt;br /&gt;
* &amp;lt;code&amp;gt;$E0&amp;lt;/code&amp;gt; Note Data Low&lt;br /&gt;
* &amp;lt;code&amp;gt;$E1&amp;lt;/code&amp;gt; Note Data High&lt;br /&gt;
* &amp;lt;code&amp;gt;$07E9&amp;lt;/code&amp;gt; Bass Note Pointer&lt;br /&gt;
* &amp;lt;code&amp;gt;$07EA&amp;lt;/code&amp;gt; Harmony Note Pointer&lt;br /&gt;
* &amp;lt;code&amp;gt;$07E8&amp;lt;/code&amp;gt; Drum Note Pointer&lt;br /&gt;
&lt;br /&gt;
After this, the drum loop start point at &amp;lt;code&amp;gt;$E4&amp;lt;/code&amp;gt; is also set to the same value as&lt;br /&gt;
the drum note pointer.  The melody note pointer at &amp;lt;code&amp;gt;$07EB&amp;lt;/code&amp;gt; is set to &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt;, and&lt;br /&gt;
all the durations (melody at &amp;lt;code&amp;gt;$07E7&amp;lt;/code&amp;gt;, harmony at &amp;lt;code&amp;gt;$07E6&amp;lt;/code&amp;gt;, bass at &amp;lt;code&amp;gt;$07E5&amp;lt;/code&amp;gt;, and&lt;br /&gt;
drums at &amp;lt;code&amp;gt;$07E4&amp;lt;/code&amp;gt;) are set to &amp;lt;code&amp;gt;$01&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Proceeds to Play Notes.&lt;br /&gt;
&lt;br /&gt;
==== Play Notes ====&lt;br /&gt;
&lt;br /&gt;
This routine is broken into four major and similar sections: one each for&lt;br /&gt;
melody, harmony, bass, and drums.  The general process is as follows:&lt;br /&gt;
&lt;br /&gt;
 decrement duration&lt;br /&gt;
 if duration == 0 {&lt;br /&gt;
   load next note&lt;br /&gt;
   increment note pointer&lt;br /&gt;
   set duration for note&lt;br /&gt;
   if no overriding sound effect {&lt;br /&gt;
     play note on appropriate channel&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Some channels have unique processing at different points in this.  For example,&lt;br /&gt;
the melody note data is null terminated, so if the loaded note in &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt; then the&lt;br /&gt;
routine will jump to Load Song Data instead to go to the next phrase.  If the&lt;br /&gt;
drums channel gets a &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt; note it will reset the drums note pointer to the&lt;br /&gt;
drums loop start and repeat the phrase.  The bass and harmony channels don't&lt;br /&gt;
have this looping behavior.  This means that if the bass or harmony channels&lt;br /&gt;
aren't long enough, the engine will happily just play whatever data comes next&lt;br /&gt;
in the ROM.&lt;br /&gt;
&lt;br /&gt;
The melody is played on pulse 2 channel, the harmony on pulse 1 (which gets&lt;br /&gt;
subverted by sound effects that need just one pulse channel), the bass on the&lt;br /&gt;
triangle channel, and the drums on the noise channel.&lt;br /&gt;
&lt;br /&gt;
For the pulse channels, after the note is played, the envelope counter for that&lt;br /&gt;
channel (&amp;lt;code&amp;gt;$07E2&amp;lt;/code&amp;gt; for pulse 1, &amp;lt;code&amp;gt;$07E3&amp;lt;/code&amp;gt; for pulse 2) is set.  Normally the&lt;br /&gt;
envelope is set to &amp;lt;code&amp;gt;$2F&amp;lt;/code&amp;gt; but song &amp;lt;code&amp;gt;$40&amp;lt;/code&amp;gt; (crystal fanfare and final boss music)&lt;br /&gt;
sets it to &amp;lt;code&amp;gt;$18&amp;lt;/code&amp;gt; instead.  The envelope is not set if no note was played because&lt;br /&gt;
the pitch index was &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt; which indicates a rest.&lt;br /&gt;
&lt;br /&gt;
Also for the pulse channels, after the above pseudocode, the vibrato and&lt;br /&gt;
enveloping happens as long as there is no overriding sound effect.  The envelope&lt;br /&gt;
works by checking the envelope counter set previously and decrementing it.  The&lt;br /&gt;
counter value is halved and used to look up the envelope value in the LUT at&lt;br /&gt;
&amp;lt;code&amp;gt;$9135&amp;lt;/code&amp;gt;.  These values are written directly to APU register &amp;lt;code&amp;gt;$4000&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$4004&amp;lt;/code&amp;gt;&lt;br /&gt;
depending on the channel.&lt;br /&gt;
&lt;br /&gt;
The bass channel has one special handling as well.  The APU register &amp;lt;code&amp;gt;$4008&amp;lt;/code&amp;gt; is&lt;br /&gt;
set differently based on the current song id.  For song &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; (item get / level&lt;br /&gt;
up fanfare) the register is set to &amp;lt;code&amp;gt;$60&amp;lt;/code&amp;gt; but all other songs use &amp;lt;code&amp;gt;$1f&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The noise channel is not affect at all by the pitch.  Any pitch value that is&lt;br /&gt;
non zero will play a burst of noise.&lt;/div&gt;</summary>
		<author><name>DontBaguMe</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.bindingforce.net/index.php?title=Music_Engine_Description&amp;diff=341</id>
		<title>Music Engine Description</title>
		<link rel="alternate" type="text/html" href="http://wiki.bindingforce.net/index.php?title=Music_Engine_Description&amp;diff=341"/>
		<updated>2022-03-30T04:44:59Z</updated>

		<summary type="html">&lt;p&gt;DontBaguMe: /* Title Music Engine */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There are two separate music engines in Zelda 2.  Both are in bank 6 (along&lt;br /&gt;
with the song data).  The music engine for the title screen resides at&lt;br /&gt;
&amp;lt;code&amp;gt;$8000&amp;lt;/code&amp;gt; and the engine for the rest of the game is at&lt;br /&gt;
&amp;lt;code&amp;gt;$9000&amp;lt;/code&amp;gt;.  Each of these engines is called once per frame during the&lt;br /&gt;
appropriate part of the game.&lt;br /&gt;
&lt;br /&gt;
Changes to the music engine should be very easy to make, as there is an&lt;br /&gt;
abundance of free space in bank 6.&lt;br /&gt;
&lt;br /&gt;
== Title Music Engine ==&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' properly format the following by bgt (original at https://eab.xyz/W2zplGD) and dontbagume  (on discord).&lt;br /&gt;
&lt;br /&gt;
# Title Music Engine&lt;br /&gt;
&lt;br /&gt;
The title music engine is different from the engine that plays music&lt;br /&gt;
during the rest of the game.  In particular, it has a wider range of&lt;br /&gt;
notes that can be represented.  If you want to look into the actual&lt;br /&gt;
code, the title screen music engine main loop is at `$8000` (vesus&lt;br /&gt;
`$9000` for the game music loop).&lt;br /&gt;
&lt;br /&gt;
The metadata for the songs is all the same as in the other areas,&lt;br /&gt;
although the title music is actually broken up into several songs&lt;br /&gt;
itself.  These song boundaries control the timing of the title scroll.&lt;br /&gt;
&lt;br /&gt;
The following songs exist in the title song table:&lt;br /&gt;
&lt;br /&gt;
  1. Intro&lt;br /&gt;
  2. Start&lt;br /&gt;
  3. Build up&lt;br /&gt;
  4. Main&lt;br /&gt;
  5. Breakdown&lt;br /&gt;
&lt;br /&gt;
The start of song 2 triggers the title to scroll into view.  The start&lt;br /&gt;
of song 4 triggers a countdown until the story scroll begins.  After&lt;br /&gt;
song 5 finishes, the engine loops back to song 2.&lt;br /&gt;
&lt;br /&gt;
Within the note data, a special format is used.  Rather than encoding&lt;br /&gt;
the duration and pitch together in a single byte, the title music has&lt;br /&gt;
&amp;quot;pitch&amp;quot; bytes and &amp;quot;duration&amp;quot; bytes.  Any byte with the highest bit set&lt;br /&gt;
(i.e. anything &amp;gt;= `$80`) is interpreted as a duration change, which&lt;br /&gt;
sets the duration of all future notes.  Anything else is a pitch value&lt;br /&gt;
which indicates a note of the current duration should be played.  The&lt;br /&gt;
low nibble of duration values keys into a lookup table at bank 6 `$8084` &lt;br /&gt;
and store the duration byte at $07FF.  Value durations are as follows:&lt;br /&gt;
&lt;br /&gt;
  * `$80` - 8 ticks   (sixteenth note)&lt;br /&gt;
  * `$81` - 24 ticks  (dotted eighth note)&lt;br /&gt;
  * `$82` - 16 ticks  (eighth note)&lt;br /&gt;
  * `$83` - 32 ticks  (quarter note)&lt;br /&gt;
  * `$84` - 48 ticks  (dotted quarter note)&lt;br /&gt;
  * `$85` - 64 ticks  (half note)&lt;br /&gt;
  * `$86` - 96 ticks  (dotted half note)&lt;br /&gt;
  * `$87` - 128 ticks (whole note)&lt;br /&gt;
  * `$88` - 11 ticks  (eighth note triplet)&lt;br /&gt;
  * `$89` - 10 ticks  (???)&lt;br /&gt;
  * `$8A` - 80 ticks  (half note + eighth note)&lt;br /&gt;
  * `$8B` - 256 ticks (two whole notes)&lt;br /&gt;
  * `$8F` - 6 ticks   (dotted 32nd note)&lt;br /&gt;
&lt;br /&gt;
The pitch values are much more straightforward.  As far as I can tell,&lt;br /&gt;
`$4C` is A4 and every semitone away from that adds or subtracts 2 from&lt;br /&gt;
the value.  Thus, C5 (three semitones above A4) is `$52`.  The value&lt;br /&gt;
`$02` represents a rest.&lt;br /&gt;
&lt;br /&gt;
         -1-  -2-  -3-  -4-  -5-&lt;br /&gt;
    A  : $04  $1C  $34  $4C  $64&lt;br /&gt;
    Bb : $06  $1E  $36  $4E  $66&lt;br /&gt;
    B  : $08  $20  $38  $50  $68&lt;br /&gt;
    C  : $0A  $22  $3A  $52  $6A&lt;br /&gt;
    C# : $0C  $24  $3C  $54  $6C&lt;br /&gt;
    D  : $0E  $26  $3E  $56  $6E&lt;br /&gt;
    Eb : $10  $28  $40  $58  $70&lt;br /&gt;
    E  : $12  $2A  $42  $5A  $72&lt;br /&gt;
    F  : $14  $2C  $44  $5C  $74&lt;br /&gt;
    F# : $16  $2E  $46  $5E  $76&lt;br /&gt;
    G  : $18  $30  $48  $60  $78&lt;br /&gt;
    G# : $1A  $32  $4A  $62  $7A&lt;br /&gt;
&lt;br /&gt;
By way of an example, let's look at the main section of the vanilla&lt;br /&gt;
title music.  The title metadata starts at bank 6 `$84DA`:&lt;br /&gt;
&lt;br /&gt;
    08 11 14 16 19 1E 1E 1E&lt;br /&gt;
             ^ Main section&lt;br /&gt;
&lt;br /&gt;
The main section is the fourth song, starting at `$84DA + $16 = $84F0`:&lt;br /&gt;
&lt;br /&gt;
    3F 3F 00&lt;br /&gt;
&lt;br /&gt;
This means there is a single phrase for the main section, which is&lt;br /&gt;
repeated twice.  The phrase data is at `$84DA + $3F = $8519`.&lt;br /&gt;
&lt;br /&gt;
    00 3D 86 3A 1A 5F&lt;br /&gt;
&lt;br /&gt;
From this, we see the melody note data is located at `$863D`:&lt;br /&gt;
&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    02 Rest&lt;br /&gt;
    48 Ab4&lt;br /&gt;
    82 Eighth note&lt;br /&gt;
    46 G4&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    3E Eb4&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    84 Dotted quarter notes&lt;br /&gt;
    2E G3&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    30 Ab3&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    3A Db4&lt;br /&gt;
    38 C4&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    30 Ab3&lt;br /&gt;
    82 Eighth notes&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    30 Ab3&lt;br /&gt;
    85 Half notes&lt;br /&gt;
    2E G3&lt;br /&gt;
    82 Quarter notes&lt;br /&gt;
    02 Rest&lt;br /&gt;
    00 End of Data&lt;br /&gt;
&lt;br /&gt;
dontbagume additions:&lt;br /&gt;
&lt;br /&gt;
DURATION&lt;br /&gt;
  * `$80` - 6 ticks    sixteenth note&lt;br /&gt;
  * `$81` - 9 ticks    dotted eighth note&lt;br /&gt;
  * `$82` - 12 ticks    eighth note&lt;br /&gt;
  * `$83` - 24 ticks    quarter note&lt;br /&gt;
  * `$84` - 36 ticks    dotted quarter note&lt;br /&gt;
  * `$85` - 48 ticks    half note&lt;br /&gt;
  * `$86` - 72 ticks    dotted half note&lt;br /&gt;
  * `$87` - 96 ticks    whole note&lt;br /&gt;
  * `$88` - 8 ticks    eighth triplets&lt;br /&gt;
  * `$89` - 7 ticks    poor man's swung 16th (long)&lt;br /&gt;
  * `$8A` - 60 ticks    half + eighth&lt;br /&gt;
  * `$8B` - 192 ticks    two whole notes&lt;br /&gt;
  * `$8F` - 5 ticks    poor man's swung 16th (short)&lt;br /&gt;
&lt;br /&gt;
PITCH&lt;br /&gt;
         -1-    -2-    -3-    -4-    -5-&lt;br /&gt;
  A  :     $04    $1C    $34    $4C    $64&lt;br /&gt;
  Bb :     $06    $1E    $36    $4E    $66&lt;br /&gt;
  B  :     $08    $20    $38    $50    $68&lt;br /&gt;
  C  :     $0A    $22    $3A    $52    $6A&lt;br /&gt;
  C# :     $0C    $24    $3C    $54    $6C&lt;br /&gt;
  D  :     $0E    $26    $3E    $56    $6E&lt;br /&gt;
  Eb :     $10    $28    $40    $58    $70&lt;br /&gt;
  E  :     $12    $2A    $42    $5A    $72&lt;br /&gt;
  F  :     $14    $2C    $44    $5C    $74&lt;br /&gt;
  F# :     $16    $2E    $46    $5E    $76&lt;br /&gt;
  G  :     $18    $30    $48    $60    $78&lt;br /&gt;
  G# :     $1A    $32    $4A    $62    $7A&lt;br /&gt;
&lt;br /&gt;
== Main Music Engine ==&lt;br /&gt;
&lt;br /&gt;
=== Data Tables ===&lt;br /&gt;
&lt;br /&gt;
There are several data tables used by the music engine.&lt;br /&gt;
&lt;br /&gt;
==== Pulse Envelope ====&lt;br /&gt;
&lt;br /&gt;
This table is used to set the volume of the pulse channels over time.  The index&lt;br /&gt;
used starts at a certain value and decrements, so the table is in reverse order.&lt;br /&gt;
The index is halved before use, so each value lasts two frames.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ Pulse Envelope &amp;lt;code&amp;gt;$9135&amp;lt;/code&amp;gt;&lt;br /&gt;
|90||91||91||91||92||92||92||92&lt;br /&gt;
|-&lt;br /&gt;
|93||93||94||94||94||95||95||95&lt;br /&gt;
|-&lt;br /&gt;
|96||96||96||97||97||97||98||98&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Duration LUT ====&lt;br /&gt;
&lt;br /&gt;
This table is used to determine the duration of a note.  The note data is stored&lt;br /&gt;
with 5 bits of pitch information and 3 bits of duration, as such:&lt;br /&gt;
&lt;br /&gt;
 D1 D0 P4 P3 P2 P1 P0 D2&lt;br /&gt;
&lt;br /&gt;
The Get Note Duration routine shifts things around to get the D bits into the&lt;br /&gt;
three least significant bits and masks it before use.  This value is then added&lt;br /&gt;
to the tempo value in &amp;lt;code&amp;gt;$E5&amp;lt;/code&amp;gt; which is always a multiple of 8 to index&lt;br /&gt;
into the following table:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+Duration LUT &amp;lt;code&amp;gt;$914D&amp;lt;/code&amp;gt;&lt;br /&gt;
|'''00'''||04||0C||08||10||18||20||05||06&lt;br /&gt;
|-&lt;br /&gt;
|'''08'''||04||0F||09||12||1B||24||06||06&lt;br /&gt;
|-&lt;br /&gt;
|'''10'''||05||0F||0A||14||1E||28||07||06&lt;br /&gt;
|-&lt;br /&gt;
|'''18'''||06||12||0C||18||24||30||08||10&lt;br /&gt;
|-&lt;br /&gt;
|'''20'''||07||15||0E||1C||2A||38||13||12&lt;br /&gt;
|-&lt;br /&gt;
|'''28'''||07||15||0E||1C||2A||38||XX||XX&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The last two values in each row seem a bit strange, as they are not nice&lt;br /&gt;
multiples of the first entry which is usually thought of as the length of an&lt;br /&gt;
eighth note.  The last row has garbage values in those positions as that is the&lt;br /&gt;
start of another data table, so they are not even listed here.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' Check what values are used in vanilla.&lt;br /&gt;
&lt;br /&gt;
==== Pitch LUT ====&lt;br /&gt;
&lt;br /&gt;
This table is used to detemine the pitch of a note.  The note data is stored as&lt;br /&gt;
described above, and the fact that the least significant bit is masked out is&lt;br /&gt;
advantageous since the pulse channels can use 11 bits of timer data, so each&lt;br /&gt;
entry in this table is actually two bytes wide.&lt;br /&gt;
&lt;br /&gt;
Rather than show the raw bytes here, it's more useful to show the rough note&lt;br /&gt;
that those values represent.  If the raw values are interesting to you, check&lt;br /&gt;
the ROM for them.  It's worth noting that some of the higher notes are several&lt;br /&gt;
cents off from the listed note.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+Pitch LUT &amp;lt;code&amp;gt;$918F&amp;lt;/code&amp;gt;&lt;br /&gt;
|  || 00|| 02|| 04|| 06|| 08|| 0a|| 0c|| 0e&lt;br /&gt;
|-&lt;br /&gt;
|00|| C3||---|| E3|| G3||G#3|| A3||A#3|| B3&lt;br /&gt;
|-&lt;br /&gt;
|10|| C4||C#4|| D4||D#4|| E4|| F4||F#4|| G4&lt;br /&gt;
|-&lt;br /&gt;
|20||G#4|| A4||A#4|| B4|| C5||C#5|| D5||D#5&lt;br /&gt;
|-&lt;br /&gt;
|30|| E5|| F5||F#5|| G5|| A5||A#5|| B5||C#3&lt;br /&gt;
|-&lt;br /&gt;
|40|| D3||D#3|| F3||F#3||G#5|| C6||C#6|| D6&lt;br /&gt;
|-&lt;br /&gt;
|50||D#6|| E6|| F6||F#6|| G6||G#6|| A6||A#6&lt;br /&gt;
|-&lt;br /&gt;
|60|| B6|| C7||C#7|| D7||D#7|| E7|| F7||F#7&lt;br /&gt;
|-&lt;br /&gt;
|70|| G7||G#7|| A7||A#7|| B7|| C8&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Value &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt; represents a rest.&lt;br /&gt;
&lt;br /&gt;
Note that although the table goes through &amp;lt;code&amp;gt;$7A&amp;lt;/code&amp;gt; values above&lt;br /&gt;
&amp;lt;code&amp;gt;$3E&amp;lt;/code&amp;gt; aren't usable due to the 5 bit limit for pitch data in&lt;br /&gt;
the song data.  However, the sound effects routines make use of these&lt;br /&gt;
extra notes.  For example, the sword beam effect alternates between&lt;br /&gt;
playing C7 and F6.&lt;br /&gt;
&lt;br /&gt;
==== Noise Samples ====&lt;br /&gt;
&lt;br /&gt;
Several sound effects use the same routine to load &amp;quot;samples&amp;quot; for the noise&lt;br /&gt;
channel to play.  These values have the noise volume in the high nybble and the&lt;br /&gt;
noise period in the low nybble.&lt;br /&gt;
&lt;br /&gt;
 $90E8 - Sword slash&lt;br /&gt;
 $9123 - Enemy hurt&lt;br /&gt;
 $912C - Crumble block&lt;br /&gt;
&lt;br /&gt;
=== Variables ===&lt;br /&gt;
&lt;br /&gt;
This is just a dump of RAM addresses used by the music engine and a short&lt;br /&gt;
description of what they are.&lt;br /&gt;
&lt;br /&gt;
 $00E0 - Current phrase note data address (low byte)&lt;br /&gt;
 $00E1 - Current phrase note data address (high byte)&lt;br /&gt;
 $00E2 - Song offset, converted from song id to be an index instead of a bit field&lt;br /&gt;
 $00E3 - Current phrase index&lt;br /&gt;
 $00E4 - Drums loop start&lt;br /&gt;
 $00E5 - Tempo&lt;br /&gt;
 $00E6 - Pulse 1 low bits (used for vibrato effect)&lt;br /&gt;
 $00E7 - Pulse 2 low bits (used for vibrato effect)&lt;br /&gt;
 $00E8 - Pitch storage, temporary&lt;br /&gt;
 $00E9 - Play sound effect&lt;br /&gt;
 $00EA - Disable music&lt;br /&gt;
 $00EB - Request new song&lt;br /&gt;
 $00EC - Play sound effect&lt;br /&gt;
 $00ED - Play sound effect&lt;br /&gt;
 $00EE - Play sound effect&lt;br /&gt;
 $00EF - Play sound effect&lt;br /&gt;
&lt;br /&gt;
 $0707 - Current world&lt;br /&gt;
 $075F - Queued song, loaded after screen transitions&lt;br /&gt;
 $07DA - Ganon Laugh sample&lt;br /&gt;
 $07DB - Song to resume after fanfare&lt;br /&gt;
 $07DF - ??? Likely sound effect flag for pulse 1 channel override&lt;br /&gt;
&lt;br /&gt;
 $07E0 - ??? Likely sound effect flag for noise channel&lt;br /&gt;
 $07E2 - Pulse 2 envelope index&lt;br /&gt;
 $07E3 - Pulse 1 envelope index&lt;br /&gt;
 $07E4 - Drums current note duration&lt;br /&gt;
 $07E5 - Bass current note duration&lt;br /&gt;
 $07E6 - Harmony current note duration&lt;br /&gt;
 $07E7 - Melody current note duration&lt;br /&gt;
 $07E8 - Drums next note index&lt;br /&gt;
 $07E9 - Bass next note index&lt;br /&gt;
 $07EA - Harmony next note index&lt;br /&gt;
 $07EB - Melody next note index&lt;br /&gt;
&lt;br /&gt;
 $07EC - Ganon Laugh counter&lt;br /&gt;
 $07ED - Sound FX counter (E9)&lt;br /&gt;
 $07EE - Sound FX counter (E9)&lt;br /&gt;
 $07F5 - Sound FX counter (ED)&lt;br /&gt;
&lt;br /&gt;
 $07FA - Current SFX (E9)&lt;br /&gt;
 $07FB - Current song&lt;br /&gt;
 $07FD - Current SFX (ED)&lt;br /&gt;
 $07FE - ??? Likely sound effect flag for pulse 2 channel override&lt;br /&gt;
 $07FF - Current SFX (EF)&lt;br /&gt;
&lt;br /&gt;
=== Routines ===&lt;br /&gt;
&lt;br /&gt;
This is a non-exhaustive list of the routines in bank 6 involving the playing of&lt;br /&gt;
music and sound effects.  Each routine also lists its address so you can search&lt;br /&gt;
in a disassembly for the actual code for that routine if these descriptions&lt;br /&gt;
aren't clear.&lt;br /&gt;
&lt;br /&gt;
==== Main Music Loop &amp;lt;code&amp;gt;$9000&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Checks that the music disable flag is off, then runs several SFX routines.&lt;br /&gt;
After the SFX routines, the main music routine runs.  After all this, various&lt;br /&gt;
music related memory locations are cleared.&lt;br /&gt;
&lt;br /&gt;
==== Configure Pulse1 Channel &amp;lt;code&amp;gt;$9031&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes registers x and y and configures the APU registers &amp;lt;code&amp;gt;$4000&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$4001&amp;lt;/code&amp;gt;&lt;br /&gt;
respectively.&lt;br /&gt;
&lt;br /&gt;
==== Configure Pulse2 Channel &amp;lt;code&amp;gt;$9038&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes registers x and y and configures the APU registers &amp;lt;code&amp;gt;$4004&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$4005&amp;lt;/code&amp;gt;&lt;br /&gt;
respectively.&lt;br /&gt;
&lt;br /&gt;
==== Play Note Pulse1 &amp;lt;code&amp;gt;$9042&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a pitch index in register a and plays the note on the pulse 1 channel.&lt;br /&gt;
&lt;br /&gt;
==== Play Note &amp;lt;code&amp;gt;$9044&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Called by the various play note routines.  Takes a pitch index in register a and&lt;br /&gt;
uses register x as a channel selector.  For the pulse channels, this routine&lt;br /&gt;
also saves the lower 8 bits of the APU timer register to &amp;lt;code&amp;gt;$E6&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$E7&amp;lt;/code&amp;gt; which&lt;br /&gt;
are used for a vibrato effect.&lt;br /&gt;
&lt;br /&gt;
==== Play Note Pulse2 &amp;lt;code&amp;gt;$9067&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a pitch index in register a and plays the note on the pulse 2 channel.&lt;br /&gt;
&lt;br /&gt;
==== Play Note Triangle &amp;lt;code&amp;gt;$906B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a pitch index in register a and plays the note on the triangle channel.&lt;br /&gt;
&lt;br /&gt;
==== Get Note Duration &amp;lt;code&amp;gt;$906F&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes note data in register a and looks up the duration.   The original note&lt;br /&gt;
data is saved in register x and the result of the duration lookup is put in&lt;br /&gt;
register a.  The duration lookup is offset by the tempo stored in &amp;lt;code&amp;gt;$E5&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Vibrato Pulse1 &amp;lt;code&amp;gt;$9089&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Applies a vibrato effect on the pulse 1 channel.  Takes the duration of the note&lt;br /&gt;
left in register a and the APU timer low bits in register y.&lt;br /&gt;
&lt;br /&gt;
==== Vibrato Pulse2 &amp;lt;code&amp;gt;$9097&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Applies a vibrato effect on the pulse 2 channel.  Takes the duration of the note&lt;br /&gt;
left in register a and the APU timer low bits in register y.&lt;br /&gt;
&lt;br /&gt;
==== Vibrato &amp;lt;code&amp;gt;$909E&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes the duration of the note remaining in register a, the low bits of the APU&lt;br /&gt;
timer in register y, and the channel to use in register x.  This is called by&lt;br /&gt;
the other vibrato routines after setting x appropriately.&lt;br /&gt;
&lt;br /&gt;
Uses the &amp;lt;code&amp;gt;$04&amp;lt;/code&amp;gt; bit of the duration to either add or subtract 2 from the low&lt;br /&gt;
timer bits.  Only the low bits are used because writing to the high bits resets&lt;br /&gt;
the phase of the channel which can cause clicks.  None of the notes from the&lt;br /&gt;
pitch LUT are close enough to the boundary for the change to require the high&lt;br /&gt;
bits to change anyway, so this works out fine.&lt;br /&gt;
&lt;br /&gt;
==== Play E9 SFX &amp;lt;code&amp;gt;$920B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$E9&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play EF SFX &amp;lt;code&amp;gt;$92F4&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$EF&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play EE SFX &amp;lt;code&amp;gt;$9408&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$EE&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play Noise &amp;lt;code&amp;gt;$959D&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Configures the APU noise registers to make a noise burst.  Takes configuration&lt;br /&gt;
for registers &amp;lt;code&amp;gt;$400F&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;$400E&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;$400C&amp;lt;/code&amp;gt; in registers y, x, and a.&lt;br /&gt;
&lt;br /&gt;
==== Crumble Bridge SFX &amp;lt;code&amp;gt;$956C&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Starts playing the sound effect for a crumble bridge crumbling.  This uses both&lt;br /&gt;
the noise and the triangle channels so it sets flags to disable the music on&lt;br /&gt;
those channels.  Continues with SFX Noise Decay.&lt;br /&gt;
&lt;br /&gt;
==== SFX Noise Decay &amp;lt;code&amp;gt;$9587&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Counts down the timer in &amp;lt;code&amp;gt;$07F5&amp;lt;/code&amp;gt; and turns off the noise when it reaches 0.&lt;br /&gt;
Also clears the flag for the ED sound effect in &amp;lt;code&amp;gt;$07FD&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Play ED SFX &amp;lt;code&amp;gt;$95A7&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect request in &amp;lt;code&amp;gt;$ED&amp;lt;/code&amp;gt; or continues the sound effect saved in&lt;br /&gt;
&amp;lt;code&amp;gt;$07FD&amp;lt;/code&amp;gt;.  Existing sound effects which are higher than the current sound effect&lt;br /&gt;
will be ignored.  If the same sound effect is requested that is already playing,&lt;br /&gt;
it will restart the sound effect except for &amp;lt;code&amp;gt;$80&amp;lt;/code&amp;gt; which gives priority to&lt;br /&gt;
continuing.  When the sound is continued instead of newly played, the decay&lt;br /&gt;
routine will be called instead.&lt;br /&gt;
&lt;br /&gt;
; &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt;&lt;br /&gt;
: Crumble Bridge&lt;br /&gt;
; &amp;lt;code&amp;gt;$04&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt;&lt;br /&gt;
: Crumble Block / Enemy Hurt&lt;br /&gt;
; &amp;lt;code&amp;gt;$20&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$80&amp;lt;/code&amp;gt;&lt;br /&gt;
: Sword Strike&lt;br /&gt;
; &amp;lt;code&amp;gt;$40&amp;lt;/code&amp;gt;&lt;br /&gt;
: Very short noise burst, probably unused.  Doesn't store the effect, and doesn't have an associated decay.&lt;br /&gt;
&lt;br /&gt;
==== Crumble Block / Enemy Hurt SFX &amp;lt;code&amp;gt;$95E6&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Starts playing the sound of a block breaking or an enemy getting hurt.&lt;br /&gt;
&lt;br /&gt;
==== Crumble Block / Enemy Hurt SFX Decay &amp;lt;code&amp;gt;$95EE&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Looks up noise register values based on the timer and the sound effect value.&lt;br /&gt;
Crumble Block samples are at &amp;lt;code&amp;gt;$912C&amp;lt;/code&amp;gt; while Enemy Hurt is at &amp;lt;code&amp;gt;$9123&amp;lt;/code&amp;gt;.  Continues&lt;br /&gt;
on to Play Noise Samples.&lt;br /&gt;
&lt;br /&gt;
==== Play Noise Samples &amp;lt;code&amp;gt;$9612&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a sample value in register a.  The low four bits of this are used to set&lt;br /&gt;
the noise period via APU register &amp;lt;code&amp;gt;$400E&amp;lt;/code&amp;gt;.  The high four bits are used to set&lt;br /&gt;
the noise volume via APU register &amp;lt;code&amp;gt;$400C&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Continues on the SFX Noise Decay process.&lt;br /&gt;
&lt;br /&gt;
==== Sword Strike SFX &amp;lt;code&amp;gt;$9604&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Starts playing the sound of swinging your sword.  This looks up samples based on&lt;br /&gt;
the duration left at &amp;lt;code&amp;gt;$90E8&amp;lt;/code&amp;gt; and plays them via Play Noise Samples.&lt;br /&gt;
&lt;br /&gt;
==== Play EC SFX &amp;lt;code&amp;gt;$990B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$EC&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play Music &amp;lt;code&amp;gt;$9B18&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the music for the game.&lt;br /&gt;
&lt;br /&gt;
Runs Load Song if a new song is requested in &amp;lt;code&amp;gt;$EB&amp;lt;/code&amp;gt;.  Otherwise, runs Play Notes&lt;br /&gt;
if the current song is set in &amp;lt;code&amp;gt;$07FB&amp;lt;/code&amp;gt;.  If neither of these is true, do nothing.&lt;br /&gt;
&lt;br /&gt;
==== Next Song &amp;lt;code&amp;gt;$9B24&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Checks if the previous song should loop.  If not, calls Mute All and returns.&lt;br /&gt;
&lt;br /&gt;
If the previous song was &amp;lt;code&amp;gt;$01&amp;lt;/code&amp;gt; (the &amp;quot;intro&amp;quot; to the main theme), sets the next&lt;br /&gt;
song to &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt; (the main theme loop).&lt;br /&gt;
&lt;br /&gt;
If the previous song was &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; (the item get / level up fanfare), sets the&lt;br /&gt;
current song from &amp;lt;code&amp;gt;$07DB&amp;lt;/code&amp;gt;.  Otherwise, leaves the current song the same as the&lt;br /&gt;
previous song.&lt;br /&gt;
&lt;br /&gt;
Proceeds to Load Song.&lt;br /&gt;
&lt;br /&gt;
==== Mute All &amp;lt;code&amp;gt;$9B3B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Clears the current song at &amp;lt;code&amp;gt;$07FB&amp;lt;/code&amp;gt; and sets the APU registers to mute the volume&lt;br /&gt;
on both pulse channels, the triangle channel, and the noise channel.&lt;br /&gt;
&lt;br /&gt;
==== Load Song &amp;lt;code&amp;gt;$9B61&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a song id in register a and preps some variables for that song.&lt;br /&gt;
&lt;br /&gt;
If the song is &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; (item get / level up fanfare) then it first saves the&lt;br /&gt;
previous song to &amp;lt;code&amp;gt;$07FB&amp;lt;/code&amp;gt; if it is less than &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; so that the previous music&lt;br /&gt;
will resume after the fanfare.&lt;br /&gt;
&lt;br /&gt;
Sets the phrase counter at &amp;lt;code&amp;gt;$E3&amp;lt;/code&amp;gt; to 0 and the song offset at &amp;lt;code&amp;gt;$E2&amp;lt;/code&amp;gt; based on the&lt;br /&gt;
song id.  Song ids are a bit field, and the song offset is the position of the&lt;br /&gt;
lowest bit in the song id for indexing into the song table.&lt;br /&gt;
&lt;br /&gt;
Proceeds to Load Song Data.&lt;br /&gt;
&lt;br /&gt;
==== Load Song Data &amp;lt;code&amp;gt;$9B80&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Loads the music variables based on the current world and current song offset.&lt;br /&gt;
&lt;br /&gt;
There are four blocks of code that are basically the same, for each of the four&lt;br /&gt;
different music tables.  Each one just uses a different position for the tables&lt;br /&gt;
to load.&lt;br /&gt;
&lt;br /&gt;
Based on the current world in &amp;lt;code&amp;gt;$0707&amp;lt;/code&amp;gt; it loads data from these offsets:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt;&lt;br /&gt;
: Overworld music - &amp;lt;code&amp;gt;$9B87&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;$01&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt;&lt;br /&gt;
: Town music - &amp;lt;code&amp;gt;$9BC6&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;$03&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$04&amp;lt;/code&amp;gt;&lt;br /&gt;
: Palace music - &amp;lt;code&amp;gt;$9C05&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;$05&amp;lt;/code&amp;gt; or higher&lt;br /&gt;
: Great Palace music - &amp;lt;code&amp;gt;$9C42&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, uses the song offset in &amp;lt;code&amp;gt;$E2&amp;lt;/code&amp;gt;  to index into the song table at the base&lt;br /&gt;
offset given above.&lt;br /&gt;
&lt;br /&gt;
The phrase counter at &amp;lt;code&amp;gt;$E3&amp;lt;/code&amp;gt; is added to that and used as in index again to&lt;br /&gt;
retrieve the current phrase offset.  If the current phrase offset is &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt; that&lt;br /&gt;
means the song is over and the process goes back to the Next Song routine.&lt;br /&gt;
&lt;br /&gt;
Otherwise, the following structure is read at the base offset above + phrase&lt;br /&gt;
offset just calculated.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;$E5&amp;lt;/code&amp;gt; Tempo&lt;br /&gt;
* &amp;lt;code&amp;gt;$E0&amp;lt;/code&amp;gt; Note Data Low&lt;br /&gt;
* &amp;lt;code&amp;gt;$E1&amp;lt;/code&amp;gt; Note Data High&lt;br /&gt;
* &amp;lt;code&amp;gt;$07E9&amp;lt;/code&amp;gt; Bass Note Pointer&lt;br /&gt;
* &amp;lt;code&amp;gt;$07EA&amp;lt;/code&amp;gt; Harmony Note Pointer&lt;br /&gt;
* &amp;lt;code&amp;gt;$07E8&amp;lt;/code&amp;gt; Drum Note Pointer&lt;br /&gt;
&lt;br /&gt;
After this, the drum loop start point at &amp;lt;code&amp;gt;$E4&amp;lt;/code&amp;gt; is also set to the same value as&lt;br /&gt;
the drum note pointer.  The melody note pointer at &amp;lt;code&amp;gt;$07EB&amp;lt;/code&amp;gt; is set to &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt;, and&lt;br /&gt;
all the durations (melody at &amp;lt;code&amp;gt;$07E7&amp;lt;/code&amp;gt;, harmony at &amp;lt;code&amp;gt;$07E6&amp;lt;/code&amp;gt;, bass at &amp;lt;code&amp;gt;$07E5&amp;lt;/code&amp;gt;, and&lt;br /&gt;
drums at &amp;lt;code&amp;gt;$07E4&amp;lt;/code&amp;gt;) are set to &amp;lt;code&amp;gt;$01&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Proceeds to Play Notes.&lt;br /&gt;
&lt;br /&gt;
==== Play Notes ====&lt;br /&gt;
&lt;br /&gt;
This routine is broken into four major and similar sections: one each for&lt;br /&gt;
melody, harmony, bass, and drums.  The general process is as follows:&lt;br /&gt;
&lt;br /&gt;
 decrement duration&lt;br /&gt;
 if duration == 0 {&lt;br /&gt;
   load next note&lt;br /&gt;
   increment note pointer&lt;br /&gt;
   set duration for note&lt;br /&gt;
   if no overriding sound effect {&lt;br /&gt;
     play note on appropriate channel&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Some channels have unique processing at different points in this.  For example,&lt;br /&gt;
the melody note data is null terminated, so if the loaded note in &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt; then the&lt;br /&gt;
routine will jump to Load Song Data instead to go to the next phrase.  If the&lt;br /&gt;
drums channel gets a &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt; note it will reset the drums note pointer to the&lt;br /&gt;
drums loop start and repeat the phrase.  The bass and harmony channels don't&lt;br /&gt;
have this looping behavior.  This means that if the bass or harmony channels&lt;br /&gt;
aren't long enough, the engine will happily just play whatever data comes next&lt;br /&gt;
in the ROM.&lt;br /&gt;
&lt;br /&gt;
The melody is played on pulse 2 channel, the harmony on pulse 1 (which gets&lt;br /&gt;
subverted by sound effects that need just one pulse channel), the bass on the&lt;br /&gt;
triangle channel, and the drums on the noise channel.&lt;br /&gt;
&lt;br /&gt;
For the pulse channels, after the note is played, the envelope counter for that&lt;br /&gt;
channel (&amp;lt;code&amp;gt;$07E2&amp;lt;/code&amp;gt; for pulse 1, &amp;lt;code&amp;gt;$07E3&amp;lt;/code&amp;gt; for pulse 2) is set.  Normally the&lt;br /&gt;
envelope is set to &amp;lt;code&amp;gt;$2F&amp;lt;/code&amp;gt; but song &amp;lt;code&amp;gt;$40&amp;lt;/code&amp;gt; (crystal fanfare and final boss music)&lt;br /&gt;
sets it to &amp;lt;code&amp;gt;$18&amp;lt;/code&amp;gt; instead.  The envelope is not set if no note was played because&lt;br /&gt;
the pitch index was &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt; which indicates a rest.&lt;br /&gt;
&lt;br /&gt;
Also for the pulse channels, after the above pseudocode, the vibrato and&lt;br /&gt;
enveloping happens as long as there is no overriding sound effect.  The envelope&lt;br /&gt;
works by checking the envelope counter set previously and decrementing it.  The&lt;br /&gt;
counter value is halved and used to look up the envelope value in the LUT at&lt;br /&gt;
&amp;lt;code&amp;gt;$9135&amp;lt;/code&amp;gt;.  These values are written directly to APU register &amp;lt;code&amp;gt;$4000&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$4004&amp;lt;/code&amp;gt;&lt;br /&gt;
depending on the channel.&lt;br /&gt;
&lt;br /&gt;
The bass channel has one special handling as well.  The APU register &amp;lt;code&amp;gt;$4008&amp;lt;/code&amp;gt; is&lt;br /&gt;
set differently based on the current song id.  For song &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; (item get / level&lt;br /&gt;
up fanfare) the register is set to &amp;lt;code&amp;gt;$60&amp;lt;/code&amp;gt; but all other songs use &amp;lt;code&amp;gt;$1f&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The noise channel is not affect at all by the pitch.  Any pitch value that is&lt;br /&gt;
non zero will play a burst of noise.&lt;/div&gt;</summary>
		<author><name>DontBaguMe</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.bindingforce.net/index.php?title=Music_Engine_Description&amp;diff=340</id>
		<title>Music Engine Description</title>
		<link rel="alternate" type="text/html" href="http://wiki.bindingforce.net/index.php?title=Music_Engine_Description&amp;diff=340"/>
		<updated>2022-03-30T04:19:44Z</updated>

		<summary type="html">&lt;p&gt;DontBaguMe: /* Title Music Engine */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There are two separate music engines in Zelda 2.  Both are in bank 6 (along&lt;br /&gt;
with the song data).  The music engine for the title screen resides at&lt;br /&gt;
&amp;lt;code&amp;gt;$8000&amp;lt;/code&amp;gt; and the engine for the rest of the game is at&lt;br /&gt;
&amp;lt;code&amp;gt;$9000&amp;lt;/code&amp;gt;.  Each of these engines is called once per frame during the&lt;br /&gt;
appropriate part of the game.&lt;br /&gt;
&lt;br /&gt;
Changes to the music engine should be very easy to make, as there is an&lt;br /&gt;
abundance of free space in bank 6.&lt;br /&gt;
&lt;br /&gt;
== Title Music Engine ==&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' properly format the following by bgt (original at https://eab.xyz/W2zplGD) and dontbagume  (on discord).&lt;br /&gt;
&lt;br /&gt;
# Title Music Engine&lt;br /&gt;
&lt;br /&gt;
The title music engine is different from the engine that plays music&lt;br /&gt;
during the rest of the game.  In particular, it has a wider range of&lt;br /&gt;
notes that can be represented.  If you want to look into the actual&lt;br /&gt;
code, the title screen music engine main loop is at `$8000` (vesus&lt;br /&gt;
`$9000` for the game music loop).&lt;br /&gt;
&lt;br /&gt;
The metadata for the songs is all the same as in the other areas,&lt;br /&gt;
although the title music is actually broken up into several songs&lt;br /&gt;
itself.  These song boundaries control the timing of the title scroll.&lt;br /&gt;
&lt;br /&gt;
The following songs exist in the title song table:&lt;br /&gt;
&lt;br /&gt;
  1. Intro&lt;br /&gt;
  2. Start&lt;br /&gt;
  3. Build up&lt;br /&gt;
  4. Main&lt;br /&gt;
  5. Breakdown&lt;br /&gt;
&lt;br /&gt;
The start of song 2 triggers the title to scroll into view.  The start&lt;br /&gt;
of song 4 triggers a countdown until the story scroll begins.  After&lt;br /&gt;
song 5 finishes, the engine loops back to song 2.&lt;br /&gt;
&lt;br /&gt;
Within the note data, a special format is used.  Rather than encoding&lt;br /&gt;
the duration and pitch together in a single byte, the title music has&lt;br /&gt;
&amp;quot;pitch&amp;quot; bytes and &amp;quot;duration&amp;quot; bytes.  Any byte with the highest bit set&lt;br /&gt;
(i.e. anything &amp;gt;= `$80`) is interpreted as a duration change, which&lt;br /&gt;
sets the duration of all future notes.  Anything else is a pitch value&lt;br /&gt;
which indicates a note of the current duration should be played.  The&lt;br /&gt;
low nibble of duration values keys into a lookup table at bank 6 `$8084` &lt;br /&gt;
and store the duration byte at $07FF.  Value durations are as follows:&lt;br /&gt;
&lt;br /&gt;
  * `$80` - 8 ticks   (sixteenth note)&lt;br /&gt;
  * `$81` - 24 ticks  (dotted eighth note)&lt;br /&gt;
  * `$82` - 16 ticks  (eighth note)&lt;br /&gt;
  * `$83` - 32 ticks  (quarter note)&lt;br /&gt;
  * `$84` - 48 ticks  (dotted quarter note)&lt;br /&gt;
  * `$85` - 64 ticks  (half note)&lt;br /&gt;
  * `$86` - 96 ticks  (dotted half note)&lt;br /&gt;
  * `$87` - 128 ticks (whole note)&lt;br /&gt;
  * `$88` - 11 ticks  (eighth note triplet)&lt;br /&gt;
  * `$89` - 10 ticks  (???)&lt;br /&gt;
  * `$8A` - 256 ticks (two whole notes)&lt;br /&gt;
  * `$8F` - 6 ticks   (dotted 32nd note)&lt;br /&gt;
&lt;br /&gt;
The pitch values are much more straightforward.  As far as I can tell,&lt;br /&gt;
`$4C` is A4 and every semitone away from that adds or subtracts 2 from&lt;br /&gt;
the value.  Thus, C5 (three semitones above A4) is `$52`.  The value&lt;br /&gt;
`$02` represents a rest.&lt;br /&gt;
&lt;br /&gt;
         -1-  -2-  -3-  -4-  -5-&lt;br /&gt;
    A  : $04  $1C  $34  $4C  $64&lt;br /&gt;
    Bb : $06  $1E  $36  $4E  $66&lt;br /&gt;
    B  : $08  $20  $38  $50  $68&lt;br /&gt;
    C  : $0A  $22  $3A  $52  $6A&lt;br /&gt;
    C# : $0C  $24  $3C  $54  $6C&lt;br /&gt;
    D  : $0E  $26  $3E  $56  $6E&lt;br /&gt;
    Eb : $10  $28  $40  $58  $70&lt;br /&gt;
    E  : $12  $2A  $42  $5A  $72&lt;br /&gt;
    F  : $14  $2C  $44  $5C  $74&lt;br /&gt;
    F# : $16  $2E  $46  $5E  $76&lt;br /&gt;
    G  : $18  $30  $48  $60  $78&lt;br /&gt;
    G# : $1A  $32  $4A  $62  $7A&lt;br /&gt;
&lt;br /&gt;
By way of an example, let's look at the main section of the vanilla&lt;br /&gt;
title music.  The title metadata starts at bank 6 `$84DA`:&lt;br /&gt;
&lt;br /&gt;
    08 11 14 16 19 1E 1E 1E&lt;br /&gt;
             ^ Main section&lt;br /&gt;
&lt;br /&gt;
The main section is the fourth song, starting at `$84DA + $16 = $84F0`:&lt;br /&gt;
&lt;br /&gt;
    3F 3F 00&lt;br /&gt;
&lt;br /&gt;
This means there is a single phrase for the main section, which is&lt;br /&gt;
repeated twice.  The phrase data is at `$84DA + $3F = $8519`.&lt;br /&gt;
&lt;br /&gt;
    00 3D 86 3A 1A 5F&lt;br /&gt;
&lt;br /&gt;
From this, we see the melody note data is located at `$863D`:&lt;br /&gt;
&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    02 Rest&lt;br /&gt;
    48 Ab4&lt;br /&gt;
    82 Eighth note&lt;br /&gt;
    46 G4&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    3E Eb4&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    84 Dotted quarter notes&lt;br /&gt;
    2E G3&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    30 Ab3&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    3A Db4&lt;br /&gt;
    38 C4&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    30 Ab3&lt;br /&gt;
    82 Eighth notes&lt;br /&gt;
    34 Bb3&lt;br /&gt;
    83 Quarter notes&lt;br /&gt;
    30 Ab3&lt;br /&gt;
    85 Half notes&lt;br /&gt;
    2E G3&lt;br /&gt;
    82 Quarter notes&lt;br /&gt;
    02 Rest&lt;br /&gt;
    00 End of Data&lt;br /&gt;
&lt;br /&gt;
dontbagume additions:&lt;br /&gt;
&lt;br /&gt;
DURATION&lt;br /&gt;
  * `$80` - 6 ticks    sixteenth note&lt;br /&gt;
  * `$81` - 9 ticks    dotted eighth note&lt;br /&gt;
  * `$82` - 12 ticks    eighth note&lt;br /&gt;
  * `$83` - 24 ticks    quarter note&lt;br /&gt;
  * `$84` - 36 ticks    dotted quarter note&lt;br /&gt;
  * `$85` - 48 ticks    half note&lt;br /&gt;
  * `$86` - 72 ticks    dotted half note&lt;br /&gt;
  * `$87` - 96 ticks    whole note&lt;br /&gt;
  * `$88` - 8 ticks    eighth triplets&lt;br /&gt;
  * `$89` - 7 ticks    poor man's swung 16th (long)&lt;br /&gt;
  * `$8A` - 60 ticks    half + eighth&lt;br /&gt;
  * `$8B` - 192 ticks    two whole notes&lt;br /&gt;
  * `$8F` - 5 ticks    poor man's swung 16th (short)&lt;br /&gt;
&lt;br /&gt;
PITCH&lt;br /&gt;
         -1-    -2-    -3-    -4-    -5-&lt;br /&gt;
  A  :     $04    $1C    $34    $4C    $64&lt;br /&gt;
  Bb :     $06    $1E    $36    $4E    $66&lt;br /&gt;
  B  :     $08    $20    $38    $50    $68&lt;br /&gt;
  C  :     $0A    $22    $3A    $52    $6A&lt;br /&gt;
  C# :     $0C    $24    $3C    $54    $6C&lt;br /&gt;
  D  :     $0E    $26    $3E    $56    $6E&lt;br /&gt;
  Eb :     $10    $28    $40    $58    $70&lt;br /&gt;
  E  :     $12    $2A    $42    $5A    $72&lt;br /&gt;
  F  :     $14    $2C    $44    $5C    $74&lt;br /&gt;
  F# :     $16    $2E    $46    $5E    $76&lt;br /&gt;
  G  :     $18    $30    $48    $60    $78&lt;br /&gt;
  G# :     $1A    $32    $4A    $62    $7A&lt;br /&gt;
&lt;br /&gt;
== Main Music Engine ==&lt;br /&gt;
&lt;br /&gt;
=== Data Tables ===&lt;br /&gt;
&lt;br /&gt;
There are several data tables used by the music engine.&lt;br /&gt;
&lt;br /&gt;
==== Pulse Envelope ====&lt;br /&gt;
&lt;br /&gt;
This table is used to set the volume of the pulse channels over time.  The index&lt;br /&gt;
used starts at a certain value and decrements, so the table is in reverse order.&lt;br /&gt;
The index is halved before use, so each value lasts two frames.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+ Pulse Envelope &amp;lt;code&amp;gt;$9135&amp;lt;/code&amp;gt;&lt;br /&gt;
|90||91||91||91||92||92||92||92&lt;br /&gt;
|-&lt;br /&gt;
|93||93||94||94||94||95||95||95&lt;br /&gt;
|-&lt;br /&gt;
|96||96||96||97||97||97||98||98&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Duration LUT ====&lt;br /&gt;
&lt;br /&gt;
This table is used to determine the duration of a note.  The note data is stored&lt;br /&gt;
with 5 bits of pitch information and 3 bits of duration, as such:&lt;br /&gt;
&lt;br /&gt;
 D1 D0 P4 P3 P2 P1 P0 D2&lt;br /&gt;
&lt;br /&gt;
The Get Note Duration routine shifts things around to get the D bits into the&lt;br /&gt;
three least significant bits and masks it before use.  This value is then added&lt;br /&gt;
to the tempo value in &amp;lt;code&amp;gt;$E5&amp;lt;/code&amp;gt; which is always a multiple of 8 to index&lt;br /&gt;
into the following table:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+Duration LUT &amp;lt;code&amp;gt;$914D&amp;lt;/code&amp;gt;&lt;br /&gt;
|'''00'''||04||0C||08||10||18||20||05||06&lt;br /&gt;
|-&lt;br /&gt;
|'''08'''||04||0F||09||12||1B||24||06||06&lt;br /&gt;
|-&lt;br /&gt;
|'''10'''||05||0F||0A||14||1E||28||07||06&lt;br /&gt;
|-&lt;br /&gt;
|'''18'''||06||12||0C||18||24||30||08||10&lt;br /&gt;
|-&lt;br /&gt;
|'''20'''||07||15||0E||1C||2A||38||13||12&lt;br /&gt;
|-&lt;br /&gt;
|'''28'''||07||15||0E||1C||2A||38||XX||XX&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The last two values in each row seem a bit strange, as they are not nice&lt;br /&gt;
multiples of the first entry which is usually thought of as the length of an&lt;br /&gt;
eighth note.  The last row has garbage values in those positions as that is the&lt;br /&gt;
start of another data table, so they are not even listed here.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' Check what values are used in vanilla.&lt;br /&gt;
&lt;br /&gt;
==== Pitch LUT ====&lt;br /&gt;
&lt;br /&gt;
This table is used to detemine the pitch of a note.  The note data is stored as&lt;br /&gt;
described above, and the fact that the least significant bit is masked out is&lt;br /&gt;
advantageous since the pulse channels can use 11 bits of timer data, so each&lt;br /&gt;
entry in this table is actually two bytes wide.&lt;br /&gt;
&lt;br /&gt;
Rather than show the raw bytes here, it's more useful to show the rough note&lt;br /&gt;
that those values represent.  If the raw values are interesting to you, check&lt;br /&gt;
the ROM for them.  It's worth noting that some of the higher notes are several&lt;br /&gt;
cents off from the listed note.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+Pitch LUT &amp;lt;code&amp;gt;$918F&amp;lt;/code&amp;gt;&lt;br /&gt;
|  || 00|| 02|| 04|| 06|| 08|| 0a|| 0c|| 0e&lt;br /&gt;
|-&lt;br /&gt;
|00|| C3||---|| E3|| G3||G#3|| A3||A#3|| B3&lt;br /&gt;
|-&lt;br /&gt;
|10|| C4||C#4|| D4||D#4|| E4|| F4||F#4|| G4&lt;br /&gt;
|-&lt;br /&gt;
|20||G#4|| A4||A#4|| B4|| C5||C#5|| D5||D#5&lt;br /&gt;
|-&lt;br /&gt;
|30|| E5|| F5||F#5|| G5|| A5||A#5|| B5||C#3&lt;br /&gt;
|-&lt;br /&gt;
|40|| D3||D#3|| F3||F#3||G#5|| C6||C#6|| D6&lt;br /&gt;
|-&lt;br /&gt;
|50||D#6|| E6|| F6||F#6|| G6||G#6|| A6||A#6&lt;br /&gt;
|-&lt;br /&gt;
|60|| B6|| C7||C#7|| D7||D#7|| E7|| F7||F#7&lt;br /&gt;
|-&lt;br /&gt;
|70|| G7||G#7|| A7||A#7|| B7|| C8&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Value &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt; represents a rest.&lt;br /&gt;
&lt;br /&gt;
Note that although the table goes through &amp;lt;code&amp;gt;$7A&amp;lt;/code&amp;gt; values above&lt;br /&gt;
&amp;lt;code&amp;gt;$3E&amp;lt;/code&amp;gt; aren't usable due to the 5 bit limit for pitch data in&lt;br /&gt;
the song data.  However, the sound effects routines make use of these&lt;br /&gt;
extra notes.  For example, the sword beam effect alternates between&lt;br /&gt;
playing C7 and F6.&lt;br /&gt;
&lt;br /&gt;
==== Noise Samples ====&lt;br /&gt;
&lt;br /&gt;
Several sound effects use the same routine to load &amp;quot;samples&amp;quot; for the noise&lt;br /&gt;
channel to play.  These values have the noise volume in the high nybble and the&lt;br /&gt;
noise period in the low nybble.&lt;br /&gt;
&lt;br /&gt;
 $90E8 - Sword slash&lt;br /&gt;
 $9123 - Enemy hurt&lt;br /&gt;
 $912C - Crumble block&lt;br /&gt;
&lt;br /&gt;
=== Variables ===&lt;br /&gt;
&lt;br /&gt;
This is just a dump of RAM addresses used by the music engine and a short&lt;br /&gt;
description of what they are.&lt;br /&gt;
&lt;br /&gt;
 $00E0 - Current phrase note data address (low byte)&lt;br /&gt;
 $00E1 - Current phrase note data address (high byte)&lt;br /&gt;
 $00E2 - Song offset, converted from song id to be an index instead of a bit field&lt;br /&gt;
 $00E3 - Current phrase index&lt;br /&gt;
 $00E4 - Drums loop start&lt;br /&gt;
 $00E5 - Tempo&lt;br /&gt;
 $00E6 - Pulse 1 low bits (used for vibrato effect)&lt;br /&gt;
 $00E7 - Pulse 2 low bits (used for vibrato effect)&lt;br /&gt;
 $00E8 - Pitch storage, temporary&lt;br /&gt;
 $00E9 - Play sound effect&lt;br /&gt;
 $00EA - Disable music&lt;br /&gt;
 $00EB - Request new song&lt;br /&gt;
 $00EC - Play sound effect&lt;br /&gt;
 $00ED - Play sound effect&lt;br /&gt;
 $00EE - Play sound effect&lt;br /&gt;
 $00EF - Play sound effect&lt;br /&gt;
&lt;br /&gt;
 $0707 - Current world&lt;br /&gt;
 $075F - Queued song, loaded after screen transitions&lt;br /&gt;
 $07DA - Ganon Laugh sample&lt;br /&gt;
 $07DB - Song to resume after fanfare&lt;br /&gt;
 $07DF - ??? Likely sound effect flag for pulse 1 channel override&lt;br /&gt;
&lt;br /&gt;
 $07E0 - ??? Likely sound effect flag for noise channel&lt;br /&gt;
 $07E2 - Pulse 2 envelope index&lt;br /&gt;
 $07E3 - Pulse 1 envelope index&lt;br /&gt;
 $07E4 - Drums current note duration&lt;br /&gt;
 $07E5 - Bass current note duration&lt;br /&gt;
 $07E6 - Harmony current note duration&lt;br /&gt;
 $07E7 - Melody current note duration&lt;br /&gt;
 $07E8 - Drums next note index&lt;br /&gt;
 $07E9 - Bass next note index&lt;br /&gt;
 $07EA - Harmony next note index&lt;br /&gt;
 $07EB - Melody next note index&lt;br /&gt;
&lt;br /&gt;
 $07EC - Ganon Laugh counter&lt;br /&gt;
 $07ED - Sound FX counter (E9)&lt;br /&gt;
 $07EE - Sound FX counter (E9)&lt;br /&gt;
 $07F5 - Sound FX counter (ED)&lt;br /&gt;
&lt;br /&gt;
 $07FA - Current SFX (E9)&lt;br /&gt;
 $07FB - Current song&lt;br /&gt;
 $07FD - Current SFX (ED)&lt;br /&gt;
 $07FE - ??? Likely sound effect flag for pulse 2 channel override&lt;br /&gt;
 $07FF - Current SFX (EF)&lt;br /&gt;
&lt;br /&gt;
=== Routines ===&lt;br /&gt;
&lt;br /&gt;
This is a non-exhaustive list of the routines in bank 6 involving the playing of&lt;br /&gt;
music and sound effects.  Each routine also lists its address so you can search&lt;br /&gt;
in a disassembly for the actual code for that routine if these descriptions&lt;br /&gt;
aren't clear.&lt;br /&gt;
&lt;br /&gt;
==== Main Music Loop &amp;lt;code&amp;gt;$9000&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Checks that the music disable flag is off, then runs several SFX routines.&lt;br /&gt;
After the SFX routines, the main music routine runs.  After all this, various&lt;br /&gt;
music related memory locations are cleared.&lt;br /&gt;
&lt;br /&gt;
==== Configure Pulse1 Channel &amp;lt;code&amp;gt;$9031&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes registers x and y and configures the APU registers &amp;lt;code&amp;gt;$4000&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$4001&amp;lt;/code&amp;gt;&lt;br /&gt;
respectively.&lt;br /&gt;
&lt;br /&gt;
==== Configure Pulse2 Channel &amp;lt;code&amp;gt;$9038&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes registers x and y and configures the APU registers &amp;lt;code&amp;gt;$4004&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$4005&amp;lt;/code&amp;gt;&lt;br /&gt;
respectively.&lt;br /&gt;
&lt;br /&gt;
==== Play Note Pulse1 &amp;lt;code&amp;gt;$9042&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a pitch index in register a and plays the note on the pulse 1 channel.&lt;br /&gt;
&lt;br /&gt;
==== Play Note &amp;lt;code&amp;gt;$9044&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Called by the various play note routines.  Takes a pitch index in register a and&lt;br /&gt;
uses register x as a channel selector.  For the pulse channels, this routine&lt;br /&gt;
also saves the lower 8 bits of the APU timer register to &amp;lt;code&amp;gt;$E6&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$E7&amp;lt;/code&amp;gt; which&lt;br /&gt;
are used for a vibrato effect.&lt;br /&gt;
&lt;br /&gt;
==== Play Note Pulse2 &amp;lt;code&amp;gt;$9067&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a pitch index in register a and plays the note on the pulse 2 channel.&lt;br /&gt;
&lt;br /&gt;
==== Play Note Triangle &amp;lt;code&amp;gt;$906B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a pitch index in register a and plays the note on the triangle channel.&lt;br /&gt;
&lt;br /&gt;
==== Get Note Duration &amp;lt;code&amp;gt;$906F&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes note data in register a and looks up the duration.   The original note&lt;br /&gt;
data is saved in register x and the result of the duration lookup is put in&lt;br /&gt;
register a.  The duration lookup is offset by the tempo stored in &amp;lt;code&amp;gt;$E5&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Vibrato Pulse1 &amp;lt;code&amp;gt;$9089&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Applies a vibrato effect on the pulse 1 channel.  Takes the duration of the note&lt;br /&gt;
left in register a and the APU timer low bits in register y.&lt;br /&gt;
&lt;br /&gt;
==== Vibrato Pulse2 &amp;lt;code&amp;gt;$9097&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Applies a vibrato effect on the pulse 2 channel.  Takes the duration of the note&lt;br /&gt;
left in register a and the APU timer low bits in register y.&lt;br /&gt;
&lt;br /&gt;
==== Vibrato &amp;lt;code&amp;gt;$909E&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes the duration of the note remaining in register a, the low bits of the APU&lt;br /&gt;
timer in register y, and the channel to use in register x.  This is called by&lt;br /&gt;
the other vibrato routines after setting x appropriately.&lt;br /&gt;
&lt;br /&gt;
Uses the &amp;lt;code&amp;gt;$04&amp;lt;/code&amp;gt; bit of the duration to either add or subtract 2 from the low&lt;br /&gt;
timer bits.  Only the low bits are used because writing to the high bits resets&lt;br /&gt;
the phase of the channel which can cause clicks.  None of the notes from the&lt;br /&gt;
pitch LUT are close enough to the boundary for the change to require the high&lt;br /&gt;
bits to change anyway, so this works out fine.&lt;br /&gt;
&lt;br /&gt;
==== Play E9 SFX &amp;lt;code&amp;gt;$920B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$E9&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play EF SFX &amp;lt;code&amp;gt;$92F4&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$EF&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play EE SFX &amp;lt;code&amp;gt;$9408&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$EE&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play Noise &amp;lt;code&amp;gt;$959D&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Configures the APU noise registers to make a noise burst.  Takes configuration&lt;br /&gt;
for registers &amp;lt;code&amp;gt;$400F&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;$400E&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;$400C&amp;lt;/code&amp;gt; in registers y, x, and a.&lt;br /&gt;
&lt;br /&gt;
==== Crumble Bridge SFX &amp;lt;code&amp;gt;$956C&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Starts playing the sound effect for a crumble bridge crumbling.  This uses both&lt;br /&gt;
the noise and the triangle channels so it sets flags to disable the music on&lt;br /&gt;
those channels.  Continues with SFX Noise Decay.&lt;br /&gt;
&lt;br /&gt;
==== SFX Noise Decay &amp;lt;code&amp;gt;$9587&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Counts down the timer in &amp;lt;code&amp;gt;$07F5&amp;lt;/code&amp;gt; and turns off the noise when it reaches 0.&lt;br /&gt;
Also clears the flag for the ED sound effect in &amp;lt;code&amp;gt;$07FD&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Play ED SFX &amp;lt;code&amp;gt;$95A7&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect request in &amp;lt;code&amp;gt;$ED&amp;lt;/code&amp;gt; or continues the sound effect saved in&lt;br /&gt;
&amp;lt;code&amp;gt;$07FD&amp;lt;/code&amp;gt;.  Existing sound effects which are higher than the current sound effect&lt;br /&gt;
will be ignored.  If the same sound effect is requested that is already playing,&lt;br /&gt;
it will restart the sound effect except for &amp;lt;code&amp;gt;$80&amp;lt;/code&amp;gt; which gives priority to&lt;br /&gt;
continuing.  When the sound is continued instead of newly played, the decay&lt;br /&gt;
routine will be called instead.&lt;br /&gt;
&lt;br /&gt;
; &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt;&lt;br /&gt;
: Crumble Bridge&lt;br /&gt;
; &amp;lt;code&amp;gt;$04&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt;&lt;br /&gt;
: Crumble Block / Enemy Hurt&lt;br /&gt;
; &amp;lt;code&amp;gt;$20&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$80&amp;lt;/code&amp;gt;&lt;br /&gt;
: Sword Strike&lt;br /&gt;
; &amp;lt;code&amp;gt;$40&amp;lt;/code&amp;gt;&lt;br /&gt;
: Very short noise burst, probably unused.  Doesn't store the effect, and doesn't have an associated decay.&lt;br /&gt;
&lt;br /&gt;
==== Crumble Block / Enemy Hurt SFX &amp;lt;code&amp;gt;$95E6&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Starts playing the sound of a block breaking or an enemy getting hurt.&lt;br /&gt;
&lt;br /&gt;
==== Crumble Block / Enemy Hurt SFX Decay &amp;lt;code&amp;gt;$95EE&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Looks up noise register values based on the timer and the sound effect value.&lt;br /&gt;
Crumble Block samples are at &amp;lt;code&amp;gt;$912C&amp;lt;/code&amp;gt; while Enemy Hurt is at &amp;lt;code&amp;gt;$9123&amp;lt;/code&amp;gt;.  Continues&lt;br /&gt;
on to Play Noise Samples.&lt;br /&gt;
&lt;br /&gt;
==== Play Noise Samples &amp;lt;code&amp;gt;$9612&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a sample value in register a.  The low four bits of this are used to set&lt;br /&gt;
the noise period via APU register &amp;lt;code&amp;gt;$400E&amp;lt;/code&amp;gt;.  The high four bits are used to set&lt;br /&gt;
the noise volume via APU register &amp;lt;code&amp;gt;$400C&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Continues on the SFX Noise Decay process.&lt;br /&gt;
&lt;br /&gt;
==== Sword Strike SFX &amp;lt;code&amp;gt;$9604&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Starts playing the sound of swinging your sword.  This looks up samples based on&lt;br /&gt;
the duration left at &amp;lt;code&amp;gt;$90E8&amp;lt;/code&amp;gt; and plays them via Play Noise Samples.&lt;br /&gt;
&lt;br /&gt;
==== Play EC SFX &amp;lt;code&amp;gt;$990B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the sound effect requested in &amp;lt;code&amp;gt;$EC&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 '''TODO''' document further.&lt;br /&gt;
&lt;br /&gt;
==== Play Music &amp;lt;code&amp;gt;$9B18&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Plays the music for the game.&lt;br /&gt;
&lt;br /&gt;
Runs Load Song if a new song is requested in &amp;lt;code&amp;gt;$EB&amp;lt;/code&amp;gt;.  Otherwise, runs Play Notes&lt;br /&gt;
if the current song is set in &amp;lt;code&amp;gt;$07FB&amp;lt;/code&amp;gt;.  If neither of these is true, do nothing.&lt;br /&gt;
&lt;br /&gt;
==== Next Song &amp;lt;code&amp;gt;$9B24&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Checks if the previous song should loop.  If not, calls Mute All and returns.&lt;br /&gt;
&lt;br /&gt;
If the previous song was &amp;lt;code&amp;gt;$01&amp;lt;/code&amp;gt; (the &amp;quot;intro&amp;quot; to the main theme), sets the next&lt;br /&gt;
song to &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt; (the main theme loop).&lt;br /&gt;
&lt;br /&gt;
If the previous song was &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; (the item get / level up fanfare), sets the&lt;br /&gt;
current song from &amp;lt;code&amp;gt;$07DB&amp;lt;/code&amp;gt;.  Otherwise, leaves the current song the same as the&lt;br /&gt;
previous song.&lt;br /&gt;
&lt;br /&gt;
Proceeds to Load Song.&lt;br /&gt;
&lt;br /&gt;
==== Mute All &amp;lt;code&amp;gt;$9B3B&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Clears the current song at &amp;lt;code&amp;gt;$07FB&amp;lt;/code&amp;gt; and sets the APU registers to mute the volume&lt;br /&gt;
on both pulse channels, the triangle channel, and the noise channel.&lt;br /&gt;
&lt;br /&gt;
==== Load Song &amp;lt;code&amp;gt;$9B61&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Takes a song id in register a and preps some variables for that song.&lt;br /&gt;
&lt;br /&gt;
If the song is &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; (item get / level up fanfare) then it first saves the&lt;br /&gt;
previous song to &amp;lt;code&amp;gt;$07FB&amp;lt;/code&amp;gt; if it is less than &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; so that the previous music&lt;br /&gt;
will resume after the fanfare.&lt;br /&gt;
&lt;br /&gt;
Sets the phrase counter at &amp;lt;code&amp;gt;$E3&amp;lt;/code&amp;gt; to 0 and the song offset at &amp;lt;code&amp;gt;$E2&amp;lt;/code&amp;gt; based on the&lt;br /&gt;
song id.  Song ids are a bit field, and the song offset is the position of the&lt;br /&gt;
lowest bit in the song id for indexing into the song table.&lt;br /&gt;
&lt;br /&gt;
Proceeds to Load Song Data.&lt;br /&gt;
&lt;br /&gt;
==== Load Song Data &amp;lt;code&amp;gt;$9B80&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
Loads the music variables based on the current world and current song offset.&lt;br /&gt;
&lt;br /&gt;
There are four blocks of code that are basically the same, for each of the four&lt;br /&gt;
different music tables.  Each one just uses a different position for the tables&lt;br /&gt;
to load.&lt;br /&gt;
&lt;br /&gt;
Based on the current world in &amp;lt;code&amp;gt;$0707&amp;lt;/code&amp;gt; it loads data from these offsets:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt;&lt;br /&gt;
: Overworld music - &amp;lt;code&amp;gt;$9B87&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;$01&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt;&lt;br /&gt;
: Town music - &amp;lt;code&amp;gt;$9BC6&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;$03&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$04&amp;lt;/code&amp;gt;&lt;br /&gt;
: Palace music - &amp;lt;code&amp;gt;$9C05&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;$05&amp;lt;/code&amp;gt; or higher&lt;br /&gt;
: Great Palace music - &amp;lt;code&amp;gt;$9C42&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, uses the song offset in &amp;lt;code&amp;gt;$E2&amp;lt;/code&amp;gt;  to index into the song table at the base&lt;br /&gt;
offset given above.&lt;br /&gt;
&lt;br /&gt;
The phrase counter at &amp;lt;code&amp;gt;$E3&amp;lt;/code&amp;gt; is added to that and used as in index again to&lt;br /&gt;
retrieve the current phrase offset.  If the current phrase offset is &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt; that&lt;br /&gt;
means the song is over and the process goes back to the Next Song routine.&lt;br /&gt;
&lt;br /&gt;
Otherwise, the following structure is read at the base offset above + phrase&lt;br /&gt;
offset just calculated.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;$E5&amp;lt;/code&amp;gt; Tempo&lt;br /&gt;
* &amp;lt;code&amp;gt;$E0&amp;lt;/code&amp;gt; Note Data Low&lt;br /&gt;
* &amp;lt;code&amp;gt;$E1&amp;lt;/code&amp;gt; Note Data High&lt;br /&gt;
* &amp;lt;code&amp;gt;$07E9&amp;lt;/code&amp;gt; Bass Note Pointer&lt;br /&gt;
* &amp;lt;code&amp;gt;$07EA&amp;lt;/code&amp;gt; Harmony Note Pointer&lt;br /&gt;
* &amp;lt;code&amp;gt;$07E8&amp;lt;/code&amp;gt; Drum Note Pointer&lt;br /&gt;
&lt;br /&gt;
After this, the drum loop start point at &amp;lt;code&amp;gt;$E4&amp;lt;/code&amp;gt; is also set to the same value as&lt;br /&gt;
the drum note pointer.  The melody note pointer at &amp;lt;code&amp;gt;$07EB&amp;lt;/code&amp;gt; is set to &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt;, and&lt;br /&gt;
all the durations (melody at &amp;lt;code&amp;gt;$07E7&amp;lt;/code&amp;gt;, harmony at &amp;lt;code&amp;gt;$07E6&amp;lt;/code&amp;gt;, bass at &amp;lt;code&amp;gt;$07E5&amp;lt;/code&amp;gt;, and&lt;br /&gt;
drums at &amp;lt;code&amp;gt;$07E4&amp;lt;/code&amp;gt;) are set to &amp;lt;code&amp;gt;$01&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Proceeds to Play Notes.&lt;br /&gt;
&lt;br /&gt;
==== Play Notes ====&lt;br /&gt;
&lt;br /&gt;
This routine is broken into four major and similar sections: one each for&lt;br /&gt;
melody, harmony, bass, and drums.  The general process is as follows:&lt;br /&gt;
&lt;br /&gt;
 decrement duration&lt;br /&gt;
 if duration == 0 {&lt;br /&gt;
   load next note&lt;br /&gt;
   increment note pointer&lt;br /&gt;
   set duration for note&lt;br /&gt;
   if no overriding sound effect {&lt;br /&gt;
     play note on appropriate channel&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Some channels have unique processing at different points in this.  For example,&lt;br /&gt;
the melody note data is null terminated, so if the loaded note in &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt; then the&lt;br /&gt;
routine will jump to Load Song Data instead to go to the next phrase.  If the&lt;br /&gt;
drums channel gets a &amp;lt;code&amp;gt;$00&amp;lt;/code&amp;gt; note it will reset the drums note pointer to the&lt;br /&gt;
drums loop start and repeat the phrase.  The bass and harmony channels don't&lt;br /&gt;
have this looping behavior.  This means that if the bass or harmony channels&lt;br /&gt;
aren't long enough, the engine will happily just play whatever data comes next&lt;br /&gt;
in the ROM.&lt;br /&gt;
&lt;br /&gt;
The melody is played on pulse 2 channel, the harmony on pulse 1 (which gets&lt;br /&gt;
subverted by sound effects that need just one pulse channel), the bass on the&lt;br /&gt;
triangle channel, and the drums on the noise channel.&lt;br /&gt;
&lt;br /&gt;
For the pulse channels, after the note is played, the envelope counter for that&lt;br /&gt;
channel (&amp;lt;code&amp;gt;$07E2&amp;lt;/code&amp;gt; for pulse 1, &amp;lt;code&amp;gt;$07E3&amp;lt;/code&amp;gt; for pulse 2) is set.  Normally the&lt;br /&gt;
envelope is set to &amp;lt;code&amp;gt;$2F&amp;lt;/code&amp;gt; but song &amp;lt;code&amp;gt;$40&amp;lt;/code&amp;gt; (crystal fanfare and final boss music)&lt;br /&gt;
sets it to &amp;lt;code&amp;gt;$18&amp;lt;/code&amp;gt; instead.  The envelope is not set if no note was played because&lt;br /&gt;
the pitch index was &amp;lt;code&amp;gt;$02&amp;lt;/code&amp;gt; which indicates a rest.&lt;br /&gt;
&lt;br /&gt;
Also for the pulse channels, after the above pseudocode, the vibrato and&lt;br /&gt;
enveloping happens as long as there is no overriding sound effect.  The envelope&lt;br /&gt;
works by checking the envelope counter set previously and decrementing it.  The&lt;br /&gt;
counter value is halved and used to look up the envelope value in the LUT at&lt;br /&gt;
&amp;lt;code&amp;gt;$9135&amp;lt;/code&amp;gt;.  These values are written directly to APU register &amp;lt;code&amp;gt;$4000&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;$4004&amp;lt;/code&amp;gt;&lt;br /&gt;
depending on the channel.&lt;br /&gt;
&lt;br /&gt;
The bass channel has one special handling as well.  The APU register &amp;lt;code&amp;gt;$4008&amp;lt;/code&amp;gt; is&lt;br /&gt;
set differently based on the current song id.  For song &amp;lt;code&amp;gt;$10&amp;lt;/code&amp;gt; (item get / level&lt;br /&gt;
up fanfare) the register is set to &amp;lt;code&amp;gt;$60&amp;lt;/code&amp;gt; but all other songs use &amp;lt;code&amp;gt;$1f&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The noise channel is not affect at all by the pitch.  Any pitch value that is&lt;br /&gt;
non zero will play a burst of noise.&lt;/div&gt;</summary>
		<author><name>DontBaguMe</name></author>
		
	</entry>
</feed>