Author Topic: speech in freebasic  (Read 13416 times)

0 Members and 1 Guest are viewing this topic.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
speech in freebasic
« on: January 09, 2007 »
Here it is then, made all the more difficult by the fact the Speech API (SAPI) isn't present in FreeBasic - I had to do it myself...

Save this one as sapi.bi
NB. This file implements just enough to speak and change the pitch and volume of the voice.  I've left loads of it stubbed.
Code: [Select]
dim IID_ISpVoice as GUID    => ( &H6c44df74, &H72b9, &H4992, {&Ha1, &Hec, &Hef, &H99, &H6e, &H04, &H22, &Hd4 })
dim CLSID_SpVoice as GUID  => ( &H96749377, &H3391, &H11d2, {&H9e, &He3, &H00, &Hc0, &H4f, &H79, &H73, &H96 })

type ISpVoiceVtbl_ as ISpVoiceVtbl

type ISpVoice
lpVtbl as ISpVoiceVtbl_ ptr
end type

#define SPF_DEFAULT 0

type ISpVoiceVtbl
rem iunknown
QueryInterface as function(byval as ISpVoice ptr, byval as IID ptr, byval as any ptr) as HRESULT
AddRef as function(byval as ISpVoice ptr) as ULONG
Release as function(byval as ISpVoice ptr) as ULONG

rem stubs
SetNotifySink as function() as HRESULT
SetNotifyWindowMessage as function() as HRESULT
SetNotifyCallbackFunction as function() as HRESULT
SetNotifyCallbackInterface as function() as HRESULT
SetNotifyWin32Event as function() as HRESULT
WaitForNotifyEvent as function() as HRESULT
GetNotifyEventHandle as function() as HRESULT
SetInterest as function() as HRESULT
GetEvents as function() as HRESULT   
GetInfo as function() as HRESULT
SetOutput as function() as HRESULT
GetOutputObjectToken as function() as HRESULT
GetOutputStream as function() as HRESULT

rem done
Pause as function(byval as ISpVoice ptr) as HRESULT
Resume as function(byval as ISpVoice ptr) as HRESULT

rem stubs
SetVoice as function() as HRESULT
GetVoice as function() as HRESULT

        rem done
Speak as function(byval as ISpVoice ptr, byval pwcs as wstring ptr, byval dwFlags as DWORD, byval pulStreamNumber as ULONG ptr) as HRESULT

rem stubs
SpeakStream as function() as HRESULT
GetStatus as function() as HRESULT
Skip as function() as HRESULT
SetPriority as function() as HRESULT
GetPriority as function() as HRESULT
SetAlertBoundary as function() as HRESULT
GetAlertBoundary as function() as HRESULT

rem done       
        SetRate as function(byval as ISpVoice ptr, byval RateAdjust as integer) as HRESULT
        GetRate as function(byval as ISpVoice ptr, byval RateAdjust as integer ptr) as HRESULT
        SetVolume as function(byval as ISpVoice ptr, byval usVolume as ushort) as HRESULT
        GetVolume as function(byval as ISpVoice ptr, byval pusVolume as ushort ptr) as HRESULT
WaitUntilDone as function(byval as ISpVoice ptr, byval msTimeout as ULONG) as HRESULT       

rem stubs
SetSyncSpeakTimeout as function() as HRESULT
GetSyncSpeakTimeout as function() as HRESULT
SpeakCompleteEvent as function() as HRESULT
IsUISupported as function() as HRESULT
DisplayUI as function() as HRESULT
end type

Save this one as speech.bas
Code: [Select]
option explicit

#include once "windows.bi"
#include once "win/winnt.bi"
#include once "win/objbase.bi"
#include once "sapi.bi"
#inclib "ole32"

dim voice as ISpVoice ptr

dim text as wstring * 14 => "Hello, World"

CoInitialize(NULL)

CoCreateInstance(@CLSID_SpVoice, NULL, CLSCTX_ALL, @IID_ISpVoice, cast (any ptr, @voice))

voice->lpVtbl->Speak(voice, text, SPF_DEFAULT, NULL)
voice->lpVtbl->WaitUntilDone(voice, INFINITE)

voice->lpVtbl->Release(voice)

CoUninitialize()

Save both files into the same folder.

Jim
« Last Edit: January 09, 2007 by Jim »
Challenge Trophies Won:

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: speech in freebasic
« Reply #1 on: January 09, 2007 »
That's awesome work man. I think I was trying to post at the same time you were editing your last post in the other thread. Anyway, did you know about Swig? It's an utility to help in translating headers to FB.
The Dr. is INsane!!!

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: speech in freebasic
« Reply #2 on: January 09, 2007 »
Very cool, Jim.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: speech in freebasic
« Reply #3 on: January 09, 2007 »
I didn't know about it, but I'll definitely use it next time since it's a total pain doing it by hand.  Karma+!

Jim
Challenge Trophies Won:

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: speech in freebasic
« Reply #4 on: January 09, 2007 »
I know I'm childish... I just feel the need to put it in a loop saying, "Shut the !@#$ up, newb!"  :2funny: :2funny: :2funny:

