Sound_Sample *Sound_create_stream(char *file)
{
	int n_ext=6;
	char *ext[6]={".WAV",".OGG",".MP3",".wav",".ogg",".mp3"};
	char name[256],name2[256];
	int i;

	Sound_AudioInfo inf;

	inf.format=AUDIO_S16;
	inf.channels=2;
	inf.rate=44100;

	if (sound_enabled) {
		for(i=0;i<n_ext;i++) {
			strcpy(name,file);
			strcat(name,ext[i]);
			sprintf(name2,"%s%s",s_path,name);
			if (file_check(name2)) return Sound_NewSampleFromFile(name2,&inf,AUDIO_BUFFER);
//			if (file_check(name2)) return Sound_NewSampleFromFile(name2,0,AUDIO_BUFFER);
		} /* for */ 
		for(i=0;i<n_ext;i++) {
			strcpy(name,file);
			strcat(name,ext[i]);
			sprintf(name2,"%s%s",default_s_path,name);
			if (file_check(name2)) return Sound_NewSampleFromFile(name2,&inf,AUDIO_BUFFER);
//			if (file_check(name2)) return Sound_NewSampleFromFile(name2,0,AUDIO_BUFFER);
		} /* for */ 
		
		fprintf(stderr,"ERROR in Sound_create_stream(): Could not load sound file: %s%s.(wav|ogg|mp3)\n",s_path, file);
		exit(1);
	} else {
		return 0;
	} /* if */ 
} /* Sound_create_stream */ 
Exemple #2
0
int openAudio_platform(AudioInstance* instance)
{
    char path[512];
    char fullPath[512];
    getcwd(path, sizeof(path));
    snprintf(fullPath, sizeof(fullPath)-1, "%s\\data\\%s", path, instance->mClip->mPath);

    char* slash = strchr(fullPath, '/');
    while(slash)
    {
        *slash = '\\';
        slash = strchr(slash+1, '/');
    }

    instance->mAudio.mSample = Sound_NewSampleFromFile(fullPath, NULL, SDL_BUFFER_SIZE);
    if(instance->mAudio.mSample == NULL)
    {
        dprintf("%s", Sound_GetError());
        return -1;
    }

    dprintf("Opened as:\n  channels: %d\n  format: 0x%x\n  rate: %d",
        instance->mAudio.mSample->actual.channels,
        instance->mAudio.mSample->actual.format,
        instance->mAudio.mSample->actual.rate
        );

    int bytes;
    switch(instance->mAudio.mSample->actual.channels)
    {
    case 1:
        if((instance->mAudio.mSample->actual.format & 0x0008) == 0x0008)
        {
            instance->mFormat = AL_FORMAT_MONO8;
            bytes = 1;
        }
        else
        {
            instance->mFormat = AL_FORMAT_MONO16;
            bytes = 2;
        }
        break;
    case 2:
        if((instance->mAudio.mSample->actual.format & 0x0008) == 0x0008)
        {
            instance->mFormat = AL_FORMAT_STEREO8;
            bytes = 1;
        }
        else
        {
            instance->mFormat = AL_FORMAT_STEREO16;
            bytes = 2;
        }
        break;
    default:
        dprintf("WTF");
    }
    instance->mSampleRate = instance->mAudio.mSample->actual.rate;

    instance->mAudio.mReadFreq = ((float)SDL_BUFFER_SIZE/(float)(instance->mSampleRate * bytes)) * 1000000.0f;
    instance->mAudio.mReadFreq -= 500000;
    instance->mAudio.mNextRead = getTime();

    return 0;
}
Exemple #3
0
int
main (int argc, char **argv)
{
  Sound_AudioInfo  info;
  Sound_Sample    *stream;
  SDL_AudioSpec    spec;

  /* Handle command line options. */
  if (options (argc, argv) < 0) {
    puts ("Usage: amaranth OPTIONS FILE\n\n");
    puts ("Options\n");
    puts ("    -r RATE    sampling rate bits/second (default: 441000)\n");
    puts ("    -f FMT     sample format (default: signed 16-bit little-endian)\n");
    puts ("    -c COUNT   number of channels (default: 2)\n");
    puts ("    -s SIZE    audio buffer size in samples (default: 4096)\n");
    puts ("\nFormats accepted by -f option:\n");
    puts ("    S16LSB     signed 16-bit little-endian\n");
    return 1;
  }
  
  /* Initialize SDL audio subsystem. */
  if (SDL_Init (SDL_INIT_AUDIO) < 0) {
    fprintf (stderr, "amaranth: cannot initialize SDL audio: %s\n",
             SDL_GetError ());
    return 1;
  }

  /* Initialize SDL_sound library. */
  if (Sound_Init () < 0) {
    fprintf (stderr, "amaranth: cannot initialize SDL_sound: %s\n",
             Sound_GetError ());
    return 1;
  }

  /* Open the sound stream. */
  info.format   = sample_format;
  info.channels = channel_count;
  info.rate     = sample_rate;

  stream = Sound_NewSampleFromFile (filename, &info, 4096);

  if (stream == NULL) {
    fprintf (stderr, "amaranth: cannot open sound stream %s: %s\n",
             filename, Sound_GetError ());
    return 1;
  }
  
  /* Open the audio device. */
  spec.freq     = sample_rate;
  spec.format   = sample_format;
  spec.channels = channel_count;
  spec.samples  = samples;
  spec.callback = stream_callback;
  spec.userdata = (void *) stream;

  if (SDL_OpenAudio (&spec, NULL) < 0) {
    fprintf (stderr, "amaranth: cannot open audio device: %s\n",
             SDL_GetError ());
    return 1;
  }

  /* Start playing audio and engage infinite loop. */
  SDL_PauseAudio (0);
  for (;;) SDL_Delay (10000);

  /* NOT REACHED */
  return 0;
}
static void playOneSoundFile(const char *fname)
{
    PlaysoundAudioCallbackData data;

    memset(&data, '\0', sizeof (PlaysoundAudioCallbackData));
    data.sample = Sound_NewSampleFromFile(fname, NULL, 65536);
    if (data.sample == NULL)
    {
        fprintf(stderr, "Couldn't load '%s': %s.\n", fname, Sound_GetError());
        return;
    } /* if */

    /*
     * Open device in format of the the sound to be played.
     *  We open and close the device for each sound file, so that SDL
     *  handles the data conversion to hardware format; this is the
     *  easy way out, but isn't practical for most apps. Usually you'll
     *  want to pick one format for all the data or one format for the
     *  audio device and convert the data when needed. This is a more
     *  complex issue than I can describe in a source code comment, though.
     */
    data.devformat.freq = data.sample->actual.rate;
    data.devformat.format = data.sample->actual.format;
    data.devformat.channels = data.sample->actual.channels;
    data.devformat.samples = 4096;  /* I just picked a largish number here. */
    data.devformat.callback = audio_callback;
    data.devformat.userdata = &data;
    if (SDL_OpenAudio(&data.devformat, NULL) < 0)
    {
        fprintf(stderr, "Couldn't open audio device: %s.\n", SDL_GetError());
        Sound_FreeSample(data.sample);
        return;
    } /* if */

    printf("Now playing [%s]...\n", fname);
    SDL_PauseAudio(0);  /* SDL audio device is "paused" right after opening. */

    global_done_flag = 0;  /* the audio callback will flip this flag. */
    while (!global_done_flag)
        SDL_Delay(10);  /* just wait for the audio callback to finish. */

    /* at this point, we've played the entire audio file. */
    SDL_PauseAudio(1);  /* so stop the device. */

    /*
     * Sleep two buffers' worth of audio before closing, in order
     *  to allow the playback to finish. This isn't always enough;
     *   perhaps SDL needs a way to explicitly wait for device drain?
     * Most apps don't have this issue, since they aren't explicitly
     *  closing the device as soon as a sound file is done playback.
     * As an alternative for this app, you could also change the callback
     *  to write silence for a call or two before flipping global_done_flag.
     */
    SDL_Delay(2 * 1000 * data.devformat.samples / data.devformat.freq);

    /* if there was an error, tell the user. */
    if (data.sample->flags & SOUND_SAMPLEFLAG_ERROR)
        fprintf(stderr, "Error decoding file: %s\n", Sound_GetError());

    Sound_FreeSample(data.sample);  /* clean up SDL_Sound resources... */
    SDL_CloseAudio();  /* will reopen with next file's format. */
} /* playOneSoundFile */