1 / 51

語音、音樂與 API

語音、音樂與 API. Beep 敘述. VB6 中唯一會發出聲音的指令敘述 語法 Beep 功能 由 PC 喇叭發出「嗶」 / 「叮」的警示聲. 除了一般的「元件」外 …. 也許您也發現了, VB6 所提供的一般標準元件似乎不敷使用 沒關係,其實 VB6 提供了非常豐富的元件庫,就在 …. 這裡. 除了一般的「元件」外 …. 1. 3. 2. 牛刀小試 (1) 寫個萬年曆就是這麼簡單 …. 不需要任何程式碼. 牛刀小試 (2) 讓電腦說話 …. MicroSoft Direct Text-To-Speech 元件.

latanya
Download Presentation

語音、音樂與 API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 語音、音樂與API

  2. Beep 敘述 • VB6中唯一會發出聲音的指令敘述 • 語法 • Beep • 功能 • 由PC喇叭發出「嗶」/「叮」的警示聲

  3. 除了一般的「元件」外… • 也許您也發現了,VB6所提供的一般標準元件似乎不敷使用 • 沒關係,其實VB6提供了非常豐富的元件庫,就在… 這裡

  4. 除了一般的「元件」外… 1 3 2

  5. 牛刀小試(1) 寫個萬年曆就是這麼簡單… 不需要任何程式碼

  6. 牛刀小試(2) 讓電腦說話… • MicroSoft Direct Text-To-Speech元件

  7. 牛刀小試(2) 讓電腦說話…續 • MicroSoft Direct Text-To-Speech元件 • 重要方法(method) • Speak

  8. 牛刀小試(3) 讓電腦說話… • Microsoft Voice Text元件

  9. 牛刀小試(3) 讓電腦說話…續 • 屬性 • Speed • 說話速度,數字愈小速度愈慢 • TTSMode • Text To Mode • 英語男聲 • c5c35d60-da44-11d1-B1F1-0000F803E456 • 日語女聲 • a778e060-a936-11d1-B17B-0020AFED142E • 下載Text To Speech Engine • http://www.microsoft.com/msagent/downloads.htm

  10. 牛刀小試(3) 讓電腦說話…續

  11. 牛刀小試(3) 讓電腦說話…續 PictureBox option TextToSpeech textbox commandbutton

  12. 牛刀小試(3) 讓電腦說話…續 man.bmp woman.bmp

  13. 牛刀小試(4) 引用Media Player

  14. 牛刀小試(4) 引用Media Player---續

  15. 應用程式介面 • 應用程式介面(Application Programming Interface, API) • 定義某服務之函數呼叫介面的規範. • WIN32 API • Microsoft Windows 32位作業平台與其應用程式間的溝通橋樑 • API函数是構築整個WINDOWS框架的基石

  16. 應用程式介面(續) • 當WINDOWS作業系統開始佔據主導地位的時候,開發WINDOWS平臺下的應用程式便成爲程式設計開發人員的需要。 • 在WINDOWS程式設計領域處於發展的初期,WINDOWS程式師所能使用的撰寫程式工具只有API函數,而這些函數是WINDOWS提供給應用程式與作業系統的之間的介面 • 這些API就像是「積木」一樣,可以搭建出各種介面豐富,功能靈活的應用程式

  17. 應用程式介面(續)

  18. 應用程式介面(續) • 諸如Visual Bacis, Visual C++, Visual J++, Borland C++ Builder, Delphi …,所提供的類別及物件都是架構在Win 32 API函數的基礎上,簡單的說,就是已經封裝完成的API • 倘若我們要開發出更靈活、更實用、更具效率的應用程式,必然要涉及到直接使用API函數,雖然類別和控制項使應用程式的開發簡單的多,但它們只提供WINDOWS的一般功能,對於比較複雜和特殊的功能來說,使用類別和控制項是非常難以實現的,這時就需要採用API函數來實現

  19. 哪裡可以找到API的宣告?

  20. 播放Wav聲音檔的API • PlaySound函數 Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long ret = PlaySound(Wav檔案名稱, 0, &H0) 同步 Handle of the executable file that contains the resource to be loaded. The sound is played using an application-specific association

  21. 播放Wav聲音檔的API (續) • dwFlags • SND_APPLICATION (&H80) • SND_ALIAS (&H10000) • SND_ALIAS_ID (&H110000) • SND_ASYNC (&H1) • SND_FILENAME (&H20000) • SND_LOOP (&H8) • SND_MEMORY (&H4) • SND_NODEFAULT (&H2) • SND_NOSTOP (&H10) • SND_NOWAIT (&H2000) • SND_PURGE (&H40) • SND_RESOURCE (&H40004) • SND_SYNC (&H0)  內定

  22. 牛刀小試(5)---認識動物

  23. 牛刀小試(5)---認識動物—續 ListBox *1 Image *1 CommandButton*2

  24. 牛刀小試(5)---認識動物—續 • 屬性設定 • Image (Image1) • Stretch:True • CommandButton (Command1~2) • Caption • Command1:聲音 • Command2:離開

  25. 牛刀小試(5)---認識動物—續 • 蒐集聲音及圖片 ………… 1.bmp 2.bmp 3.bmp 15.bmp ………… 1.wav 2.wav 3.wav 15.wav

  26. 牛刀小試(5)---認識動物—續 宣告PlaySound API函數 當按下Command1時,播放相對應的動物叫聲 當按下Command2時,結束程式執行 將“1 牛 2 貓 … 15 蟬 ”等選項依序加入List1中 當點選List1時,對Image1進行圖片的更換

  27. 與MIDI 相關的API • 樂器數位介面 • Musical Instrument Digital Interface • 1983年時由多家樂器製造商共同制定出MIDI 1.0的規格,使得不同的合成器可以互相溝通 • 每個MIDI裝置皆有16個Channel,每個Channel皆可以執行獨立的工作

  28. 開啟MIDI裝置 確認開啟的輸出裝置 Private Declare Function midiOutOpen Lib "winmm.dll" (lphMidiOut As Long, ByVal uDeviceID As Long, ByVal dwCallback As Long, ByVal dwInstance As Long, ByVal dwFlags As Long) As Long 輸出裝置代碼 ret = midiOutOpen(hMidi, dev_id, 0, 0, 0)

  29. 開啟MIDI裝置(續) Parameters lphmidiout Pointer to an HMIDIOUT handle. This location is filled with a handle identifying the opened MIDI output device. The handle is used to identify the device in calls to other MIDI output functions. uDeviceID Identifier of the MIDI output device that is to be opened. dwCallback Pointer to a callback function, an event handle, a thread identifier, or a handle of a window or thread called during MIDI playback to process messages related to the progress of the playback. If no callback is desired, specify NULL for this parameter. For more information on the callback function, see MidiOutProc. dwInstance User instance data passed to the callback. This parameter is not used with window callbacks or threads. dwFlags Callback flag for opening the device. It can be the following values.

  30. 關閉MIDI裝置 Private Declare Function midiOutClose Lib "winmm.dll" (ByVal hMidiOut As Long) As Long Handle to the MIDI output device. If the function is successful, the handle is no longer valid after the call to this function ret = midiOutClose(hMidi)

  31. 播放聲音 Private Declare Function midiOutShortMsg Lib "winmm.dll" (ByVal hMidiOut As Long, ByVal dwMsg As Long) As Long Channel 音調 速度 midiOutShortMsg hMidi, 127 * &H10000 + (i + Note) * &H100 + &H90

  32. Command3 牛刀小試(6) 簡易電子琴 Command4 Command2 Command5 Command1(0)~(12)

  33. 程式碼—變數宣告 Dim hMidi As Long Dim ret As Integer Dim index1 As Integer Dim Note As Integer Dim Channel As Integer Private Declare Function midiOutShortMsg Lib "winmm.dll" (ByVal hMidiOut As Long, ByVal dwMsg As Long) As Long Private Declare Function midiOutOpen Lib "winmm.dll" (lphMidiOut As Long, ByVal uDeviceID As Long, ByVal dwCallback As Long, ByVal dwInstance As Long, ByVal dwFlags As Long) As Long Private Declare Function midiOutClose Lib "winmm.dll" (ByVal hMidiOut As Long) As Long

  34. 程式碼 Private Sub Form_Load() OpenMidi (-1) '開啟MIDI設備,代號內定為-1 Note = 36 End Sub Private Function OpenMidi(ByVal dev_id As Long) As Integer Call CloseMidi ret = midiOutOpen(hMidi, dev_id, 0, 0, 0) OpenMidi = (hMidi <> 0) End Function

  35. 程式碼(續) Private Sub CloseMidi() ' 關閉所有的MIDI裝置 If hMidi <> 0 Then ret = midiOutClose(hMidi) hMidi = 0 End If End Sub Private Sub Command5_Click() Unload Me End Sub Private Sub Form_Unload(Cancel As Integer) Call CloseMidi '關閉音源 End Sub

  36. 程式碼(續) Private Sub Command3_Click() '切換音調 Note = Note + 1 End Sub Private Sub Command4_Click() Note = Note - 1 End Sub

  37. 程式碼(續) Private Sub Command1_Click(Index As Integer) index1 = Index Call StopNote(index1) Call PlayNote(index1) End Sub Private Sub Command2_Click() '關閉聲音 Call StopNote(index1) End Sub

  38. 程式碼(續) Private Sub PlayNote(ByVal i As Integer) midiOutShortMsg hMidi, 127 * &H10000 + (i + Note) * &H100 + &H90 End Sub Private Sub StopNote(ByVal i As Integer) midiOutShortMsg hMidi, (i + 36) * &H100 + &H80 End Sub

  39. 我的主機有多少MIDI設備 尋獲設備及其資訊 Public Declare Function midiOutGetDevCaps Lib "winmm.dll" Alias "midiOutGetDevCapsA" (ByVal uDeviceID As Long, lpCaps As MIDIOUTCAPS, ByVal uSize As Long) As Long Type MIDIOUTCAPS wMid As Integer wPid As Integer vDriverVersion As Long szPname As String * 32 wTechnology As Integer wVoices As Integer wNotes As Integer wChannelMask As Integer dwSupport As Long End Type

  40. 我的主機有多少MIDI設備(續) 統計設備數量 Public Declare Function midiOutGetNumDevs Lib "winmm" () As Integer Private Sub Form_Load() Dim midicaps As MIDIOUTCAPS If midiOutGetDevCaps(-1, midicaps, Len(midicaps)) = 0 Then Combo1.AddItem midicaps.szPname Combo1.ItemData(Combo1.NewIndex) = -1 End If For i = 0 To midiOutGetNumDevs() - 1 If midiOutGetDevCaps(i, midicaps, Len(midicaps)) = 0 Then Combo1.AddItem midicaps.szPname Combo1.ItemData(Combo1.NewIndex) = i End If Next Combo1.ListIndex = 0 End Sub

  41. 我可以模擬多少種樂器? Piano 1 Piano 2 Piano 3 Honky-tonk E.Piano 1 E.piano 2 Harpsichord Clavinet Celesta Glockenspiel Music Box Vibraphone Marimba 等137種樂器

  42. 我可以模擬多少種樂器?(續) midiOutShortMsg hMidi, &HB0 midiOutShortMsg hMidi, (樂器代碼) * &H100 + &HC0 Piano 1 0 Piano 2 1 Piano 3 2 Honky-tonk 3 E.Piano 1 4 E.piano 2 5 :

  43. 參數 意義 uAction Long,指定要設置的參數。參考uAction常數表 uParam Long,參考uAction常數表 lpvParam Any,按引用調用的Integer、Long和資料結構。 fuWinIni 這個參數規定了在設置系統參數的時候,是否應更新用戶設置參數 範例一 自動更換螢幕背景 • 使用到的API • SystemParametersInfo函數 Public Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long

  44. 參數 意義和使用方法 6 設置視窗的大小,SystemParametersInfo(6, 放大縮小值, P, 0),lpvParam爲long型 17 開關屏保程式,SystemParametersInfo(17, False, P, 1),uParam爲布林型 13,24 改變桌面圖示水平和垂直間距,uParam爲間距值(圖元),lpvParam爲long型 15 設置屏保等待時間,SystemParametersInfo(15, 秒數, P, 1),lpvParam爲long型 20 設置桌面背景,SystemParametersInfo(20, True, 圖片路徑, 1) 93 開關滑鼠軌迹,SystemParametersInfo(93, 數值, P, 1),uParam爲False則關閉 97 開關Ctrl+Alt+Del視窗,SystemParametersInfo(97, False, A, 0),uParam爲布林型 自動更換螢幕背景(續)-部分uAction參數及用法

  45. 程式說明 • 套用元件Common Dialog Control • 開啟檔案選擇的對話框

  46. 程式說明(續) • 參數說明 • Const WALLPAPER = 20 • 設置桌面背景 • SystemParametersInfo(20, True, 圖片路徑, 1)

  47. 面板配置 Label ListBox Common Dialog Timer Command Button*3

  48. 程式碼 Private Sub Form_Load() flag = False Timer1.Interval = 1000 End Sub Private Sub Command3_Click() Unload Me End Sub

  49. 程式碼(續) Private Sub Command1_Click() Dim bmpfile As String If Listfile.ListCount <> 0 Then bmpfile = Listfile.List(Listfile.ListIndex) SystemParametersInfo WALLPAPER, 0, bmpfile, &H1 End If End Sub Private Sub Command2_Click() ComOpen.ShowOpen Listfile.AddItem ComOpen.FileName End Sub

  50. 程式碼(續) Private Sub Timer1_Timer() If Listfile.ListCount <> 0 Then flag = True Dim bmpfile As String If flag Then bmpfile = Listfile.List(Listfile.ListIndex) SystemParametersInfo WALLPAPER, 0, bmpfile,1 If Listfile.ListIndex = Listfile.ListCount - 1 Then Listfile.ListIndex = 0 Else Listfile.ListIndex = Listfile.ListIndex + 1 End If End If End If End Sub

More Related