I'm trying to make working my Sound Blaster sound card using FreeBASIC. I saw that there are many QB programs which do that using INP/OUT statements only. I took one of such program, modified it to be FB-compatible, and tried to generate some sounds.
I do get some noise from my sound card, but no "clear" sound.
Here's the code:
Code: Select all
DECLARE FUNCTION RDTSC() AS ULONGINT
DECLARE SUB uSLEEP(dur AS INTEGER)
DIM SHARED AS INTEGER x, f
DIM SHARED AS ULONGINT CpuClock
DIM SHARED AS DOUBLE HoldTimer
REM *** Calculating CPU freq
PRINT " * Calculating CPU frequency... ";
HoldTimer = TIMER
CpuClock = RDTSC()
DO: LOOP UNTIL TIMER - HoldTimer >= 1
CpuClock = (RDTSC() - CpuClock)
PRINT CpuClock \ 1000000; "MHz"
REM *** START OF RESET SECTION
OUT &H226, 1
uSLEEP(5) ' Should wait 5 us
OUT &H226, 0
DO
x = INP(&H22E)
IF x AND 128 THEN
x = INP(&H22A)
IF x = &HAA THEN
' PRINT "reset!"
EXIT DO
END IF
END IF
LOOP
PRINT " * DSP reset done. Press any key to turn the speaker ON..."
DO: SLEEP: LOOP UNTIL INKEY <> ""
REM *** TURN SPEAKER ON
DO
x = INP(&H22C)
LOOP WHILE x AND 128
OUT &H22C, &HD1 ' Send "speaker ON" code
PRINT " * Speaker turned ON. Press any key to send the signal..."
DO: SLEEP: LOOP UNTIL INKEY <> ""
PRINT " * Generating signal... You should hear it now. Press any key to exit...";
DO
FOR x = 1 TO 1000 ' Start of the loop
OUT &H22C, &H10 ' Prepare the DSP to receive raw data
OUT &H22C, f ' send no.
uSLEEP(10) ' delay (10us)
IF f = 255 THEN f = 0 ELSE f = 255 ' alternate between 0 and 255
NEXT x ' End of the loop
LOOP UNTIL INKEY <> "" ' Exit if any key pressed
END
REM *** END OF THE PROGRAM ***
FUNCTION RDTSC() AS ULONGINT
ASM
rdtsc
mov [Function], eax
mov [Function+4], edx
END ASM
END FUNCTION
SUB uSLEEP(dur AS INTEGER)
DIM Waitloop AS ULONGINT
Waitloop = RDTSC()
DO
LOOP UNTIL (RDTSC() - Waitloop) > (CpuClock / 1000000 * dur)
END SUB