home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Programming / Source / Gopher_1.12 / SimpleSound.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-16  |  2.4 KB  |  104 lines

  1. ///////////////////////////////////////////////////////////////////////
  2. // Most of the code in this file was simply copied from the play
  3. // program from the University of Minnesota Gopher client.
  4.  
  5. #import "Sound.h"
  6.  
  7. #define SOUNDBUFSIZE 1400  /* A pretty good value for ethernet */
  8.  
  9. // This is a flag indicating whether we are currently playing a sound.
  10. mutex_t    soundIsPlaying;
  11.  
  12. // This is a flag indicating that a currently playing sound should stop asap.
  13. mutex_t    soundShouldStop;
  14. int        soundShouldStopFlag;
  15.  
  16. // Make it global so it can be freed by other functions.
  17. static ThreadArgument *threadArgument;
  18.  
  19. static int currentsocket;
  20.  
  21. // Put this here to avoid "warning: Objective-C text in `.c' file".
  22. extern int NXRunAlertPanel(const char *title, const char *msg, const char *defaultButton, const char *alternateButton, const char *otherButton, ...);
  23.  
  24.  
  25. ////////////////////////////////////////////////////////////
  26. // This function is thread-forked. It must free the argument
  27. // before leaving.
  28.  
  29. int PlaySound(ThreadArgument *theArgument)
  30. {
  31. FILE *Play;
  32. int j;
  33. char buf[SOUNDBUFSIZE];
  34.  
  35. threadArgument = theArgument;
  36.  
  37. // Check that we're the only thread.
  38. if (mutex_try_lock(soundIsPlaying) == 0)
  39.     {
  40.     fprintf(stderr, "Error : too many threads.\n");
  41.     free(theArgument);
  42.     close(currentsocket);
  43.     cthread_exit(0);
  44.     }
  45.  
  46.  
  47. currentsocket = create_socket(theArgument->hostname, theArgument->portnumber);
  48. if ( currentsocket < 0)
  49.     {
  50.     switch (currentsocket)
  51.         {
  52.         case -1 :
  53.             fprintf(stderr, "Unknown host\n");
  54.             break;
  55.         case -2 :
  56.             fprintf(stderr, "Socket call failed\n");
  57.             break;
  58.         case -3 :
  59.             fprintf(stderr, "Connect call failed\n");
  60.             break;
  61.         default :
  62.             fprintf(stderr, "Unknown network error\n");
  63.             break;
  64.         }
  65.     free(theArgument);
  66.     close(currentsocket);
  67.     mutex_unlock(soundIsPlaying);
  68.     cthread_exit(0);
  69.     }
  70.  
  71. writestring(currentsocket, theArgument->filename);
  72. writestring(currentsocket, "\r\n");
  73.  
  74. Play = popen(PlayCommand, "w");
  75. while(1)
  76.     {
  77.     j = read(currentsocket, buf, SOUNDBUFSIZE);
  78.     if (j == 0)
  79.         break;
  80.     fwrite(buf, 1, j, Play);
  81.     if (mutex_try_lock(soundShouldStop) != 0)
  82.         if (soundShouldStopFlag)
  83.             {
  84.             soundShouldStopFlag = 0;
  85.             mutex_unlock(soundShouldStop);
  86.             break;
  87.             }
  88.         else
  89.             mutex_unlock(soundShouldStop);
  90.     }
  91.  
  92. free(theArgument);
  93. close(currentsocket);
  94. fclose(Play);
  95. #ifdef DEBUG
  96.     fprintf(stderr, "SoundPlay exiting.\n");
  97. #endif /* DEBUG */
  98.  
  99. sleep(2);        // Wait for play to exit
  100. mutex_unlock(soundIsPlaying);
  101. cthread_exit(0);
  102.  
  103. return 0;
  104. }