Welcome to the Creatures Wiki! Log in and join the community.

Editing Music

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 13: Line 13:
  
 
The music of other games can be manipulated using normal audio or music editors.
 
The music of other games can be manipulated using normal audio or music editors.
 +
 +
==Outline==
 +
Music is stored in 'munge' files, with the extension ''MNG'' (at the time, the MNG extension was not in use - it was later appropriated as a replacement for PNG). The music is formed of separate samples in a partial WAV format combined with a script which is interpreted by the music engine. The sample format is a standard Windows WAV format with the first 16 bytes removed - typically in '''16-bit 22kHz mono''' (if you need to convert your WAV files to this, open them up in Sound Recorder and Save As to this format).
 +
 +
The file starts off with an index of samples, then the script data chunk, then sample chunks. The ''names'' of samples are stored implicitly - the first name in the script refers to the first sample chunk, the second name is the second, and so on, ignoring names that are already associated with samples.
 +
 +
''Although you do not need to understand the above to write a script, you '''do''' need to understand that if you save a file all the samples must be used in a "Wave()" somewhere in the script, otherwise there will be no way for programs to know what their name is later.''
 +
 +
==Script Format==
 +
===Tracks===
 +
The basic unit of music is a Track, which in the game is associated with either a specific event - for example, the death of a creature - or an area, like the Volcano track. Typically only one track plays at once; switching between tracks is accomplished by fading in and out, adhering to the FadeIn and FadeOut track parameters. Each Track has one or more Layers, which are played simultaneously.
 +
