This commit is contained in:
y4my4my4m 2023-09-04 03:06:13 +09:00
parent 77bcef3667
commit db77fc64d9
6 changed files with 96 additions and 23 deletions

View file

@ -107,7 +107,7 @@ I64 AudioSFXPlay(U32 *data, I64 length)
return stream;
for (i = 0; i < length; i++)
FifoI64Ins(audio.output[stream].data, data[i]);
return stream;
 return stream;
}
U0 AC97OutputMix(U32 *buf, I64 length = NULL)
@ -188,6 +188,13 @@ I64 AC97Init()
OutU32(ac97.nabm + GLOBAL_CTL, 0x03);
OutU16(ac97.nam + RESET, 0xFFFF);
// FIXME:
// i set the frequency to 44.1 manually
// DONT DO THIS!!!
OutU16(ac97.nam + EXT_FRONT_RATE, 44100);
// Set PCM Output to Max volume
OutU16(ac97.nam + PCM_VOL, 0x0000);

View file

@ -17,6 +17,8 @@ include_noreindex "Lib/ELF64";
//include_noreindex "Lib/uPNG";
// include_noreindex "Player";
U16 currentRate = InU16(ac97.nam + EXT_FRONT_RATE);
"currentRate: %d\n", currentRate;
#include "Classes"
// include_noreindex "MIDIHandling"

View file

