Tutorial: playing sound
=======================

Example for basic sound functions.


step 1
At first we have to initialize VgaGames
and open a window or switch to graphical screen.

==>
int main(int argc, char ** argv) {
  char * arg0;  // for the name of the program
  char buf[64];  // for explaining text
  int lastvol;

  /* initialize vgagames, always pass argv[0] */
  if (vg_init_vgagames(argv[0],0,NULL) < 0) {exit(1);}

  /* open window */
  if ((arg0=strrchr(argv[0],'/'))==NULL) {arg0=argv[0];} else {arg0++;}
  if (vg_window_open(arg0,0,0) < 0) {exit(1);}
<== step 2 Start soundserver and load the music file. ==>
  /* start soundserver */
  if (vg_sound_startserver(0,0,NULL) < 0) {vg_window_close(); exit(1);}

  /* load midi-file attaching it to nickname "s-music" using orignal volume */
  vg_sound_attach("sound/phatjail.mid","s-music",100);
<== step 3 Play, pause, continue the music and change volume. For a better overview, functions for the screen output are omitted. ==>
  /* start midi-file becoming louder for 5 seconds up to its normal volume */
  vg_sound_play("s-music",5,1);
  sleep(5);
  /* music is playing */
  sleep(5);

  /* pausing midi-file */
  vg_sound_paus("s-music");
  sleep(3);

  /* continuing midi-file */
  vg_sound_cont("s-music");
  sleep(5);

  /* changing main volume */
  lastvol=vg_sound_volm("0",-1);  /* get current volume */
  vg_sound_volm("0",lastvol+10);  /* add 10 to current volume */
  sleep(3);

  /* stopping music */
  vg_sound_stop("s-music",5);
  sleep(5);
  /* music is stopped */
  sleep(3);

  /* end soundserver and close window */
  vg_sound_endserver();
  vg_window_close();

  exit(0);
}
<== The whole program tut-sound1.c
[Previous] - [Index] - [Next]