TGC Codebase Backup



Media player style visualisations from a sound by David T

20th Sep 2003 8:47
Summary

Demo using memblocks and sound. Produces a WMP-like visualisation from any sound. Commented.



Description

Thanks to Van-B for the basics on how to read sounds from memblocks.

The code works by taking the elapsed and running time of the sound. Then we wrk out what the interval between memblock chunks is for one millisecond. Move along the required number of chunks then get the sound value at that point to create a visualisation.

Code is commented.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    rem LOAD SOUND + MAKE VISUALISATIONS
rem David Tattersall / David89 Jul 2003


rem Load the sound to be used
sync rate 0
`load sound "C:\Documents and Settings\David\My Documents\Media\Music and Audio\3KP2.wav",1
load sound "C:\Program Files\Dark Basic Software\Dark Basic Professional\Media\Music\wav\music fun loop.wav",1

rem Make memblocks
sync rate 0 : make memblock from sound 1,1 : sndlen=get memblock size(1)

Dim OutBits(1000)

do

  play sound 1 : start = timer()

  do


    rem Get length of sound in SECONDS
    secs = int((sndlen / (sound speed(1))))

    rem Get the elapsed time in MILLISECONDS
    elapsed = timer()-start

    rem Get the stepvalue between each millisecond
    stepval# = (sndlen / secs)/1000

    rem Get the current position based on stepvalue
    p = int(elapsed * stepval#)

    rem Do not get info if past end of sound
    if sound playing(1)=0 then p = sndlen
    if p < sndlen-int(stepval#*10) then psnd=memblock word(1,p)

    rem Divide result by 256
    psnd = psnd/256

    rem Get ready to draw the visualisation
    ink rgb(255,255,255),0

    rem Draw circle
    goSub Calculate
    goSub Fade
    goSub Draw

    rem Repeat
    if spacekey()=1 then exit

    rem Time elapsed in centre
    ink rgb(255-cr,255-cg,255-cb),0
    s$ = MStoSTR(elapsed)
    if sound playing(1)
    center text 320,240-(text height(s$)/2),s$
    endif

    rem Update screen
    sync

  loop

loop

Calculate:

  ` Rainbow colours
  inc i : `sin seed
  cr = int(125 + sin(i) * 125)
  if cg = 0 then inc cr,5
  if cb = 0 then inc cg,5
  inc cb,5

  if cr > 255 then cr = 0
  if cg > 255 then cg = 0
  if cb > 255 then cb = 0


  ink rgb(cr,cg,cb),0
  r = curvevalue(psnd,r,10)
  OutBits(r) = rgb(cr,cg,cb)

return

Fade:

  for rad = 0 to 1000

    col = outbits(rad)

    o_r = rgbr(col)
    o_g = rgbg(col)
    o_b = rgbb(col)

    dec o_r,5
    dec o_g,5
    dec o_b,5

    if o_r < 0 then o_r = 0
    if o_g < 0 then o_g = 0
    if o_b < 0 then o_b = 0

    outbits(rad) = rgb(o_r,o_g,o_b)

  next rad

return

Draw:

  for rad = 0 to 1000

    col = outbits(rad)

    ink col,0
    circle 320,240,rad

  next rad

return

rem Converts milliseconds to a string mm:ss
Function MSToStr(t)

  ` Get seconds
  t = int(t / 1000)

  ` Get mins
  while t => 60
    dec t,60
    inc mins
  endwhile

  ` Add any 0s
  if len(str$(t))=1 then add$ = "0"

  `Return string
  time$ = str$(mins)+":"+add$+str$(t)

Endfunction time$