[Solved] Sound advise

Post your Gambas programming questions here.
AndyGable
Posts: 390
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

[Solved] Sound advise

Post by AndyGable »

Hi Everyone

so I need someone who is smarter then me (so that is everyone lol)

I have a slight issue that I am not sure how to sort out

I can run this command with in Gambas

Dim SpeakerData As String = Null
SpeakerData = "speaker-test -t sine -f 1000 -l 1 & sleep .2 && kill -9 $!"
Shell SpeakerData 


and it works fine but when I run

 shell "play -n synth 0.1 sine 880 vol 5"


i get nothing but when run the play command with in a terminal i get a nice loud beep

SO am I doing something wrong here or am i missing a command that i need to use?

Any ideas are MOST welcomed
Last edited by AndyGable on Sunday 8th October 2023 11:26am, edited 1 time in total.
User avatar
BruceSteers
Posts: 1874
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Sound advise

Post by BruceSteers »

seems play does not release/end the shell process as it needs an stdout.

try it with & at the end of the command or running 'Shell sCommand for Output' ...

shell "play -n synth 0.1 sine 880 vol 5 &"


Or this....
shell "play -n synth 0.1 sine 880 vol 5" For Output
If at first you don't succeed , try doing something differently.
BruceS
vuott
Posts: 302
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Sound advise

Post by vuott »

......why not just use the resources of Gambas ? :roll:

You need to activate gb.openal Component:
Private Const AMPLITUDO As Integer = 127
Private Const FRECUENTIA As Integer = 880
Private Const DIGIT As Integer = 44100
Private Const TEMPUS As Single = 0.15


Public Sub Main()
 
  Dim disp As AlcDevice
  Dim cont As AlcContext
  Dim src, buffer As Integer[]
  Dim err As Boolean
  Dim data As New Byte[]
  
' Configures the device and the audio context with the "Alc" Class:
   disp = Alc.OpenDevice(Null)
   cont = Alc.CreateContext(disp)
  
   err = cont.MakeCurrent()
   If err = False Then Error.Raise("Error !")
  
   src = Al.GenSources(1)

' Configures the audio buffer:
   buffer = Al.GenBuffers(1)
  
   Wave(data)
  
' The audio data is loaded into the audio buffer:
   Al.BufferData(buffer[0], 4352, data.Data, data.Count, DIGIT)
  
' Connects the audio buffer to the audio source:
   Al.Sourcei(src[0], Al.BUFFER, buffer[0])
  
' Play the audio source:
   Al.SourcePlay(src[0])
  
' Allows performance for the entire duration of the sound wave:
   Wait TEMPUS
  
' Releases the memory:
   Al.DeleteBuffers(buffer)
   Al.DeleteSources(src)
   Alc.DestroyContext(cont)
   Alc.CloseDevice(disp)
  
End


Private Function Wave(bb As Byte[])    ' Creates the sine wave audio data
 
  Dim i As Integer
  
   For i = 0 To (TEMPUS * 2 * DIGIT) - 1
     bb.Push(CByte(128 + AMPLITUDO * Sin(CFloat(i / DIGIT * FRECUENTIA * (2 * Pi)))))
   Next

End


or by using gb.media resources:
Public Sub Main()
 
  Dim pl As New MediaPipeline
  Dim src, snk As MediaControl
  
  src = New MediaControl(pl, "audiotestsrc")
  src["freq"] = "880"
  src["wave"] = 0     ' A value of 0 allows a sine wave to be reproduced
  snk = New MediaControl(pl, "autoaudiosink")
 
  src.LinkTo(snk)
  
  pl.Play()
 
  Wait 0.15
  
  pl.Close
 
End
Last edited by vuott on Monday 2nd October 2023 11:26am, edited 1 time in total.
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
cogier
Site Admin
Posts: 1160
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Sound advise

Post by cogier »

Dim SpeakerData As String = Null
SpeakerData = "speaker-test -t sine -f 1000 -l 1 & sleep .2 && kill -9 $!"
Shell SpeakerData 
There is no need for = Null, you could have just followed the equal sign with "speaker - test -t.....". Better still: -
Shell "speaker-test -t sine -f 1000 -l 1 & sleep .2 && kill -9 $!"

Which works for me
************************************
shell "play -n synth 0.1 sine 880 vol 5"
You are correct, it doesn't work, and I have no idea why.

If you want to make sounds in Gambas, I suggest you make a recoding of what you want or get one from the internet, there are plenty of sites with free sounds you can download. You can then use code as below: -

Music.Load(User.Home &/ "MySound.ogg") ''Requires gb.sdl2.audio
Music.Play  
AndyGable
Posts: 390
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Sound advise

Post by AndyGable »

cogier wrote: Sunday 1st October 2023 12:42pm
Dim SpeakerData As String = Null
SpeakerData = "speaker-test -t sine -f 1000 -l 1 & sleep .2 && kill -9 $!"
Shell SpeakerData 
There is no need for = Null, you could have just followed the equal sign with "speaker - test -t.....". Better still: -
Shell "speaker-test -t sine -f 1000 -l 1 & sleep .2 && kill -9 $!"

