+#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;
+ }
+}
+
+static signed char OPENAL_Sample_SetMode(OPENAL_SAMPLE *sptr, unsigned int mode)
+{
+ if (!initialized)
+ return false;
+ if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF))
+ return false;
+ if (!sptr)
+ return false;
+ sptr->mode = mode;
+ return true;
+}
+
+AL_API signed char OPENAL_SetFrequency(int channel, int freq)
+{
+ if (!initialized)
+ return false;
+ if (channel == OPENAL_ALL) {
+ for (int i = 0; i < num_channels; i++)
+ OPENAL_SetFrequency(i, freq);
+ return true;
+ }
+
+ if ((channel < 0) || (channel >= num_channels))
+ return false;
+ if (freq == 8012)
+ // hack
+ alSourcef(impl_channels[channel].sid, AL_PITCH, 8012.0f / 44100.0f);
+ else
+ alSourcef(impl_channels[channel].sid, AL_PITCH, 1.0f);
+ return true;
+}
+
+AL_API signed char OPENAL_SetVolume(int channel, int vol)
+{
+ if (!initialized)
+ return false;
+
+ if (channel == OPENAL_ALL) {
+ for (int i = 0; i < num_channels; i++)
+ OPENAL_SetVolume(i, vol);
+ return true;
+ }
+
+ if ((channel < 0) || (channel >= num_channels))
+ return false;
+
+ if (vol < 0)
+ vol = 0;
+ else if (vol > 255)
+ vol = 255;
+ ALfloat gain = ((ALfloat) vol) / 255.0f;
+ alSourcef(impl_channels[channel].sid, AL_GAIN, gain);
+ return true;