CQBS: Canonical Quick Basic Sound

User projects written in or related to FreeBASIC.
darkblacklife
Posts: 149
Joined: Jun 18, 2008 22:10

CQBS: Canonical Quick Basic Sound

Postby darkblacklife » Oct 02, 2008 2:54

WHAT IS CQBS ?

CQBS means "Canonical Quick Basic Sound", where "canonical" has two senses. CQBS attempts to closely imitate QB's sound features, so QB is a canon. And otherwise a "God" thread governs all sound features, so there is a divine mandate (or canon) :)

BELL (QB's BEEP), SOUND & PLAY functions attempts to work like QB's functions, but sounds and noises go out through system sound card instead of PC-Speaker. For free, additional features with CQBS_SFX functions give a mixture of PLAY and SOUND functionality and amplitude variability.

Current version only works on Windows (like a WindowsXP on Pentium III, 128mb). Maybe some day CQBS will work under other platform (probably Linux).

"libCQBS.a" library:
http://www.mediafire.com/?fyc2w3mdg2m

"CQBS.bi" headers:
http://www.mediafire.com/?zizmlzy2nmt

Documentation (.txt):
http://www.mediafire.com/?mqzjgmg1twd

Example 1:

Code: Select all

'*******************************************************************************
'
' CQBS:       Canonical Quick Basic Sound
' Version:    09/2008 (Testing)
' Programmer: Leonardo Leitao
'
' This is a program distributed under the terms of my own IYTCFTIKY license.
' IYTCFTIKY means "If You Take Credit For This I'll Kill You".
'
' I don't give any kind of warranty, use it under your own risk
'
' You need "libCQBS.a" and must compile (using FB) with options: -lang fb -mt
'
'*******************************************************************************

' It links/includes "libCQBS.a"
#include once "CQBS.bi"

'*******************************************************************************
' Example Program


CQBS_initialize (5461)

Cls
Print "*** CQBS examples and tests ***"
Print "*******************************"
Sleep
Print
Print "BELL function (like QB's BEEP) example ..."
Print "------------------------------------------"
Bell
Print "it was a Bell, it only gets back when concluded"
Bell
Print "it was a Bell, it only gets back when concluded"
Bell
Print "it was a Bell, it only gets back when concluded"
Bell
Print "it was a Bell, it only gets back when concluded"
Sleep

Print
Print "SOUND function example ..."
Print "--------------------------"

sound (200,50)
Print "It is ""SOUND 200, 50"" and it's back while it's playing yet"

Sleep
sound (4000,5000)
Print "and the next is ""SOUND 3000, 5000"" .... ugly noise"
Print "you can stop it calling ""SOUND X, 0"", duration equal zero stops all sound"
Sleep
sound (200,0)

Print
Print "PLAY function example ..."
Print "-------------------------"

play "o1 c2 l4 e g < b. > l16 c d l2 c"
play "> a l4 g > c < g l16 g f e f l2 e"
play "< a8 l16 b > c d e f g a g f e d c < b a"
play "g8 a b > c d e f g f e d c < b a g f8 g a b > c d e"
play "f e d c < b a g f e8 f g a b > c d e d c < b a g f e"
play "d8 e f g a b > c# d < a b > c# d e f g"
play "a b > c < b a g f e f g a g f e d c"
play "< l8 b ms > g e c ml d g ms e c"
play "d4 g4 < g2 g2 > c4 e4 g2"
play "l16 a g f e f e d c e d e d e d e d e d e d e d c d"
play "c4 c < g > c e g e c e f d < b > d"
play "c4 < c < g > c e g e c e f d < b > d c4 > c4 c2"

Print
Print "ON FOREGROUND IT WAS: "
Print "Mozart's Sonata in C"
Sleep

play "MBo2l16e-9n0e-d-d-n0e-n0e-n0e-d-d-d-n0e-9n0e-d-d-n0e-g-9n0g-een0g-n0g-n0g-eeen0g-9n0g-een0g-b9n0baan0g-n0g-n0g-eeen0o1b9n0baan0b"

Print
Print "NOW ON BACKGROUND:"
Print "You ";
Sleep (500)
Print "are ";
Sleep (500)
Print "listening ";
Sleep (500)
Print "music ";
Sleep (500)
Print "of ";
Sleep (500)
Print "the ";
Sleep (500)
Print "game ";
Sleep (500)
Print """Gusanito"", ";
Sleep (500)
Print "from ";
Sleep (500)
Print "same ";
Sleep (500)
Print "author ";
Sleep (500)
Print "of ";
Sleep (500)
Print """CQBS"""

Sleep
CQBS_finish
End

Example 2:

Code: Select all

'*******************************************************************************
'
' CQBS:       Canonical Quick Basic Sound
' Version:    09/2008 (Testing)
' Programmer: Leonardo Leitao
'
' This is a program distributed under the terms of my own IYTCFTIKY license.
' IYTCFTIKY means "If You Take Credit For This I'll Kill You".
'
' I don't give any kind of warranty, use it under your own risk
'
' You need "libCQBS.a" and must compile (using FB) with options: -lang fb -mt
'
'*******************************************************************************

' It links/includes "libCQBS.a"
#include once "CQBS.bi"

'*******************************************************************************
' Example Program

CQBS_initialize (5461)
Cls

Print "*** CQBS examples and tests ***"
Print "*******************************"
Print
Print "SFX functions example ..."
Print "-------------------------"

Print
Print "Missile ..."
For i As Short = 0 To 15000 Step 300
  CQBS_SFX_Input (i,1000,0.01,0)
  CQBS_SFX_Input (i+1000,3000,0.01,0)
  CQBS_SFX_Input (i+5000,50,0.02,0)
Next
CQBS_SFX_Input (100,50,0.5,0)
For i As Short = 1 To 30
  CQBS_SFX_Input (32760,Cushort(50+Rnd*30),0.02,0)
Next
For i As Short = 1 To 10
  CQBS_SFX_Input (15000,Cushort(50+Rnd*30),0.02,0)
Next 
For i As Short = 1 To 5
  CQBS_SFX_Input (5000,Cushort(50+Rnd*30),0.02,0)
Next
CQBS_SFX_Play
Sleep

Print
Print "Race car ..."
For i As Short = 0 To 30000 Step 600
  CQBS_SFX_Input (i,100,0.05,0)
Next
For i As Short = 100 To 50 Step -1
  CQBS_SFX_Input (30000,i,0.01,0)
Next
For i As Short = 30000 To 0 Step -600
  CQBS_SFX_Input (i,50,0.05,0)
Next
CQBS_SFX_Play
Sleep

Print
Print "Alien abduction ..."
For i As Short = 10000 To 0 Step -1000
  For j As Short = 120 To 220 Step 20
    CQBS_SFX_Input (i,j,0.05,0)
  Next
  For j As Short = 210 To 130 Step -20
    CQBS_SFX_Input (i,j,0.05,0)
  Next
Next
CQBS_SFX_Play
Sleep

CQBS_finish
End

Please test it, post bugs, I want suggestions...

Source Code:
http://www.mediafire.com/?wejydimkdim
Last edited by darkblacklife on Oct 04, 2008 17:15, edited 4 times in total.
darkblacklife
Posts: 149
Joined: Jun 18, 2008 22:10

Postby darkblacklife » Oct 03, 2008 23:03

Some body know what must I do for full C compatibility ?
I mean "libCQBS.a" 100% C compatible (gcc of course)

thanks
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Oct 04, 2008 6:23

darkblacklife wrote:Some body know what must I do for full C compatibility ?
I mean "libCQBS.a" 100% C compatible (gcc of course)

thanks


I'm pretty sure you can use the libCQBS.a directly with gcc, you'll just have to write a .h file that mirrors the functions you declare in your library's .bi file.
DrV
Site Admin
Posts: 2116
Joined: May 27, 2005 18:39
Location: Midwestern USA
Contact:

Postby DrV » Oct 04, 2008 16:35

You also must link with -lfb (or -lfbmt if you are using threads), and somewhere fb_Init should be called (called in main automatically in FB programs).
darkblacklife
Posts: 149
Joined: Jun 18, 2008 22:10

Postby darkblacklife » Oct 04, 2008 16:48

cha0s wrote:
darkblacklife wrote:Some body know what must I do for full C compatibility ?
I mean "libCQBS.a" 100% C compatible (gcc of course)

thanks


I'm pretty sure you can use the libCQBS.a directly with gcc, you'll just have to write a .h file that mirrors the functions you declare in your library's .bi file.


ok... I was thinking I would need some "cdecl" and change some "string" for "zstring".... when i have some time I'll write .h file (or somebody could write it too :) )
darkblacklife
Posts: 149
Joined: Jun 18, 2008 22:10

Postby darkblacklife » Oct 04, 2008 17:04

Sorry I've forget a "CQBS.bi" link... now I edit 1st post and put a link... :)
rmann05
Posts: 50
Joined: May 31, 2005 16:02
Location: Florida
Contact:

Postby rmann05 » Oct 06, 2008 3:19

What would have to be done to get this library to work under Linux? Does it use Windows API or what? I miss the sound and play commands.
darkblacklife
Posts: 149
Joined: Jun 18, 2008 22:10

Postby darkblacklife » Oct 06, 2008 4:40

rmann05 wrote:What would have to be done to get this library to work under Linux? Does it use Windows API or what? I miss the sound and play commands.


MySelf wrote:Current version only works on Windows (like a WindowsXP on Pentium III, 128mb). Maybe some day CQBS will work under other platform (probably Linux).


I don't know what would have to be done to get this library to work under Linux... some day I will research about Linux api....

Yes it uses Windows API... mmsystem functions:
waveOutOpen, waveOutClose, waveOutPrepareHeader, waveOutUnprepareHeader, waveOutWrite, waveOutReset

Most CQBS code doesn't use OS API (like mmsystem for Windows). The code that uses OS API functions is only for monotones generation and could be extended to different platforms easily (well... I believe).

any suggestion ??????
Leonheart
Posts: 82
Joined: Nov 20, 2007 9:08
Contact:

Postby Leonheart » Oct 07, 2008 8:08

Tht should be similar WINAPI in ALSA API on linux. take look some alsa code in fbsound.
angros47
Posts: 1822
Joined: Jun 21, 2005 19:04

Postby angros47 » Nov 03, 2008 18:15

I'm working on a similar project (based on Keeling's one, that have been dropped and doesn't work any more on FB 0.20)
It is based on MIDI notes, not on Wave synthesis (a good solution for PLAY command, not for SOUND)

I ported it to DOS, too.

Should we join our efforts? Maybe we can do a multi-platform library, that can be integrated in FB like FBGFX.
darkblacklife
Posts: 149
Joined: Jun 18, 2008 22:10

Postby darkblacklife » Nov 08, 2008 22:22

angros47 wrote:I'm working on a similar project (based on Keeling's one, that have been dropped and doesn't work any more on FB 0.20)
It is based on MIDI notes, not on Wave synthesis (a good solution for PLAY command, not for SOUND)

I ported it to DOS, too.

Should we join our efforts? Maybe we can do a multi-platform library, that can be integrated in FB like FBGFX.


Maybe... Now I'm using windows because my Debian etch for 64bit (pentium 4 em64t) doesn't have freebasic compiler... I wish to write linux code too... You could read my source code if you want and write a linux code (to replace CQBS_WIN.bas)... Then we could join all in one multiplataform source code using

#ifdef __FB_WIN32__
My win32 code (#include CQBS_WIN.bas)
#elseif __FB_LINUX__
Your linux code
#endif

or something like this....

I think I know keeling's qbsound...

Midi's sounds are not the same sounds than qb sound functions.... Maybe my CQBS doesn't have same sounds but I think is closer than keeling's qbsound... but who know??... It doesn't sound equal than a qbasic code running on a DosBox emulation... but is DosBox the right emulation???

I think CQBS could be better, I think It could be multiplatform... If you want write linux code it will be great... we could join windows and linux codes... I think you only need replace CQBS_WIN.bas because the other procedures and functions are multi-platform yet
angros47
Posts: 1822
Joined: Jun 21, 2005 19:04

Postby angros47 » Nov 09, 2008 20:12

If you look in the DOS forum, you can see that, by now, I made two version for DOS: one using AdLib (I know that it's obsolete, but many cards can use/emulate it, and can be programmed in a more standard way than wav), the other using PC-speaker (and in that way the sound will be 100% identical to QB: it could work even under windows and linux if you remove the IRS part)

Since QB sound can be obtained, identical, using the speaker, having a soundcard I wanted a library able to do something more.

So: Play command is implemented using MIDI, because even if the music isn't 100% identical to QB, it should play good anyway, and in turn you get poliphony and chords.

I didn't like the way Keeling implemented the sound command: since it's used to make special effects, it shouldn't use an istrument, instead it should use a sinusal or pulse wave.

My idea was to use a syntax like:

Sound Frequency, Duration, [Attack], [Decay], [Sustain],......

and other optional params to define the volume, the wave form.... so that the old syntax can always be used, and old programs can be ported, but sound effects can be improved.

I was thinking about a sort of FM synthesizer: it shouldn't be too difficult to build, doesn't require too many input parameters (a wave table would require a sample to work), and allows a large set of waveforms.

About Linux: I don't have it installed, so I still haven't any working code.

Anyway, I found some examples, and ported one of them to FB (I sent it in the Linux forum). If it works, it should emit a single note.... Anyone care to test it? It could be a start.
darkblacklife
Posts: 149
Joined: Jun 18, 2008 22:10

Postby darkblacklife » Nov 12, 2008 17:22

angros47 wrote:and other optional params to define the volume, the wave form.... so that the old syntax can always be used, and old programs can be ported, but sound effects can be improved.

I was thinking about a sort of FM synthesizer: it shouldn't be too difficult to build, doesn't require too many input parameters (a wave table would require a sample to work), and allows a large set of waveforms.


I was thinking in a callback function to customized wave forms. User could generate a function with a DOUBLE type argument (and a SHORT (16bits) output) and the sound library could call this function for 0 to 2*Pi range, with spaces proportional to Frequency/Samplefrequency, and generated sound output....
angros47
Posts: 1822
Joined: Jun 21, 2005 19:04

Postby angros47 » Nov 13, 2008 21:37

The idea of a callback is interesting, but it isn't very beginner-friendly, and would require a syntax very different from original QB, requiring a complete redesign of sound routines.

Maybe the waveform can be passed as an array, or as a string (so the SOUND command would work like the PAINT command, where a tile can be defined like a sequenche of bytes)

Something like that:

Code: Select all

sound 100,100          'Classic syntax


dim s as string
for i as integer=1 to 628
    s=s+chr(128+127*sin(i))      'define a sinusal wave
next


sound 100,100,s        'play the wave 's' at frequency 100 for a duration of 100
darkblacklife
Posts: 149
Joined: Jun 18, 2008 22:10

Postby darkblacklife » Nov 18, 2008 21:01

angros47 wrote:The idea of a callback is interesting, but it isn't very beginner-friendly, and would require a syntax very different from original QB, requiring a complete redesign of sound routines.

Maybe the waveform can be passed as an array, or as a string (so the SOUND command would work like the PAINT command, where a tile can be defined like a sequenche of bytes)

Something like that:

Code: Select all

sound 100,100          'Classic syntax


dim s as string
for i as integer=1 to 628
    s=s+chr(128+127*sin(i))      'define a sinusal wave
next


sound 100,100,s        'play the wave 's' at frequency 100 for a duration of 100


yeah, maybe ... but I think it should be a SHORT type array (16 bits) for best sound resolution... do you see the CQBS_SFX functions on my code? only with a function that uses their you could have something like this

Return to “Projects”

Who is online

Users browsing this forum: Nothoro and 10 guests