Which works for me
************************************
shell "play -n synth 0.1 sine 880 vol 5"
You are correct, it doesn't work, and I have no idea why.

If you want to make sounds in Gambas, I suggest you make a recoding of what you want or get one from the internet, there are plenty of sites with free sounds you can download. You can then use code as below: -

Music.Load(User.Home &/ "MySound.ogg") ''Requires gb.sdl2.audio
Music.Play  
The problem with the above is the systems I am using do not have any sound cards on them they only have the PC speaker.
User avatar
thatbruce
Posts: 237
Joined: Saturday 4th September 2021 11:29pm

Re: Sound advise

Post by thatbruce »

Try:
play -V3 -n synth 0.1 sine 880 vol 0.4

vol 5 means 5 times the normal volume which clips to a DC voltage.
vuott
Posts: 302
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Sound advise

Post by vuott »

AndyGable wrote: Sunday 1st October 2023 5:20pm ...do not have any sound cards on them they only have the PC speaker.
Have you tried the :? old ways...
Print "\x07"

and
Shell "printf '\x07'"

or this by using the external ioctl() function:
Library "libc:6"

Private Const KIOCSOUND As Integer = &4B2F

' int ioctl(int __fd, unsigned long int __request, ...)
' Perform the I/O control operation specified by REQUEST on FD.
Private Extern ioctl(__fd As Integer, __request As Long, arg As Long) As Integer


Public Sub Main()

  Dim freq As Short[] = [523, 587, 659, 698, 784, 880, 988, 1046]
  Dim fl As File
  Dim i, err As Integer
  
  Shell "echo MY_PASSWORD | sudo -S chmod 666 '/dev/tty0'" Wait
  
  fl = Open "/dev/tty0" For Write
  
  For i = 0 To 7
    err = ioctl(fl.Handle, KIOCSOUND, 1193180 / freq[i])
    If err == -1 Then Error.Raise("Error !")
    Wait 200
    err = ioctl(fl.Handle, KIOCSOUND, 0)
    If err == -1 Then Error.Raise("Error !")
  Next
  
  fl.Close

End
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
AndyGable
Posts: 390
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Sound advise

Post by AndyGable »

thatbruce wrote: Monday 2nd October 2023 2:15am Try:
play -V3 -n synth 0.1 sine 880 vol 0.4

vol 5 means 5 times the normal volume which clips to a DC voltage.
I tried this one and I got the following show up in the console of the Gambas enveroment.

play WARN alsa: can't encode 0-bit Unknown or not applicable
play:      SoX v14.4.2
play INFO nulfile: sample rate not specified; using 48000

Input File     : '' (null)
Channels       : 1
Sample Rate    : 48000
Precision      : 32-bit


Output File    : 'default' (alsa)
Channels       : 1
Sample Rate    : 48000
Precision      : 32-bit
Sample Encoding: 32-bit Signed Integer PCM
Endian Type    : little
Reverse Nibbles: no
Reverse Bits   : no

play INFO sox: effects chain: input        48000Hz  1 channels
play INFO sox: effects chain: synth        48000Hz  1 channels
play INFO sox: effects chain: vol          48000Hz  1 channels
play INFO sox: effects chain: output       48000Hz  1 channels


and no sound
Last edited by AndyGable on Monday 2nd October 2023 1:54pm, edited 1 time in total.
AndyGable
Posts: 390
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Sound advise

Post by AndyGable »

vuott wrote: Monday 2nd October 2023 10:44am Have you tried the :? old ways...
Print "\x07"

and
Shell "printf '\x07'"

or this by using the external ioctl() function:
Library "libc:6"

Private Const KIOCSOUND As Integer = &4B2F

' int ioctl(int __fd, unsigned long int __request, ...)
' Perform the I/O control operation specified by REQUEST on FD.
Private Extern ioctl(__fd As Integer, __request As Long, arg As Long) As Integer


Public Sub Main()

  Dim freq As Short[] = [523, 587, 659, 698, 784, 880, 988, 1046]
  Dim fl As File
  Dim i, err As Integer
  
  Shell "echo MY_PASSWORD | sudo -S chmod 666 '/dev/tty0'" Wait
  
  fl = Open "/dev/tty0" For Write
  
  For i = 0 To 7
    err = ioctl(fl.Handle, KIOCSOUND, 1193180 / freq[i])
    If err == -1 Then Error.Raise("Error !")
    Wait 200
    err = ioctl(fl.Handle, KIOCSOUND, 0)
    If err == -1 Then Error.Raise("Error !")
  Next
  
  fl.Close

End
Yes I did try the above the \x07 just flashed the screen at me and no sound and the other asked for my password and then errored out at Error !
vuott
Posts: 302
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Sound advise

Post by vuott »

Uhmmm... some time ago, stevedee proposed using the "Screen" Class of "gb.ncurses" Component.
Try:
Public Sub Main()

  Screen.Beep()

End
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
Post Reply