hap
New member
I'm looking at gbsound3.pdf..
Start with duty and frequency of the square wave channels. For sound channel 1, duty is $ff11.6,7 and frequency is $ff13 and $ff14.0,1,2
The duty is basically an 8 bit buffer, where 0 is no sound output (volume=0) and 1 is sound output (volume=insertvalue). There's a timer that's obtained from the 11 bit frequency like this ($ff13 OR $ff14(012)<<8)*4. The timer is decremented each cpu cycle. Whenever the timer reaches zero, it is re obtained, and the current sample is the next bit from the duty (keep a duty counter that goes from 0 to 7, to 0 again). Create a function with a loop, that writes 1 sample per cpu cycle to a buffer big enough for 1 frame (4mhz/idunnotheframerate). Call this function whenever there's a write to any of the relating sound registers, and at the end of the frame.
In pseudocode:
At the end of the frame, downsample and do actual sound output.
Start with duty and frequency of the square wave channels. For sound channel 1, duty is $ff11.6,7 and frequency is $ff13 and $ff14.0,1,2
The duty is basically an 8 bit buffer, where 0 is no sound output (volume=0) and 1 is sound output (volume=insertvalue). There's a timer that's obtained from the 11 bit frequency like this ($ff13 OR $ff14(012)<<8)*4. The timer is decremented each cpu cycle. Whenever the timer reaches zero, it is re obtained, and the current sample is the next bit from the duty (keep a duty counter that goes from 0 to 7, to 0 again). Create a function with a loop, that writes 1 sample per cpu cycle to a buffer big enough for 1 frame (4mhz/idunnotheframerate). Call this function whenever there's a write to any of the relating sound registers, and at the end of the frame.
In pseudocode:
Code:
const int duty[][]={
{0,0,1,0,0,0,0,0},
{0,1,1,0,0,0,0,0},
{0,1,1,1,1,0,0,0},
{1,0,0,1,1,1,1,1}
};
channel1_update {
(cpu_getcycles: i'm assuming you have a cpu cycle downcounter per frame)
for (int cpu_cycles=cpu_getcycles();c1_cycles>cpu_cycles;c1_cycles--) {
c1_timer--;
if (c1_timer<=0) {
c1_duty_counter++;
c1_duty_counter&=7;
c1_sample=duty[$ff11.67][c1_duty_counter];
obtain_timer_from_frequency();
}
c1_buffer[c1_buffer_counter]=c1_sample*volume;
c1_buffer_counter++;
}
}