Diff for /np2/x11/soundmng.c between versions 1.14 and 1.18

version 1.14, 2011/01/15 16:01:52 version 1.18, 2011/12/24 17:50:44
Line 1 Line 1
 /*      $Id$    */  
   
 /*  /*
  * Copyright (c) 2001-2003 NONAKA Kimihiro   * Copyright (c) 2001-2003 NONAKA Kimihiro
  * All rights reserved.   * All rights reserved.
Line 70  MIDIMOD vermouth_module = NULL; Line 68  MIDIMOD vermouth_module = NULL;
  * driver   * driver
  */   */
 static struct {  static struct {
         BOOL (*drvinit)(UINT rate, UINT samples);          BOOL (*drvinit)(UINT rate, UINT samples);
         BOOL (*drvterm)(void);          BOOL (*drvterm)(void);
         void (*drvlock)(void);          void (*drvlock)(void);
         void (*drvunlock)(void);          void (*drvunlock)(void);
   
         void (*sndplay)(void);          void (*sndplay)(void);
         void (*sndstop)(void);          void (*sndstop)(void);
   
         void *(*pcmload)(UINT num, const char *path);          void *(*pcmload)(UINT num, const char *path);
         void (*pcmdestroy)(void *chanp, UINT num);          void (*pcmdestroy)(void *cookie, UINT num);
         void (*pcmplay)(void *chanp, UINT num, BOOL loop);          void (*pcmplay)(void *cookie, UINT num, BOOL loop);
         void (*pcmstop)(void *chanp, UINT num);          void (*pcmstop)(void *cookie, UINT num);
         void (*pcmvolume)(void *chanp, UINT num, int volume);          void (*pcmvolume)(void *cookie, UINT num, int volume);
 } snddrv;  } snddrv;
 static int audio_fd = -1;  static int audio_fd = -1;
 static BOOL opened = FALSE;  static BOOL opened = FALSE;
