Added pcspk.h for PC Speaker and notes.h that contains notes and their frequencies

This commit is contained in:
2024-05-19 17:05:53 +02:00
parent 347bf17586
commit dcbf24beae
4 changed files with 170 additions and 3 deletions
+2 -2
View File
@@ -67,8 +67,8 @@ run:
-drive format=raw,file="./kern.iso",index=0,media=disk\ -drive format=raw,file="./kern.iso",index=0,media=disk\
-m 256M\ -m 256M\
-accel tcg\ -accel tcg\
-rtc base=localtime,clock=host,driftfix=slew -rtc base=localtime,clock=host,driftfix=slew\
# -machine pcspk-audiodev=test -audiodev coreaudio,id=audio0 -machine pcspk-audiodev=audio0
# Clean all build files # Clean all build files
clean: clean:
+108
View File
@@ -0,0 +1,108 @@
#ifndef NOTES_H
#define NOTES_H
/*
0 1 2 3 4 5 6 7 8
C 16.35 32.7 65.41 130.81 261.63 523.25 1046.5 2093 4186
C# 17.32 34.65 69.3 138.59 277.18 554.37 1108.73 2217.46 4434.92
D 18.35 36.71 73.42 146.83 293.66 587.33 1174.66 2349.32 4698.63
D# 19.45 38.89 77.78 155.56 311.13 622.25 1244.51 2489 4978
E 20.6 41.2 82.41 164.81 329.63 659.25 1318.51 2637 5274
F 21.83 43.65 87.31 174.61 349.23 698.46 1396.91 2793.83 5587.65
F# 23.12 46.25 92.5 185 369.99 739.99 1479.98 2959.96 5919.91
G 24.5 49 98 196 392 783.99 1567.98 3135.96 6271.93
G# 25.96 51.91 103.83 207.65 415.3 830.61 1661.22 3322.44 6644.88
A 27.5 55 110 220 440 880 1760 3520 7040
A# 29.14 58.27 116.54 233.08 466.16 932.33 1864.66 3729.31 7458.62
B 30.87 61.74 123.47 246.94 493.88 987.77 1975.53 3951 7902.13
*/
// Note frequencies in Hz (rounded down)
// b - Flat
// s - Sharp (#)
typedef enum {
// 2nd Octave
C2 = 65,
Cs2 = 69,
Db2 = Cs2,
D2 = 73,
Ds2 = 77,
Eb2 = Ds2,
E2 = 82,
F2 = 87,
Fs2 = 92,
Gb2 = Fs2,
G2 = 98,
Gs2 = 103,
Ab2 = Gs2,
A2 = 110,
As2 = 116,
Hb2 = As2,
H2 = 123,
// 3rd Octave
C3 = 130,
Cs3 = 138,
Db3 = Cs3,
D3 = 146,
Ds3 = 155,
Eb3 = Ds3,
E3 = 164,
F3 = 174,
Fs3 = 185,
Gb3 = Fs3,
G3 = 196,
Gs3 = 207,
Ab3 = Gs3,
A3 = 220,
As3 = 233,
Hb3 = As3,
H3 = 246,
// 4th Octave
C4 = 261,
Cs4 = 277,
Db4 = Cs4,
D4 = 293,
Ds4 = 311,
Eb4 = Ds4,
E4 = 329,
F4 = 349,
Fs4 = 370,
Gb4 = Fs4,
G4 = 392,
Gs4 = 415,
Ab4 = Gs4,
A4 = 440, // Standard A, 440Hz
As4 = 466,
Hb4 = As4,
H4 = 493,
// 5th Octave
C5 = 523,
Cs5 = 554,
Db5 = Cs5,
D5 = 587,
Ds5 = 622,
Eb5 = Ds5,
E5 = 659,
F5 = 698,
Fs5 = 739,
Gb5 = Fs5,
G5 = 783,
Gs5 = 830,
Ab5 = Gs5,
A5 = 880,
As5 = 932,
Hb5 = As5,
H5 = 987,
} note_t;
typedef enum {
WHOLE = 1,
HALF = 2,
QUARTER = 4,
EIGHT = 8,
SIXTEENTH = 16,
THIRTYSECONDTH = 32,
} beatType_t;
#endif
+32
View File
@@ -0,0 +1,32 @@
#ifndef PCSPK_H
#define PCSPK_H
#include <stdint.h>
#include <sound/notes.h>
#include <interrupt/pit.h>
#include <ports/io.h>
#include <time.h>
#define PC_SPEAKER 0x61
void enableSpeaker() {
uint8_t temp = inb(PC_SPEAKER);
outb(PC_SPEAKER, temp | 0x03); // Set first 2 bits
}
void disableSpeaker() {
uint8_t temp = inb(PC_SPEAKER);
outb(PC_SPEAKER, temp & 0xfc); // Set first 2 bits
}
void playNote(note_t note, uint32_t ms) {
setPitPhase(2, 3, note);
sleep(ms);
}
void rest(uint32_t ms) {
setPitPhase(2, 3, 30000); // Unhearable noise
sleep(ms);
}
#endif
+28 -1
View File
@@ -12,6 +12,8 @@
#include <interrupt/pit.h> #include <interrupt/pit.h>
#include <interrupt/rtc.h> #include <interrupt/rtc.h>
#include <time.h> #include <time.h>
#include <sound/pcspk.h>
#include <sound/notes.h>
#define PROMPT "#> " #define PROMPT "#> "
@@ -183,6 +185,7 @@ void main() {
print(" memmap | Prints the memory map and info\n"); print(" memmap | Prints the memory map and info\n");
print(" reboot | Reboots the system\n"); print(" reboot | Reboots the system\n");
print(" sleep | Sleeps for input number of seconds\n"); print(" sleep | Sleeps for input number of seconds\n");
print(" soundtest | Plays a tune :)\n");
print(" test | Performs tests\n"); print(" test | Performs tests\n");
} else if (strcmp(input, "ls") == 0) { } else if (strcmp(input, "ls") == 0) {
printFiletable(filetable); printFiletable(filetable);
@@ -197,6 +200,30 @@ void main() {
if (ms == 0) continue; if (ms == 0) continue;
sleep(ms); sleep(ms);
} else if (strcmp(input, "soundtest") == 0) {
print("Playing...\n");
enableSpeaker();
/*
* Megalovania - Toby Fox
* |-D-D-d--A---Ab--G--F--D-F-G-| (Goofy ahh notation)
* (The timing may be off)
*/
for (uint8_t i = 0; i < 4; i++) {
note_t first[4] = { D4, C4, H3, Hb3 };
playNote(first[i], 120);
playNote(first[i], 120);
playNote(D5, 240);
playNote(A4, 360);
playNote(Ab4, 240);
playNote(G4, 240);
playNote(F4, 240);
playNote(D4, 120);
playNote(F4, 120);
playNote(G4, 120);
}
disableSpeaker();
} else if (strcmp(input, "test") == 0) { } else if (strcmp(input, "test") == 0) {
print("Running Tests...\n"); print("Running Tests...\n");
uint8_t buf[512]; uint8_t buf[512];
@@ -205,7 +232,7 @@ void main() {
diskRead(0, 1, (char*)buf); diskRead(0, 1, (char*)buf);
if (buf[510] == 0x55 && buf[511] == 0xaa) { if (buf[510] == 0x55 && buf[511] == 0xaa) {
print("[ DONE ]\n"); print("[ DONE ]\n");
} else { } else {
print("[ FAIL ]\n"); print("[ FAIL ]\n");
} }
print("Write Test ......... "); print("Write Test ......... ");