Skip to content

ThePseudo/Music_for_Landtiger

Repository files navigation

Music_for_Landtiger

This project for Keil uVision plays the first 4 beats of Toby Fox's Megalovania. Enjoy.

Audio Management in the Code

Code Structure

The provided code handles the playback of musical notes using hardware timers. The main file of interest for audio management is music.c, which defines the playNote() method to play a note.

music.c

In the music.c file, we find the function:

void playNote(NOTE note)
{
    if(note.freq != REST)
    {
        reset_timer(0);
        init_timer(0, note.freq * AMPLIFIER * VOLUME);
        enable_timer(0);
    }
    reset_timer(1);
    init_timer(1, note.duration);
    enable_timer(1);
}

This function performs the following operations:

  1. Checks if the note is not a REST (pause).
  2. If the note needs to be played:
    • Resets timer 0.
    • Initializes timer 0 with a frequency modified by AMPLIFIER and VOLUME.
    • Enables timer 0.
  3. Sets timer 1 to control the duration of the note.
  4. Enables timer 1.

The audio intensity can be adjusted by modifying the value of the VOLUME constant in the music.h file.

music.h

In the music.h file, we find the definition of constants used for sound control:

#define TIMER_FREQUENCY 25000000  // Timer frequency at 25 MHz
#define NOTE_DIVISOR 45   // Divisor for note frequency calculation

#define TIMERSCALER 1  // Scale factor for time adjustment
#define SECOND 0x17D7840 * TIMERSCALER  // Representation of one second

#define VOLUME 8  // Controls sound amplification

#ifdef SIMULATOR
    #define SPEEDUP 1.4f  // Increases speed in simulation
    #define AMPLIFIER 2  // Amplifies sound in simulation
#else
    #define SPEEDUP 1.0f  // Normal speed
    #define AMPLIFIER 1  // No amplification
#endif

The VOLUME constant is used in init_timer() to adjust the intensity of the audio output:

init_timer(0, note.freq * AMPLIFIER * VOLUME);

Increasing the VOLUME value amplifies the sound produced by the timer.

Adjusting the Volume

To increase the volume of the sound generated by the system, simply increment the value of the VOLUME constant in the music.h file:

#define VOLUME 12  // Increases the signal amplitude

Warning: A value that is too high may cause distortion in the reproduced sound. It is recommended to test different values to find the optimal level.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published