Line 102  void PARTSCALL saturation_s16mmx(SINT16  Line 100  void PARTSCALL saturation_s16mmx(SINT16 
 /*  /*
  * PCM   * PCM
  */   */
 static void *pcm_channel[SOUND_MAXPCM];  typedef struct {
           void *cookie;
           char *path;
           int volume;
   } pcm_channel_t;
   static pcm_channel_t *pcm_channel[SOUND_MAXPCM];
   
 static void soundmng_pcminit(void);  static void soundmng_pcminit(void);
 static void soundmng_pcmdestroy(void);  static void soundmng_pcmdestroy(void);
   
   #ifndef PCM_VOULE_DEFAULT
   #define PCM_VOULE_DEFAULT       25
   #endif
   int pcm_volume_default = PCM_VOULE_DEFAULT;
   
 /*  /*
  * buffer   * buffer
  */   */
Line 182  sounddrv_unlock(void) Line 190  sounddrv_unlock(void)
 UINT  UINT
 soundmng_create(UINT rate, UINT bufmsec)  soundmng_create(UINT rate, UINT bufmsec)
 {  {
           pcm_channel_t *chan;
         UINT samples;          UINT samples;
           int i;
   
         if (opened || ((rate != 11025) && (rate != 22050) && (rate != 44100))) {          if (opened || ((rate != 11025) && (rate != 22050) && (rate != 44100))) {
                 return 0;                  return 0;
Line 193  soundmng_create(UINT rate, UINT bufmsec) Line 203  soundmng_create(UINT rate, UINT bufmsec)
         else if (bufmsec > 1000)          else if (bufmsec > 1000)
                 bufmsec = 1000;                  bufmsec = 1000;
   
           for (i = 0; i < SOUND_MAXPCM; i++) {
                   chan = pcm_channel[i];
                   if (chan != NULL && chan->cookie != NULL) {
                           soundmng_pcmstop(i);
                           (*snddrv.pcmdestroy)(chan->cookie, i);
                           chan->cookie = NULL;
                   }
           }
   
         snddrv_setup();          snddrv_setup();
   
         samples = (rate * bufmsec) / 1000 / 2;          samples = (rate * bufmsec) / 1000 / 2;
Line 216  soundmng_create(UINT rate, UINT bufmsec) Line 235  soundmng_create(UINT rate, UINT bufmsec)
         buffer_init();          buffer_init();
         soundmng_reset();          soundmng_reset();
   
           for (i = 0; i < SOUND_MAXPCM; i++) {
                   chan = pcm_channel[i];
                   if (chan != NULL && chan->path != NULL) {
                           chan->cookie = (*snddrv.pcmload)(i, chan->path);
                           if (chan->cookie != NULL)
                                   (*snddrv.pcmvolume)(chan->cookie, i, chan->volume);
                   }
           }
   
         opened = TRUE;          opened = TRUE;
   
         return samples;          return samples;
Line 340  soundmng_setreverse(BOOL reverse) Line 368  soundmng_setreverse(BOOL reverse)
 /*  /*
  * PCM function   * PCM function
  */   */
 void  static void
 soundmng_pcminit(void)  soundmng_pcminit(void)
 {  {
         int i;          int i;
Line 350  soundmng_pcminit(void) Line 378  soundmng_pcminit(void)
         }          }
 }  }
   
 void  static void
 soundmng_pcmdestroy(void)  soundmng_pcmdestroy(void)
 {  {
           pcm_channel_t *chan;
         int i;          int i;
   
         for (i = 0; i < SOUND_MAXPCM; i++) {          for (i = 0; i < SOUND_MAXPCM; i++) {
                 if (pcm_channel[i]) {                  chan = pcm_channel[i];
                         (*snddrv.pcmdestroy)(pcm_channel[i], i);                  if (chan != NULL) {
                         pcm_channel[i] = NULL;                          pcm_channel[i] = NULL;
                           if (chan->cookie != NULL) {
                                   (*snddrv.pcmdestroy)(chan->cookie, i);
                                   chan->cookie = NULL;
                           }
                           if (chan->path != NULL) {
                                   _MFREE(chan->path);
                                   chan->path = NULL;
                           }
                 }                  }
         }          }
 }  }
Line 366  soundmng_pcmdestroy(void) Line 403  soundmng_pcmdestroy(void)
 BOOL  BOOL
 soundmng_pcmload(UINT num, const char *filename)  soundmng_pcmload(UINT num, const char *filename)
 {  {
           pcm_channel_t *chan;
           struct stat sb;
           int rv;
   
         if (num < SOUND_MAXPCM) {          if (num < SOUND_MAXPCM) {
                 if (pcm_channel[num])                  rv = stat(filename, &sb);
                         (*snddrv.pcmdestroy)(pcm_channel[num], num);                  if (rv < 0)
                 pcm_channel[num] = (*snddrv.pcmload)(num, filename);                          return FAILURE;
                 if (pcm_channel[num])  
                         return SUCCESS;                  chan = pcm_channel[num];
                   if (chan != NULL) {
                           if (strcmp(filename, chan->path)) {
                                   _MFREE(chan->path);
                                   chan->path = strdup(filename);
                           }
                   } else {
                           chan = _MALLOC(sizeof(*chan), "pcm channel");
                           if (chan == NULL)
                                   return FAILURE;
                           chan->cookie = NULL;
                           chan->path = strdup(filename);
                           chan->volume = pcm_volume_default;
                           pcm_channel[num] = chan;
                   }
                   return SUCCESS;
         }          }
         return FAILURE;          return FAILURE;
 }  }
Line 380  soundmng_pcmload(UINT num, const char *f Line 435  soundmng_pcmload(UINT num, const char *f
 void  void
 soundmng_pcmvolume(UINT num, int volume)  soundmng_pcmvolume(UINT num, int volume)
 {  {
           pcm_channel_t *chan;
   
         if ((num < SOUND_MAXPCM) && (pcm_channel[num])) {          if (num < SOUND_MAXPCM) {
                 (*snddrv.pcmvolume)(pcm_channel[num], num, volume);                  chan = pcm_channel[num];
                   if (chan != NULL) {
                           chan->volume = volume;
                           if (chan->cookie != NULL)
                                   (*snddrv.pcmvolume)(chan->cookie, num, volume);
                   }
         }          }
 }  }
   
 BOOL  BOOL
 soundmng_pcmplay(UINT num, BOOL loop)  soundmng_pcmplay(UINT num, BOOL loop)
 {  {
           pcm_channel_t *chan;
   
         if ((num < SOUND_MAXPCM) && (pcm_channel[num])) {          if (num < SOUND_MAXPCM) {
                 (*snddrv.pcmplay)(pcm_channel[num], num, loop);                  chan = pcm_channel[num];
                   if (chan != NULL && chan->cookie != NULL) {
                           (*snddrv.pcmplay)(chan->cookie, num, loop);
                   }
                 return SUCCESS;                  return SUCCESS;
         }          }
         return FAILURE;          return FAILURE;
Line 400  soundmng_pcmplay(UINT num, BOOL loop) Line 465  soundmng_pcmplay(UINT num, BOOL loop)
 void  void
 soundmng_pcmstop(UINT num)  soundmng_pcmstop(UINT num)
 {  {
           pcm_channel_t *chan;
   
         if ((num < SOUND_MAXPCM) && (pcm_channel[num])) {          if (num < SOUND_MAXPCM) {
                 (*snddrv.pcmstop)(pcm_channel[num], num);                  chan = pcm_channel[num];
                   if (chan != NULL && chan->cookie != NULL) {
                           (*snddrv.pcmstop)(chan->cookie, num);
                   }
         }          }
 }  }
   
Line 416  buffer_init(void) Line 485  buffer_init(void)
   
         sounddrv_lock();          sounddrv_lock();
         for (i = 0; i < NSOUNDBUFFER; i++) {          for (i = 0; i < NSOUNDBUFFER; i++) {
                 if (sound_buffer[i]) {                  if (sound_buffer[i] != NULL) {
                         _MFREE(sound_buffer[i]);                          _MFREE(sound_buffer[i]);
                 }                  }
                 sound_buffer[i] = (char *)_MALLOC(opna_frame, "sound buffer");                  sound_buffer[i] = (char *)_MALLOC(opna_frame, "sound buffer");
                 if (sound_buffer[i] == NULL) {                  if (sound_buffer[i] == NULL) {
                         fprintf(stderr, "buffer_init: can't alloc memory\n");                          g_printerr("buffer_init: can't alloc memory\n");
                         sounddrv_unlock();                          sounddrv_unlock();
                         return FAILURE;                          return FAILURE;
                 }                  }
Line 451  buffer_destroy(void) Line 520  buffer_destroy(void)
   
         sounddrv_lock();          sounddrv_lock();
         for (i = 0; i < NSOUNDBUFFER; i++) {          for (i = 0; i < NSOUNDBUFFER; i++) {
                 if (sound_buffer[i]) {                  if (sound_buffer[i] != NULL) {
                         _MFREE(sound_buffer[i]);                          _MFREE(sound_buffer[i]);
                         sound_buffer[i] = NULL;                          sound_buffer[i] = NULL;
                 }                  }
Line 512  nosound_pcmload(UINT num, const char *pa Line 581  nosound_pcmload(UINT num, const char *pa
 }  }
   
 static void  static void
 nosound_pcmdestroy(void *chanp, UINT num)  nosound_pcmdestroy(void *cookie, UINT num)
 {  {
   
         /* Nothing to do */          /* Nothing to do */
 }  }
   
 static void  static void
 nosound_pcmplay(void *chanp, UINT num, BOOL loop)  nosound_pcmplay(void *cookie, UINT num, BOOL loop)
 {  {
   
         /* Nothing to do */          /* Nothing to do */
 }  }
   
 static void  static void
 nosound_pcmstop(void *chanp, UINT num)  nosound_pcmstop(void *cookie, UINT num)
 {  {
   
         /* Nothing to do */          /* Nothing to do */
 }  }
   
 static void  static void
 nosound_pcmvolume(void *chanp, UINT num, int volume)  nosound_pcmvolume(void *cookie, UINT num, int volume)
 {  {
   
         /* Nothing to do */          /* Nothing to do */
Line 787  sdlaudio_init(UINT rate, UINT samples) Line 856  sdlaudio_init(UINT rate, UINT samples)
   
         rv = SDL_InitSubSystem(SDL_INIT_AUDIO);          rv = SDL_InitSubSystem(SDL_INIT_AUDIO);
         if (rv < 0) {          if (rv < 0) {
                 fprintf(stderr, "sdlaudio_init: SDL_InitSubSystem\n");                  g_printerr("sdlaudio_init: SDL_InitSubSystem(): %s\n",
                       SDL_GetError());
                 return FAILURE;                  return FAILURE;
         }          }
   
         audio_fd = SDL_OpenAudio(&fmt, NULL);          audio_fd = SDL_OpenAudio(&fmt, NULL);
         if (audio_fd < 0) {          if (audio_fd < 0) {
                 fprintf(stderr, "sdlaudio_init: SDL_OpenAudio\n");                  g_printerr("sdlaudio_init: SDL_OpenAudio(): %s\n",
                       SDL_GetError());
                 SDL_QuitSubSystem(SDL_INIT_AUDIO);                  SDL_QuitSubSystem(SDL_INIT_AUDIO);
                 return FAILURE;                  return FAILURE;
         }          }
Line 841  sdlmixer_init(UINT rate, UINT samples) Line 912  sdlmixer_init(UINT rate, UINT samples)
   
         rv = SDL_InitSubSystem(SDL_INIT_AUDIO);          rv = SDL_InitSubSystem(SDL_INIT_AUDIO);
         if (rv < 0) {          if (rv < 0) {
                 fprintf(stderr, "sdlmixer_init: SDL_InitSubSystem(): %s\n",                  g_printerr("sdlmixer_init: SDL_InitSubSystem(): %s\n",
                     SDL_GetError());                      SDL_GetError());
                 goto failure;                  goto failure;
         }          }
   
         rv = Mix_OpenAudio(rate, AUDIO_S16SYS, 2, samples);          rv = Mix_OpenAudio(rate, AUDIO_S16SYS, 2, samples);
         if (rv < 0) {          if (rv < 0) {
                 fprintf(stderr, "sdlmixer_init: Mix_OpenAudio(): %s\n",                  g_printerr("sdlmixer_init: Mix_OpenAudio(): %s\n",
                     Mix_GetError());                      Mix_GetError());
                 goto failure1;                  goto failure1;
         }          }
   
         rv = Mix_AllocateChannels(SOUND_MAXPCM);          rv = Mix_AllocateChannels(SOUND_MAXPCM);
         if (rv < 0) {          if (rv < 0) {
                 fprintf(stderr, "sdlmixer_init: Mix_AllocateChannels(): %s\n",                  g_printerr("sdlmixer_init: Mix_AllocateChannels(): %s\n",
                     Mix_GetError());                      Mix_GetError());
                 goto failure1;                  goto failure1;
         }          }
Line 883  sdlmixer_term(void) Line 954  sdlmixer_term(void)
 static void *  static void *
 sdlmixer_pcmload(UINT num, const char *path)  sdlmixer_pcmload(UINT num, const char *path)
 {  {
         Mix_Chunk *chunk;  
   
         chunk = Mix_LoadWAV(path);          return Mix_LoadWAV(path);
         return (void *)chunk;  
 }  }
   
 static void  static void
 sdlmixer_pcmdestroy(void *chanp, UINT num)  sdlmixer_pcmdestroy(void *cookie, UINT num)
 {  {
         Mix_Chunk *chunk = (Mix_Chunk *)chanp;          Mix_Chunk *chunk = cookie;
   
         Mix_HaltChannel(num);  
         Mix_FreeChunk(chunk);          Mix_FreeChunk(chunk);
 }  }
   
 static void  static void
 sdlmixer_pcmplay(void *chanp, UINT num, BOOL loop)  sdlmixer_pcmplay(void *cookie, UINT num, BOOL loop)
 {  {
         Mix_Chunk *chunk = (Mix_Chunk *)chanp;          Mix_Chunk *chunk = cookie;
   
         Mix_PlayChannel(num, chunk, loop ? -1 : 1);          Mix_PlayChannel(num, chunk, loop ? -1 : 1);
 }  }
   
 static void  static void
 sdlmixer_pcmstop(void *chanp, UINT num)  sdlmixer_pcmstop(void *cookie, UINT num)
 {  {
   
         Mix_HaltChannel(num);          Mix_HaltChannel(num);
 }  }
   
 static void  static void
 sdlmixer_pcmvolume(void *chanp, UINT num, int volume)  sdlmixer_pcmvolume(void *cookie, UINT num, int volume)
 {  {
   
         Mix_Volume(num, (MIX_MAX_VOLUME * volume) / 100);          Mix_Volume(num, (MIX_MAX_VOLUME * volume) / 100);

Removed from v.1.14  
changed lines
  Added in v.1.18


RetroPC.NET-CVS <cvs@retropc.net>