home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / m / m003_1 / sdk_dos.ddi / TPASCAL / MUSIC / DEMOCMF.PAS next >
Encoding:
Pascal/Delphi Source File  |  1991-12-13  |  10.0 KB  |  279 lines

  1. { ------------------------------------------------------------------------ }
  2. {  @@ Source Documentation                           *** TP6 Version ***   }
  3. {                                                                          }
  4. {  Copyright (c) Creative Technology Pte Ltd, 1991. All rights reserved.   }
  5. {                                                                          }
  6. {   TITLE       : DEMOCMF.PAS                                              }
  7. {                                                                          }
  8. {   DESCRIPTION :                                                          }
  9. {       This program demonstrates how to use the SBFM high level functions }
  10. {       to play back the music file FFARES.CMF. The user is allowed to     }
  11. {       control the music output from the keyboard.                        }
  12. {                                                                          }
  13. {       Note that the BLASTER environment has to be set and SBFMDRV.COM    }
  14. {       has to be installed before executing this program.                 }
  15. {                                                                          }
  16. { ------------------------------------------------------------------------ }
  17.  
  18. program democmf;
  19.  
  20. { Include the SBC Unit, and any other units needed }
  21. uses sbc_tp6, dos, crt;
  22.  
  23. { Include type-defined for CMF header }
  24. {$I sbcmusic.inc }
  25.  
  26. var
  27.   transpose : Integer;
  28.   lpMusicBuf : pointer;
  29.  
  30.  
  31. { ------------------------------------------------------------------------ }
  32. {  @@ Usage                                                                }
  33. {                                                                          }
  34. {   function LoadFile (szFilename : string) : Boolean                      }
  35. {                                                                          }
  36. {   DESCRIPTION:                                                           }
  37. {       Load file into memory. The Global variable lpMusicBuf is used to   }
  38. {       point to the loaded buffer.                                        }
  39. {                                                                          }
  40. {   ENTRY:                                                                 }
  41. {       szFileName :- File to be loaded.                                   }
  42. {                                                                          }
  43. {   EXIT:                                                                  }
  44. {       True if successful, else return False.                             }
  45. {                                                                          }
  46. { ------------------------------------------------------------------------ }
  47.  
  48. function LoadFile (szFilename : string) : Boolean;
  49. type
  50.     PtrRec = record
  51.         lo, hi : word
  52.     end;
  53.  
  54. var
  55.     wTemp, wByteRead : word;
  56.     lpTmpPtr : pointer;
  57.     lFSize : longint;
  58.     F : file;
  59.  
  60. begin
  61.     {$I-}
  62.     Assign(F, szFilename);
  63.     Reset(F,1);
  64.     {$I+}
  65.  
  66.     LoadFile := False;
  67.  
  68.     if IOResult = 0 then begin
  69.         lFSize := FileSize(F);
  70.  
  71.         { allocate memory }
  72.         Mark(lpMusicBuf);
  73.  
  74.         repeat
  75.             wTemp := $8000;
  76.  
  77.             if lFSize < $8000 then
  78.                 wTemp := word(lFSize);
  79.  
  80.             GetMem(lpTmpPtr,wTemp);
  81.  
  82.             lFSize := lFSize - wTemp;
  83.         until not Boolean(Lo(word(lFSize)));
  84.  
  85.  
  86.         lpTmpPtr := lpMusicBuf;
  87.         LoadFile := True;
  88.         wByteRead := 0;
  89.  
  90.         { Read data from file to buffer }
  91.         repeat
  92.             BlockRead(F,lpTmpPtr^,$8000,wTemp);
  93.             wByteRead := wByteRead + wTemp;
  94.  
  95.             { advance pointer }
  96.             PtrRec(lpTmpPtr).lo := PtrRec(lpTmpPtr).lo + wTemp;
  97.  
  98.             { adjust when cross segment }
  99.             if not Boolean(Hi(wByteRead)) then
  100.                 PtrRec(lpTmpPtr).hi := PtrRec(lpTmpPtr).hi + $1000;
  101.  
  102.         until wTemp <> $8000;
  103.  
  104.         close(F);
  105.     end
  106.     else
  107.         writeln('Open ',szFilename,' error ...');
  108. end;
  109.  
  110.  
  111. { ------------------------------------------------------------------------ }
  112. {  @@ Usage                                                                }
  113. {                                                                          }
  114. {   procedure StartMusic                                                   }
  115. {                                                                          }
  116. {   DESCRIPTION:                                                           }
  117. {       Retrieves music information from the CMF music file header and     }
  118. {       starts playing music.                                              }
  119. {                                                                          }
  120. {   ENTRY:                                                                 }
  121. {       None.                                                              }
  122. {                                                                          }
  123. {   EXIT:                                                                  }
  124. {       None.                                                              }
  125. {                                                                          }
  126. { ------------------------------------------------------------------------ }
  127.  
  128. procedure StartMusic;
  129. var
  130.     lTmp : longint;
  131.     lpInstPtr, lpMusicPtr : pointer;
  132.     Timer0Freq : word;
  133.  
  134. begin
  135.  
  136.     lTmp := longint(lpMusicBuf) + longint((CMFHDR(lpMusicBuf^)).inst_blk);
  137.     lpInstPtr := pointer(lTmp);
  138.  
  139.     lTmp := longint(lpMusicBuf) + longint((CMFHDR(lpMusicBuf^)).music_blk);
  140.     lpMusicPtr := pointer(lTmp);
  141.  
  142.     sbfm_reset;
  143.  
  144.     Timer0Freq := word(longint(1193180) div (CMFHDR(lpMusicBuf^)).clock_ticks);
  145.     sbfm_song_speed(Timer0Freq);
  146.  
  147.     if Boolean(Ofs(lpInstPtr)) then
  148.         sbfm_instrument(lpInstPtr,(CMFHDR(lpMusicBuf^)).inst_num);
  149.  
  150.     sbfm_play_music(lpMusicPtr);
  151.  
  152. end;
  153.  
  154.  
  155. { ------------------------------------------------------------------------ }
  156. {  @@ Usage                                                                }
  157. {                                                                          }
  158. {   procedure WaitMusicEnd                                                 }
  159. {                                                                          }
  160. {   DESCRIPTION:                                                           }
  161. {       Control the music output from keyboard.                            }
  162. {                                                                          }
  163. {   ENTRY:                                                                 }
  164. {       None.                                                              }
  165. {                                                                          }
  166. {   EXIT:                                                                  }
  167. {       None.                                                              }
  168. {                                                                          }
  169. { ------------------------------------------------------------------------ }
  170.  
  171. procedure WaitMusicEnd;
  172. const
  173.     ESC            = 27;
  174.     up_P           = 80;
  175.     lo_p           = 112;
  176.     up_C           = 67;
  177.     lo_c           = 99;
  178.  
  179.     EXT            = 256;
  180.     LEFTARROW      = (EXT+75);
  181.     RIGHTARROW     = (EXT+77);
  182.  
  183. var
  184.     key : char;
  185.     keyval : integer;
  186.  
  187. begin
  188.  
  189.     repeat
  190.         if KeyPressed then begin
  191.  
  192.             key := ReadKey;
  193.             keyval := ord(key);
  194.  
  195.             if ((key = #0) and KeyPressed) then begin
  196.                 key := ReadKey;
  197.                 keyval := ord(key)+EXT;
  198.             end;
  199.  
  200.             case (keyval) of
  201.                 ESC :
  202.                     sbfm_stop_music;
  203.  
  204.                 LEFTARROW :
  205.                     begin
  206.                         transpose := transpose - 1;
  207.                         sbfm_transpose(transpose);
  208.                         writeln('Transpose : ',transpose);
  209.                     end;
  210.  
  211.                 RIGHTARROW :
  212.                     begin
  213.                         transpose := transpose + 1;
  214.                         sbfm_transpose(transpose);
  215.                         writeln('Transpose : ',transpose);
  216.                     end;
  217.  
  218.                 up_P,lo_p :
  219.                     sbfm_pause_music;
  220.  
  221.                 up_C,lo_c :
  222.                     sbfm_resume_music;
  223.             end;
  224.         end;
  225.     until not Boolean(_ct_music_status);
  226.  
  227. end;
  228.  
  229.  
  230.  
  231. { ------------------------------------------------------------------------ }
  232. {  @@ Usage                                                                }
  233. {                                                                          }
  234. {   procedure PlayCmfFile (szFilename: string)                             }
  235. {                                                                          }
  236. {   DESCRIPTION:                                                           }
  237. {       Play a CMF file and wait for music end.                            }
  238. {                                                                          }
  239. {   ENTRY:                                                                 }
  240. {       szFileName :- Music file to be played.                             }
  241. {                                                                          }
  242. {   EXIT:                                                                  }
  243. {       None.                                                              }
  244. {                                                                          }
  245. { ------------------------------------------------------------------------ }
  246.  
  247. procedure PlayCmfFile (szFilename: string);
  248.  
  249. begin
  250.  
  251.     if LoadFile(szFilename)  then begin
  252.         StartMusic;
  253.         WaitMusicEnd;
  254.     end;
  255.  
  256. end;
  257.  
  258.  
  259. { ------------------------------------------------------------------------ }
  260.  
  261. { main function }
  262. var
  263.     wVersion : word;
  264.  
  265. begin
  266.  
  267.    if Boolean(sbfm_init) then begin
  268.  
  269.         wVersion := sbfm_version;
  270.         writeln('     SBFMDRV version ',Hi(wVersion),'.',Lo(wVersion):2);
  271.  
  272.         PlayCmfFile('FFARES.CMF');
  273.  
  274.         sbfm_terminate;
  275.     end
  276.     else
  277.         writeln('SBFMDRV not installed or FM Driver initialization error.');
  278. end.
  279.