..like QuickBasic "play"-function

Share your advanced PureBasic knowledge/code with the community.
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

..like QuickBasic "play"-function

Post by Hroudtwolf »

Code updated For 5.20+

A lot of people knows this command from QuickBasic.
Yes, I know, its 15 years to late...
But here is it. The "play"-function for PureBasic.

Reference:
Playspeed : t{value from 0 to 255} Example: t100
Notelenght : l{value from 0 to 255} Example: l100
Octave : o{value from 0 to 255} Example: o2

Notes:

  • c
    #c
    d
    #d
    e
    f
    #f
    g
    #g
    a
    #a
    b (like german h)



The notes with rhombus sounds a halftone higher.

Important: Set spaces between the commands.

Code: Select all

Procedure PlayJob (a) ; Don't use this function
  Shared ShNoten.s
  octave.l=1
  notelenght.l=100
  speed.l=0
  For x=1 To CountString (ShNoten.s,Chr(32))+1
    part.s=Trim(LCase (StringField (ShNoten.s,x,Chr(32))))
    Select part.s
      Case "c"
        Beep_(278*octave.l,notelenght.l)
      Case "#c"
        Beep_(295*octave.l,notelenght.l)
      Case "d"
        Beep_(313*octave.l,notelenght.l)
      Case "#d"
        Beep_(331*octave.l,notelenght.l)
      Case "e"
        Beep_(351*octave.l,notelenght.l)
      Case "f"
        Beep_(372*octave.l,notelenght.l)
      Case "#f"
        Beep_(394*octave.l,notelenght.l)
      Case "g"
        Beep_(417*octave.l,notelenght.l)
      Case "#g"
        Beep_(442*octave.l,notelenght.l)         
      Case "a"
        Beep_(468*octave.l,notelenght.l)
      Case "#a"
        Beep_(496*octave.l,notelenght.l)
      Case "b"
        Beep_(526*octave.l,notelenght.l)
      Case "p"
        Delay (notelenght.l)   
    EndSelect
    If Mid(part.s,1,1)="o": octave.l=Val(Mid(part.s,2,1)) :psoff.l=1:EndIf
    If octave.l<1:octave.l=1:EndIf
    If octave.l>4:octave.l=4:EndIf
    If Mid(part.s,1,1)="l": notelenght.l=Val(Mid(part.s,2,Len(part.s)-1)):psoff.l=1 :EndIf
    If notelenght.l<1:notelenght.l=1:EndIf
    If notelenght.l>255:notelenght.l=255:EndIf
    If Mid(part.s,1,1)="t": tempo.l=Val(Mid(part.s,2,Len(part.s)-1)) :psoff.l=1:EndIf
    If tempo.l<1:tempo.l=1:EndIf
    If tempo.l>255:tempo.l=255:EndIf
    If psoff.l=0 And part.s<>"":Delay (255-tempo.l):EndIf
    psoff.l=0
  Next x
  
EndProcedure

Procedure PlayMelody (noten.s)
  Shared ShNoten.s
  ShNoten.s=noten.s
  CreateThread(@PlayJob(),0)
EndProcedure



PlayMelody ("t200 l50 o1 c e g c e g c e g e g b e g b f a c f a c f a c b g e b g e b g e o2 l50 c e g c e g c e g e g b e g b f a c f a c f a c b g e b g e b g e ")

MessageRequester ("Pure playing","Music non stop ;-)",0)


Feel free to optimize this code how ever you want.
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

Awesome, I remember discovering the Imperial March from Star Wars in Qbasic!
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

If Windows Me and 98 users would like to hear this play, replace each 'Beep_(' by 'Beep(' and include the Beep function in my post "Beep() under Windows Me, 98 and 95" in 'Tips 'n' Tricks' (05-Sep-05).
Anthony Jordan
Post Reply