Comments are indicated by a double slash (//) and may be placed within Track, Effect, Voice or Update declarations, as well as at the top level.
 +
 +
<pre>
 +
Track(UpperTemple)
 +
    {
 +
    FadeIn(5)
 +
    FadeOut(5)
 +
 +
    LoopLayer(Chord)
 +
        {
 +
        …
 +
        }
 +
 +
    AleotoricLayer(StickMelody)
 +
        {
 +
        …
 +
        }
 +
    }
 +
</pre>
 +
 +
===Variables===
 +
MNG scripts have a concept of local variables, which reside within these layers. Variables must be declared before use, with the name and an initial value. The variables are floating point values associated with names. Some variables are special – Pan and Volume because they affect the samples to be played, and Interval because it affects the length of the track.
 +
 +
<pre>
 +
AleotoricLayer(Pad)
 +
    {
 +
    Variable(temp,4.0)
 +
        …
 +
</pre>
 +
 +
===Layers===
 +
Layers are the “instruments�? of a track, in that they either play one sample repeatedly (in the case of LoopLayers) or one or more samples, enclosed within Voices (AleotoricLayers).
 +
 +
====LoopLayers====
 +
A LoopLayer consists of a single Wave and an Update block. The Wave is played constantly and repeatedly. The Update is called at regular intervals and typically causes some change in the presentation of the samples (for example, it may pan the output from side to side, or alter the volume).
 +
 +
<pre>
 +
    LoopLayer(HighBreath)
 +
        {
 +
        Variable(counter,0.0)
 +
        Variable(temp,0.0)
 +
        Update
 +
            {
 +
            // Gradually, pan around at a random rate
 +
            temp = Random(0.0, 0.1)
 +
            counter = Add(counter, temp)
 +
            Pan = CosineWave(counter, 30)
 +
            // Scale the volume according to mood
 +
            Volume = Multiply(Mood,0.4)
 +
            Volume = Add(Volume,0.6)
 +
            }
 +
        UpdateRate(0.1)
 +
        Wave(HighBreathG)
 +
        }
 +
</pre>
 +
 +
====AleotoricLayers and Voices====
 +
An AleotoricLayer consists of one or more Voices to be played sequentially. Effects and Volume may be specified for the layer. The Interval of a layer specifies how long it is before the next Voice of an AleotoricLayer is to be played – it is possible to change this within the Voice.
 +
 +
Voices are individual Waves with optional Conditions and Intervals. Conditions are used to decide whether or not the Wave should be played – the value of the specified variable must be between the two specified values. Intervals allow the script to specify how long to wait before the next sample.
 +
 +
<pre>
 +
    AleotoricLayer(BendyEcho)
 +
        {
 +
        Volume(0.4)
 +
        Effect(PingPong160)
 +
        Interval(4)
 +
        Voice
 +
            {
 +
            Condition(Mood,0.2,0.6)
 +
            Wave(Bnd0)
 +
            Interval ( Random( 4.0, 9.4) )
 +
            }
 +
        Voice
 +
            {
 +
            Condition(Mood,0.4,1.0)
 +
            Wave(Bnd1)
 +
            Interval ( Random( 4.0, 9.4) )
 +
            }
 +
        }
 +
</pre>
 +
 +
===Update Blocks===
 +
Both the LoopLayer and AleotoricLayer structures may have one Update block. This block consists of assignments to variables (which may be special variables such as Volume or Pan) that are carried out each time an update is called, and also when beginning to play a layer. The time period for updates is set by the UpdateRate or BeatSynch statement in LoopLayers, or each time the last Voice is considered for AleotoricLayers. The Update blocks may also be placed within Voice blocks, in which case the update takes effect after the voice’s Wave has been played.
 +
 +
<pre>
 +
AleotoricLayer(Pad)
 +
    {
 +
    // The track sparsely plays pads ranging from the gentle (drm)
 +
    // for low threat, with harsher (vce) for heigher threats
 +
    // Volume increases with mood and threat,
 +
    // The interval is decreased with threat
 +
    Volume(0.4)
 +
    Variable(temp,0.0)
 +
    Update
 +
        {
 +
        // Volume = 0.5 + 0.25 * (Mood + Threat)
 +
        temp = Multiply(Mood, 0.25)
 +
        Volume = Add(0.5,temp)
 +
        temp = Multiply(Threat, 0.25)
 +
        Volume = Add(Volume,temp)
 +
        Interval = Random ( 4.0, 6.0 )
 +
        temp = Multiply( Threat, 2.0)
 +
        Interval = Subtract( Interval, temp)
 +
        }
 +
    …
 +
</pre>
 +
 +
===Intervals===
 +
Intervals represent a pause in the output of a layer, either between Voices if specified for a particular voice or between iterations of a layer if in the main body. Processing of a layer does not continue until a pause for the length of the interval has taken place. The expression is evaluated anew each time.
 +
 +
===Beats, BeatLength and BeatSynch===
 +
Beats are an alternative method of specifying intervals between periods of music. Instead of directly specifying a length in seconds, the BeatLength is specified in the Track, and a BeatSynch is given that measures an Interval in this number of beats. The following specifies an interval of 0.3 * 16 = 4.8 seconds for the Guitar:
 +
 +
<pre>
 +
Track(Underwater)
 +
    {
 +
    BeatLength(0.3)
 +
 +
    AleotoricLayer(Guitar)
 +
        {
 +
        BeatSynch(16.0)
 +
 +
</pre>
 +
 +
===Effects===
 +
The script also specifies Effects, which are preset sequences of setting changes applied to AleotoricLayers (not LoopLayers). An Effect has one or more Stages, each of which may make changes to the panning or volume for that layer. After a specified delay, the effect moves onto the next Stage in the sequence.
 +
 +
Effects are applied to the output of a Layer; they essentially take this output and repeat it several times . How many times is defined by the number of Stage declarations in the Effect. As an example, a simple effect might “bounce�? the sound from one side to the other, slowly fading the volume at the same time.
 +
 +
Each Stage contains a declaration for the Volume to play the output at, the Pan value (how far to the left or right of centre the sound should be played) and either a Delay or TempoDelay, indicating how long to pause before moving on to start the next effect. Values may be expressions or constants. TempoDelays are present in effects intended for layers using the BeatSynch and are measured in beats as defined in the Track currently playing, whilst Delay is measured in seconds.
 +
<pre>
 +
Effect(RandomPad)
 +
    {
 +
    // Produces randomly panned echoes, staggered at close
 +
    // random times
 +
    Stage
 +
        {
 +
        Pan(Random(-1.0,1.0)) Volume(1) Delay(Random(0.25,0.4))
 +
        }
 +
    Stage
 +
        {
 +
        Pan(Random(-1.0,1.0)) Volume(0.92) Delay(Random(0.25,0.4))
 +
        }
 +
    Stage
 +
        {
 +
        Pan(Random(-1.0,1.0)) Volume(0.84) Delay(Random(0.25,0.4))
 +
        }
 +
    }
 +
</pre>
 +
 +
'''If you just want to edit music files, not write music editing programs, you can stop reading now.'''
 +
----
 +
==File Structure==
 +
Remember that Windows uses least-significant bit byte ordering for numbers. You may have to convert on other platforms.
 +
 +
<table cellspacing="0" width="559">
 +
<tr>
 +
  <td style="border-width: thin; border-style: dashed;width: 68.3pt;" valign="top" width="91">
 +
  <p>&nbsp;Position</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 55.45pt;" valign="top" width="74">
 +
 +
  <p>&nbsp;Length</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 142.65pt;" valign="top" width="190">
 +
  <p>&nbsp;Description</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 153pt;" valign="top" width="204">
 +
  <p>&nbsp;Notes</p>
 +
 +
  </td>
 +
</tr>
 +
<tr>
 +
  <td style="border-width: thin; border-style: dashed;width: 68.3pt;" valign="top" width="91">
 +
  <p>&nbsp;0</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 55.45pt;" valign="top" width="74">
 +
  <p>&nbsp;4</p>
 +
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 142.65pt;" valign="top" width="190">
 +
  <p>&nbsp;Number of samples</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 153pt;" valign="top" width="204">
 +
  <p>&nbsp;&nbsp;</p>
 +
  </td>
 +
</tr>
 +
 +
<tr>
 +
  <td style="border-width: thin; border-style: dashed;width: 68.3pt;" valign="top" width="91">
 +
  <p>&nbsp;4</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 55.45pt;" valign="top" width="74">
 +
  <p>&nbsp;4</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 142.65pt;" valign="top" width="190">
 +
 +
  <p>&nbsp;Position of
 +
  script</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 153pt;" valign="top" width="204">
 +
  <p>&nbsp;Zero-based byte
 +
  position</p>
 +
  </td>
 +
</tr>
 +
<tr>
 +
  <td style="border-width: thin; border-style: dashed;width: 68.3pt;" valign="top" width="91">
 +
 +
  <p>&nbsp;8</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 55.45pt;" valign="top" width="74">
 +
  <p>&nbsp;4</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 142.65pt;" valign="top" width="190">
 +
  <p>&nbsp;Length of script</p>
 +
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 153pt;" valign="top" width="204">
 +
  <p>&nbsp;Length in bytes</p>
 +
  </td>
 +
</tr>
 +
<tr>
 +
  <td style="border-width: thin; border-style: dashed;width: 68.3pt;" valign="top" width="91">
 +
  <p>&nbsp;12</p>
 +
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 55.45pt;" valign="top" width="74">
 +
  <p>&nbsp;4</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 142.65pt;" valign="top" width="190">
 +
  <p>&nbsp;Position of first
 +
  sample</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 153pt;" valign="top" width="204">
 +
 +
  <p>&nbsp;Zero-based byte
 +
  position</p>
 +
  </td>
 +
</tr>
 +
<tr>
 +
  <td style="border-width: thin; border-style: dashed;width: 68.3pt;" valign="top" width="91">
 +
  <p>&nbsp;16</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 55.45pt;" valign="top" width="74">
 +
 +
  <p>&nbsp;4</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 142.65pt;" valign="top" width="190">
 +
  <p>&nbsp;Length of first
 +
  sample</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 153pt;" valign="top" width="204">
 +
  <p>&nbsp;Length in bytes</p>
 +
 +
  </td>
 +
</tr>
 +
<tr>
 +
  <td style="border-width: thin; border-style: dashed;width: 68.3pt;" valign="top" width="91">
 +
  <p>&nbsp;20</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 55.45pt;" valign="top" width="74">
 +
  <p>&nbsp;4</p>
 +
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 142.65pt;" valign="top" width="190">
 +
  <p>&nbsp;Position of
 +
  second sample</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 153pt;" valign="top" width="204">
 +
  <p>&nbsp;Zero-based byte
 +
  position</p>
 +
  </td>
 +
</tr>
 +
 +
<tr>
 +
  <td style="border-width: thin; border-style: dashed;width: 68.3pt;" valign="top" width="91">
 +
  <p>&nbsp;…</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 55.45pt;" valign="top" width="74">
 +
  <p>&nbsp;…</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 142.65pt;" valign="top" width="190">
 +
 +
  <p>&nbsp;…</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 153pt;" valign="top" width="204">
 +
  <p>&nbsp;&nbsp;</p>
 +
  </td>
 +
</tr>
 +
<tr>
 +
  <td style="border-width: thin; border-style: dashed;width: 68.3pt;" valign="top" width="91">
 +
 +
  <p>&nbsp;N * 8 + 8</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 55.45pt;" valign="top" width="74">
 +
  <p>&nbsp;4</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;width: 142.65pt;" valign="top" width="190">
 +
  <p>&nbsp;Position of last sample</p>
 +
  </td>
 +
 +
  <td style="border-width: thin; border-style: dashed;width: 153pt;" valign="top" width="204">
 +
  <p>&nbsp;N is the number
 +
  of samples</p>
 +
  </td>
 +
</tr>
 +
<tr>
 +
  <td style="border-width: thin; border-style: dashed;width: 68.3pt;" valign="top" width="91">
 +
  <p>&nbsp;N * 8 + 12</p>
 +
  </td>
 +
 +
  <td style="border-width: thin; border-style: dashed;width: 55.45pt;" valign="top" width="74">
 +
  <p>&nbsp;Variable</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;" valign="top" width="190">
 +
  <p>&nbsp;First sample</p>
 +
  </td>
 +
  <td style="border-width: thin; border-style: dashed;" valign="top" width="204">
 +
  <p>&nbsp;Followed by the
 +
  rest of the samples</p>
 +
  </td>
 +
 +
</tr>
 +
</table>
 +
==Script Encryption==
 +
The music scripts of MNG files are saved in a 'scrambled' format, encrypted with an XOR function. This function works on a byte level, with a starting operand value of 0x5 and an increment of 0xC1. Sample routines follow - as XOR is a reversible operation, they can be used to both scramble and descramble scripts.
 +
 +
===Visual Basic Routine===
 +
<pre>
 +
Private Function Scramble(ByVal data As Byte()) As Byte()
 +
  Dim hb as Byte, count as Integer
 +
  hb = 5
 +
  For count = 0 to data.Length – 1
 +
    data(count) = data(count) Xor hb
 +
    If hb < &H3F Then
 +
      hb = CByte(hb + &HC1)
 +
    Else
 +
      hb = Cbyte(hb + (&HC1 - &H100))
 +
    End If
 +
  Next count
 +
  Return data
 +
End Function
 +
</pre>
 +
 +
===Assembly Routine===
 +
<pre>
 +
.text:00543400 sub_0_543400    proc near              .text:00543400
 +
.text:00543400 arg_0          = dword ptr  8
 +
.text:00543400 arg_4          = dword ptr  0Ch
 +
.text:00543400
 +
.text:00543400                push    ebp
 +
.text:00543401                mov    ebp, esp
 +
.text:00543403                push    esi
 +
.text:00543404                mov    esi, [ebp+arg_4]
 +
.text:00543407                xor    eax, eax
 +
.text:00543409                mov    cl, 5
 +
.text:0054340B                test    esi, esi
 +
.text:0054340D                jle    short loc_0_543424
 +
.text:0054340F                mov    edx, [ebp+arg_0]
 +
.text:00543412                push    ebx
 +
.text:00543413
 +
.text:00543413 loc_0_543413:  ; CODE XREF: sub_0_543400+21
 +
.text:00543413                mov    bl, [eax+edx]
 +
.text:00543416                xor    bl, cl
 +
.text:00543418                add    cl, 0C1h
 +
.text:0054341B                mov    [eax+edx], bl
 +
.text:0054341E                inc    eax
 +
.text:0054341F                cmp    eax, esi
 +
.text:00543421                jl      short loc_0_543413
 +
.text:00543423                pop    ebx
 +
.text:00543424
 +
.text:00543424 loc_0_543424:  ; CODE XREF: sub_0_543400+D
 +
.text:00543424                pop    esi
 +
.text:00543425                pop    ebp
 +
.text:00543426                retn
 +
.text:00543426 sub_0_543400    endp
 +
</pre>
  
 
==DJ_G==
 
==DJ_G==

All contributions to Creatures Wiki are considered to be released under the CC-BY-SA and GFDL (see Creatures Wiki:Copyrights for details).

To edit this page, please answer the question that appears below (more info):

Cancel Editing help (opens in new window)