proper stereo osc

This commit is contained in:
y4my4my4m 2023-09-06 01:50:30 +09:00
parent 59d8ba00d8
commit 7d75cfe958
2 changed files with 103 additions and 108 deletions

View file

@ -51,15 +51,9 @@ U0 AudioPlayNote(U8 note, U8 velocity, U8 instrument) {
//ApplyEnvelope(buffer, SAMPLE_RATE); // TODO: Apply fade in and fade out
//I64 i;
//for (i = 0; i < bufferSize; i++) {
// Copy data to the shared oscilloscope buffer
//gSharedBuffer[i] = buffer[i];
//}
UpdateSharedBuffer(buffer, SAMPLE_RATE/winmgr.fps);
 gSharedBuffer = buffer;
gSharedBufferSize = bufferSize;
gSharedBufferPosition = 0; // Resetting the position
// Play the buffer using the AC97 driver:
AudioSFXPlay(buffer, bufferSize);
// silly debug print

View file

@ -1,12 +1,19 @@
// #define CHUNK_SIZE 1470 // (44100 / 30) // 30 FPS
// winmgr.fps
// Globals
//#define CHUNK_SIZE 1470
#define SAMPLE_RATE 44100
U32 *gSharedBuffer;
I64 gSharedBufferPosition = 0;
I64 gSharedBufferSize;
I32 GetSignedSample(I64 index) {
U32 sample = gSharedBuffer[index];
I32 signedValue;
signedValue = sample - 0x80000000; // Convert U32 to centered I32
return signedValue;
}
// Initialize shared buffer
U0 InitSharedBuffer(I64 size)
{
@ -15,58 +22,51 @@ U0 InitSharedBuffer(I64 size)
gSharedBufferPosition = 0;
}
// Update shared buffer in your audio playback function
U0 UpdateSharedBuffer(U32 *buffer, I64 size)
U0 DrawIt(CTask *task, CDC *dc)
{
I64 i;
for (i = 0; i < size; i++)
I64 CHUNK_SIZE = SAMPLE_RATE/winmgr.fps;
F64 snd_vol = 1; // effectively the height of the lines?
I64 i, x1, y1, x2, y2, startIndex, endIndex,
cy = task->pix_height >> 1;
if(gSharedBufferPosition >= gSharedBufferSize) return; // If we reached the end of the buffer, don't draw.
// Start drawing from the current position
startIndex = gSharedBufferPosition;
endIndex = gSharedBufferPosition + CHUNK_SIZE;
// If endIndex goes beyond the buffer, adjust it.
if(endIndex > gSharedBufferSize) endIndex = gSharedBufferSize;
dc->color = BLACK;
x2 = 0; y2 = cy;
for (i = startIndex; i < endIndex; i += 2) // Assuming stereo, hence i+=2
{
gSharedBuffer[gSharedBufferPosition] = buffer[i];
gSharedBufferPosition = (gSharedBufferPosition + 1) % gSharedBufferSize;
I32 signedValue = GetSignedSample(i);
x1 = (i - startIndex) * task->pix_width / CHUNK_SIZE;
y1 = cy - (signedValue * cy / snd_vol / I32_MAX);
GrLine(dc, x2, y2, x1, y1);
x2 = x1; y2 = y1;
}
// I64 i;
// for (i = 0; i < size; i++) {
// gSharedBuffer[gSharedBufferPosition] = buffer[i];
// gSharedBufferPosition = (gSharedBufferPosition + 1) % gSharedBufferSize;
// }
}
U0 OscilloscopeDraw(CTask *task, CDC *dc)
{
I64 i, y;
I64 CHUNK_SIZE = SAMPLE_RATE / winmgr.fps;
// Start from the most recent sample and display CHUNK_SIZE samples
I64 startPosition = (gSharedBufferPosition - CHUNK_SIZE + gSharedBufferSize) % gSharedBufferSize;
for (i = 0; i < CHUNK_SIZE; i++)
dc->color = PURPLE;
x2 = 0; y2 = cy;
for (i = startIndex + 1; i < endIndex; i += 2)
{
I16 sample_value = (gSharedBuffer[startPosition] & 0xFFFF) - 32768;
F64 normalized_sample = ToF64(sample_value) / 32768.0;
y = 300 / 2 - (normalized_sample * 300 / 2);
GrPlot(dc, i, y);
startPosition = (startPosition + 1) % gSharedBufferSize;
I32 signedValueG = GetSignedSample(i);
x1 = (i - startIndex -1) * task->pix_width / CHUNK_SIZE;
y1 = cy - (signedValueG * cy / snd_vol / I32_MAX);
GrLine(dc, x2, y2, x1, y1);
x2 = x1; y2 = y1;
}
// I64 i, y;
// //DCClear(dc);
// I64 startPosition = gSharedBufferPosition;
// for (i = 0; i < 400; i++) {
// I16 sample_value = (gSharedBuffer[startPosition] & 0xFFFF) - 32768; // extract left channel and center around 0
// F64 normalized_sample = ToF64(sample_value) / 32768.0;
// y = 300 / 2 - (normalized_sample * 300 / 2);
// GrPlot(dc, i, y); // Using GrPlot instead of DrawPoint
// startPosition = (startPosition + 1) % gSharedBufferSize;
// }
gSharedBufferPosition += CHUNK_SIZE; // Move forward by the chunk size
//Sleep(10);
}
U0 StartOscilloscope()
{
I64 ch, sc;
@ -76,65 +76,55 @@ U0 StartOscilloscope()
U8 *name = "Oscilloscope";
CTask *task = Spawn(&ServerCmdLine, NULL, name,, Fs);
StrCopy(task->task_title, name);
task->title_src = TTS_LOCKED_CONST;
task->border_src = BDS_CONST;
task->border_attr = LTGREEN << 4 + WHITE;
// this permits the task not to clog MusicTracker
TaskExe(task, Fs, ";", 1 << JOBf_WAKE_MASTER | 1 << JOBf_FREE_ON_COMPLETE);
TaskWait(task);
StrCopy(task->task_title, name);
task->text_attr = DKGRAY << 4 + BLUE;
task->title_src = TTS_LOCKED_CONST;
task->border_src = BDS_CONST;
task->border_attr = TRANSPARENT << 4 + WHITE;
//WinBorder(OFF,task);
DocClear(task->border_doc, TRUE);
DocCursor(OFF, task->display_doc);
//DocClear(task);
WinHorz((TEXT_COLS / 2) - 32, (TEXT_COLS / 2) + 32, task);
WinVert((TEXT_ROWS / 2) - 16, (TEXT_ROWS / 2) + 16, task);
WinVert((TEXT_ROWS / 2) - 8, (TEXT_ROWS / 2) + 8, task);
task->win_inhibit = WIG_NO_FOCUS_TASK_DEFAULT;
// WinHorz(task->win_left, task->win_left + 50, task);
// WinVert(2, 2 + 8, task);
// task->animate_task = Spawn(&OscilloscopeAnimate, NULL, "Animate",, task);
task->draw_it = &OscilloscopeDraw; // Set the draw callback
// DocPut(task)->max_entries = 100;
// CCtrl *oscWindow = OscWindow;
// windowed
// oscWindow->win_task->win_left = (GR_WIDTH / 23);
// oscWindow->win_task->win_right = (GR_WIDTH / 13);
// oscWindow->win_task->win_top = GR_HEIGHT / 80;
// oscWindow->win_task->win_bottom = GR_HEIGHT / 10;
//task->animate_task = Spawn(&AnimateTask, NULL, "Animate",, Fs);
task->draw_it = &DrawIt; // Set the draw callback
// SettingsPush(); // Push current settings
//Fs->win_width = 400; // Width of the oscilloscope window
//Fs->win_height = 300; // Height of the oscilloscope window
// WinHorz((TEXT_COLS / 2) - 32, (TEXT_COLS / 2) + 32, Fs);
// WinVert((TEXT_ROWS / 2) - 16, (TEXT_ROWS / 2) + 16, Fs);
// oscWindow->win_task->border_src = BDS_CONST;
// oscWindow->win_task->border_attr = LTGREEN << 4 + WHITE;
// StrCopy(oscWindow->win_task->task_title, "Oscilloscope");
//Spawn("Oscilloscope", OscilloscopeTask); // Start the oscilloscope task
// Fs->draw_it = &OscilloscopeDraw; // Set the draw callback
//try
//{
// do
// switch (ch = KeyGet(&sc))
// {
// case '\n':
// "dood\n";
// break;
// }
// while (ch != CH_ESC && ch != CH_SHIFT_ESC);
//}
//catch
// PutExcept;
// OscWindowDel(c);
// SettingsPop(); // Pop settings
}
//StartOscilloscope;
// U0 OscilloscopeDraw(CTask *task, CDC *dc)
// {
// I64 i, y;
// I64 CHUNK_SIZE = SAMPLE_RATE / winmgr.fps;
// // Start from the most recent sample and display CHUNK_SIZE samples
// I64 startPosition = (gSharedBufferPosition - CHUNK_SIZE + gSharedBufferSize) % gSharedBufferSize;
// for (i = 0; i < CHUNK_SIZE; i++)
// {
// I16 sample_value = (gSharedBuffer[startPosition] & 0xFFFF) - 32768;
// F64 normalized_sample = ToF64(sample_value) / 32768.0;
// y = 300 / 2 - (normalized_sample * 300 / 2);
// GrPlot(dc, i, y);
// startPosition = (startPosition + 1) % gSharedBufferSize;
// }
// }
// U0 OscilloscopeAnimate()
// {
// while (TRUE)
@ -175,4 +165,15 @@ U0 StartOscilloscope()
// QueueRemove(c);
// Free(c);
// }
//StartOscilloscope;
// Update shared buffer in your audio playback function
//U0 UpdateSharedBuffer(U32 *buffer, I64 size)
//{
// I64 i;
// for (i = 0; i < size; i++)
// {
// gSharedBuffer[gSharedBufferPosition] = buffer[i];
// gSharedBufferPosition = (gSharedBufferPosition + 1) % gSharedBufferSize;
// }
//}