@ -2,9 +2,26 @@
#include "WaveformGen"
U0 AudioPlayNote(U8 note, U8 velocity, U8 instrument) {
U32 buffer[SAMPLE_RATE];
// ultimately, we should use AC97OutputMix to be multi channel
//U32 buffer[SAMPLE_RATE];
U32 *buffer;
I64 bufferSize;
if (instrument == SAMPLE)
{
bufferSize = 132300;
buffer = MAlloc(bufferSize * sizeof(U32));
}
else
{
bufferSize = SAMPLE_RATE;
buffer = MAlloc(bufferSize * sizeof(U32));
}
F64 freq = MidiToFreq(note);
// GenerateSineWave(buffer, SAMPLE_RATE, freq, velocity);
switch(instrument) {
case PULSE1:
@ -20,7 +37,7 @@ U0 AudioPlayNote(U8 note, U8 velocity, U8 instrument) {
GenerateNoiseWave(buffer, SAMPLE_RATE, freq, velocity);
break;
case SAMPLE:
PlaySample(buffer, SAMPLE_RATE, velocity);
PlaySample(buffer, 132300, velocity);
break;
default:
GenerateSineWave(buffer, SAMPLE_RATE, freq, velocity);
@ -31,7 +48,28 @@ U0 AudioPlayNote(U8 note, U8 velocity, U8 instrument) {
// Play the buffer using the AC97 driver:
AudioSFXPlay(buffer, SAMPLE_RATE);
"Instrument: $$LTGREEN$$ %d $$DKGRAY$$|$$FG$$ Note: $$YELLOW$$%d$$FG$$\n", instrument, note;
U8 *instrument_name = "DEFAULT";
if (instrument == SAMPLE) instrument_name = "SAMPLE";
if (instrument == PULSE1) instrument_name = "PULSE1";
if (instrument == PULSE2) instrument_name = "PULSE2";
if (instrument == TRIANGLE) instrument_name = "TRIANGLE";
if (instrument == NOISE) instrument_name = "NOISE";
"Instrument: $$LTGREEN$$ %s $$DKGRAY$$|$$FG$$ Note: $$YELLOW$$%d$$FG$$\n", instrument_name, note;
Free(buffer);
}
U0 PlayPattern(Pattern *pattern) {
I64 row;
NoteCell *cell;
for (row = 0; row < TRACK_LENGTH; row++) {
cell = &pattern->cells[row];
if (cell->note) {
AudioPlayNote(cell->note, cell->velocity, cell->instrument);
}
//if (cell->instrument == SAMPLE) Sleep(2000);
Sleep(1000); // Adjust for tempo
}
}
U0 EnterPattern(Pattern *pattern) {
@ -48,21 +86,9 @@ U0 EnterPattern(Pattern *pattern) {
}
}
U0 PlayPattern(Pattern *pattern) {
I64 row;
NoteCell *cell;
for (row = 0; row < TRACK_LENGTH; row++) {
cell = &pattern->cells[row];
if (cell->note) {
AudioPlayNote(cell->note, cell->velocity, cell->instrument);
}
Sleep(2900); // Adjust for tempo
}
}
U0 MusicTracker() {
LoadSample("~/Tracker/Samples/Sample.WAV");
LoadSample("~/Tracker/Samples/OrchHit.WAV");
Song song;

Binary file not shown.

Binary file not shown.

View file

@ -39,7 +39,7 @@ U0 GeneratePulse2Wave(U32 *buffer, I64 length, F64 freq, U8 velocity) {
for (i = 0; i < length; i++) {
I16 sample_value;
if (phase < 0.5 * PI) {
if (phase < 0.5 * PI) {
sample_value = -ToI64(amplitude);
} else {
sample_value = ToI64(amplitude);
@ -132,7 +132,7 @@ U0 LoadSample(U8 *filename) {
}
gSampleSize = fileSize - sizeof(WAVHeader);
//gSampleData = MAlloc(gSampleSize);
//gSampleData = MAlloc(gSampleSize);
//U8 *buffer = FileRead(filename, &fileSize);
@ -162,6 +162,7 @@ U0 LoadSample(U8 *filename) {
// DEBUG
//gSampleSize = sizeof(fileContent) - 36;
//gSampleSize = fileSize - 36;
//U8 *audioDataStart = fileContent + 36; // Move pointer after header MANUAL 36 bytes
//MemCopy(gSampleData, audioDataStart, 36);
@ -169,6 +170,7 @@ U0 LoadSample(U8 *filename) {
//Free(fileContent);
// test without skipping header
gSampleData = FileRead(filename, &gSampleSize);
// Print some info about the sample
@ -185,11 +187,47 @@ U0 PlaySample(U32 *buffer, I64 duration, U8 velocity) {
return;
}
// Just as an example: copy loaded sample to buffer
// This step will depend on how you are processing and playing the audio
I64 samplesToCopy = Min(gSampleSize, duration); // don't overflow the buffer
I64 samplesToCopy = Min(gSampleSize, duration); // don't overflow the buffer
MemCopy(buffer, gSampleData, samplesToCopy);
//I64 numSamples = gSampleSize / 2;
//I64 numStereoPairs = numSamples / 2;
//I64 pairsToCopy = Min(numStereoPairs, duration);
// Here you'd typically send the buffer to your audio playing routine
//I64 i;
//for (i = 0; i < pairsToCopy; i++) {
// I64 leftSample = (gSampleData[4*i] | (gSampleData[4*i + 1] << 8));
// I64 rightSample = (gSampleData[4*i + 2] | (gSampleData[4*i + 3] << 8));
// buffer[i] = (leftSample << 16) | (rightSample & 0xFFFF);
//}
// speed is almost good but right ear only...
//I64 numSamples = gSampleSize / 2;
//I64 samplesToCopy = Min(numSamples, duration); // don't overflow the buffer
//I64 i;
//for (i=0; i < samplesToCopy; i++)
//{
// I64 sample = (gSampleData[2 * i] | (gSampleData[2 *i + 1] << 8));
// buffer[i] = sample << 16;
//}
//F64 playbackStep = 1.0; // 1.0 means original speed. <1.0 is slower, >1.0 is faster.
//F64 playbackPointer = 0.0;
//I64 i;
//for (i = 0; i < duration; i++) {
// if (playbackPointer < gSampleSize) {
// buffer[i] = gSampleData[ToI64(playbackPointer)]; // get the sample at the rounded-down playback pointer
// playbackPointer += playbackStep;
// } else {
// buffer[i] = 0; // or whatever value represents silence in your buffer
// }
//}
}
// I64 sample_rate = SAMPLE_RATE // whatever your sample rate is