Peak Volume gfx

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Peak Volume gfx

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by Rings.

'did not work on older SB Cards.

Code: Select all

;VolumePeaker
;(C) 2002 by S.Rings

Global WaveFormat.WaveFormatEx

#WAVE_INVALIDFORMAT = $0                 ;/* invalid format */
#WAVE_FORMAT_1M08 = $1                   ;/* 11.025 kHz, Mono,   8-bit
#WAVE_FORMAT_1S08 = $2                   ;/* 11.025 kHz, Stereo, 8-bit
#WAVE_FORMAT_1M16 = $4                   ;/* 11.025 kHz, Mono,   16-bit
#WAVE_FORMAT_1S16 = $8                   ;/* 11.025 kHz, Stereo, 16-bit
#WAVE_FORMAT_2M08 = $10                  ;/* 22.05  kHz, Mono,   8-bit
#WAVE_FORMAT_2S08 = $20                  ;/* 22.05  kHz, Stereo, 8-bit
#WAVE_FORMAT_2M16 = $40                  ;/* 22.05  kHz, Mono,   16-bit
#WAVE_FORMAT_2S16 = $80                  ;/* 22.05  kHz, Stereo, 16-bit
#WAVE_FORMAT_4M08 = $100                 ;/* 44.1   kHz, Mono,   8-bit
#WAVE_FORMAT_4S08 = $200                 ;/* 44.1   kHz, Stereo, 8-bit
#WAVE_FORMAT_4M16 = $400                 ;/* 44.1   kHz, Mono,   16-bit
#WAVE_FORMAT_4S16 = $800                 ;/* 44.1   kHz, Stereo, 16-bit

#WAVE_FORMAT_PCM = 1

#WHDR_DONE = $1              ;/* done bit */
#WHDR_PREPARED = $2          ;/* set If this header has been prepared */
#WHDR_BEGINLOOP = $4         ;/* loop start block */
#WHDR_ENDLOOP = $8           ;/* loop End block */
#WHDR_INQUEUE = $10          ;/* reserved For driver */

#WIM_OPEN = $3BE
#WIM_CLOSE = $3BF
#WIM_DATA = $3C0

Procedure InitDevices()
  Caps.WaveInCaps
  Which.i
  sdummy.s
  For Which = 0 To waveInGetNumDevs - 1
    Result = waveInGetDevCaps_(Which, Caps, SizeOf(WaveInCaps))
    ;If Caps.Formats And WAVE_FORMAT_1M08 Then
    If Caps\dwFormats And #WAVE_FORMAT_1S08
    ;Now is 1S08 -- Check For devices that can do stereo 8-bit 11kHz
      sdummy.s = sdummy.s + PeekS(@Caps\szPname[0]) + Chr(13)
    EndIf
  Next
  MessageRequester("Info", sdummy, 0)
EndProcedure

WaveFormat.WaveFormatEx

WaveFormat\wFormatTag = #WAVE_FORMAT_PCM
WaveFormat\nChannels = 2   ;Two channels -- left And right
WaveFormat\nSamplesPerSec = 11025   ;11khz
WaveFormat\wBitsPerSample = 8
WaveFormat\nBlockAlign = (WaveFormat\nChannels * WaveFormat\wBitsPerSample) / 8
WaveFormat\nAvgBytesPerSec = WaveFormat\nBlockAlign * WaveFormat\nSamplesPerSec
WaveFormat\cbSize = 0

DevHandle.i
Result = waveInOpen_(@DevHandle, 0, WaveFormat, 0, 0, 0)
If DevHandle = 0 
  MessageRequester("Info", "Wave input device didn't open!", 0)
  End
EndIf

waveInStart_(DevHandle)

Wave.WaveHdr
Dim Indat.b(513)
Wave\lpData = @InDat(0)
Wave\dwBufferLength = 512   ;This is now 512 so there's still 256 samples per channel
Wave\dwFlags = 0

