+ if (!initialized) return -1;
+ if (sptr == NULL) return -1;
+ if (dsp != NULL) return -1;
+ if (channel == OPENAL_FREE)
+ {
+ for (int i = 0; i < num_channels; i++)
+ {
+ ALint state = 0;
+ alGetSourceiv(impl_channels[i].sid, AL_SOURCE_STATE, &state);
+ if ((state != AL_PLAYING) && (state != AL_PAUSED))
+ {
+ channel = i;
+ break;
+ }
+ }
+ }
+
+ if ((channel < 0) || (channel >= num_channels)) return -1;
+ alSourceStop(impl_channels[channel].sid);
+ impl_channels[channel].sample = sptr;
+ alSourcei(impl_channels[channel].sid, AL_BUFFER, sptr->bid);
+ alSourcei(impl_channels[channel].sid, AL_LOOPING, (sptr->mode == OPENAL_LOOP_OFF) ? AL_FALSE : AL_TRUE);
+ set_channel_position(channel, 0.0f, 0.0f, 0.0f);
+
+ impl_channels[channel].startpaused = ((startpaused) ? true : false);
+ if (!startpaused)
+ alSourcePlay(impl_channels[channel].sid);
+ return channel;
+}
+
+
+static void *decode_to_pcm(const char *_fname, ALenum &format, ALsizei &size, ALuint &freq)
+{
+#ifdef __POWERPC__
+ const int bigendian = 1;
+#else
+ const int bigendian = 0;
+#endif
+
+ // !!! FIXME: if it's not Ogg, we don't have a decoder. I'm lazy. :/
+ char *fname = (char *) alloca(strlen(_fname) + 16);
+ strcpy(fname, _fname);
+ char *ptr = strchr(fname, '.');
+ if (ptr) *ptr = '\0';
+ strcat(fname, ".ogg");
+
+ // just in case...
+ #undef fopen
+ FILE *io = fopen(fname, "rb");
+ if (io == NULL)
+ return NULL;
+
+ ALubyte *retval = NULL;
+
+ #if 0 // untested, so disable this!
+ // Can we just feed it to the AL compressed?
+ if (alIsExtensionPresent((const ALubyte *) "AL_EXT_vorbis"))
+ {
+ format = alGetEnumValue((const ALubyte *) "AL_FORMAT_VORBIS_EXT");
+ freq = 44100;
+ fseek(io, 0, SEEK_END);
+ size = ftell(io);
+ fseek(io, 0, SEEK_SET);
+ retval = (ALubyte *) malloc(size);
+ size_t rc = fread(retval, size, 1, io);
+ fclose(io);
+ if (rc != 1)
+ {
+ free(retval);
+ return NULL;
+ }
+ return retval;
+ }
+ #endif
+
+ // Uncompress and feed to the AL.
+ OggVorbis_File vf;
+ memset(&vf, '\0', sizeof (vf));
+ if (ov_open(io, &vf, NULL, 0) == 0)
+ {
+ int bitstream = 0;
+ vorbis_info *info = ov_info(&vf, -1);
+ size = 0;
+ format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
+ freq = info->rate;
+
+ if ((info->channels != 1) && (info->channels != 2))
+ {
+ ov_clear(&vf);
+ return NULL;
+ }
+
+ char buf[1024 * 16];
+ long rc = 0;
+ size_t allocated = 64 * 1024;
+ retval = (ALubyte *) malloc(allocated);
+ while ( (rc = ov_read(&vf, buf, sizeof (buf), bigendian, 2, 1, &bitstream)) != 0 )
+ {
+ if (rc > 0)
+ {
+ size += rc;
+ if (size >= allocated)
+ {
+ allocated *= 2;
+ ALubyte *tmp = (ALubyte *) realloc(retval, allocated);
+ if (tmp == NULL)
+ {
+ free(retval);
+ retval = NULL;
+ break;
+ }
+ retval = tmp;
+ }
+ memcpy(retval + (size - rc), buf, rc);
+ }
+ }
+ ov_clear(&vf);
+ return retval;
+ }
+
+ fclose(io);
+ return NULL;
+}
+
+
+AL_API OPENAL_SAMPLE *OPENAL_Sample_Load(int index, const char *name_or_data, unsigned int mode, int offset, int length)
+{
+ if (!initialized) return NULL;
+ if (index != OPENAL_FREE) return NULL; // this is all the game does...
+ if (offset != 0) return NULL; // this is all the game does...
+ if (length != 0) return NULL; // this is all the game does...
+ if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D)) return NULL; // this is all the game does...
+
+ OPENAL_SAMPLE *retval = NULL;
+ ALuint bufferName = 0;
+ ALenum format = AL_NONE;
+ ALsizei size = 0;
+ ALuint frequency = 0;
+ void *data = decode_to_pcm(name_or_data, format, size, frequency);
+ if (data == NULL)
+ return NULL;
+
+ ALuint bid = 0;
+ alGetError();
+ alGenBuffers(1, &bid);
+ if (alGetError() == AL_NO_ERROR)
+ {
+ alBufferData(bid, format, data, size, frequency);
+ retval = new OPENAL_SAMPLE;
+ retval->bid = bid;
+ retval->mode = OPENAL_LOOP_OFF;
+ retval->is2d = (mode == OPENAL_2D);
+ retval->name = new char[strlen(name_or_data) + 1];
+ if (retval->name)
+ strcpy(retval->name, name_or_data);
+ }
+
+ free(data);
+ return(retval);
+}
+
+AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE *sptr)
+{
+ if (!initialized) return;
+ if (sptr)
+ {
+ for (int i = 0; i < num_channels; i++)
+ {
+ if (impl_channels[i].sample == sptr)
+ {
+ alSourceStop(impl_channels[i].sid);
+ alSourcei(impl_channels[i].sid, AL_BUFFER, 0);
+ impl_channels[i].sample = NULL;
+ }
+ }
+ alDeleteBuffers(1, &sptr->bid);
+ delete[] sptr->name;
+ delete sptr;
+ }