Guidance
指路人
g.yi.org
software / rapidq / Examples / Audio & Video / soundgenerator.bas

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
'##
'##  A simple sound generator
'##
'##  By Premysl Janouch a.k.a. [W]arriant, August 2006
'##  warriant@gmail.com
'##
'##

'## internal PlayWav can't play files from memory
     DECLARE FUNCTION PlaySound LIB "winmm.dll" ALIAS "PlaySoundA" (lpszName AS LONG, hModule AS LONG, dwFlags AS LONG) AS LONG


     DECLARE SUB play

     DIM soundstream AS QMEMORYSTREAM
     application.title = "Test"

     CREATE form AS QFORM
      clientheight = 85'45
      clientwidth = 120
      center
      borderstyle = 3
      CAPTION = "Sound"

      CREATE label AS QLABEL
       top     = 5
       left    = 5
       width   = 100
       height  = 15
       CAPTION = "Volume:"
      END CREATE
      CREATE trackbar AS QTRACKBAR
       top      = 20
       left     = 5
       width    = 110
       height   = 22
       tickstyle = 0
       max      = 100
       position = 90
      END CREATE

      CREATE playbutton AS QCOOLBTN
       CAPTION = "Play"
       top     = 55
       left    = 5
       height  = 25
       width   = 110
       onclick = play
      END CREATE
     END CREATE

     form.SHOWMODAL


     SUB play
      playbutton.CAPTION = "Wait..."
      DOEVENTS

	'## stop sound and delete it
      playsound(0, 0, 0)
      soundstream.CLOSE

      soundstream.writestr("RIFF", 4) '## indicates wave file header chunk
      soundstream.writenum(640032, 4) '## total length of package to follow, 4 bytes

      soundstream.writestr("WAVE", 4)

      soundstream.writestr("fmt ", 4) '## begin format chunk
      soundstream.writenum(16, 4)     '## length of format chunk
      soundstream.writenum(1,  2)     '## WAVE_FORMAT_PCM
      soundstream.writenum(2,  2)     '## channels 1-mono 2-stereo
      soundstream.writenum(44100,  4) '## sample rate in Hz
      soundstream.writenum(176400, 4) '## bytes per second (samplerate * channels * bytespersample)
      soundstream.writenum(2,  2)     '## bytes per sample
      soundstream.writenum(16, 2)     '## bits per sample

      soundstream.writestr("data", 4) '## begin data chunk
      soundstream.writenum(640000, 4) '## length of data to follow

	'## generate a sound
	'## 160000 samples * 2 bytes * 2 channels = 640000 bytes
      FOR t = 1 TO 160000
		'## I know, it isn't very nice, but it's simple
		'## use "+" instead of "xor" for clean mixing of frequencies
       soundstream.writenum((COS(t / 51) XOR SIN(t / 150)) * (t / 160000)            * (163.83 * trackbar.position), 2) '## left channel
       soundstream.writenum((SIN(t / 51) XOR COS(t / 150)) * ((160000 - t) / 160000) * (163.83 * trackbar.position), 2) '## right channel
      NEXT

	'## play from memory
      playsound(soundstream.pointer, 0, 5)

      playbutton.CAPTION = "Play"
     END SUB

掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Thu 2024-5-30  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2010-05-30 09:37:50