2012-12-29 10:57:24 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2009 Jonathon Fowler <jf@jonof.id.au>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2014-07-20 08:55:56 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2012-12-29 10:57:24 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FLAC source support for MultiVoc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_FLAC
|
|
|
|
|
2014-02-10 10:59:42 +00:00
|
|
|
#define FLAC__NO_DLL
|
|
|
|
|
2012-12-29 10:57:24 +00:00
|
|
|
#ifdef __APPLE__
|
2015-07-08 03:33:56 +00:00
|
|
|
#include <FLAC/all.h>
|
2012-12-29 10:57:24 +00:00
|
|
|
#else
|
2015-07-08 03:33:56 +00:00
|
|
|
#include "FLAC/all.h"
|
2012-12-29 10:57:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <errno.h>
|
|
|
|
#include "pitch.h"
|
|
|
|
#include "multivoc.h"
|
|
|
|
#include "_multivc.h"
|
|
|
|
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
size_t length;
|
|
|
|
size_t pos;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
FLAC__StreamDecoder *stream;
|
|
|
|
FLAC__uint64 sample_pos;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
char *block;
|
|
|
|
size_t blocksize;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
VoiceNode *owner;
|
2012-12-29 10:57:24 +00:00
|
|
|
} flac_data;
|
|
|
|
|
|
|
|
// callbacks, round 1
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
static size_t read_flac(void *ptr, size_t size, size_t nmemb, FLAC__IOHandle datasource)
|
2012-12-29 10:57:24 +00:00
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
flac_data *flac = (flac_data *)datasource;
|
|
|
|
size_t nread = 0;
|
|
|
|
size_t bytes;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
errno = 0;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
if (flac->length == flac->pos)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
for (; nmemb > 0; nmemb--, nread++)
|
|
|
|
{
|
|
|
|
bytes = flac->length - flac->pos;
|
|
|
|
if (size < bytes)
|
|
|
|
{
|
|
|
|
bytes = size;
|
|
|
|
}
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
memcpy(ptr, (uint8_t *)flac->ptr + flac->pos, bytes);
|
|
|
|
flac->pos += bytes;
|
|
|
|
ptr = (uint8_t *)ptr + bytes;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
if (flac->length == flac->pos)
|
|
|
|
{
|
|
|
|
nread++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
return nread;
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
static size_t write_flac(const void *ptr, size_t size, size_t nmemb, FLAC__IOHandle datasource)
|
2012-12-29 10:57:24 +00:00
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(ptr);
|
|
|
|
UNREFERENCED_PARAMETER(size);
|
|
|
|
UNREFERENCED_PARAMETER(nmemb);
|
|
|
|
UNREFERENCED_PARAMETER(datasource);
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
return 0;
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t seek_flac(FLAC__IOHandle datasource, FLAC__int64 offset, int32_t whence)
|
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
flac_data *flac = (flac_data *)datasource;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
switch (whence)
|
|
|
|
{
|
|
|
|
case SEEK_SET: flac->pos = 0; break;
|
|
|
|
case SEEK_CUR: break;
|
|
|
|
case SEEK_END: flac->pos = flac->length; break;
|
|
|
|
}
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
flac->pos += offset;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
if (flac->pos > flac->length)
|
|
|
|
{
|
|
|
|
flac->pos = flac->length;
|
|
|
|
}
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
return 0;
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static FLAC__int64 tell_flac(FLAC__IOHandle datasource)
|
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
flac_data *flac = (flac_data *)datasource;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
return flac->pos;
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static FLAC__int64 length_flac(FLAC__IOHandle datasource)
|
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
flac_data *flac = (flac_data *)datasource;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
return flac->length;
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t eof_flac(FLAC__IOHandle datasource)
|
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
flac_data *flac = (flac_data *)datasource;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
return (flac->pos == flac->length);
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t close_flac(FLAC__IOHandle datasource)
|
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
UNREFERENCED_PARAMETER(datasource);
|
|
|
|
return 0;
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static FLAC__IOCallbacks flac_callbacks = {
|
2015-07-08 03:33:56 +00:00
|
|
|
read_flac, write_flac, seek_flac, tell_flac, eof_flac, close_flac,
|
2012-12-29 10:57:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// callbacks, round 2
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
FLAC__StreamDecoderReadStatus read_flac_stream(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes,
|
|
|
|
void *client_data)
|
2012-12-29 10:57:24 +00:00
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
UNREFERENCED_PARAMETER(decoder);
|
|
|
|
if (*bytes > 0)
|
|
|
|
{
|
|
|
|
*bytes = read_flac(buffer, sizeof(FLAC__byte), *bytes, client_data);
|
|
|
|
if (errno)
|
|
|
|
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
|
|
|
|
else if (*bytes == 0)
|
|
|
|
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
|
|
|
|
else
|
|
|
|
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
|
|
|
|
}
|
2012-12-29 10:57:24 +00:00
|
|
|
else
|
2015-07-08 03:33:56 +00:00
|
|
|
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
FLAC__StreamDecoderSeekStatus seek_flac_stream(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset,
|
|
|
|
void *client_data)
|
2012-12-29 10:57:24 +00:00
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
UNREFERENCED_PARAMETER(decoder);
|
|
|
|
if (seek_flac(client_data, absolute_byte_offset, SEEK_SET) < 0)
|
|
|
|
return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
|
|
|
|
else
|
|
|
|
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
FLAC__StreamDecoderTellStatus tell_flac_stream(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset,
|
|
|
|
void *client_data)
|
2012-12-29 10:57:24 +00:00
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(decoder);
|
|
|
|
*absolute_byte_offset = tell_flac(client_data);
|
|
|
|
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
FLAC__StreamDecoderLengthStatus length_flac_stream(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length,
|
|
|
|
void *client_data)
|
2012-12-29 10:57:24 +00:00
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(decoder);
|
|
|
|
*stream_length = length_flac(client_data);
|
|
|
|
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
FLAC__bool eof_flac_stream(const FLAC__StreamDecoder *decoder, void *client_data)
|
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
UNREFERENCED_PARAMETER(decoder);
|
|
|
|
return eof_flac(client_data);
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
FLAC__StreamDecoderWriteStatus write_flac_stream(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
|
|
|
|
const FLAC__int32 *const ibuffer[], void *client_data)
|
2012-12-29 10:57:24 +00:00
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
flac_data *fd = (flac_data *)client_data;
|
2012-12-29 10:57:24 +00:00
|
|
|
VoiceNode *voice = fd->owner;
|
2015-04-24 00:09:42 +00:00
|
|
|
FLAC__uint64 samples = frame->header.blocksize;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
|
|
|
UNREFERENCED_PARAMETER(decoder);
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
voice->channels = frame->header.channels;
|
|
|
|
voice->bits = frame->header.bits_per_sample;
|
2012-12-29 10:57:24 +00:00
|
|
|
voice->SamplingRate = frame->header.sample_rate;
|
|
|
|
|
2015-04-24 00:09:42 +00:00
|
|
|
if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER)
|
|
|
|
fd->sample_pos = frame->header.number.frame_number;
|
|
|
|
else if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER)
|
|
|
|
fd->sample_pos = frame->header.number.sample_number;
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
if ((FLAC__uint64)(uintptr_t)voice->LoopEnd > 0 &&
|
|
|
|
fd->sample_pos + samples >= (FLAC__uint64)(uintptr_t)voice->LoopEnd)
|
2015-04-24 00:09:42 +00:00
|
|
|
{
|
|
|
|
samples = (FLAC__uint64)(uintptr_t)voice->LoopEnd - fd->sample_pos;
|
|
|
|
if (!FLAC__stream_decoder_seek_absolute(fd->stream, (FLAC__uint64)(uintptr_t)voice->LoopStart))
|
|
|
|
MV_Printf("MV_GetNextFLACBlock FLAC__stream_decoder_seek_absolute: LOOP_START %ul, LOOP_END %ul\n",
|
2015-07-08 03:33:56 +00:00
|
|
|
(FLAC__uint64)(uintptr_t)voice->LoopStart, (FLAC__uint64)(uintptr_t)voice->LoopEnd);
|
2015-04-24 00:09:42 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
voice->length = ((samples * (voice->bits / 8)) / 2) << voice->bits;
|
|
|
|
voice->position = 0;
|
|
|
|
voice->BlockLength = 0;
|
2012-12-29 10:59:21 +00:00
|
|
|
// CODEDUP multivoc.c MV_SetVoicePitch
|
2015-07-08 03:33:56 +00:00
|
|
|
voice->RateScale = (voice->SamplingRate * voice->PitchScale) / MV_MixRate;
|
|
|
|
voice->FixedPointBufferSize = (voice->RateScale * MV_MIXBUFFERSIZE) - voice->RateScale;
|
|
|
|
MV_SetVoiceMixMode(voice);
|
2012-12-29 10:57:24 +00:00
|
|
|
|
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
const size_t size = samples * voice->channels * (voice->bits / 8);
|
2012-12-29 10:57:24 +00:00
|
|
|
if (size > fd->blocksize)
|
|
|
|
{
|
|
|
|
fd->blocksize = size;
|
2015-07-08 03:33:56 +00:00
|
|
|
fd->block = (char *)realloc(fd->block, sizeof(char) * size);
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fd->block)
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
voice->sound = fd->block;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
char *obuffer = fd->block;
|
|
|
|
FLAC__uint64 sample;
|
|
|
|
uint8_t channel;
|
|
|
|
|
|
|
|
// this loop is adapted from code in ov_read_filter() in vorbisfile.c in libvorbis
|
2015-07-08 03:33:56 +00:00
|
|
|
for (sample = 0; sample < samples; ++sample)
|
|
|
|
for (channel = 0; channel < frame->header.channels; ++channel)
|
2012-12-29 10:57:24 +00:00
|
|
|
{
|
|
|
|
int8_t byte;
|
2015-07-08 03:33:56 +00:00
|
|
|
FLAC__int32 val = ibuffer[channel][sample];
|
|
|
|
if (val > (1 << (voice->bits - 1)) - 1)
|
|
|
|
val = (1 << (voice->bits - 1)) - 1;
|
|
|
|
else if (val < -(1 << (voice->bits - 1)))
|
|
|
|
val = -(1 << (voice->bits - 1));
|
|
|
|
for (byte = 0; byte < voice->bits; byte += 8) *obuffer++ = ((val >> byte) & 0x000000FF);
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void error_flac_stream(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
|
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
// flac_data * fd = (flac_data *) client_data;
|
2012-12-29 10:57:24 +00:00
|
|
|
UNREFERENCED_PARAMETER(client_data);
|
|
|
|
UNREFERENCED_PARAMETER(decoder);
|
|
|
|
MV_Printf("%s\n", FLAC__StreamDecoderErrorStatusString[status]);
|
2015-07-08 03:33:56 +00:00
|
|
|
// FLAC__stream_decoder_flush(fd->stream);
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
2015-01-17 00:28:49 +00:00
|
|
|
int32_t MV_GetFLACPosition(VoiceNode *voice)
|
|
|
|
{
|
|
|
|
FLAC__uint64 position = 0;
|
2015-07-08 03:33:56 +00:00
|
|
|
flac_data *fd = (flac_data *)voice->rawdataptr;
|
2015-01-17 00:28:49 +00:00
|
|
|
|
|
|
|
FLAC__stream_decoder_get_decode_position(fd->stream, &position);
|
|
|
|
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MV_SetFLACPosition(VoiceNode *voice, int32_t position)
|
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
flac_data *fd = (flac_data *)voice->rawdataptr;
|
2015-01-17 00:28:49 +00:00
|
|
|
|
|
|
|
FLAC__stream_decoder_seek_absolute(fd->stream, position);
|
|
|
|
}
|
|
|
|
|
2012-12-29 10:57:24 +00:00
|
|
|
/*---------------------------------------------------------------------
|
|
|
|
Function: MV_GetNextFLACBlock
|
|
|
|
|
|
|
|
Controls playback of FLAC data
|
|
|
|
---------------------------------------------------------------------*/
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
static playbackstatus MV_GetNextFLACBlock(VoiceNode *voice)
|
2012-12-29 10:57:24 +00:00
|
|
|
|
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
flac_data *fd = (flac_data *)voice->rawdataptr;
|
2012-12-29 10:57:24 +00:00
|
|
|
FLAC__StreamDecoderState decode_state;
|
2015-07-08 03:33:56 +00:00
|
|
|
// FLAC__bool decode_status;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
|
|
|
voice->Playing = TRUE;
|
|
|
|
|
|
|
|
if ((FLAC__uint64)(uintptr_t)voice->LoopEnd > 0 && fd->sample_pos >= (FLAC__uint64)(uintptr_t)voice->LoopEnd)
|
|
|
|
if (!FLAC__stream_decoder_seek_absolute(fd->stream, (FLAC__uint64)(uintptr_t)voice->LoopStart))
|
|
|
|
MV_Printf("MV_GetNextFLACBlock FLAC__stream_decoder_seek_absolute: LOOP_START %ul, LOOP_END %ul\n",
|
2015-07-08 03:33:56 +00:00
|
|
|
(FLAC__uint64)(uintptr_t)voice->LoopStart, (FLAC__uint64)(uintptr_t)voice->LoopEnd);
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
/*decode_status =*/FLAC__stream_decoder_process_single(fd->stream);
|
2012-12-29 10:57:24 +00:00
|
|
|
decode_state = FLAC__stream_decoder_get_state(fd->stream);
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
/*
|
|
|
|
if (!decode_status)
|
|
|
|
{
|
|
|
|
MV_Printf("MV_GetNextFLACBlock: %s\n", FLAC__StreamDecoderStateString[decode_state]);
|
|
|
|
voice->Playing = FALSE;
|
|
|
|
return NoMoreData;
|
|
|
|
}
|
|
|
|
*/
|
2012-12-29 10:57:24 +00:00
|
|
|
|
|
|
|
if (decode_state == FLAC__STREAM_DECODER_SEEK_ERROR)
|
|
|
|
{
|
|
|
|
FLAC__stream_decoder_flush(fd->stream);
|
|
|
|
decode_state = FLAC__stream_decoder_get_state(fd->stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decode_state == FLAC__STREAM_DECODER_END_OF_STREAM)
|
|
|
|
{
|
|
|
|
if (voice->LoopSize > 0)
|
|
|
|
{
|
|
|
|
if (!FLAC__stream_decoder_seek_absolute(fd->stream, (FLAC__uint64)(uintptr_t)voice->LoopStart))
|
|
|
|
MV_Printf("MV_GetNextFLACBlock FLAC__stream_decoder_seek_absolute: LOOP_START %ul\n",
|
2015-07-08 03:33:56 +00:00
|
|
|
(FLAC__uint64)(uintptr_t)voice->LoopStart);
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
voice->Playing = FALSE;
|
|
|
|
return NoMoreData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// unnecessary: duplicated in write_flac_stream()
|
|
|
|
voice->channels = FLAC__stream_decoder_get_channels(fd->stream);
|
|
|
|
voice->bits = FLAC__stream_decoder_get_bits_per_sample(fd->stream);
|
|
|
|
voice->SamplingRate = FLAC__stream_decoder_get_sample_rate(fd->stream);
|
2012-12-29 10:59:21 +00:00
|
|
|
// CODEDUP multivoc.c MV_SetVoicePitch
|
2012-12-29 10:57:24 +00:00
|
|
|
voice->RateScale = ( voice->SamplingRate * voice->PitchScale ) / MV_MixRate;
|
|
|
|
voice->FixedPointBufferSize = ( voice->RateScale * MV_MIXBUFFERSIZE ) - voice->RateScale;
|
|
|
|
MV_SetVoiceMixMode( voice );
|
|
|
|
#endif
|
|
|
|
|
2012-12-29 10:59:21 +00:00
|
|
|
return KeepPlaying;
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------
|
|
|
|
Function: MV_PlayFLAC3D
|
|
|
|
|
|
|
|
Begin playback of sound data at specified angle and distance
|
|
|
|
from listener.
|
|
|
|
---------------------------------------------------------------------*/
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
int32_t MV_PlayFLAC3D(char *ptr, uint32_t ptrlength, int32_t loophow, int32_t pitchoffset, int32_t angle,
|
|
|
|
int32_t distance, int32_t priority, uint32_t callbackval)
|
2012-12-29 10:57:24 +00:00
|
|
|
|
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
int32_t left;
|
|
|
|
int32_t right;
|
|
|
|
int32_t mid;
|
|
|
|
int32_t volume;
|
|
|
|
int32_t status;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
if (!MV_Installed)
|
|
|
|
{
|
|
|
|
MV_SetErrorCode(MV_NotInstalled);
|
|
|
|
return MV_Error;
|
|
|
|
}
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
if (distance < 0)
|
|
|
|
{
|
|
|
|
distance = -distance;
|
|
|
|
angle += MV_NUMPANPOSITIONS / 2;
|
|
|
|
}
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
volume = MIX_VOLUME(distance);
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
// Ensure angle is within 0 - 127
|
|
|
|
angle &= MV_MAXPANPOSITION;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
left = MV_PanTable[angle][volume].left;
|
|
|
|
right = MV_PanTable[angle][volume].right;
|
|
|
|
mid = max(0, 255 - distance);
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
status = MV_PlayFLAC(ptr, ptrlength, loophow, -1, pitchoffset, mid, left, right, priority, callbackval);
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
return status;
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------
|
|
|
|
Function: MV_PlayFLAC
|
|
|
|
|
|
|
|
Begin playback of sound data with the given sound levels and
|
|
|
|
priority.
|
|
|
|
---------------------------------------------------------------------*/
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
int32_t MV_PlayFLAC(char *ptr, uint32_t ptrlength, int32_t loopstart, int32_t loopend, int32_t pitchoffset, int32_t vol,
|
|
|
|
int32_t left, int32_t right, int32_t priority, uint32_t callbackval)
|
2012-12-29 10:57:24 +00:00
|
|
|
|
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
VoiceNode *voice;
|
|
|
|
flac_data *fd = 0;
|
|
|
|
FLAC__Metadata_Chain *metadata_chain;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
UNREFERENCED_PARAMETER(loopend);
|
2012-12-29 10:58:09 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
if (!MV_Installed)
|
|
|
|
{
|
|
|
|
MV_SetErrorCode(MV_NotInstalled);
|
|
|
|
return MV_Error;
|
|
|
|
}
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
fd = (flac_data *)calloc(1, sizeof(flac_data));
|
|
|
|
if (!fd)
|
|
|
|
{
|
2016-08-27 01:39:38 +00:00
|
|
|
MV_SetErrorCode(MV_InvalidFile);
|
2015-07-08 03:33:56 +00:00
|
|
|
return MV_Error;
|
|
|
|
}
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
fd->ptr = ptr;
|
|
|
|
fd->pos = 0;
|
|
|
|
fd->blocksize = 0;
|
|
|
|
fd->length = ptrlength;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
fd->block = NULL;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
fd->stream = FLAC__stream_decoder_new();
|
|
|
|
fd->sample_pos = 0;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
|
|
|
FLAC__stream_decoder_set_metadata_ignore_all(fd->stream);
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
if (FLAC__stream_decoder_init_stream(fd->stream, read_flac_stream, seek_flac_stream, tell_flac_stream,
|
|
|
|
length_flac_stream, eof_flac_stream, write_flac_stream,
|
|
|
|
/*metadata_flac_stream*/ NULL, error_flac_stream,
|
|
|
|
(void *)fd) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
|
|
|
|
{
|
|
|
|
MV_Printf("MV_PlayFLAC: %s\n", FLAC__stream_decoder_get_resolved_state_string(fd->stream));
|
2016-08-27 01:39:38 +00:00
|
|
|
MV_SetErrorCode(MV_InvalidFile);
|
2015-07-08 03:33:56 +00:00
|
|
|
return MV_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request a voice from the voice pool
|
|
|
|
voice = MV_AllocVoice(priority);
|
|
|
|
if (voice == NULL)
|
|
|
|
{
|
|
|
|
FLAC__stream_decoder_finish(fd->stream);
|
|
|
|
FLAC__stream_decoder_delete(fd->stream);
|
|
|
|
free(fd);
|
|
|
|
MV_SetErrorCode(MV_NoVoices);
|
|
|
|
return MV_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd->owner = voice;
|
|
|
|
|
|
|
|
voice->wavetype = FMT_FLAC;
|
|
|
|
voice->rawdataptr = (void *)fd;
|
|
|
|
voice->GetSound = MV_GetNextFLACBlock;
|
|
|
|
voice->NextBlock = fd->block;
|
|
|
|
voice->LoopCount = 0;
|
|
|
|
voice->BlockLength = 0;
|
|
|
|
voice->PitchScale = PITCH_GetScale(pitchoffset);
|
|
|
|
voice->next = NULL;
|
|
|
|
voice->prev = NULL;
|
|
|
|
voice->priority = priority;
|
|
|
|
voice->callbackval = callbackval;
|
|
|
|
|
|
|
|
voice->Playing = TRUE;
|
|
|
|
voice->Paused = FALSE;
|
|
|
|
|
|
|
|
voice->LoopStart = 0;
|
|
|
|
voice->LoopEnd = 0;
|
|
|
|
voice->LoopSize = (loopstart >= 0 ? 1 : 0);
|
2012-12-29 10:57:24 +00:00
|
|
|
|
|
|
|
// parse metadata
|
|
|
|
// loop parsing designed with multiple repetitions in mind
|
2015-07-08 03:33:56 +00:00
|
|
|
// In retrospect, it may be possible to MV_GetVorbisCommentLoops(voice, (vorbis_comment *)
|
|
|
|
// &tags->data.vorbis_comment)
|
|
|
|
// but libvorbisfile may be confused by the signedness of char* vs FLAC__byte* and this code does not depend on
|
|
|
|
// HAVE_VORBIS.
|
2012-12-29 10:57:24 +00:00
|
|
|
metadata_chain = FLAC__metadata_chain_new();
|
|
|
|
if (metadata_chain != NULL)
|
|
|
|
{
|
|
|
|
if (FLAC__metadata_chain_read_with_callbacks(metadata_chain, fd, flac_callbacks))
|
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
FLAC__Metadata_Iterator *metadata_iterator = FLAC__metadata_iterator_new();
|
2012-12-29 10:57:24 +00:00
|
|
|
if (metadata_iterator != NULL)
|
|
|
|
{
|
|
|
|
char *vc_loopstart = NULL;
|
|
|
|
char *vc_loopend = NULL;
|
|
|
|
char *vc_looplength = NULL;
|
|
|
|
|
|
|
|
FLAC__metadata_iterator_init(metadata_iterator, metadata_chain);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
FLAC__StreamMetadata *tags = FLAC__metadata_iterator_get_block(metadata_iterator);
|
|
|
|
|
|
|
|
if (tags->type == FLAC__METADATA_TYPE_STREAMINFO)
|
|
|
|
{
|
|
|
|
const FLAC__StreamMetadata_StreamInfo *info = &tags->data.stream_info;
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
if (info->channels != 1 && info->channels != 2)
|
|
|
|
{
|
|
|
|
FLAC__metadata_object_delete(tags);
|
|
|
|
FLAC__metadata_iterator_delete(metadata_iterator);
|
|
|
|
// FLAC__metadata_chain_delete(metadata_chain);
|
|
|
|
FLAC__stream_decoder_finish(fd->stream);
|
|
|
|
FLAC__stream_decoder_delete(fd->stream);
|
|
|
|
free(fd);
|
2016-08-27 01:39:38 +00:00
|
|
|
MV_SetErrorCode(MV_InvalidFile);
|
2015-07-08 03:33:56 +00:00
|
|
|
return MV_Error;
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
voice->channels = info->channels;
|
|
|
|
voice->bits = info->bits_per_sample;
|
2012-12-29 10:57:24 +00:00
|
|
|
voice->SamplingRate = info->sample_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load loop tags from metadata
|
2012-12-29 10:58:09 +00:00
|
|
|
if (tags->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
|
2012-12-29 10:57:24 +00:00
|
|
|
{
|
|
|
|
FLAC__uint32 comment;
|
|
|
|
for (comment = 0; comment < tags->data.vorbis_comment.num_comments; ++comment)
|
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
const char *entry = (const char *)tags->data.vorbis_comment.comments[comment].entry;
|
2012-12-29 10:57:24 +00:00
|
|
|
if (entry != NULL && entry[0] != '\0')
|
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
const char *value = strchr(entry, '=');
|
|
|
|
const size_t field = value - entry;
|
2012-12-29 10:57:24 +00:00
|
|
|
value += 1;
|
|
|
|
|
2016-10-14 07:40:59 +00:00
|
|
|
for (size_t t = 0; t < loopStartTagCount && vc_loopstart == NULL; ++t)
|
|
|
|
{
|
|
|
|
char const * const tag = loopStartTags[t];
|
|
|
|
if (field == strlen(tag) && strncasecmp(entry, tag, field) == 0)
|
2012-12-29 10:57:24 +00:00
|
|
|
vc_loopstart = strdup(value);
|
2016-10-14 07:40:59 +00:00
|
|
|
}
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2016-10-14 07:40:59 +00:00
|
|
|
for (size_t t = 0; t < loopEndTagCount && vc_loopend == NULL; ++t)
|
|
|
|
{
|
|
|
|
char const * const tag = loopEndTags[t];
|
|
|
|
if (field == strlen(tag) && strncasecmp(entry, tag, field) == 0)
|
2012-12-29 10:57:24 +00:00
|
|
|
vc_loopend = strdup(value);
|
2016-10-14 07:40:59 +00:00
|
|
|
}
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2016-10-14 07:40:59 +00:00
|
|
|
for (size_t t = 0; t < loopLengthTagCount && vc_looplength == NULL; ++t)
|
|
|
|
{
|
|
|
|
char const * const tag = loopLengthTags[t];
|
|
|
|
if (field == strlen(tag) && strncasecmp(entry, tag, field) == 0)
|
2012-12-29 10:57:24 +00:00
|
|
|
vc_looplength = strdup(value);
|
2016-10-14 07:40:59 +00:00
|
|
|
}
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
FLAC__metadata_object_delete(
|
|
|
|
tags); // If it were not for this, I would assign pointers instead of strdup().
|
|
|
|
} while (FLAC__metadata_iterator_next(metadata_iterator));
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2012-12-29 10:58:09 +00:00
|
|
|
if (vc_loopstart != NULL)
|
2012-12-29 10:57:24 +00:00
|
|
|
{
|
|
|
|
{
|
2012-12-29 10:58:09 +00:00
|
|
|
const FLAC__int64 flac_loopstart = atol(vc_loopstart);
|
2015-07-08 03:33:56 +00:00
|
|
|
if (flac_loopstart >= 0) // a loop starting at 0 is valid
|
2012-12-29 10:57:24 +00:00
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
voice->LoopStart = (const char *)(intptr_t)flac_loopstart;
|
2012-12-29 10:58:09 +00:00
|
|
|
voice->LoopSize = 1;
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-29 10:58:09 +00:00
|
|
|
free(vc_loopstart);
|
|
|
|
}
|
|
|
|
if (vc_loopend != NULL)
|
|
|
|
{
|
|
|
|
if (voice->LoopSize > 0)
|
2012-12-29 10:57:24 +00:00
|
|
|
{
|
2012-12-29 10:58:09 +00:00
|
|
|
const FLAC__int64 flac_loopend = atol(vc_loopend);
|
2015-07-08 03:33:56 +00:00
|
|
|
if (flac_loopend > 0) // a loop ending at 0 is invalid
|
|
|
|
voice->LoopEnd = (const char *)(intptr_t)flac_loopend;
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
2012-12-29 10:58:09 +00:00
|
|
|
free(vc_loopend);
|
|
|
|
}
|
|
|
|
if (vc_looplength != NULL)
|
|
|
|
{
|
|
|
|
if (voice->LoopSize > 0 && voice->LoopEnd == 0)
|
2012-12-29 10:57:24 +00:00
|
|
|
{
|
2012-12-29 10:58:09 +00:00
|
|
|
const FLAC__int64 flac_looplength = atol(vc_looplength);
|
2015-07-08 03:33:56 +00:00
|
|
|
if (flac_looplength > 0) // a loop of length 0 is invalid
|
|
|
|
voice->LoopEnd = (const char *)((intptr_t)flac_looplength + (intptr_t)voice->LoopStart);
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
2012-12-29 10:58:09 +00:00
|
|
|
free(vc_looplength);
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FLAC__metadata_iterator_delete(metadata_iterator);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
MV_Printf("Error allocating FLAC__Metadata_Iterator!\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
MV_Printf("%s\n", FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(metadata_chain)]);
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
// FLAC__metadata_chain_delete(metadata_chain); // when run with GDB, this throws SIGTRAP about freed heap
|
|
|
|
// memory being modified
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
MV_Printf("Error allocating FLAC__Metadata_Chain!\n");
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
// CODEDUP multivoc.c MV_SetVoicePitch
|
|
|
|
voice->RateScale = (voice->SamplingRate * voice->PitchScale) / MV_MixRate;
|
|
|
|
voice->FixedPointBufferSize = (voice->RateScale * MV_MIXBUFFERSIZE) - voice->RateScale;
|
|
|
|
MV_SetVoiceMixMode(voice);
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
MV_SetVoiceVolume(voice, vol, left, right);
|
|
|
|
MV_PlayVoice(voice);
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
return voice->handle;
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
void MV_ReleaseFLACVoice(VoiceNode *voice)
|
2012-12-29 10:57:24 +00:00
|
|
|
{
|
2015-07-08 03:33:56 +00:00
|
|
|
flac_data *fd = (flac_data *)voice->rawdataptr;
|
2012-12-29 10:57:24 +00:00
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
if (voice->wavetype != FMT_FLAC)
|
|
|
|
{
|
2012-12-29 10:57:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd->stream != NULL)
|
|
|
|
{
|
|
|
|
FLAC__stream_decoder_finish(fd->stream);
|
|
|
|
FLAC__stream_decoder_delete(fd->stream);
|
|
|
|
}
|
2015-09-23 17:55:31 +00:00
|
|
|
free(fd->block);
|
2012-12-29 10:57:24 +00:00
|
|
|
free(fd);
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
voice->rawdataptr = 0;
|
|
|
|
}
|
|
|
|
#else
|
2015-07-08 03:34:09 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "_multivc.h"
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
int32_t MV_PlayFLAC(char *ptr, uint32_t ptrlength, int32_t loopstart, int32_t loopend, int32_t pitchoffset,
|
|
|
|
int32_t vol, int32_t left, int32_t right, int32_t priority, uint32_t callbackval)
|
|
|
|
{
|
2016-01-12 10:31:05 +00:00
|
|
|
UNREFERENCED_PARAMETER(ptr);
|
|
|
|
UNREFERENCED_PARAMETER(ptrlength);
|
|
|
|
UNREFERENCED_PARAMETER(loopstart);
|
|
|
|
UNREFERENCED_PARAMETER(loopend);
|
|
|
|
UNREFERENCED_PARAMETER(pitchoffset);
|
|
|
|
UNREFERENCED_PARAMETER(vol);
|
|
|
|
UNREFERENCED_PARAMETER(left);
|
|
|
|
UNREFERENCED_PARAMETER(right);
|
|
|
|
UNREFERENCED_PARAMETER(priority);
|
|
|
|
UNREFERENCED_PARAMETER(callbackval);
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
MV_Printf("MV_PlayFLAC: FLAC support not included in this binary.\n");
|
|
|
|
return -1;
|
2012-12-29 10:57:24 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
int32_t MV_PlayFLAC3D(char *ptr, uint32_t ptrlength, int32_t loophow, int32_t pitchoffset, int32_t angle,
|
|
|
|
int32_t distance, int32_t priority, uint32_t callbackval)
|
|
|
|
{
|
2016-01-12 10:31:05 +00:00
|
|
|
UNREFERENCED_PARAMETER(ptr);
|
|
|
|
UNREFERENCED_PARAMETER(ptrlength);
|
|
|
|
UNREFERENCED_PARAMETER(loophow);
|
|
|
|
UNREFERENCED_PARAMETER(pitchoffset);
|
|
|
|
UNREFERENCED_PARAMETER(angle);
|
|
|
|
UNREFERENCED_PARAMETER(distance);
|
|
|
|
UNREFERENCED_PARAMETER(priority);
|
|
|
|
UNREFERENCED_PARAMETER(callbackval);
|
|
|
|
|
2015-07-08 03:33:56 +00:00
|
|
|
MV_Printf("MV_PlayFLAC: FLAC support not included in this binary.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif // HAVE_FLAC
|