coding with sound for morse program

Just starting out? Need help? Post your questions and find answers here.
wimapon3
New User
New User
Posts: 6
Joined: Wed Jun 04, 2025 11:30 am

coding with sound for morse program

Post by wimapon3 »

hi folks,
i am trying to make a sending morse coding program with purebasic.
i need 3 simple parts of code

1. gives a sound for 1 second
2. gives a sound for 3 seconds
3. gives no sound for 1 second.
'
i do not come further then:

Code: Select all

InitSound()           ; Initialize Sound system
 UseOGGSoundDecoder()  ; Use ogg files

 ; Loads a sound from a file
 LoadSound(0 ,"geluid.wav")    ; allready existing .wav file of 10 seconds
 
 ; The sound is playing
 PlaySound(0, #PB_Sound_Loop, 20)
 
 MessageRequester("Info", "Ok to stop.")
 
 FreeSound(0) ; The sound is freed
 End
please no complicated tricks....

thanks you very much
User avatar
NicTheQuick
Addict
Addict
Posts: 1510
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: coding with sound for morse program

Post by NicTheQuick »

That's basically the example you can find in the help for `PlaySound()`.
Do you have any experience in coding at all?

And please use the [code][/code]-Tags when you post code in the forum.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 565
Joined: Tue Jan 04, 2011 6:21 pm

Re: coding with sound for morse program

Post by SPH »

That ? :o

Code: Select all

InitSound() ; Initialiser le système sonore
UseOGGSoundDecoder() ; Utiliser les fichiers ogg

; Charger un son à partir d'un fichier
LoadSound(0 ,"geluid.wav") ; fichier .wav déjà existant de 10 secondes

; Le son est en cours de lecture
PlaySound(0, 0, 100)
Delay(300)
StopSound(0)
Delay(300)

PlaySound(0, 0, 100)
Delay(600)
StopSound(0)
Delay(300)

PlaySound(0, 0, 100)
Delay(300)
StopSound(0)
Delay(300)



MessageRequester("Info", "Ok pour arrêter.")

FreeSound(0) ; Le son est libéré
End


!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
wimapon3
New User
New User
Posts: 6
Joined: Wed Jun 04, 2025 11:30 am

Re: coding with sound for morse program

Post by wimapon3 »

Hi nick,
thanks for your reply. I am wimapon, and i am here for some 10 years now. i lost the credentials for wimapon.

so i do have a lot of experience, but i am 80 years old, and my programming style is as for quick basic and gwbasic.

i never use all the fancy pure basic features.......

so i need one blok of code which gives the sound for 1 second.... no stuctures, no other things... hi hi hi

Wim
wimapon3
New User
New User
Posts: 6
Joined: Wed Jun 04, 2025 11:30 am

Re: coding with sound for morse program

Post by wimapon3 »

Hi SPH and Nick,
Thank you very much. This is exacly what i needed!
now i can make my program.

Very nice to get help so quick!

but if i read the help file for "playsound" i can not understantd the command: playsound(0,0,100)
but anyhow, it works nice.

if you like to see my software style of programming see my site:
www.ebl21.nl
radio astronomie
experiment 5
the software
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: coding with sound for morse program

Post by Marc56us »

Hi,

If you just want to make Morse code and use Windows, use the Beep_ command: it's faster and lighter than loading a sound file.

This is the classic Beep or Sound command that exists in many languages, but not natively in PB (why?), so we use the system API. Beep_ (A command suffixed with an underscore is a direct call to the API)

Beep_ (Frequency, Duration)
Duration in milliseconds

Code: Select all

Beep_(2000, 300)
Beep_(2000, 300)
Beep_(2000, 300)

Delay(500)

Beep_(2000, 800) : Delay(200)
Beep_(2000, 800) : Delay(200)
Beep_(2000, 800) : Delay(200)

Delay(500)

Beep_(2000, 300)
Beep_(2000, 300)
Beep_(2000, 300)
PS.
Delay wouldn't be the right command to use because it pauses the program completely, so here it's just for an example.
User avatar
idle
Always Here
Always Here
Posts: 5855
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: coding with sound for morse program

Post by idle »

This might work for you, it's not complicated but decoding it would be!

edit: a dot is 1 sound unit and dash is 3 sound units and prosign eg <AA> has no delay between chars.

Code: Select all

#dot = 1 
#dash = 2 

Structure morse 
  code.u
  count.u 
EndStructure 

Global Dim amorse.morse(128) 

For a = 0 To 128 
   amorse.morse(a)\code = $ffff 
Next 
amorse('<')\code = 0
amorse('>')\code = 0 

Procedure Addcode(symbol,a,b=0,c=0,d=0,e=0,f=0,g=0) 
    
  amorse(symbol)\code = a-1  
  If b 
    amorse(symbol)\code << 1 | (b-1)
    amorse(symbol)\count=1
  EndIf 
  If c 
    amorse(symbol)\code << 1 | (c-1) 
    amorse(symbol)\count=2
  EndIf 
  If d 
    amorse(symbol)\code << 1 | (d-1)
    amorse(symbol)\count=3
  EndIf 
  If e 
    amorse(symbol)\code << 1 | (e-1)
    amorse(symbol)\count=4
  EndIf 
  If f 
    amorse(symbol)\code << 1 | (f-1)
    amorse(symbol)\count=5
  EndIf 
  If g 
    amorse(symbol)\code << 1 | (g-1)
    amorse(symbol)\count=6
  EndIf 
EndProcedure   

Addcode('A',#dot,#dash) 
Addcode('B',#dash,#dot,#dot,#dot)
Addcode('C',#dash,#dot,#dash,#dot)
Addcode('D',#dash,#dot,#dot)
Addcode('E',#dot)
Addcode('F',#dot,#dot,#dash,#dot)
Addcode('G',#dash,#dash,#dot)
Addcode('H',#dot,#dot,#dot,#dot)
Addcode('I',#dot,#dot)
Addcode('J',#dot,#dash,#dash,#dash) 
Addcode('K',#dash,#dot,#dash)
Addcode('L',#dot,#dash,#dot,#dot)
Addcode('M',#dash,#dash) 
Addcode('N',#dash,#dot)
Addcode('O',#dash,#dash,#dash)
Addcode('P',#dot,#dash,#dash,#dot)
Addcode('Q',#dash,#dash,#dot,#dash)
Addcode('R',#dot,#dash,#dot)
Addcode('S',#dot,#dot,#dot)
Addcode('T',#dash)
Addcode('U',#dot,#dot,#dash)
Addcode('V',#dot,#dot,#dot,#dash)
Addcode('W',#dot,#dash,#dash)
Addcode('X',#dash,#dot,#dot,#dash)
Addcode('Y',#dash,#dot,#dash,#dash)
Addcode('Z',#dash,#dash,#dot,#dot)
Addcode('0',#dash,#dash,#dash,#dash,#dash)
Addcode('1',#dot,#dash,#dash,#dash,#dash)
Addcode('2',#dot,#dot,#dash,#dash,#dash)
Addcode('3',#dot,#dot,#dot,#dash,#dash)
Addcode('4',#dot,#dot,#dot,#dot,#dash)
Addcode('5',#dot,#dot,#dot,#dot,#dot) 
Addcode('6',#dash,#dot,#dot,#dot,#dot) 
Addcode('7',#dash,#dash,#dot,#dot,#dot) 
Addcode('8',#dash,#dash,#dash,#dot,#dot)
Addcode('9',#dash,#dash,#dash,#dash,#dot)

Addcode('&',#dot,#dash,#dot,#dot,#dot)
Addcode(39, #dot,#dash,#dash,#dash,#dash,#dot)
Addcode('@',#dot,#dash,#dash,#dot,#dash,#dot) 
Addcode(')',#dash,#dot,#dash,#dash,#dot,#dash)
Addcode('(',#dash,#dot,#dash,#dash,#dot)
Addcode(':',#dash,#dash,#dash,#dot,#dot,#dot)
Addcode(',',#dash,#dash,#dot,#dot,#dash,#dash)
Addcode('=',#dash,#dot,#dot,#dot,#dash)
Addcode('!',#dash,#dot,#dash,#dot,#dash,#dash)
Addcode('.',#dot,#dash,#dot,#dash,#dot,#dash)
Addcode('-',#dash,#dot,#dot,#dot,#dot,#dash) 
Addcode('*',#dash,#dot,#dot,#dash)
Addcode('+',#dot,#dash,#dot,#dash,#dot)
Addcode(34,#dot,#dash,#dot,#dot,#dash,#dot)
Addcode('?',#dot,#dot,#dash,#dash,#dot,#dot)
Addcode('/',#dash,#dot,#dot,#dash,#dot) 

Procedure playcode(msg.s,speed) 
  Protected *char.Unicode =@Msg 
  Protected ct,v,out.s,bdelay=1    
  
  While *char\u <> 0 
    
    If amorse(*char\u)\code <> $ffff 
     
      If *char\u = '<'  ;if Prosign no delay between chars  
        bdelay = 0 
        *char+2
        Continue 
      ElseIf *char\u  = '>' 
        bdelay = 1
        *char+2 
        Continue
      EndIf   
      
      ct = amorse(*char\u)\count 
      
        For a = 0 To amorse(*char\u)\count 
          v = ((amorse(*char\u)\code >> ct) & 1) + 1
          ct-1 
          If v = 1 
            out+"."
          Else 
            out + "-"
            v=3 
          EndIf   
          Beep_(550,v*speed)
          Delay(speed)
        Next
        Debug Chr(*char\u) + " " + out
        out = "" 
        If bdelay 
          Delay(speed*3)
        EndIf 
       
    Else   
      If *char\u = 32 
      Debug " " 
      Delay(speed*7)
      EndIf 
    EndIf 
    *char+2  
  Wend 
  
EndProcedure   

playcode(UCase("Testing morse in purebasic 123 <AA> <SOS> <HH> <0/0> !"),50) 


wimapon3
New User
New User
Posts: 6
Joined: Wed Jun 04, 2025 11:30 am

Re: coding with sound for morse program

Post by wimapon3 »

hi folks,
NicTheQuick, SPH, Marc56us and idle,
thank you very much for your help.

I made my program with your help,, and it is working very nice.

i will open a new threat for the question "how to make a .mpe3 file from the output"

anyhow thank you a 1000 times.

Wim
wimapon3
New User
New User
Posts: 6
Joined: Wed Jun 04, 2025 11:30 am

Re: coding with sound for morse program

Post by wimapon3 »

How can i put < SOLVED> in the title ??
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: coding with sound for morse program

Post by Marc56us »

wimapon3 wrote: Thu Jun 05, 2025 7:26 am How can i put < SOLVED> in the title ??
You can edit your articles (and their titles) by clicking on the “pencil” icon in the top right-hand corner.
If you are unable to do so, click on the exclamation mark (still top right) and this will alert the moderators who will make the change.
You can also delete one of your article by clicking on the cross icon (as long as it's the last item in the list).
User avatar
idle
Always Here
Always Here
Posts: 5855
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: coding with sound for morse program

Post by idle »

I updated my previous code the delay between letters was wrong it should be three units.
A dot is one unit of time a dash is three
The space between a code is one unit
The space between letters of a sequence is 3 units
The space between words is 7 units.

I was trying to code this with a 2 year old running around creating mayhem.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 565
Joined: Tue Jan 04, 2011 6:21 pm

Re: coding with sound for morse program

Post by SPH »

I seem to remember that Beep_(x,y) can be heard if the computer has properly wired the speaker (which wasn't the case not so long ago).

PS: For [Solved], you have to put it in the very first post by clicking the pencil and editing the title...

Enjoy PB

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
Post Reply