This project for Keil uVision plays the first 4 beats of Toby Fox's Megalovania. Enjoy.
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.
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:
- Checks if the note is not a
REST
(pause). - If the note needs to be played:
- Resets timer 0.
- Initializes timer 0 with a frequency modified by
AMPLIFIER
andVOLUME
. - Enables timer 0.
- Sets timer 1 to control the duration of the note.
- Enables timer 1.
The audio intensity can be adjusted by modifying the value of the VOLUME
constant in the music.h
file.
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.
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.