EDIT... Where did you find documentation for this? The MSDN is chocked full of gobbledy gook! I was wondering if there are functions to change the pitch & stuff also? Maybe a way t query which part of the sentence is being spoken at the time? That would make it easy to sync with a talking head, ya know? ;)
« Last Edit: January 09, 2007 by Dr_D »
The Dr. is INsane!!!

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: speech in freebasic
« Reply #5 on: January 09, 2007 »
Code: [Select]
voice->lpVtbl->SetRate(voice, speed)

speed is between -10 (fast) and 10 (slow).
Volume's something similar.

It's all in MSDN.  I was looking at the SAPI reference
http://msdn2.microsoft.com/en-us/library/ms719576.aspx

I have all the headers and help in with Visual Studio which makes it a bit easier.  It's all downloadable, but it's a monstrous download (several Gb).  I also know COM (a.k.a. gobbledegook) pretty well :P

Not sure how you'd go about synching things up.  Some of that API looks hopeful though.

Jim
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: speech in freebasic
« Reply #6 on: January 09, 2007 »
Thats awesome work Jim, welldone.
How would you go about changing the sound of the computers voice?

Cheers,
Clyde.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: speech in freebasic
« Reply #7 on: January 09, 2007 »
Nice one Jim  :) Have some of that Karma stuff!
Shockwave ^ Codigos
Challenge Trophies Won:

Offline psygate

  • Completly Insane.
  • Atari ST
  • ***
  • Posts: 173
  • Karma: 7
  • That boy needs therapy.
    • View Profile
Re: speech in freebasic
« Reply #8 on: January 09, 2007 »
Good Work! I will use that voice from now on a bit  ;D
He who controlles the minds commands the many.
He who commands the many, conqueres the minds.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: speech in freebasic
« Reply #9 on: January 09, 2007 »
Quote
How would you go about changing the sound of the computers voice?
I looked at that - there's a function called SetVoice in there, which will let you choose different speakers, male, female, robot, etc. but it takes pointers to a whole new class of objects, all as complicated as the ISpVoiceVtbl one.  So it'd be 2 or 3 times the work to make that happen.  But it's do-able.
The SetRate function gives a little bit of variation on the default voice.

Jim
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: speech in freebasic
« Reply #10 on: January 09, 2007 »
Ok, Cheers Jim.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: speech in freebasic
« Reply #11 on: January 10, 2007 »
Yeah, I played around with the set rate attribute. It's pretty funny. :D It looks like switching from the default voice would also require you to distribute the voice patch dll's with your demos. I could be wrong though.
The Dr. is INsane!!!

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: speech in freebasic
« Reply #12 on: January 10, 2007 »
I had a look at what would be required to make the voice change.  It's just far too much work for a mess about, it's a serious amount of work.  Sorry, but I'll have to leave that to someone else.

Jim
Challenge Trophies Won:

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: speech in freebasic
« Reply #13 on: March 08, 2007 »
This is probably really cool but i cant get it to work.

: error 1: Argument count mismatch, found: '|'
CoCreateInstance(@CLSID_SpVoice, NULL, CLSCTX_ALL, @IID_ISpVoice, cast (any ptr, @voice))

Doing know what I'm doing wrong.

Edit, downloaded a newer version of free basic and now it works:)
Cool :D
« Last Edit: March 08, 2007 by Paul »
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: speech in freebasic
« Reply #14 on: March 09, 2007 »
Nice one :) Thanks for posting the solution.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Voltage

  • Professor
  • Pentium
  • *****
  • Posts: 857
  • Karma: 53
    • View Profile
Re: speech in freebasic
« Reply #15 on: March 18, 2007 »
Great work Jim.   My daughter was a little bit freaked, but I like it.
Challenge Trophies Won:

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: speech in freebasic
« Reply #16 on: March 18, 2007 »
hahaha! my daughter was a little freaked by it too, but she thought it was neat after a while. I sent it some children's text to help. :cheers:
The Dr. is INsane!!!

Offline Voltage

  • Professor
  • Pentium
  • *****
  • Posts: 857
  • Karma: 53
    • View Profile
Re: speech in freebasic
« Reply #17 on: March 18, 2007 »
I had the thing singing "do the hokie pokie, and turn around"...   But its tempo threw her off a little :)  She's just learning (she is 25)....... kidding, she is nearly 2.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: speech in freebasic
« Reply #18 on: March 18, 2007 »
Must've sounded like Ned off South Park singing Kumbya.
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: speech in freebasic
« Reply #19 on: October 21, 2007 »
i was wondering how i could get this going in dev c++ ive downloaded the redist SpeechSDK51MSM.exe from microsoft.

now is it just a case of linking my project to the libs and headers and will my compiled exe atomatically  be shipped with the required libs built in to run on other systems.

edit just unziped SpeechSDK51MSM.exe and its full of msm files?
« Last Edit: October 21, 2007 by ninogenio »
Challenge Trophies Won: