'
' DS4QB++ 2D sound demo
'

DEFINT A-Z

'$INCLUDE: 'DEXTERN.BI'

CONST PI = 3.14859
CONST SoundCount = 6
CONST TurnSpeed = 15
CONST MoveSpeed = 5
CONST Radius = 8
CONST WaveLength = 10
CONST WaveCount = 3

TYPE SoundDisp
 X         AS DOUBLE
 Y         AS DOUBLE
 Angle     AS INTEGER
 WaveFrame AS INTEGER
END TYPE

DIM Cosine(360) AS DOUBLE, Sine(360) AS DOUBLE
DIM SoundDisps(SoundCount) AS SoundDisp, Ear AS SoundDisp

FOR Angle = 1 TO 360
 Cosine(Angle) = COS(Angle * PI / 180)
 Sine(Angle) = SIN(Angle * PI / 180)
NEXT

RANDOMIZE TIMER

FOR I = 1 TO SoundCount
 SoundDisps(I).X = INT(RND * (320 - Radius * 2)) + Radius
 SoundDisps(I).Y = INT(RND * (200 - Radius * 2)) + Radius
 SoundDisps(I).Angle = INT(RND * 360) + 1
NEXT

R = DS4QB.Init(CURRENT, DEFAULT)

IF R = -1 THEN
  PRINT "Error! SOUNDSYS.DAT is missing!": END
 ELSEIF R = -3 THEN
  PRINT "Error! Please run SETUP.EXE!": END
 ELSEIF R = -2 THEN
  PRINT "Error! Could not connect to slave!": END
END IF

FOR I = 1 TO SoundCount
 RawExtract "Sounds.raw", I, "Temp.mp3"
 DS4QB.LoadSound I, "Temp.mp3", DEFAULT
NEXT

KILL "Temp.mp3"

DS4QB.Set2DDistFactor DEFAULT

Ear.X = 160
Ear.Y = 100
Ear.Angle = 90

SCREEN 7, , 1, 0

FOR I = 16 TO 24
 OUT &H3C8, I
 OUT &H3C9, I * 8
 OUT &H3C9, I * 8
 OUT &H3C9, I * 8
NEXT

DO

 Ky = INP(&H60)

 FOR I = 1 TO 16
  Dump$ = INKEY$
 NEXT

 SELECT CASE Ky
  CASE 2 TO SoundCount + 1
   IF TIMER - KeyWait# > .25 OR SoundNum <> LSoundNum THEN
    SoundNum = Ky - 1
    SoundDisps(SoundNum).WaveFrame = WaveLength
    DS4QB.Add2DSound SoundNum, (SoundDisps(SoundNum).X), (SoundDisps(SoundNum).Y), (SoundDisps(SoundNum).Angle), CURRENT, CURRENT
    KeyWait# = TIMER
    LSoundNum = SoundNum
   END IF
  CASE &H4B
   Ear.Angle = Ear.Angle - TurnSpeed
   IF Ear.Angle < 1 THEN Ear.Angle = 360 + Ear.Angle
  CASE &H4D
   Ear.Angle = Ear.Angle + TurnSpeed
   IF Ear.Angle > 360 THEN Ear.Angle = (Ear.Angle MOD 360) + 1
  CASE &H48
   Ear.X = Ear.X + Cosine(Ear.Angle) * MoveSpeed
   Ear.Y = Ear.Y + Sine(Ear.Angle) * MoveSpeed
  CASE &H50
   Ear.X = Ear.X - Cosine(Ear.Angle) * MoveSpeed
   Ear.Y = Ear.Y - Sine(Ear.Angle) * MoveSpeed
  CASE &H1
   EXIT DO
 END SELECT

 IF Ear.X > 320 + Radius THEN Ear.X = -Radius
 IF Ear.X < -Radius THEN Ear.X = 320 + Radius
 IF Ear.Y > 200 + Radius THEN Ear.Y = -Radius
 IF Ear.Y < -Radius THEN Ear.Y = 200 + Radius

 CLS

 FOR I = 1 TO SoundCount
  IF SoundDisps(I).WaveFrame THEN
   FOR J = -(1 / 3) * WaveCount TO (1 / 3) * WaveCount
    CIRCLE (SoundDisps(I).X, SoundDisps(I).Y), Radius + (16 - SoundDisps(I).WaveFrame) + 4 * J, SoundDisps(I).WaveFrame * (8 / WaveLength) + 8
   NEXT
   SoundDisps(I).WaveFrame = SoundDisps(I).WaveFrame - 1
  END IF
 
  CIRCLE (SoundDisps(I).X, SoundDisps(I).Y), Radius, 1
  LINE (SoundDisps(I).X, SoundDisps(I).Y)-STEP(Cosine(SoundDisps(I).Angle) * Radius, Sine(SoundDisps(I).Angle) * Radius), 15
 NEXT

 CIRCLE (Ear.X, Ear.Y), Radius, 4
 LINE (Ear.X, Ear.Y)-STEP(Cosine(Ear.Angle) * Radius, Sine(Ear.Angle) * Radius), 15

 LOCATE 1, 1: PRINT " [1] - [6] : Play sound [#]"

 Time# = TIMER
 WHILE TIMER = Time#: WEND

 PCOPY 1, 0

 DS4QB.Play2DSounds (Ear.X), (Ear.Y), (Ear.Angle)

LOOP

DS4QB.Close