If OpenWindow(0, 100, 200, 256, 256, "Peak Volume gfx", 
              #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
  
  If CreateImage(0, 255, 255)
    
  Else
    Beep_(100, 1000)  
  EndIf
  
  For I = 1 To 10
    ;TextGadget  (i, 10, 10+I*20, 500, 16, "PureBasic - Gadget demonstration")
  Next I
  
  Repeat
    EventID.i = WindowEvent()
    Delay(1)
    
    If EventID = #PB_Event_CloseWindow   ;If the user has pressed on the close button
      Quit = 1
    EndIf
    
    waveInPrepareHeader_(DevHandle, Wave, SizeOf(WaveHdr))
    waveInAddBuffer_(DevHandle, Wave,SizeOf(WaveHdr))
    
    ;Do
    ;  Nothing -- we're waiting For the audio driver To mark
    ;  this wave chunk as done.
    ;Loop Until ((Wave.dwFlags And WHDR_DONE) = WHDR_DONE) Or DevHandle = 0
    
    waveInUnprepareHeader_(DevHandle, Wave, SizeOf(WaveHdr))   
    StartDrawing(WindowOutput(0))
      
      FrontColor(RGB(0, 0, 0))
      Box(0, 0, 256, 256) 
      For I = 0 To 255
        FrontColor(RGB(100, 255, 100))
        ;If Indat(I*2)>-128 And Indat(I*2)-128 And Indat(I*2+1)<127
        ;Line(I, 0, 1,128+Indat(i+256))
        v = Indat(i * 2 + 1)
        FrontColor(RGB(255, 100, 100))   ;a rainbow, from black to pink
        Plot(I, 128 + v)
        ;EndIf 
      Next i
    StopDrawing() 
    
  Until Quit = 1
  
EndIf

;Close all !
waveInReset_(DevHandle)
waveInClose_(DevHandle)

End   ;All the opened windows are closed automatically by PureBasic
Its a long way to the top if you wanna .....CodeGuru
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

Is it supposed to do something, except messing up the graphic on my desktop window(all app. icons disappear, normal fonts turns to bold fonts)?;) I also see that the procedure InitDevices() is never called, is there a clever idea behind this that i can not see :wink:
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.
Is it supposed to do something, except messing up the graphic on my desktop window(all app. icons disappear, normal fonts turns to bold fonts)?;) I also see that the procedure InitDevices() is never called, is there a clever idea behind this that i can not see :wink:
This is a WindowsBasedCode only.
Yes InitDevices is never used, but can be.
This snippets show the actual Volumepeak(it records constantly and examine the buffer).So if you cannot see anything, check your soundcard-configuration or perhaps you use a older soundblaster which did not work in same cases.
This is a simple 15minute Port from a VB-Snippet.If you don't like it,forget it.

Its a long way to the top if you wanna .....CodeGuru
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
Is it supposed to do something, except messing up the graphic on my desktop window(all app. icons disappear, normal fonts turns to bold fonts)?;) I also see that the procedure InitDevices() is never called, is there a clever idea behind this that i can not see :wink:
This is a WindowsBasedCode only.
Yes InitDevices is never used, but can be.
This snippets show the actual Volumepeak(it records constantly and examine the buffer).So if you cannot see anything, check your soundcard-configuration or perhaps you use a older soundblaster which did not work in same cases.
This is a simple 15minute Port from a VB-Snippet.If you don't like it,forget it.

Its a long way to the top if you wanna .....CodeGuru
I have a soundblaster live! so the card isn't old. Do you mean that i have to make some noise on one of the input lines to my card or does this program check what's currently being played back on the sound card and displays that?

Sorry you got offendend by my previous post, wasn't my meaning and i do realise that it was a little on the offensive side. But the snippet did really screw up my windows desktop;)

Also i noticed when i tried to run winamp in parrallel that, well basicly you can't, winamp totally died. Is there some kind of winapi that tells other applications that certain devices is owned by another app?

It's not that i don't like your program, it would just be interesting to get it to work properly here aswell.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

As i say, its a small fast port from VB.
I have a soundblaster live! so the card isn't old. Do you mean that i have to make some noise on one of the input lines to my card or does this program check what's currently being played back on the sound card and displays that?
Yes.Absolutly right, with SB-Cards there were problems(Don't know why)
Sorry you got offendend by my previous post, wasn't my meaning and i do realise that it was a little on the offensive side. But the snippet did really screw up my windows desktop;)
:( yes i wished never posted this code here to prevent your screen.Sorry
but here it works 100% fine.
Also i noticed when i tried to run winamp in parrallel that, well basicly you can't, winamp totally died. Is there some kind of winapi that tells other applications that certain devices is owned by another app?
Don't know.perhaps.The win-Api is deep




Its a long way to the top if you wanna .....CodeGuru
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
:( yes i wished never posted this code here to prevent your screen.Sorry
but here it works 100% fine.
No no, please DO post code here! Why it's not working here has most likely something to do with MS windows or SB(i've had trouble with those before). I'm running WIN98SE, which version are you running?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

i'm using Win2000 with a OnBoardCheapAudio.
If anyone else having trouble with these code, plz post here your results.


Its a long way to the top if you wanna .....CodeGuru
Post Reply