Generate a tone .pb example ?

Just starting out? Need help? Post your questions and find answers here.
vmars316
Enthusiast
Enthusiast
Posts: 360
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Generate a tone .pb example ?

Post by vmars316 »

TIA ,
How can I generate a tone (a specific Frequency , perhaps A 440 ) ?
I see Frequency examples , but they come from a .ogg file .
Can't find any docs for Beep .
A simple example like:
[Play Tone] Button , [Stop Play] Button .
vmars.us Win11 x64 , Martin 000-16 (1995)
"All things in moderation , except for love and forgiveness."
infratec
Always Here
Always Here
Posts: 5878
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Generate a tone .pb example ?

Post by infratec »

Maybe something like that:

https://www.purebasic.fr/english/viewto ... 06#p467606

Code: Select all

Structure RIFFStructure
  Riff.a[4]
  Length.l
  Wave.a[4]
EndStructure


Structure fmtStructure
  fmt.a[4]
  Length.l
  Format.u
  Channels.u
  SampleRate.l
  BytesPerSecond.l
  BlockAlign.u
  BitsPerSample.u
EndStructure


Structure dataStructure
  Signature.a[4]
  Length.l
EndStructure



Procedure.i CreateSine24BitMonoWAV(SampleRate.i, Freq.i)
  
  Protected.i i, n, Points, HeaderSize, DataSize, File, Value
  Protected.d StepWidth, Angle
  Protected *WAVBuffer, *RiffPtr.RIFFStructure, *fmtPtr.fmtStructure, *dataPtr.dataStructure, *audioPtr
  
  
  Points = SampleRate / Freq
  
  Debug "Points per wave: " + Str(Points)
  
  HeaderSize = SizeOf(RIFFStructure)
  HeaderSize + SizeOf(fmtStructure)
  HeaderSize + SizeOf(dataStructure)
  
  DataSize + (Points * 3)
  
  *WAVBuffer = AllocateMemory(HeaderSize + DataSize)
  If *WAVBuffer
    
    *RiffPtr = *WAVBuffer
    PokeS(@*RiffPtr\Riff, "RIFF", 4, #PB_Ascii|#PB_String_NoZero)
    *RiffPtr\Length = HeaderSize + DataSize - 8
    PokeS(@*RiffPtr\Wave, "WAVE", 4, #PB_Ascii|#PB_String_NoZero)
    
    *fmtPtr = *WAVBuffer + SizeOf(RIFFStructure)
    PokeS(@*fmtPtr\fmt, "fmt ", 4, #PB_Ascii|#PB_String_NoZero)
    *fmtPtr\Length = SizeOf(fmtStructure) - 8
    *fmtPtr\Format = 1
    *fmtPtr\Channels = 1
    *fmtPtr\SampleRate = SampleRate
    *fmtPtr\BitsPerSample = 24
    *fmtPtr\BlockAlign = *fmtPtr\Channels * ((*fmtPtr\BitsPerSample + 7) / 8)
    *fmtPtr\BytesPerSecond = *fmtPtr\SampleRate * *fmtPtr\BlockAlign
    
    *dataPtr = *WAVBuffer + SizeOf(RIFFStructure) + SizeOf(fmtStructure)
    PokeS(@*dataPtr\Signature, "data", 4, #PB_Ascii|#PB_String_NoZero)
    *dataPtr\Length = DataSize
    
    *audioPtr = *WAVBuffer + SizeOf(RIFFStructure) + SizeOf(fmtStructure) + SizeOf(dataStructure) ; just behind the wav header
    
    StepWidth = 2 * #PI / Points
    For n = 0 To Points - 1
      Value = Sin(Angle) * 8385000  ; max. $7FFFFF -> 100% better 80% -> 6710880 ($666660)
      PokeA(*audioPtr + 2, (Value >> 16) & $FF)
      PokeA(*audioPtr + 1, (Value >> 8) & $FF)
      PokeA(*audioPtr + 0, (Value) & $FF)
      Angle + StepWidth
      *audioPtr + 3
    Next n
    
  EndIf
  
  ProcedureReturn *WAVBuffer
  
EndProcedure



If InitSound()
  
  *Buffer = CreateSine24BitMonoWAV(48000, 440)
  If *Buffer
    
;     If CreateFile(0, "Sine24BitMono.wav")
;       WriteData(0, *Buffer, MemorySize(*Buffer))
;       CloseFile(0)
;     EndIf
    
    Sound = CatchSound(#PB_Any, *Buffer)
    If Sound
      PlaySound(Sound, #PB_Sound_Loop)
      Delay(3000)
      StopSound(Sound)
    EndIf
    FreeMemory(*Buffer)
  EndIf
EndIf
Or simply:
https://docs.microsoft.com/en-us/window ... piset-beep

Code: Select all

Beep_(440, 3000)
But you can not stop Beep_(). So my solution is more what you want.
vmars316
Enthusiast
Enthusiast
Posts: 360
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: Generate a tone .pb example ?

Post by vmars316 »

Thank you very much , infratec !

Your "Re: Audio MultiTone 48000 Hz @ 24 Bit" Answer is quite interesting also .
viewtopic.php?p=467606#p467606

Thanks
vmars.us Win11 x64 , Martin 000-16 (1995)
"All things in moderation , except for love and forgiveness."
infratec
Always Here
Always Here
Posts: 5878
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Generate a tone .pb example ?

Post by infratec »

If you save the file to disk and open it with Audacity, you can 'see' the wave.
vmars316
Enthusiast
Enthusiast
Posts: 360
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: Generate a tone .pb example ?

Post by vmars316 »

Thanks again !
vmars.us Win11 x64 , Martin 000-16 (1995)
"All things in moderation , except for love and forgiveness."
Post Reply