Announcement

Collapse
No announcement yet.

Sound in PB ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sound in PB ?

    I want to experiment with some sound in PB .
    Used to play with it in older Basic programs.
    But ... don't find a command for it in the indexes .
    Have I overlooked something ?

  • #2
    This isn't a sound statement, but a way to play files.
    Code:
    #DIM ALL
    #COMPILE EXE 'playwave.bas
    DECLARE FUNCTION PlaySound LIB "WINMM.DLL" ALIAS "PlaySoundA" (lpszName AS ASCIIZ, BYVAL hModule AS DWORD, BYVAL dwFlags AS DWORD) AS LONG
    %NULL = 0
    %SND_SYNC = &H0 ' play synchronously (default)
    FUNCTION PBMAIN AS LONG
      LOCAL zWav AS ASCIIZ * 256
      zwav = COMMAND$
      IF LEN(zwav) = 0 THEN zwav = "\windows\media\tada.wav"
      CALL PlaySound(zWav, %NULL ,%SND_SYNC) 'use sync so sound file completes
    END FUNCTION

    Comment


    • #3
      Sound

      Thanks for the answer already . So no statements in PB directly to generate sound ?
      To be more specific : I don't want to play wave or .wav files but want to generate a sound given a dataset of frequencies , duration and volume .
      To be more specific : I want to write code to generate the sound observed when a bunch of music sources of unique frequency approach to the listener . Does "PlaySound " allows this ?

      Comment


      • #4
        Semen Matusovski wrote a complete replacement for the old DOS Sound but it only plays through the speaker, the assembler portion of the code is for 9X so probably not needed now. You will find it here
        http://www.powerbasic.com/support/pb...ad.php?t=22549

        Comment


        • #5
          Frank,
          to do what you want you need to create the WAV in memory then play it. Here's an example.

          Paul.
          Code:
          'PBWin7.02 and PB/CC1.0
          'generate DTMF tones as a WAV and play them
          
          DECLARE SUB DTMF(dial$)
          
          FUNCTION PBMAIN()
          
          dtmf("141 800 555 1212")  'dial the number 141 800 555 1212. Invalid characters are ignored
          
          END FUNCTION
          
          
          
          '####### the DTMF routines #########
          'Extracted from WinAPI
          %SND_MEMORY         = &H4         ' lpszSoundName points to a memory file
          DECLARE FUNCTION PlaySound LIB "WINMM.DLL" ALIAS "PlaySoundA" (lpszName AS ASCIIZ, BYVAL hModule AS DWORD, BYVAL dwFlags AS DWORD) AS LONG
          
          'define types
          TYPE WAVEheader
                          rID              AS STRING*4  ' Contains the characters "RIFF"
                          rLen             AS LONG      ' The length of the data in the next chunk
                          wID              AS STRING*4  ' Contains the characters "WAVE"
                          fId              AS STRING*4  ' Contains the characters "fmt "
                          fLen             AS LONG      ' Length of data in the format chunk
                          wFormatTag       AS INTEGER   ' specifies the wave format, eg 1 = Pulse Code Modulation
                          nChannels        AS INTEGER   ' Number of channels, 1=mono, 2=stereo
                          nSamplesPerSec   AS LONG      ' Playback frequency
                          nAvgBytesPerSec  AS LONG      ' Indicates the average number of bytes a second the data should be
                                                        ' transferred at = nChannels * nSamplesPerSec * (nBitsPerSample / 8)
                          nBlockAlign      AS INTEGER   ' Indicates the block alignment of the data in the data chunk. Software
                                                        ' needs to process a multiplt of nBlockAlign at a time.
                                                        ' nBlockAlign = nChannels * (nBitsPerSample / 8)
                          wBitsPerSample   AS INTEGER   ' Format specific data area
                          dId              AS STRING*4  ' Contains the characters "data"
                          dLen             AS LONG      ' Length of data in the dData field
                      END TYPE
          
          TYPE onesecwav
               wv AS waveheader
               dta AS ASCIIZ *8000
               END TYPE
          
          GLOBAL w AS waveheader
          GLOBAL ww AS onesecwav
          
          
          
          
          SUB DTMF(dial$)
          REM dial$ is the string to dial
          DIM p AS BYTE PTR ,q AS LONG PTR
          DIM LoBand(3) AS LONG, HiBand(3) AS LONG
          
          duration&=100  '100ms per digit, spec says >40. Must not be >1000 unless buffer is made bigger.
          scale#=2*3.14159265358979323#/8000
          OldLetter$=" "
          
          'specify the frequency pairs
          LoBand(0)=697
          LoBand(1)=770
          LoBand(2)=852
          LoBand(3)=941
          
          HiBand(0)=1209
          HiBand(1)=1336
          HiBand(2)=1477
          HiBand(3)=1633
          
          code$="147*2580369#ABCD"
          Samples&=duration&*8000/1000
          
                          w.rID             = "RIFF"
                          w.rLen            = 36+Samples&
                          w.wID             = "WAVE"
                          w.fId             = "fmt "
                          w.fLen            = 16
                          w.wFormatTag      = 1
                          w.nChannels       = 1 'nChannels
                          w.nSamplesPerSec  = 8000 'nSamplesPerSec
                          w.nAvgBytesPerSec = 8000  '44100  '88200 'nChannels * nSamplesPerSec * (wBitsPerSample / 8)
                          w.nBlockAlign     = 1 ' nChannels * (wBitsPerSample / 8)
                          w.wBitsPerSample  = 8 'wBitsPerSample
                          w.dId             = "data"
                          w.dLen            = Samples&
          
          ww.wv=w
          p=VARPTR(ww.dta)
          q=VARPTR(ww)
          UDial$=UCASE$(dial$)
          
          FOR digit& = 1 TO LEN(Udial$)
              index&=INSTR(code$,MID$(UCASE$(dial$),digit&,1))-1
              IF index&<>-1 THEN
                  freq1&=LoBand(index& AND &h03)
                  freq2&=HiBand(index& \ 4)
                  'generate the tone samples
                  FOR r& = 0 TO Samples&-1
                       @p[r&]=50*(SIN(r&*scale#*freq1&) + SIN(r&*scale#*freq2&)) +128
                       NEXT
          
                  playsound BYVAL q,0 ,%SND_MEMORY ':REM play from memory,
                  SLEEP 40   'ensure a gap between digits
                  END IF
          
                  NEXT
          
          END SUB

          Comment


          • #6
            Sound

            Thanks for the code . The first tunes already come up !
            Any news about Semen ?

            Comment

            Working...
            X