From daaa68cea255cc07e30b957e720af453578385a4 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 5 Aug 2018 20:39:30 +1000 Subject: [PATCH] start porting audio to C client --- ClassicalSharp/Audio/Soundboard.cs | 2 - src/Client/Audio.c | 1572 ++++++---------------------- src/Client/Audio.h | 40 - src/Client/Client.vcxproj | 10 +- src/Client/Client.vcxproj.filters | 20 +- src/Client/Platform.h | 11 + src/Client/Vorbis.c | 1337 +++++++++++++++++++++++ src/Client/Vorbis.h | 49 + src/Client/WinPlatform.c | 94 ++ 9 files changed, 1839 insertions(+), 1296 deletions(-) create mode 100644 src/Client/Vorbis.c create mode 100644 src/Client/Vorbis.h diff --git a/ClassicalSharp/Audio/Soundboard.cs b/ClassicalSharp/Audio/Soundboard.cs index 9cce30060..6655ab002 100644 --- a/ClassicalSharp/Audio/Soundboard.cs +++ b/ClassicalSharp/Audio/Soundboard.cs @@ -16,8 +16,6 @@ namespace ClassicalSharp.Audio { } public class Soundboard { - - public byte[] Data; List groups = new List(); Random rnd = new Random(); diff --git a/src/Client/Audio.c b/src/Client/Audio.c index 13e786586..8e5f0433f 100644 --- a/src/Client/Audio.c +++ b/src/Client/Audio.c @@ -5,1333 +5,413 @@ #include "Block.h" #include "ExtMath.h" #include "Funcs.h" +#include "Block.h" +#include "Game.h" /*########################################################################################################################* -*-------------------------------------------------------Ogg stream--------------------------------------------------------* +*------------------------------------------------------Soundboard---------------------------------------------------------* *#########################################################################################################################*/ -#define OGG_FourCC(a, b, c, d) (((UInt32)a << 24) | ((UInt32)b << 16) | ((UInt32)c << 8) | (UInt32)d) -static ReturnCode Ogg_NextPage(struct Stream* stream) { - UInt8 header[27]; - struct Stream* source = stream->Meta.Ogg.Source; - Stream_Read(source, header, sizeof(header)); +struct Sound { + struct AudioFormat Format; + UInt8* Data; UInt32 DataSize; +}; - UInt32 sig = Stream_GetU32_BE(&header[0]); - if (sig != OGG_FourCC('O','g','g','S')) return OGG_ERR_INVALID_SIG; - if (header[4] != 0) return OGG_ERR_VERSION; - UInt8 bitflags = header[5]; - /* (8) granule position */ - /* (4) serial number */ - /* (4) page sequence number */ - /* (4) page checksum */ +#define AUDIO_MAX_SOUNDS 10 +struct SoundGroup { + String Name; UInt8 Count; + struct Sound Sounds[AUDIO_MAX_SOUNDS]; +}; - Int32 i, numSegments = header[26]; - UInt8 segments[255]; - Stream_Read(source, segments, numSegments); +struct Soundboard { + Random Rnd; UInt8 Count; + struct SoundGroup Groups[AUDIO_MAX_SOUNDS]; +}; - UInt32 dataSize = 0; - for (i = 0; i < numSegments; i++) { dataSize += segments[i]; } - Stream_Read(source, stream->Meta.Ogg.Base, dataSize); +#define WAV_FourCC(a, b, c, d) (((UInt32)a << 24) | ((UInt32)b << 16) | ((UInt32)c << 8) | (UInt32)d) +enum WAV_ERR { + WAV_ERR_STREAM_HDR = 329701, WAV_ERR_STREAM_TYPE, WAV_ERR_DATA_TYPE, WAV_ERR_NO_DATA +}; - stream->Meta.Ogg.Cur = stream->Meta.Ogg.Base; - stream->Meta.Ogg.Left = dataSize; - stream->Meta.Ogg.Last = bitflags & 4; - return 0; -} +static ReturnCode Sound_ReadWaveData(struct Stream* stream, struct Sound* snd) { + UInt32 fourCC, size, pos, len; + ReturnCode res; -static ReturnCode Ogg_Read(struct Stream* stream, UInt8* data, UInt32 count, UInt32* modified) { - for (;;) { - if (stream->Meta.Ogg.Left) { - count = min(count, stream->Meta.Ogg.Left); - Platform_MemCpy(data, stream->Meta.Ogg.Cur, count); + fourCC = Stream_ReadU32_LE(&stream); + if (fourCC != WAV_FourCC('R','I','F','F')) return WAV_ERR_STREAM_HDR; + Stream_ReadU32_LE(&stream); /* file size, but we don't care */ + fourCC = Stream_ReadU32_LE(&stream); + if (fourCC != WAV_FourCC('W','A','V','E')) return WAV_ERR_STREAM_TYPE; - *modified = count; - stream->Meta.Ogg.Cur += count; - stream->Meta.Ogg.Left -= count; + while (!(res = stream->Position(&stream, &pos)) && !(res = stream->Length(&stream, &len)) && len < pos) { + fourCC = Stream_ReadU32_LE(&stream); + size = Stream_ReadU32_LE(&stream); + + if (fourCC == WAV_FourCC('f','m','t',' ')) { + if (Stream_GetU16_LE(&stream) != 1) return WAV_ERR_DATA_TYPE; + + snd->Format.Channels = Stream_ReadU16_LE(&stream); + snd->Format.Frequency = Stream_ReadU32_LE(&stream); + Stream_Skip(&stream, 6); + snd->Format.BitsPerSample = Stream_ReadU16_LE(&stream); + size -= 16; + } else if (fourCC == WAV_FourCC('d','a','t','a')) { + snd->Data = Platform_MemAlloc(size, sizeof(UInt8), "WAV sound data"); + Stream_Read(&stream, snd->Data, size); return 0; } - /* try again with data from next page*/ - *modified = 0; - if (stream->Meta.Ogg.Last) return 0; - - ReturnCode result = Ogg_NextPage(stream); - if (result != 0) return result; + /* Skip over unhandled data */ + if (size) Stream_Skip(&stream, size); } + return res ? res : WAV_ERR_NO_DATA; } -void Ogg_MakeStream(struct Stream* stream, UInt8* buffer, struct Stream* source) { - Stream_Init(stream, &source->Name); - stream->Read = Ogg_Read; +static ReturnCode Sound_ReadWave(STRING_PURE String* filename, struct Sound* snd) { + UChar pathBuffer[String_BufferSize(FILENAME_SIZE)]; + String path = String_InitAndClearArray(pathBuffer); + String_Format2(&path, "audio%r%s", &Platform_DirectorySeparator, filename); - stream->Meta.Ogg.Cur = buffer; - stream->Meta.Ogg.Base = buffer; - stream->Meta.Ogg.Left = 0; - stream->Meta.Ogg.Last = 0; - stream->Meta.Ogg.Source = source; + void* file = NULL; + ReturnCode result = Platform_FileOpen(&file, &path); + if (result) return result; + ReturnCode fileResult = 0; + + struct Stream stream; Stream_FromFile(&stream, file, &path); + { + fileResult = Sound_ReadWaveData(&stream, snd); + } + + result = stream.Close(&stream); + return fileResult ? fileResult : result; } - -/*########################################################################################################################* -*------------------------------------------------------Vorbis utils-------------------------------------------------------* -*#########################################################################################################################*/ -/* Insert next byte into the bit buffer */ -#define Vorbis_GetByte(ctx) ctx->Bits |= (UInt32)Stream_ReadU8(ctx->Source) << ctx->NumBits; ctx->NumBits += 8; -/* Retrieves bits from the bit buffer */ -#define Vorbis_PeekBits(ctx, bits) (ctx->Bits & ((1UL << (bits)) - 1UL)) -/* Consumes/eats up bits from the bit buffer */ -#define Vorbis_ConsumeBits(ctx, bits) ctx->Bits >>= (bits); ctx->NumBits -= (bits); -/* Aligns bit buffer to be on a byte boundary */ -#define Vorbis_AlignBits(ctx) UInt32 alignSkip = ctx->NumBits & 7; Vorbis_ConsumeBits(ctx, alignSkip); -/* Ensures there are 'bitsCount' bits */ -#define Vorbis_EnsureBits(ctx, bitsCount) while (ctx->NumBits < bitsCount) { Vorbis_GetByte(ctx); } -/* Peeks then consumes given bits */ -/* TODO: Make this an inline macro somehow */ -static UInt32 Vorbis_ReadBits(struct VorbisState* ctx, UInt32 bitsCount) { - Vorbis_EnsureBits(ctx, bitsCount); - UInt32 result = Vorbis_PeekBits(ctx, bitsCount); Vorbis_ConsumeBits(ctx, bitsCount); - return result; -} - -#define VORBIS_MAX_CHANS 8 -#define Vorbis_ChanData(ctx, ch) (ctx->Values + (ch) * ctx->DataSize) - -static Int32 iLog(Int32 x) { - Int32 bits = 0; - while (x > 0) { bits++; x >>= 1; } - return bits; -} - -static Real32 float32_unpack(struct VorbisState* ctx) { - /* encoder macros can't reliably read over 24 bits */ - UInt32 lo = Vorbis_ReadBits(ctx, 16); - UInt32 hi = Vorbis_ReadBits(ctx, 16); - UInt32 x = (hi << 16) | lo; - - Int32 mantissa = x & 0x1fffff; - UInt32 exponent = (x & 0x7fe00000) >> 21; - if (x & 0x80000000UL) mantissa = -mantissa; - - #define LOG_2 0.693147180559945 - /* TODO: Can we make this more accurate? maybe ldexp ?? */ - return (Real32)(mantissa * Math_Exp(LOG_2 * ((Int32)exponent - 788))); /* pow(2, x) */ -} - - -/*########################################################################################################################* -*----------------------------------------------------Vorbis codebooks-----------------------------------------------------* -*#########################################################################################################################*/ -#define CODEBOOK_SYNC 0x564342 -struct Codebook { - UInt32 Dimensions, Entries, NumCodewords; - UInt32* Codewords; - UInt8* CodewordLens; - UInt32* Values; - /* vector quantisation values */ - Real32 MinValue, DeltaValue; - UInt32 SequenceP, LookupType, LookupValues; - UInt16* Multiplicands; -}; - -static UInt32 Codebook_Pow(UInt32 base, UInt32 exp) { - UInt32 result = 1; /* exponentiation by squaring */ - while (exp) { - if (exp & 1) result *= base; - exp >>= 1; - base *= base; - } - return result; -} - -static UInt32 lookup1_values(UInt32 entries, UInt32 dimensions) { - UInt32 i; - /* the greatest integer value for which [value] to the power of [dimensions] is less than or equal to [entries] */ - /* TODO: verify this */ - for (i = 1; ; i++) { - UInt32 pow = Codebook_Pow(i, dimensions); - UInt32 next = Codebook_Pow(i + 1, dimensions); - - if (next < pow) return i; /* overflow */ - if (pow == entries) return i; - if (next > entries) return i; - } - return 0; -} - -static bool Codebook_CalcCodewords(struct Codebook* c, UInt8* len) { - c->Codewords = Platform_MemAlloc(c->NumCodewords, sizeof(UInt32), "codewords"); - c->CodewordLens = Platform_MemAlloc(c->NumCodewords, sizeof(UInt8), "raw codeword lens"); - c->Values = Platform_MemAlloc(c->NumCodewords, sizeof(UInt32), "values"); - - /* This is taken from stb_vorbis.c because I gave up trying */ - UInt32 i, j, depth; - UInt32 next_codewords[33] = { 0 }; - - /* add codeword 0 to tree */ - for (i = 0; i < c->Entries; i++) { - if (len[i] == UInt8_MaxValue) continue; - - c->Codewords[0] = 0; - c->CodewordLens[0] = len[i]; - c->Values[0] = i; - break; - } - - /* set codewords that new nodes can start from */ - for (depth = 1; depth <= len[i]; depth++) { - next_codewords[depth] = 1U << (32 - depth); - } - - i++; /* first codeword was already handled */ - for (j = 1; i < c->Entries; i++) { - UInt32 root = len[i]; - if (root == UInt8_MaxValue) continue; - - /* per spec, find lowest possible value (leftmost) */ - while (root && next_codewords[root] == 0) root--; - if (root == 0) return false; - - UInt32 codeword = next_codewords[root]; - next_codewords[root] = 0; - - c->Codewords[j] = codeword; - c->CodewordLens[j] = len[i]; - c->Values[j] = i; - - for (depth = len[i]; depth > root; depth--) { - next_codewords[depth] = codeword + (1U << (32 - depth)); - } - j++; - } - return true; -} - -static ReturnCode Codebook_DecodeSetup(struct VorbisState* ctx, struct Codebook* c) { - UInt32 sync = Vorbis_ReadBits(ctx, 24); - if (sync != CODEBOOK_SYNC) return VORBIS_ERR_CODEBOOK_SYNC; - c->Dimensions = Vorbis_ReadBits(ctx, 16); - c->Entries = Vorbis_ReadBits(ctx, 24); - - UInt8* codewordLens = Platform_MemAlloc(c->Entries, sizeof(UInt8), "raw codeword lens"); - Int32 i, ordered = Vorbis_ReadBits(ctx, 1), usedEntries = 0; - - if (!ordered) { - Int32 sparse = Vorbis_ReadBits(ctx, 1); - for (i = 0; i < c->Entries; i++) { - if (sparse) { - Int32 flag = Vorbis_ReadBits(ctx, 1); - if (!flag) { - codewordLens[i] = UInt8_MaxValue; - continue; /* unused entry */ - } - } - - Int32 len = Vorbis_ReadBits(ctx, 5); len++; - codewordLens[i] = len; - usedEntries++; - } - } else { - Int32 entry; - Int32 curLength = Vorbis_ReadBits(ctx, 5); curLength++; - for (entry = 0; entry < c->Entries;) { - Int32 runBits = iLog(c->Entries - entry); - Int32 runLen = Vorbis_ReadBits(ctx, runBits); - - for (i = entry; i < entry + runLen; i++) { - codewordLens[i] = curLength; - } - - entry += runLen; - curLength++; - if (entry > c->Entries) return VORBIS_ERR_CODEBOOK_ENTRY; - } - usedEntries = c->Entries; - } - c->NumCodewords = usedEntries; - - static int booknummm; - Platform_Log1("### BUILDING %i ###", &booknummm); - Codebook_CalcCodewords(c, codewordLens); - Platform_MemFree(&codewordLens); - booknummm++; - - c->LookupType = Vorbis_ReadBits(ctx, 4); - if (c->LookupType == 0) return 0; - if (c->LookupType > 2) return VORBIS_ERR_CODEBOOK_LOOKUP; - - c->MinValue = float32_unpack(ctx); - c->DeltaValue = float32_unpack(ctx); - Int32 valueBits = Vorbis_ReadBits(ctx, 4); valueBits++; - c->SequenceP = Vorbis_ReadBits(ctx, 1); - - UInt32 lookupValues; - if (c->LookupType == 1) { - lookupValues = lookup1_values(c->Entries, c->Dimensions); - } else { - lookupValues = c->Entries * c->Dimensions; - } - c->LookupValues = lookupValues; - - c->Multiplicands = Platform_MemAlloc(lookupValues, sizeof(UInt16), "multiplicands"); - for (i = 0; i < lookupValues; i++) { - c->Multiplicands[i] = Vorbis_ReadBits(ctx, valueBits); - } - return 0; -} - -static UInt32 Codebook_DecodeScalar(struct VorbisState* ctx, struct Codebook* c) { - UInt32 codeword = 0, shift = 31, depth, i; - /* TODO: This is so massively slow */ - for (depth = 1; depth <= 32; depth++, shift--) { - //codeword >>= 1; - UInt32 bit = Vorbis_ReadBits(ctx, 1); - codeword |= bit << shift; - for (i = 0; i < c->NumCodewords; i++) { - if (depth != c->CodewordLens[i]) continue; - if (codeword != c->Codewords[i]) continue; - - UInt32 value = c->Values[i]; - //Platform_Log2("read value %i of len %i", &value, &depth); - return value; - } - } - ErrorHandler_Fail("????????????"); - return -1; -} - -static void Codebook_DecodeVectors(struct VorbisState* ctx, struct Codebook* c, Real32* v, Int32 step) { - UInt32 lookupOffset = Codebook_DecodeScalar(ctx, c); - Real32 last = 0.0f, value; - UInt32 i, offset; - - if (c->LookupType == 1) { - UInt32 indexDivisor = 1; - for (i = 0; i < c->Dimensions; i++, v += step) { - offset = (lookupOffset / indexDivisor) % c->LookupValues; - value = c->Multiplicands[offset] * c->DeltaValue + c->MinValue + last; - - *v += value; - if (c->SequenceP) last = value; - indexDivisor *= c->LookupValues; - } - } else if (c->LookupType == 2) { - offset = lookupOffset * c->Dimensions; - for (i = 0; i < c->Dimensions; i++, offset++, v += step) { - value = c->Multiplicands[offset] * c->DeltaValue + c->MinValue + last; - - *v += value; - if (c->SequenceP) last = value; - } - } else { - ErrorHandler_Fail("???????"); - } -} - -/*########################################################################################################################* -*-----------------------------------------------------Vorbis floors-------------------------------------------------------* -*#########################################################################################################################*/ -#define FLOOR_MAX_PARTITIONS 32 -#define FLOOR_MAX_CLASSES 16 -#define FLOOR_MAX_VALUES (FLOOR_MAX_PARTITIONS * 8 + 2) -struct Floor { - UInt8 Partitions, Multiplier; Int32 Range, Values; - UInt8 PartitionClasses[FLOOR_MAX_PARTITIONS]; - UInt8 ClassDimensions[FLOOR_MAX_CLASSES]; - UInt8 ClassSubClasses[FLOOR_MAX_CLASSES]; - UInt8 ClassMasterbooks[FLOOR_MAX_CLASSES]; - Int16 SubclassBooks[FLOOR_MAX_CLASSES][8]; - Int16 XList[FLOOR_MAX_VALUES]; - UInt16 ListOrder[FLOOR_MAX_VALUES]; - Int32 YList[VORBIS_MAX_CHANS][FLOOR_MAX_VALUES]; -}; - -/* TODO: Make this thread safe */ -Int16* tmp_xlist; -UInt16* tmp_order; -static void Floor_SortXList(Int32 left, Int32 right) { - UInt16* values = tmp_order; UInt16 value; - Int16* keys = tmp_xlist; Int16 key; - while (left < right) { - Int32 i = left, j = right; - Int16 pivot = keys[(i + j) / 2]; - - /* partition the list */ - while (i <= j) { - while (pivot > keys[i]) i++; - while (pivot < keys[j]) j--; - QuickSort_Swap_KV_Maybe(); - } - /* recurse into the smaller subset */ - QuickSort_Recurse(Floor_SortXList) - } -} - -static ReturnCode Floor_DecodeSetup(struct VorbisState* ctx, struct Floor* f) { - f->Partitions = Vorbis_ReadBits(ctx, 5); - Int32 i, j, idx, maxClass = -1; - - for (i = 0; i < f->Partitions; i++) { - f->PartitionClasses[i] = Vorbis_ReadBits(ctx, 4); - maxClass = max(maxClass, f->PartitionClasses[i]); - } - - for (i = 0; i <= maxClass; i++) { - f->ClassDimensions[i] = Vorbis_ReadBits(ctx, 3); f->ClassDimensions[i]++; - f->ClassSubClasses[i] = Vorbis_ReadBits(ctx, 2); - if (f->ClassSubClasses[i]) { - f->ClassMasterbooks[i] = Vorbis_ReadBits(ctx, 8); - } - for (j = 0; j < (1 << f->ClassSubClasses[i]); j++) { - f->SubclassBooks[i][j] = Vorbis_ReadBits(ctx, 8); f->SubclassBooks[i][j]--; - } - } - - f->Multiplier = Vorbis_ReadBits(ctx, 2); f->Multiplier++; - static Int16 ranges[4] = { 256, 128, 84, 64 }; - f->Range = ranges[f->Multiplier - 1]; - - UInt32 rangeBits = Vorbis_ReadBits(ctx, 4); - f->XList[0] = 0; - f->XList[1] = 1 << rangeBits; - - for (i = 0, idx = 2; i < f->Partitions; i++) { - Int32 classNum = f->PartitionClasses[i]; - for (j = 0; j < f->ClassDimensions[classNum]; j++) { - f->XList[idx++] = Vorbis_ReadBits(ctx, rangeBits); - } - } - f->Values = idx; - - /* sort X list for curve computation later */ - Int16 xlist_sorted[FLOOR_MAX_VALUES]; - Platform_MemCpy(xlist_sorted, f->XList, idx * sizeof(Int16)); - for (i = 0; i < idx; i++) { f->ListOrder[i] = i; } - - tmp_xlist = xlist_sorted; - tmp_order = f->ListOrder; - Floor_SortXList(0, idx - 1); - return 0; -} - -static bool Floor_DecodeFrame(struct VorbisState* ctx, struct Floor* f, Int32 ch) { - Int32 nonZero = Vorbis_ReadBits(ctx, 1); - if (!nonZero) return false; - Int32* yList = f->YList[ch]; - - Int32 i, j, idx, rangeBits = iLog(f->Range - 1); - yList[0] = Vorbis_ReadBits(ctx, rangeBits); - yList[1] = Vorbis_ReadBits(ctx, rangeBits); - - for (i = 0, idx = 2; i < f->Partitions; i++) { - UInt8 class = f->PartitionClasses[i]; - UInt8 cdim = f->ClassDimensions[class]; - UInt8 cbits = f->ClassSubClasses[class]; - - UInt32 csub = (1 << cbits) - 1; - UInt32 cval = 0; - if (cbits) { - UInt8 bookNum = f->ClassMasterbooks[class]; - cval = Codebook_DecodeScalar(ctx, &ctx->Codebooks[bookNum]); - } - - for (j = 0; j < cdim; j++) { - Int16 bookNum = f->SubclassBooks[class][cval & csub]; - cval >>= cbits; - if (bookNum >= 0) { - yList[idx + j] = Codebook_DecodeScalar(ctx, &ctx->Codebooks[bookNum]); - } else { - yList[idx + j] = 0; - } - } - idx += cdim; - } - return true; -} - -static Int32 render_point(Int32 x0, Int32 y0, Int32 x1, Int32 y1, Int32 X) { - Int32 dy = y1 - y0, adx = x1 - x0; - Int32 ady = Math_AbsI(dy); - Int32 err = ady * (X - x0); - Int32 off = err / adx; - - if (dy < 0) { - return y0 - off; - } else { - return y0 + off; - } -} - -static Real32 floor1_inverse_dB_table[256]; -static void render_line(Int32 x0, Int32 y0, Int32 x1, Int32 y1, Real32* data) { - Int32 dy = y1 - y0, adx = x1 - x0; - Int32 ady = Math_AbsI(dy); - Int32 base = dy / adx, sy; - Int32 x = x0, y = y0, err = 0; - - if (dy < 0) { - sy = base - 1; - } else { - sy = base + 1; - } - - ady = ady - Math_AbsI(base) * adx; - data[x] *= floor1_inverse_dB_table[y]; - - for (x = x0 + 1; x < x1; x++) { - err = err + ady; - if (err >= adx) { - err = err - adx; - y = y + sy; - } else { - y = y + base; - } - data[x] *= floor1_inverse_dB_table[y]; - } -} - -static Int32 low_neighbor(Int16* v, Int32 x) { - Int32 n = 0, i, max = Int32_MinValue; - for (i = 0; i < x; i++) { - if (v[i] < v[x] && v[i] > max) { n = i; max = v[i]; } - } - return n; -} - -static Int32 high_neighbor(Int16* v, Int32 x) { - Int32 n = 0, i, min = Int32_MaxValue; - for (i = 0; i < x; i++) { - if (v[i] > v[x] && v[i] < min) { n = i; min = v[i]; } - } - return n; -} - -static void Floor_Synthesis(struct VorbisState* ctx, struct Floor* f, Int32 ch) { - /* amplitude value synthesis */ - Int32 YFinal[FLOOR_MAX_VALUES]; - bool Step2[FLOOR_MAX_VALUES]; - - Real32* data = Vorbis_ChanData(ctx, ch); - Int32* yList = f->YList[ch]; - - Step2[0] = true; - Step2[1] = true; - YFinal[0] = yList[0]; - YFinal[1] = yList[1]; - +static struct SoundGroup* Soundboard_Find(struct Soundboard* board, STRING_PURE String* name) { Int32 i; - for (i = 2; i < f->Values; i++) { - Int32 lo_offset = low_neighbor(f->XList, i); - Int32 hi_offset = high_neighbor(f->XList, i); + struct SoundGroup* groups = board->Groups; - Int32 predicted = render_point(f->XList[lo_offset], YFinal[lo_offset], - f->XList[hi_offset], YFinal[hi_offset], f->XList[i]); - - Int32 val = yList[i]; - Int32 highroom = f->Range - predicted; - Int32 lowroom = predicted, room; - - if (highroom < lowroom) { - room = highroom * 2; - } else { - room = lowroom * 2; - } - - if (val) { - Step2[lo_offset] = true; - Step2[hi_offset] = true; - Step2[i] = true; - - if (val >= room) { - if (highroom > lowroom) { - YFinal[i] = val - lowroom + predicted; - } else { - YFinal[i] = predicted - val + highroom - 1; - } - } else { - if (val & 1) { - YFinal[i] = predicted - (val + 1) / 2; - } else { - YFinal[i] = predicted + val / 2; - } - } - } else { - Step2[i] = false; - YFinal[i] = predicted; - } + for (i = 0; i < Array_Elems(board->Groups); i++) { + if (String_CaselessEquals(&groups[i].Name, name)) return &groups[i]; } - - /* curve synthesis */ - Int32 hx = 0, lx = 0, rawI; - Int32 ly = YFinal[f->ListOrder[0]] * f->Multiplier, hy = ly; - - for (rawI = 1; rawI < f->Values; rawI++) { - i = f->ListOrder[rawI]; - if (!Step2[i]) continue; - - hx = f->XList[i]; hy = YFinal[i] * f->Multiplier; - if (lx < hx) { - render_line(lx, ly, min(hx, ctx->DataSize), hy, data); - } - lx = hx; ly = hy; - } - - /* fill remainder of floor with a flat line */ - /* TODO: Is this right? should hy be 0, if Step2 is false for all */ - if (hx >= ctx->DataSize) return; - lx = hx; hx = ctx->DataSize; - Real32 value = floor1_inverse_dB_table[hy]; - for (; lx < hx; lx++) { data[lx] *= value; } + return NULL; } - -/*########################################################################################################################* -*----------------------------------------------------Vorbis residues------------------------------------------------------* -*#########################################################################################################################*/ -#define RESIDUE_MAX_CLASSIFICATIONS 65 -struct Residue { - UInt8 Type, Classifications, Classbook; - UInt32 Begin, End, PartitionSize; - UInt8 Cascade[RESIDUE_MAX_CLASSIFICATIONS]; - Int16 Books[RESIDUE_MAX_CLASSIFICATIONS][8]; -}; - -static ReturnCode Residue_DecodeSetup(struct VorbisState* ctx, struct Residue* r, UInt16 type) { - r->Type = type; - r->Begin = Vorbis_ReadBits(ctx, 24); - r->End = Vorbis_ReadBits(ctx, 24); - r->PartitionSize = Vorbis_ReadBits(ctx, 24); r->PartitionSize++; - r->Classifications = Vorbis_ReadBits(ctx, 6); r->Classifications++; - r->Classbook = Vorbis_ReadBits(ctx, 8); - +static void Soundboard_Init(struct Soundboard* board, STRING_PURE String* boardName, StringsBuffer* files) { Int32 i; - for (i = 0; i < r->Classifications; i++) { - r->Cascade[i] = Vorbis_ReadBits(ctx, 3); - Int32 moreBits = Vorbis_ReadBits(ctx, 1); - if (!moreBits) continue; - Int32 bits = Vorbis_ReadBits(ctx, 5); - r->Cascade[i] |= bits << 3; - } + for (i = 0; i < files->Count; i++) { + String name = StringsBuffer_UNSAFE_Get(files, i); + Utils_UNSAFE_GetFilename(&name); - Int32 j; - for (i = 0; i < r->Classifications; i++) { - for (j = 0; j < 8; j++) { - Int16 codebook = -1; - if (r->Cascade[i] & (1 << j)) { - codebook = Vorbis_ReadBits(ctx, 8); - } - r->Books[i][j] = codebook; + /* dig_grass1.wav -> dig_grass1 */ + Int32 dotIndex = String_LastIndexOf(&name, '.'); + if (dotIndex >= 0) { name = String_UNSAFE_Substring(0, &name, dotIndex); } + if (!String_CaselessStarts(&name, boardName)) continue; + + /* Convert dig_grass1 to grass */ + name = String_UNSAFE_SubstringAt(&name, boardName->length); + name = String_UNSAFE_Substring(&name, 0, name.length - 1); + + struct SoundGroup* group = Soundboard_Find(board, &name); + if (group == NULL) { + if (board->Count == AUDIO_MAX_SOUNDS) ErrorHandler_Fail("Soundboard_Init - too many sounds"); + group = &board->Groups[board->Count++]; + group.Name = name; } - } - return 0; -} -static void Residue_DecodeCore(struct VorbisState* ctx, struct Residue* r, UInt32 size, Int32 ch, bool* doNotDecode, Real32** data) { - UInt32 residueBeg = min(r->Begin, size); - UInt32 residueEnd = min(r->End, size); - Int32 pass, i, j, k; - - struct Codebook* classbook = &ctx->Codebooks[r->Classbook]; - UInt32 classwordsPerCodeword = classbook->Dimensions; - UInt32 nToRead = residueEnd - residueBeg; - UInt32 partitionsToRead = nToRead / r->PartitionSize; - - UInt8* classifications_raw = Platform_MemAlloc(ch * partitionsToRead * classbook->Dimensions, sizeof(UInt8), "temp classicifcations"); - UInt8* classifications[VORBIS_MAX_CHANS]; /* TODO ????? */ - for (i = 0; i < ch; i++) { - classifications[i] = classifications_raw + (i * partitionsToRead * classbook->Dimensions); - } - - if (nToRead == 0) return; - for (pass = 0; pass < 8; pass++) { - UInt32 partitionCount = 0; - while (partitionCount < partitionsToRead) { - - /* read classifications in pass 0 */ - if (pass == 0) { - for (j = 0; j < ch; j++) { - if (doNotDecode[j]) continue; - - UInt32 temp = Codebook_DecodeScalar(ctx, classbook); - for (i = classwordsPerCodeword - 1; i >= 0; i--) { - classifications[j][i + partitionCount] = temp % r->Classifications; - temp /= r->Classifications; - } - } - } - - for (i = 0; i < classwordsPerCodeword && partitionCount < partitionsToRead; i++) { - for (j = 0; j < ch; j++) { - if (doNotDecode[j]) continue; - UInt8 class = classifications[j][partitionCount]; - Int16 book = r->Books[class][pass]; - if (book < 0) continue; - - UInt32 offset = residueBeg + partitionCount * r->PartitionSize; - Real32* v = data[j] + offset; - struct Codebook* c = &ctx->Codebooks[book]; - - if (r->Type == 0) { - Int32 step = r->PartitionSize / c->Dimensions; - for (k = 0; k < step; k++) { - Codebook_DecodeVectors(ctx, c, v, step); v++; - } - } else { - for (k = 0; k < r->PartitionSize; k += c->Dimensions) { - Codebook_DecodeVectors(ctx, c, v, 1); v += c->Dimensions; - } - } - } - partitionCount++; - } - } - } -} - -static void Residue_DecodeFrame(struct VorbisState* ctx, struct Residue* r, Int32 ch, bool* doNotDecode, Real32** data) { - UInt32 size = ctx->DataSize; - if (r->Type == 2) { - bool decodeAny = false; - Int32 i, j; - - /* type 2 decodes all channel vectors 2, if at least 1 channel to decode */ - for (i = 0; i < ch; i++) { - if (!doNotDecode[i]) decodeAny = true; - } - if (!decodeAny) return; - - decodeAny = false; /* because DecodeCore expects this to be 'false' for 'do not decode' */ - Real32* interleaved = Platform_MemAllocCleared(ctx->DataSize * ctx->Channels, sizeof(Real32), "residue 2 temp"); - Residue_DecodeCore(ctx, r, size * ch, 1, &decodeAny, &interleaved); - - /* de interleave type 2 output */ - for (i = 0; i < size; i++) { - for (j = 0; j < ch; j++) { - data[j][i] = interleaved[i * ch + j]; - } - } - } else { - Residue_DecodeCore(ctx, r, size, ch, doNotDecode, data); + Sound snd = ReadWave(files[i]); + group.Sounds.Add(snd); } } /*########################################################################################################################* -*----------------------------------------------------Vorbis mappings------------------------------------------------------* +*-----------------------------------------------------Audio sounds--------------------------------------------------------* *#########################################################################################################################*/ -#define MAPPING_MAX_COUPLINGS 256 -#define MAPPING_MAX_SUBMAPS 15 -struct Mapping { - UInt8 CouplingSteps, Submaps; - UInt8 Mux[VORBIS_MAX_CHANS]; - UInt8 FloorIdx[MAPPING_MAX_SUBMAPS]; - UInt8 ResidueIdx[MAPPING_MAX_SUBMAPS]; - UInt8 Magnitude[MAPPING_MAX_COUPLINGS]; - UInt8 Angle[MAPPING_MAX_COUPLINGS]; -}; +struct Soundboard digBoard, stepBoard; +#define AUDIO_MAX_HANDLES 6 -static ReturnCode Mapping_DecodeSetup(struct VorbisState* ctx, struct Mapping* m) { - Int32 i, submaps = 1, submapFlag = Vorbis_ReadBits(ctx, 1); - if (submapFlag) { - submaps = Vorbis_ReadBits(ctx, 4); submaps++; +void Audio_SetSounds(Int32 volume) { + if (volume > 0) InitSound(); + else DisposeSound(); +} + +static void Audio_InitSound(void) { + if (digBoard == null) InitSoundboards(); + monoOutputs = new IAudioOutput[maxSounds]; + stereoOutputs = new IAudioOutput[maxSounds]; +} + +static void Audio_InitSoundboards(void) { + digBoard = new Soundboard(); + digBoard.Init("dig_", files); + stepBoard = new Soundboard(); + stepBoard.Init("step_", files); +} + +static void Audio_PlayBlockSound(object sender, BlockChangedEventArgs e) { + if (e.Block == BLOCK_AIR) { + PlayDigSound(Block_DigSounds[e.OldBlock]); + } else if (!Game_ClassicMode) { + PlayDigSound(Block_StepSounds[e.Block]); } +} - Int32 couplingSteps = 0, couplingFlag = Vorbis_ReadBits(ctx, 1); - if (couplingFlag) { - couplingSteps = Vorbis_ReadBits(ctx, 8); couplingSteps++; - /* TODO: How big can couplingSteps ever really get in practice? */ - Int32 couplingBits = iLog(ctx->Channels - 1); - for (i = 0; i < couplingSteps; i++) { - m->Magnitude[i] = Vorbis_ReadBits(ctx, couplingBits); - m->Angle[i] = Vorbis_ReadBits(ctx, couplingBits); - if (m->Magnitude[i] == m->Angle[i]) return VORBIS_ERR_MAPPING_CHANS; - } - } +AudioChunk chunk = new AudioChunk(); +static void PlaySound(byte type, Soundboard board) { + if (type == SoundType.None || monoOutputs == null) return; + Sound snd = board.PickRandomSound(type); + if (snd == null) return; - Int32 reserved = Vorbis_ReadBits(ctx, 2); - if (reserved != 0) return VORBIS_ERR_MAPPING_RESERVED; - m->Submaps = submaps; - m->CouplingSteps = couplingSteps; + chunk.Channels = snd.Channels; + chunk.BitsPerSample = snd.BitsPerSample; + chunk.BytesOffset = 0; + chunk.BytesUsed = snd.Data.Length; + chunk.Data = snd.Data; - if (submaps > 1) { - for (i = 0; i < ctx->Channels; i++) { - m->Mux[i] = Vorbis_ReadBits(ctx, 4); - } + float volume = game.SoundsVolume / 100.0f; + if (board == digBoard) { + if (type == SoundType.Metal) chunk.SampleRate = (snd.SampleRate * 6) / 5; + else chunk.SampleRate = (snd.SampleRate * 4) / 5; } else { - for (i = 0; i < ctx->Channels; i++) { - m->Mux[i] = 0; + volume *= 0.50f; + + if (type == SoundType.Metal) chunk.SampleRate = (snd.SampleRate * 7) / 5; + else chunk.SampleRate = snd.SampleRate; + } + + if (snd.Channels == 1) { + PlayCurrentSound(monoOutputs, volume); + } else if (snd.Channels == 2) { + PlayCurrentSound(stereoOutputs, volume); + } +} + +void Audio_PlayDigSound(UInt8 type) { Audio_PlaySound(type, &digBoard); } +void Audio_PlayStepSound(UInt8 type) { Audio_PlaySound(type, &stepBoard); } + + +IAudioOutput firstSoundOut; +static void PlayCurrentSound(IAudioOutput[] outputs, float volume) { + for (int i = 0; i < monoOutputs.Length; i++) { + IAudioOutput output = outputs[i]; + if (output == null) output = MakeSoundOutput(outputs, i); + if (!output.DoneRawAsync()) continue; + + LastChunk l = output.Last; + if (l.Channels == 0 || (l.Channels == chunk.Channels && l.BitsPerSample == chunk.BitsPerSample + && l.SampleRate == chunk.SampleRate)) { + PlaySound(output, volume); return; } } - for (i = 0; i < submaps; i++) { - Vorbis_ReadBits(ctx, 8); /* time value */ - m->FloorIdx[i] = Vorbis_ReadBits(ctx, 8); - m->ResidueIdx[i] = Vorbis_ReadBits(ctx, 8); + // This time we try to play the sound on all possible devices, + // even if it requires the expensive case of recreating a device + for (int i = 0; i < monoOutputs.Length; i++) { + IAudioOutput output = outputs[i]; + if (!output.DoneRawAsync()) continue; + + PlaySound(output, volume); return; } - return 0; } -/*########################################################################################################################* -*------------------------------------------------------imdct impl---------------------------------------------------------* -*#########################################################################################################################*/ -#define PI MATH_PI -void imdct(Real32* in, Real32* out, Int32 N) { - Int32 i, k; +static IAudioOutput MakeSoundOutput(IAudioOutput[] outputs, int i) { + IAudioOutput output = GetPlatformOut(); + output.Create(1, firstSoundOut); + if (firstSoundOut == null) + firstSoundOut = output; - for (i = 0; i < 2 * N; i++) { - Real64 sum = 0; - for (k = 0; k < N; k++) { - sum += in[k] * Math_Cos((PI / N) * (i + 0.5 + N * 0.5) * (k + 0.5)); + outputs[i] = output; + return output; +} + +static void PlaySound(IAudioOutput output, float volume) { + try { + output.SetVolume(volume); + output.PlayRawAsync(chunk); + } catch (InvalidOperationException ex) { + ErrorHandler.LogError("AudioPlayer.PlayCurrentSound()", ex); + if (ex.Message == "No audio devices found") + game.Chat.Add("&cNo audio devices found, disabling sounds."); + else + game.Chat.Add("&cAn error occured when trying to play sounds, disabling sounds."); + + SetSounds(0); + game.SoundsVolume = 0; + } +} + +static void DisposeSound() { + DisposeOutputs(ref monoOutputs); + DisposeOutputs(ref stereoOutputs); + if (firstSoundOut != null) { + firstSoundOut.Dispose(); + firstSoundOut = null; + } +} + +static void DisposeOutputs(ref IAudioOutput[] outputs) { + if (outputs == null) return; + bool soundPlaying = true; + + while (soundPlaying) { + soundPlaying = false; + for (int i = 0; i < outputs.Length; i++) { + if (outputs[i] == null) continue; + soundPlaying |= !outputs[i].DoneRawAsync(); } - out[i] = sum; + if (soundPlaying) + Thread.Sleep(1); } + + for (int i = 0; i < outputs.Length; i++) { + if (outputs[i] == null || outputs[i] == firstSoundOut) continue; + outputs[i].Dispose(); + } + outputs = null; } -static UInt32 ReverseBits(UInt32 v) { - v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1); - v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2); - v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4); - v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8); - v = (v >> 16) | (v << 16); - return v; -} +// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 +using System; +using System.IO; +using System.Threading; +using SharpWave; +using SharpWave.Codecs.Vorbis; -static UInt32 Log2(UInt32 v) { - UInt32 r = 0; - while (v >>= 1) r++; - return r; -} +namespace ClassicalSharp.Audio{ -#define VORBIS_MAX_BLOCK_SIZE 8192 -void imdct_fast(Real32* in, Real32* out, Int32 n) { - Int32 n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, n3_4 = n - n4; - Int32 k, k2, k4, i, j; + public sealed partial class AudioPlayer : IGameComponent { - /* Optimised algorithm from "The use of multirate filter banks for coding of high quality digital audio" */ - /* Uses a few fixes for the paper noted at http://www.nothings.org/stb_vorbis/mdct_01.txt */ + IAudioOutput musicOut; + IAudioOutput[] monoOutputs, stereoOutputs; + string[] files, musicFiles; + Thread musicThread; + Game game; - Real32 A[VORBIS_MAX_BLOCK_SIZE / 2]; - Real32 B[VORBIS_MAX_BLOCK_SIZE / 2]; - Real32 C[VORBIS_MAX_BLOCK_SIZE / 4]; - /* setup twiddle factors */ - for (k = 0, k2 = 0; k < n4; k++, k2 += 2) { - A[k2] = Math_Cos((4*k * PI) / n); - A[k2+1] = -Math_Sin((4*k * PI) / n); - B[k2] = Math_Cos(((k2+1) * PI) / (2*n)); - B[k2+1] = Math_Sin(((k2+1) * PI) / (2*n)); - } - for (k = 0, k2 = 0; k < n8; k++, k2 += 2) { - C[k2] = Math_Cos(((k2+1) * (2*PI)) / n); - C[k2+1] = -Math_Sin(((k2+1) * (2*PI)) / n); - } - - Real32 u[VORBIS_MAX_BLOCK_SIZE]; - Real32 v[VORBIS_MAX_BLOCK_SIZE]; - Real32 w[VORBIS_MAX_BLOCK_SIZE]; - Real32 X[VORBIS_MAX_BLOCK_SIZE]; - /* spectral coefficients */ - for (k = 0; k < n2; k++) u[k] = in[k]; - for ( ; k < n; k++) u[k] = -in[n-k-1]; - - /* step 1 */ - for (k = 0, k2 = 0, k4 = 0; k < n4; k++, k2 += 2, k4 += 4) { - v[n-k4-1] = (u[k4] - u[n-k4-1]) * A[k2] - (u[k4+2] - u[n-k4-3]) * A[k2 + 1]; - v[n-k4-3] = (u[k4] - u[n-k4-1]) * A[k2+1] + (u[k4+2] - u[n-k4-3]) * A[k2]; - } - - /* step 2 */ - for (k = 0, k4 = 0; k < n8; k++, k4 += 4) { - w[n2+3+k4] = v[n2+3+k4] + v[k4+3]; - w[n2+1+k4] = v[n2+1+k4] + v[k4+1]; - - w[k4+3] = (v[n2+3+k4] - v[k4+3]) * A[n2-4-k4] - (v[n2+1+k4] - v[k4+1]) * A[n2-3-k4]; - w[k4+1] = (v[n2+1+k4] - v[k4+1]) * A[n2-4-k4] + (v[n2+3+k4] - v[k4+3]) * A[n2-3-k4]; - } - - /* step 3*/ - Int32 l, ld_n = Log2(n); - for (l = 0; l <= ld_n - 4; l++) { - Int32 k0 = n >> (l+2), k1 = 1 << (l+3); - Int32 r, r4, rMax = n >> (l+4), s2, s2Max = 1 << (l+2); - - for (r = 0, r4 = 0; r < rMax; r++, r4 += 4) { - for (s2 = 0; s2 < s2Max; s2 += 2) { - u[n-1-k0*s2-r4] = w[n-1-k0*s2-r4] + w[n-1-k0*(s2+1)-r4]; - u[n-3-k0*s2-r4] = w[n-3-k0*s2-r4] + w[n-3-k0*(s2+1)-r4]; - - u[n-1-k0*(s2+1)-r4] = (w[n-1-k0*s2-r4] - w[n-1-k0*(s2+1)-r4]) * A[r*k1] - - (w[n-3-k0*s2-r4] - w[n-3-k0*(s2+1)-r4]) * A[r*k1+1]; - u[n-3-k0*(s2+1)-r4] = (w[n-3-k0*s2-r4] - w[n-3-k0*(s2+1)-r4]) * A[r*k1] - + (w[n-1-k0*s2-r4] - w[n-1-k0*(s2+1)-r4]) * A[r*k1+1]; - } + void IGameComponent.Init(Game game) { + this.game = game; + if (Platform.DirectoryExists("audio")) { + files = Platform.DirectoryFiles("audio"); + } else { + files = new string[0]; } - if (l+1 <= ld_n - 4) { - Platform_MemCpy(w, u, sizeof(u)); - } + game.MusicVolume = GetVolume(OptionsKey.MusicVolume, OptionsKey.UseMusic); + SetMusic(game.MusicVolume); + game.SoundsVolume = GetVolume(OptionsKey.SoundsVolume, OptionsKey.UseSound); + SetSounds(game.SoundsVolume); + game.UserEvents.BlockChanged += PlayBlockSound; } - /* step 4 */ - for (i = 0; i < n8; i++) { - j = ReverseBits(i) >> (32-ld_n+3); - if (i == j) { - Int32 i8 = i << 3; - v[i8+1] = u[i8+1]; v[i8+3] = u[i8+3]; - v[i8+5] = u[i8+5]; v[i8+7] = u[i8+7]; - } else if (i < j) { - Int32 i8 = i << 3, j8 = j << 3; - v[j8+1] = u[i8+1]; v[i8+1] = u[j8+1]; - v[j8+3] = u[i8+3]; v[i8+3] = u[j8+3]; - v[j8+5] = u[i8+5]; v[i8+5] = u[j8+5]; - v[j8+7] = u[i8+7]; v[i8+7] = u[j8+7]; - } + static int GetVolume(string volKey, string boolKey) { + int volume = Options.GetInt(volKey, 0, 100, 0); + if (volume != 0) return volume; + + volume = Options.GetBool(boolKey, false) ? 100 : 0; + Options.Set(boolKey, null); + return volume; } - /* step 5 */ - for (k = 0, k2 = 0; k < n2; k++, k2 += 2) { - w[k] = v[k2+1]; + public void SetMusic(int volume) { + if (volume > 0) InitMusic(); + else DisposeMusic(); } - /* step 6 */ - for (k = 0, k2 = 0, k4 = 0; k < n8; k++, k2 += 2, k4 += 4) { - u[n-1-k2] = w[k4]; - u[n-2-k2] = w[k4+1]; - u[n3_4-1-k2] = w[k4+2]; - u[n3_4-2-k2] = w[k4+3]; - } + void InitMusic() { + if (musicThread != null) { musicOut.SetVolume(game.MusicVolume / 100.0f); return; } - /* step 7 */ - for (k = 0, k2 = 0; k < n8; k++, k2 += 2) { - v[n2+k2] = ( u[n2+k2] + u[n-2-k2] + C[k2+1] * (u[n2+k2] - u[n-2-k2]) + C[k2] * (u[n2+k2+1] + u[n-2-k2+1])) * 0.5f; - v[n-2-k2] = ( u[n2+k2] + u[n-2-k2] - C[k2+1] * (u[n2+k2] - u[n-2-k2]) - C[k2] * (u[n2+k2+1] + u[n-2-k2+1])) * 0.5f; - v[n2+k2+1] = ( u[n2+k2+1] - u[n-1-k2] + C[k2+1] * (u[n2+1+k2] + u[n-1-k2]) - C[k2] * (u[n2+k2] - u[n-2-k2])) * 0.5f; - v[n-1-k2] = (-u[n2+k2+1] + u[n-1-k2] + C[k2+1] * (u[n2+1+k2] + u[n-1-k2]) - C[k2] * (u[n2+k2] - u[n-2-k2])) * 0.5f; - } - - /* step 8 */ - for (k = 0, k2 = 0; k < n4; k++, k2 += 2) { - X[k] = v[k2+n2] * B[k2] + v[k2+1+n2] * B[k2+1]; - X[n2-1-k] = v[k2+n2] * B[k2+1] - v[k2+1+n2] * B[k2]; - } - - /* output */ - for (k = 0; k < n4; k++) out[k] = 0.5f * X[k+n4]; - for ( ; k < n3_4; k++) out[k] = 0.5f * -X[n3_4-k-1]; - for ( ; k < n; k++) out[k] = 0.5f * -X[k-n3_4]; -} - - -/*########################################################################################################################* -*-----------------------------------------------------Vorbis setup--------------------------------------------------------* -*#########################################################################################################################*/ -struct Mode { UInt8 BlockSizeFlag, MappingIdx; }; -static ReturnCode Mode_DecodeSetup(struct VorbisState* ctx, struct Mode* m) { - m->BlockSizeFlag = Vorbis_ReadBits(ctx, 1); - UInt16 windowType = Vorbis_ReadBits(ctx, 16); - if (windowType != 0) return VORBIS_ERR_MODE_WINDOW; - - UInt16 transformType = Vorbis_ReadBits(ctx, 16); - if (transformType != 0) return VORBIS_ERR_MODE_TRANSFORM; - m->MappingIdx = Vorbis_ReadBits(ctx, 8); - return 0; -} - -void Vorbis_Init(struct VorbisState* ctx, struct Stream* source) { - Platform_MemSet(ctx, 0, sizeof(struct VorbisState)); - ctx->Source = source; -} - -/* TODO: Work out Vorbis_Free implementation */ - -static bool Vorbis_ValidBlockSize(UInt32 blockSize) { - return blockSize >= 64 && blockSize <= 8192 && Math_IsPowOf2(blockSize); -} - -static ReturnCode Vorbis_DecodeHeader(struct VorbisState* ctx, UInt8 type) { - UInt8 header[7]; - Stream_Read(ctx->Source, header, sizeof(header)); - if (header[0] != type) return VORBIS_ERR_WRONG_HEADER; - - bool OK = - header[1] == 'v' && header[2] == 'o' && header[3] == 'r' && - header[4] == 'b' && header[5] == 'i' && header[6] == 's'; - return OK ? 0 : ReturnCode_InvalidArg; -} - -static ReturnCode Vorbis_DecodeIdentifier(struct VorbisState* ctx) { - UInt8 header[23]; - Stream_Read(ctx->Source, header, sizeof(header)); - UInt32 version = Stream_GetU32_LE(&header[0]); - if (version != 0) return VORBIS_ERR_VERSION; - - ctx->Channels = header[4]; - ctx->SampleRate = Stream_GetU32_LE(&header[5]); - /* (12) bitrate_maximum, nominal, minimum */ - ctx->BlockSizes[0] = 1 << (header[21] & 0xF); - ctx->BlockSizes[1] = 1 << (header[21] >> 4); - - if (!Vorbis_ValidBlockSize(ctx->BlockSizes[0])) return VORBIS_ERR_BLOCKSIZE; - if (!Vorbis_ValidBlockSize(ctx->BlockSizes[1])) return VORBIS_ERR_BLOCKSIZE; - if (ctx->BlockSizes[0] > ctx->BlockSizes[1]) return VORBIS_ERR_BLOCKSIZE; - - if (ctx->Channels == 0 || ctx->Channels > VORBIS_MAX_CHANS) return VORBIS_ERR_CHANS; - /* check framing flag */ - return (header[22] & 1) ? 0 : VORBIS_ERR_FRAMING; -} - -static ReturnCode Vorbis_DecodeComments(struct VorbisState* ctx) { - UInt32 vendorLen = Stream_ReadU32_LE(ctx->Source); - Stream_Skip(ctx->Source, vendorLen); - - UInt32 i, comments = Stream_ReadU32_LE(ctx->Source); - for (i = 0; i < comments; i++) { - UInt32 commentLen = Stream_ReadU32_LE(ctx->Source); - Stream_Skip(ctx->Source, commentLen); - } - - /* check framing flag */ - return (Stream_ReadU8(ctx->Source) & 1) ? 0 : VORBIS_ERR_FRAMING; -} - -static ReturnCode Vorbis_DecodeSetup(struct VorbisState* ctx) { - Int32 i, count; - ReturnCode result; - - count = Vorbis_ReadBits(ctx, 8); count++; - ctx->Codebooks = Platform_MemAlloc(count, sizeof(struct Codebook), "vorbis codebooks"); - for (i = 0; i < count; i++) { - result = Codebook_DecodeSetup(ctx, &ctx->Codebooks[i]); - if (result) return result; - } - - count = Vorbis_ReadBits(ctx, 6); count++; - for (i = 0; i < count; i++) { - UInt16 time = Vorbis_ReadBits(ctx, 16); - if (time != 0) return VORBIS_ERR_TIME_TYPE; - } - - count = Vorbis_ReadBits(ctx, 6); count++; - ctx->Floors = Platform_MemAlloc(count, sizeof(struct Floor), "vorbis floors"); - for (i = 0; i < count; i++) { - UInt16 floor = Vorbis_ReadBits(ctx, 16); - if (floor != 1) return VORBIS_ERR_FLOOR_TYPE; - result = Floor_DecodeSetup(ctx, &ctx->Floors[i]); - if (result) return result; - } - - count = Vorbis_ReadBits(ctx, 6); count++; - ctx->Residues = Platform_MemAlloc(count, sizeof(struct Residue), "vorbis residues"); - for (i = 0; i < count; i++) { - UInt16 residue = Vorbis_ReadBits(ctx, 16); - if (residue > 2) return VORBIS_ERR_FLOOR_TYPE; - result = Residue_DecodeSetup(ctx, &ctx->Residues[i], residue); - if (result) return result; - } - - count = Vorbis_ReadBits(ctx, 6); count++; - ctx->Mappings = Platform_MemAlloc(count, sizeof(struct Mapping), "vorbis mappings"); - for (i = 0; i < count; i++) { - UInt16 mapping = Vorbis_ReadBits(ctx, 16); - if (mapping != 0) return VORBIS_ERR_MAPPING_TYPE; - result = Mapping_DecodeSetup(ctx, &ctx->Mappings[i]); - if (result) return result; - } - - count = Vorbis_ReadBits(ctx, 6); count++; - ctx->Modes = Platform_MemAlloc(count, sizeof(struct Mode), "vorbis modes"); - for (i = 0; i < count; i++) { - result = Mode_DecodeSetup(ctx, &ctx->Modes[i]); - if (result) return result; - } - - ctx->ModeNumBits = iLog(count - 1); /* ilog([vorbis_mode_count]-1) bits */ - UInt8 framing = Vorbis_ReadBits(ctx, 1); - Vorbis_AlignBits(ctx); - /* check framing flag */ - return (framing & 1) ? 0 : VORBIS_ERR_FRAMING; -} - -enum VORBIS_PACKET { VORBIS_IDENTIFIER = 1, VORBIS_COMMENTS = 3, VORBIS_SETUP = 5, }; -ReturnCode Vorbis_DecodeHeaders(struct VorbisState* ctx) { - ReturnCode result; - - result = Vorbis_DecodeHeader(ctx, VORBIS_IDENTIFIER); - if (result) return result; - result = Vorbis_DecodeIdentifier(ctx); - if (result) return result; - - result = Vorbis_DecodeHeader(ctx, VORBIS_COMMENTS); - if (result) return result; - result = Vorbis_DecodeComments(ctx); - if (result) return result; - - result = Vorbis_DecodeHeader(ctx, VORBIS_SETUP); - if (result) return result; - result = Vorbis_DecodeSetup(ctx); - if (result) return result; - - return 0; -} - - -/*########################################################################################################################* -*-----------------------------------------------------Vorbis frame--------------------------------------------------------* -*#########################################################################################################################*/ -ReturnCode Vorbis_DecodeFrame(struct VorbisState* ctx) { - Int32 i, j, packetType = Vorbis_ReadBits(ctx, 1); - if (packetType) return VORBIS_ERR_FRAME_TYPE; - - Int32 modeIdx = Vorbis_ReadBits(ctx, ctx->ModeNumBits); - struct Mode* mode = &ctx->Modes[modeIdx]; - struct Mapping* mapping = &ctx->Mappings[mode->MappingIdx]; - - /* decode window shape */ - ctx->CurBlockSize = ctx->BlockSizes[mode->BlockSizeFlag]; - ctx->DataSize = ctx->CurBlockSize / 2; - Int32 prev_window, next_window; - /* long window lapping*/ - if (mode->BlockSizeFlag) { - prev_window = Vorbis_ReadBits(ctx, 1); - next_window = Vorbis_ReadBits(ctx, 1); - } - - ctx->Values = Platform_MemAllocCleared(ctx->Channels * ctx->DataSize, sizeof(Real32), "audio values"); - - /* decode floor */ - bool hasFloor[VORBIS_MAX_CHANS], hasResidue[VORBIS_MAX_CHANS]; - for (i = 0; i < ctx->Channels; i++) { - Int32 submap = mapping->Mux[i]; - Int32 floorIdx = mapping->FloorIdx[submap]; - hasFloor[i] = Floor_DecodeFrame(ctx, &ctx->Floors[floorIdx], i); - hasResidue[i] = hasFloor[i]; - } - - /* non-zero vector propogate */ - for (i = 0; i < mapping->CouplingSteps; i++) { - Int32 magChannel = mapping->Magnitude[i]; - Int32 angChannel = mapping->Angle[i]; - - if (hasResidue[magChannel] || hasResidue[angChannel]) { - hasResidue[magChannel] = true; hasResidue[angChannel] = true; - } - } - - /* decode residue */ - for (i = 0; i < mapping->Submaps; i++) { - Int32 ch = 0; - bool doNotDecode[VORBIS_MAX_CHANS]; - Real32* data[VORBIS_MAX_CHANS]; /* map residue data to actual channel data */ - for (j = 0; j < ctx->Channels; j++) { - if (mapping->Mux[j] != i) continue; - - doNotDecode[ch] = !hasResidue[j]; - data[ch] = Vorbis_ChanData(ctx, j); - ch++; + int musicCount = 0; + for (int i = 0; i < files.Length; i++) { + if (Utils.CaselessEnds(files[i], ".ogg")) musicCount++; } - Int32 residueIdx = mapping->FloorIdx[i]; - Residue_DecodeFrame(ctx, &ctx->Residues[residueIdx], ch, doNotDecode, data); + musicFiles = new string[musicCount]; + for (int i = 0, j = 0; i < files.Length; i++) { + if (!Utils.CaselessEnds(files[i], ".ogg")) continue; + musicFiles[j] = files[i]; j++; + } + + disposingMusic = false; + musicOut = GetPlatformOut(); + musicOut.Create(10); + musicThread = MakeThread(DoMusicThread, "ClassicalSharp.DoMusic"); } - /* inverse coupling */ - for (i = mapping->CouplingSteps - 1; i >= 0; i--) { - Real32* magValues = Vorbis_ChanData(ctx, mapping->Magnitude[i]); - Real32* angValues = Vorbis_ChanData(ctx, mapping->Angle[i]); + EventWaitHandle musicHandle = new EventWaitHandle(false, EventResetMode.AutoReset); + void DoMusicThread() { + if (musicFiles.Length == 0) return; + Random rnd = new Random(); + while (!disposingMusic) { + string file = musicFiles[rnd.Next(0, musicFiles.Length)]; + Utils.LogDebug("playing music file: " + file); - for (j = 0; j < ctx->DataSize; j++) { - Real32 m = magValues[j], a = angValues[j]; - - if (m > 0.0f) { - if (a > 0.0f) { angValues[j] = m - a; } - else { - angValues[j] = m; - magValues[j] = m + a; - } - } else { - if (a > 0.0f) { angValues[j] = m + a; } - else { - angValues[j] = m; - magValues[j] = m - a; + string path = Path.Combine("audio", file); + using (Stream fs = Platform.FileOpen(path)) { + OggContainer container = new OggContainer(fs); + try { + musicOut.SetVolume(game.MusicVolume / 100.0f); + musicOut.PlayStreaming(container); + } catch (InvalidOperationException ex) { + HandleMusicError(ex); + return; + } catch (Exception ex) { + ErrorHandler.LogError("AudioPlayer.DoMusicThread()", ex); + game.Chat.Add("&cError while trying to play music file " + file); } } + if (disposingMusic) break; + + int delay = 2000 * 60 + rnd.Next(0, 5000 * 60); + musicHandle.WaitOne(delay, false); } } - /* compute dot product of floor and residue, producing audio spectrum vector */ - for (i = 0; i < ctx->Channels; i++) { - if (!hasFloor[i]) continue; - Int32 submap = mapping->Mux[i]; - Int32 floorIdx = mapping->FloorIdx[submap]; - Floor_Synthesis(ctx, &ctx->Floors[floorIdx], i); + void HandleMusicError(InvalidOperationException ex) { + ErrorHandler.LogError("AudioPlayer.DoMusicThread()", ex); + if (ex.Message == "No audio devices found") + game.Chat.Add("&cNo audio devices found, disabling music."); + else + game.Chat.Add("&cAn error occured when trying to play music, disabling music."); + + SetMusic(0); + game.MusicVolume = 0; } - Int32 n = ctx->CurBlockSize; - Int32 window_center = n / 2; - Int32 left_window_beg, left_window_end, left_n; - Int32 right_window_beg, right_window_end, right_n; - - if (mode->BlockSizeFlag && !prev_window) { - left_window_beg = n / 4 - ctx->BlockSizes[0] / 4; - left_window_end = n / 4 + ctx->BlockSizes[0] / 4; - left_n = ctx->BlockSizes[0] / 2; - } else { - left_window_beg = 0; - left_window_end = window_center; - left_n = n / 2; + bool disposingMusic; + void IDisposable.Dispose() { + DisposeMusic(); + DisposeSound(); + musicHandle.Close(); + game.UserEvents.BlockChanged -= PlayBlockSound; } - if (mode->BlockSizeFlag && !next_window) { - right_window_beg = (n*3) / 4 - ctx->BlockSizes[0] / 4; - right_window_end = (n*3) / 4 + ctx->BlockSizes[0] / 4; - right_n = ctx->BlockSizes[0] / 2; - } else { - right_window_beg = window_center; - right_window_end = n; - right_n = n / 2; + void DisposeMusic() { + disposingMusic = true; + musicHandle.Set(); + DisposeOf(ref musicOut, ref musicThread); } - Real32* window = Platform_MemAlloc(n, sizeof(Real32), "temp window"); - for (i = 0; i < left_window_beg; i++) window[i] = 0; - for (i = left_window_beg; i < left_window_end; i++) { - Real64 inner = Math_Sin((i - left_window_beg + 0.5) / left_n * (PI/2)); - window[i] = Math_Sin((PI/2) * inner * inner); - } - for (i = left_window_end; i < right_window_beg; i++) window[i] = 1; - for (i = right_window_beg; i < right_window_end; i++) { - Real64 inner = Math_Sin((i - right_window_beg + 0.5) / right_n * (PI/2) + (PI/2)); - window[i] = Math_Sin((PI/2) * inner * inner); - } - for (i = right_window_end; i < n; i++) window[i] = 0; - - /* inverse monolithic transform of audio spectrum vector */ - for (i = 0; i < ctx->Channels; i++) { - if (!hasFloor[i]) { - ctx->CurOutput[i] = Platform_MemAllocCleared(ctx->CurBlockSize, sizeof(Real32), "empty output"); - continue; - } - Int32 submap = mapping->Mux[i]; - Int32 floorIdx = mapping->FloorIdx[submap]; - - Real32* data = Vorbis_ChanData(ctx, i); - Real32* output = Platform_MemAlloc(ctx->CurBlockSize, sizeof(Real32), "imdct output"); - imdct_fast(data, output, ctx->CurBlockSize); - - /* apply windowing */ - for (j = 0; j < n; j++) { output[j] *= window[j]; } - ctx->CurOutput[i] = output; + Thread MakeThread(ThreadStart func, string name) { + Thread thread = new Thread(func); + thread.Name = name; + thread.IsBackground = true; + thread.Start(); + return thread; } - /* discard remaining bits at end of packet */ - Vorbis_AlignBits(ctx); + IAudioOutput GetPlatformOut() { + if (OpenTK.Configuration.RunningOnWindows && !Options.GetBool(OptionsKey.ForceOpenAL, false)) + return new WinMmOut(); + return new OpenALOut(); + } + + void DisposeOf(ref IAudioOutput output, ref Thread thread) { + if (output == null) return; + output.Stop(); + thread.Join(); + + output.Dispose(); + output = null; + thread = null; + } } - -Int32 Vorbis_OutputFrame(struct VorbisState* ctx, Int16* data) { - Int32 i, j = 0, ch, size = 0; - if (ctx->PrevBlockSize == 0) goto finish; - size = (ctx->PrevBlockSize / 4) + (ctx->CurBlockSize / 4); - - Platform_LogConst("##### FRAME #####" ); - /* TODO: There's probably a nicer way of doing this.. */ - Real32* combined[VORBIS_MAX_CHANS]; - for (i = 0; i < ctx->Channels; i++) { - combined[i] = Platform_MemAllocCleared(size, sizeof(Real32), "temp combined"); - } - - Int32 prevHalf = ctx->PrevBlockSize / 2, curHalf = ctx->CurBlockSize / 2; - Int32 prevEnd = prevHalf + min(prevHalf, size); - Int32 curBeg = curHalf - min(curHalf, size); - - /* overlap and add data */ - for (i = prevHalf; i < prevEnd; i++, j++) { - for (ch = 0; ch < ctx->Channels; ch++) { - combined[ch][j] = ctx->PrevOutput[ch][i]; - } - } - - for (i = curHalf - 1, j = size - 1; i >= curBeg; i--, j--) { - for (ch = 0; ch < ctx->Channels; ch++) { - combined[ch][j] += ctx->CurOutput[ch][i]; - } - } - - for (i = 0; i < size; i++) { - for (ch = 0; ch < ctx->Channels; ch++) { - Real32 sample = combined[ch][i]; - Math_Clamp(sample, -1.0f, 1.0f); - *data++ = (Int16)(sample * 32767); - } - } - -finish: - ctx->PrevBlockSize = ctx->CurBlockSize; - for (i = 0; i < VORBIS_MAX_CHANS; i++) { - ctx->PrevOutput[i] = ctx->CurOutput[i]; - } - return size; } - -static Real32 floor1_inverse_dB_table[256] = { - 1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f, - 1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f, - 1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f, - 2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f, - 2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f, - 3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f, - 4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f, - 6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f, - 7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f, - 1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f, - 1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f, - 1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f, - 2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f, - 2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f, - 3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f, - 4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f, - 5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f, - 7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f, - 9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f, - 1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f, - 1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f, - 2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f, - 2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f, - 3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f, - 4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f, - 5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f, - 7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f, - 9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f, - 0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f, - 0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f, - 0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f, - 0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f, - 0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f, - 0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f, - 0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f, - 0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f, - 0.00092223983f, 0.00098217216f, 0.0010459992f, 0.0011139742f, - 0.0011863665f, 0.0012634633f, 0.0013455702f, 0.0014330129f, - 0.0015261382f, 0.0016253153f, 0.0017309374f, 0.0018434235f, - 0.0019632195f, 0.0020908006f, 0.0022266726f, 0.0023713743f, - 0.0025254795f, 0.0026895994f, 0.0028643847f, 0.0030505286f, - 0.0032487691f, 0.0034598925f, 0.0036847358f, 0.0039241906f, - 0.0041792066f, 0.0044507950f, 0.0047400328f, 0.0050480668f, - 0.0053761186f, 0.0057254891f, 0.0060975636f, 0.0064938176f, - 0.0069158225f, 0.0073652516f, 0.0078438871f, 0.0083536271f, - 0.0088964928f, 0.009474637f, 0.010090352f, 0.010746080f, - 0.011444421f, 0.012188144f, 0.012980198f, 0.013823725f, - 0.014722068f, 0.015678791f, 0.016697687f, 0.017782797f, - 0.018938423f, 0.020169149f, 0.021479854f, 0.022875735f, - 0.024362330f, 0.025945531f, 0.027631618f, 0.029427276f, - 0.031339626f, 0.033376252f, 0.035545228f, 0.037855157f, - 0.040315199f, 0.042935108f, 0.045725273f, 0.048696758f, - 0.051861348f, 0.055231591f, 0.058820850f, 0.062643361f, - 0.066714279f, 0.071049749f, 0.075666962f, 0.080584227f, - 0.085821044f, 0.091398179f, 0.097337747f, 0.10366330f, - 0.11039993f, 0.11757434f, 0.12521498f, 0.13335215f, - 0.14201813f, 0.15124727f, 0.16107617f, 0.17154380f, - 0.18269168f, 0.19456402f, 0.20720788f, 0.22067342f, - 0.23501402f, 0.25028656f, 0.26655159f, 0.28387361f, - 0.30232132f, 0.32196786f, 0.34289114f, 0.36517414f, - 0.38890521f, 0.41417847f, 0.44109412f, 0.46975890f, - 0.50028648f, 0.53279791f, 0.56742212f, 0.60429640f, - 0.64356699f, 0.68538959f, 0.72993007f, 0.77736504f, - 0.82788260f, 0.88168307f, 0.9389798f, 1.00000000f, -}; \ No newline at end of file diff --git a/src/Client/Audio.h b/src/Client/Audio.h index 0e7a3f28e..db6f59664 100644 --- a/src/Client/Audio.h +++ b/src/Client/Audio.h @@ -5,7 +5,6 @@ Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 */ struct IGameComponent; -struct Stream; void Audio_MakeComponent(struct IGameComponent* comp); void Audio_SetMusic(Int32 volume); @@ -13,43 +12,4 @@ void Audio_SetSounds(Int32 volume); void Audio_PlayDigSound(UInt8 type); void Audio_PlayStepSound(UInt8 type); -enum OGG_VER { OGG_ERR_INVALID_SIG = 2552401, OGG_ERR_VERSION }; -#define OGG_BUFFER_SIZE (255 * 256) -void Ogg_MakeStream(struct Stream* stream, UInt8* buffer, struct Stream* source); - -enum VORBIS_ERR { - VORBIS_ERR_HEADER = 5238001, VORBIS_ERR_WRONG_HEADER, VORBIS_ERR_FRAMING, - VORBIS_ERR_VERSION, VORBIS_ERR_BLOCKSIZE, VORBIS_ERR_CHANS, - VORBIS_ERR_TIME_TYPE, VORBIS_ERR_FLOOR_TYPE, VORBIS_ERR_RESIDUE_TYPE, - VORBIS_ERR_MAPPING_TYPE, VORBIS_ERR_MODE_TYPE, - VORBIS_ERR_CODEBOOK_SYNC, VORBIS_ERR_CODEBOOK_ENTRY, VORBIS_ERR_CODEBOOK_LOOKUP, - VORBIS_ERR_MODE_WINDOW, VORBIS_ERR_MODE_TRANSFORM, - VORBIS_ERR_MAPPING_CHANS, VORBIS_ERR_MAPPING_RESERVED, - VORBIS_ERR_FRAME_TYPE, -}; - -#define VORBIS_MAX_CHANS 8 -struct Codebook; struct Floor; struct Residue; struct Mapping; struct Mode; -struct VorbisState { - UInt32 Bits; /* Holds bits across byte boundaries*/ - UInt32 NumBits; /* Number of bits in Bits buffer*/ - struct Stream* Source; /* Source for filling Input buffer */ - - UInt8 Channels, ModeNumBits; - UInt16 CurBlockSize, PrevBlockSize, DataSize; - Int32 SampleRate; Int32 BlockSizes[2]; - Real32* Values; - Real32* PrevOutput[VORBIS_MAX_CHANS]; - Real32* CurOutput[VORBIS_MAX_CHANS]; - - struct Codebook* Codebooks; - struct Floor* Floors; - struct Residue* Residues; - struct Mapping* Mappings; - struct Mode* Modes; -}; -void Vorbis_Init(struct VorbisState* ctx, struct Stream* source); -ReturnCode Vorbis_DecodeHeaders(struct VorbisState* ctx); -ReturnCode Vorbis_DecodeFrame(struct VorbisState* ctx); -Int32 Vorbis_OutputFrame(struct VorbisState* ctx, Int16* data); #endif diff --git a/src/Client/Client.vcxproj b/src/Client/Client.vcxproj index 8ab0261e2..1a72ff294 100644 --- a/src/Client/Client.vcxproj +++ b/src/Client/Client.vcxproj @@ -99,7 +99,7 @@ true 5.02 main - d3d9.lib;opengl32.lib;ucrtd.lib;vcruntimed.lib;msvcrtd.lib;dbghelp.lib;ws2_32.lib;Wininet.lib;%(AdditionalDependencies) + d3d9.lib;opengl32.lib;ws2_32.lib;Wininet.lib;dbghelp.lib;Winmm.lib;ucrtd.lib;vcruntimed.lib;msvcrtd.lib;dbghelp.lib;%(AdditionalDependencies) @@ -119,7 +119,7 @@ Windows true main - d3d9.lib;opengl32.lib;ucrtd.lib;vcruntimed.lib;msvcrtd.lib;dbghelp.lib;ws2_32.lib;Wininet.lib;%(AdditionalDependencies) + d3d9.lib;opengl32.lib;ws2_32.lib;Wininet.lib;dbghelp.lib;Winmm.lib;ucrtd.lib;vcruntimed.lib;msvcrtd.lib;%(AdditionalDependencies) @@ -148,7 +148,7 @@ 5.02 Default main - d3d9.lib;opengl32.lib;libucrt.lib;libvcruntime.lib;dbghelp.lib;ws2_32.lib;Wininet.lib;%(AdditionalDependencies) + d3d9.lib;opengl32.lib;ws2_32.lib;Wininet.lib;dbghelp.lib;Winmm.lib;libucrt.lib;libvcruntime.lib;%(AdditionalDependencies) @@ -175,7 +175,7 @@ true true main - d3d9.lib;opengl32.lib;libucrt.lib;libvcruntime.lib;dbghelp.lib;ws2_32.lib;Wininet.lib;%(AdditionalDependencies) + d3d9.lib;opengl32.lib;ws2_32.lib;Wininet.lib;dbghelp.lib;Winmm.lib;libucrt.lib;libvcruntime.lib;%(AdditionalDependencies) @@ -242,6 +242,7 @@ + @@ -306,6 +307,7 @@ + diff --git a/src/Client/Client.vcxproj.filters b/src/Client/Client.vcxproj.filters index b15de4ec2..c29a18fab 100644 --- a/src/Client/Client.vcxproj.filters +++ b/src/Client/Client.vcxproj.filters @@ -136,6 +136,12 @@ {570b92b8-b3df-40cb-a42a-da14c81c877a} + + {fee32b2d-e320-4681-9d5d-9904ca911271} + + + {b6f24d6b-c6f6-4909-9ae1-f0c478846c6e} + @@ -321,9 +327,6 @@ Header Files\TexturePack - - Header Files\Game - Header Files\Network @@ -336,6 +339,12 @@ Header Files\Rendering + + Header Files\Audio + + + Header Files\Audio + @@ -531,7 +540,10 @@ Source Files\Platform - Source Files\Game + Source Files\Audio + + + Source Files\Audio \ No newline at end of file diff --git a/src/Client/Platform.h b/src/Client/Platform.h index 2b2f103fe..8c503145e 100644 --- a/src/Client/Platform.h +++ b/src/Client/Platform.h @@ -112,4 +112,15 @@ ReturnCode Platform_HttpGetRequestHeaders(struct AsyncRequest* request, void* ha ReturnCode Platform_HttpGetRequestData(struct AsyncRequest* request, void* handle, void** data, UInt32 size, volatile Int32* progress); ReturnCode Platform_HttpFreeRequest(void* handle); ReturnCode Platform_HttpFree(void); + +#define AUDIO_MAX_CHUNKS 5 +struct AudioFormat { UInt8 Channels, BitsPerSample; UInt16 Frequency }; +#define AudioFormat_Eq(a, b) (a->Channels == b->Channels && a->BitsPerSample == b->BitsPerSample && a->Frequency == b->Frequency) + +void Platform_AudioInit(Int32* handle, UInt8 buffers); +void Platform_AudioFree(Int32 handle); +void Platform_AudioSetFormat(Int32 handle, struct AudioFormat* format); +void Platform_AudioPlayAsync(Int32 handle, void* data, UInt32 dataSize); +Int32 Platform_AudioNextFinishedAsync(Int32 handle); +bool Platform_AudioFinishedAsync(Int32 handle); #endif diff --git a/src/Client/Vorbis.c b/src/Client/Vorbis.c new file mode 100644 index 000000000..ddeb4a12d --- /dev/null +++ b/src/Client/Vorbis.c @@ -0,0 +1,1337 @@ +#include "Vorbis.h" +#include "ErrorHandler.h" +#include "Platform.h" +#include "Event.h" +#include "Block.h" +#include "ExtMath.h" +#include "Funcs.h" + +/*########################################################################################################################* +*-------------------------------------------------------Ogg stream--------------------------------------------------------* +*#########################################################################################################################*/ +#define OGG_FourCC(a, b, c, d) (((UInt32)a << 24) | ((UInt32)b << 16) | ((UInt32)c << 8) | (UInt32)d) +static ReturnCode Ogg_NextPage(struct Stream* stream) { + UInt8 header[27]; + struct Stream* source = stream->Meta.Ogg.Source; + Stream_Read(source, header, sizeof(header)); + + UInt32 sig = Stream_GetU32_BE(&header[0]); + if (sig != OGG_FourCC('O','g','g','S')) return OGG_ERR_INVALID_SIG; + if (header[4] != 0) return OGG_ERR_VERSION; + UInt8 bitflags = header[5]; + /* (8) granule position */ + /* (4) serial number */ + /* (4) page sequence number */ + /* (4) page checksum */ + + Int32 i, numSegments = header[26]; + UInt8 segments[255]; + Stream_Read(source, segments, numSegments); + + UInt32 dataSize = 0; + for (i = 0; i < numSegments; i++) { dataSize += segments[i]; } + Stream_Read(source, stream->Meta.Ogg.Base, dataSize); + + stream->Meta.Ogg.Cur = stream->Meta.Ogg.Base; + stream->Meta.Ogg.Left = dataSize; + stream->Meta.Ogg.Last = bitflags & 4; + return 0; +} + +static ReturnCode Ogg_Read(struct Stream* stream, UInt8* data, UInt32 count, UInt32* modified) { + for (;;) { + if (stream->Meta.Ogg.Left) { + count = min(count, stream->Meta.Ogg.Left); + Platform_MemCpy(data, stream->Meta.Ogg.Cur, count); + + *modified = count; + stream->Meta.Ogg.Cur += count; + stream->Meta.Ogg.Left -= count; + return 0; + } + + /* try again with data from next page*/ + *modified = 0; + if (stream->Meta.Ogg.Last) return 0; + + ReturnCode result = Ogg_NextPage(stream); + if (result != 0) return result; + } +} + +void Ogg_MakeStream(struct Stream* stream, UInt8* buffer, struct Stream* source) { + Stream_Init(stream, &source->Name); + stream->Read = Ogg_Read; + + stream->Meta.Ogg.Cur = buffer; + stream->Meta.Ogg.Base = buffer; + stream->Meta.Ogg.Left = 0; + stream->Meta.Ogg.Last = 0; + stream->Meta.Ogg.Source = source; +} + + +/*########################################################################################################################* +*------------------------------------------------------Vorbis utils-------------------------------------------------------* +*#########################################################################################################################*/ +/* Insert next byte into the bit buffer */ +#define Vorbis_GetByte(ctx) ctx->Bits |= (UInt32)Stream_ReadU8(ctx->Source) << ctx->NumBits; ctx->NumBits += 8; +/* Retrieves bits from the bit buffer */ +#define Vorbis_PeekBits(ctx, bits) (ctx->Bits & ((1UL << (bits)) - 1UL)) +/* Consumes/eats up bits from the bit buffer */ +#define Vorbis_ConsumeBits(ctx, bits) ctx->Bits >>= (bits); ctx->NumBits -= (bits); +/* Aligns bit buffer to be on a byte boundary */ +#define Vorbis_AlignBits(ctx) UInt32 alignSkip = ctx->NumBits & 7; Vorbis_ConsumeBits(ctx, alignSkip); +/* Ensures there are 'bitsCount' bits */ +#define Vorbis_EnsureBits(ctx, bitsCount) while (ctx->NumBits < bitsCount) { Vorbis_GetByte(ctx); } +/* Peeks then consumes given bits */ +/* TODO: Make this an inline macro somehow */ +static UInt32 Vorbis_ReadBits(struct VorbisState* ctx, UInt32 bitsCount) { + Vorbis_EnsureBits(ctx, bitsCount); + UInt32 result = Vorbis_PeekBits(ctx, bitsCount); Vorbis_ConsumeBits(ctx, bitsCount); + return result; +} + +#define VORBIS_MAX_CHANS 8 +#define Vorbis_ChanData(ctx, ch) (ctx->Values + (ch) * ctx->DataSize) + +static Int32 iLog(Int32 x) { + Int32 bits = 0; + while (x > 0) { bits++; x >>= 1; } + return bits; +} + +static Real32 float32_unpack(struct VorbisState* ctx) { + /* encoder macros can't reliably read over 24 bits */ + UInt32 lo = Vorbis_ReadBits(ctx, 16); + UInt32 hi = Vorbis_ReadBits(ctx, 16); + UInt32 x = (hi << 16) | lo; + + Int32 mantissa = x & 0x1fffff; + UInt32 exponent = (x & 0x7fe00000) >> 21; + if (x & 0x80000000UL) mantissa = -mantissa; + + #define LOG_2 0.693147180559945 + /* TODO: Can we make this more accurate? maybe ldexp ?? */ + return (Real32)(mantissa * Math_Exp(LOG_2 * ((Int32)exponent - 788))); /* pow(2, x) */ +} + + +/*########################################################################################################################* +*----------------------------------------------------Vorbis codebooks-----------------------------------------------------* +*#########################################################################################################################*/ +#define CODEBOOK_SYNC 0x564342 +struct Codebook { + UInt32 Dimensions, Entries, NumCodewords; + UInt32* Codewords; + UInt8* CodewordLens; + UInt32* Values; + /* vector quantisation values */ + Real32 MinValue, DeltaValue; + UInt32 SequenceP, LookupType, LookupValues; + UInt16* Multiplicands; +}; + +static UInt32 Codebook_Pow(UInt32 base, UInt32 exp) { + UInt32 result = 1; /* exponentiation by squaring */ + while (exp) { + if (exp & 1) result *= base; + exp >>= 1; + base *= base; + } + return result; +} + +static UInt32 lookup1_values(UInt32 entries, UInt32 dimensions) { + UInt32 i; + /* the greatest integer value for which [value] to the power of [dimensions] is less than or equal to [entries] */ + /* TODO: verify this */ + for (i = 1; ; i++) { + UInt32 pow = Codebook_Pow(i, dimensions); + UInt32 next = Codebook_Pow(i + 1, dimensions); + + if (next < pow) return i; /* overflow */ + if (pow == entries) return i; + if (next > entries) return i; + } + return 0; +} + +static bool Codebook_CalcCodewords(struct Codebook* c, UInt8* len) { + c->Codewords = Platform_MemAlloc(c->NumCodewords, sizeof(UInt32), "codewords"); + c->CodewordLens = Platform_MemAlloc(c->NumCodewords, sizeof(UInt8), "raw codeword lens"); + c->Values = Platform_MemAlloc(c->NumCodewords, sizeof(UInt32), "values"); + + /* This is taken from stb_vorbis.c because I gave up trying */ + UInt32 i, j, depth; + UInt32 next_codewords[33] = { 0 }; + + /* add codeword 0 to tree */ + for (i = 0; i < c->Entries; i++) { + if (len[i] == UInt8_MaxValue) continue; + + c->Codewords[0] = 0; + c->CodewordLens[0] = len[i]; + c->Values[0] = i; + break; + } + + /* set codewords that new nodes can start from */ + for (depth = 1; depth <= len[i]; depth++) { + next_codewords[depth] = 1U << (32 - depth); + } + + i++; /* first codeword was already handled */ + for (j = 1; i < c->Entries; i++) { + UInt32 root = len[i]; + if (root == UInt8_MaxValue) continue; + + /* per spec, find lowest possible value (leftmost) */ + while (root && next_codewords[root] == 0) root--; + if (root == 0) return false; + + UInt32 codeword = next_codewords[root]; + next_codewords[root] = 0; + + c->Codewords[j] = codeword; + c->CodewordLens[j] = len[i]; + c->Values[j] = i; + + for (depth = len[i]; depth > root; depth--) { + next_codewords[depth] = codeword + (1U << (32 - depth)); + } + j++; + } + return true; +} + +static ReturnCode Codebook_DecodeSetup(struct VorbisState* ctx, struct Codebook* c) { + UInt32 sync = Vorbis_ReadBits(ctx, 24); + if (sync != CODEBOOK_SYNC) return VORBIS_ERR_CODEBOOK_SYNC; + c->Dimensions = Vorbis_ReadBits(ctx, 16); + c->Entries = Vorbis_ReadBits(ctx, 24); + + UInt8* codewordLens = Platform_MemAlloc(c->Entries, sizeof(UInt8), "raw codeword lens"); + Int32 i, ordered = Vorbis_ReadBits(ctx, 1), usedEntries = 0; + + if (!ordered) { + Int32 sparse = Vorbis_ReadBits(ctx, 1); + for (i = 0; i < c->Entries; i++) { + if (sparse) { + Int32 flag = Vorbis_ReadBits(ctx, 1); + if (!flag) { + codewordLens[i] = UInt8_MaxValue; + continue; /* unused entry */ + } + } + + Int32 len = Vorbis_ReadBits(ctx, 5); len++; + codewordLens[i] = len; + usedEntries++; + } + } else { + Int32 entry; + Int32 curLength = Vorbis_ReadBits(ctx, 5); curLength++; + for (entry = 0; entry < c->Entries;) { + Int32 runBits = iLog(c->Entries - entry); + Int32 runLen = Vorbis_ReadBits(ctx, runBits); + + for (i = entry; i < entry + runLen; i++) { + codewordLens[i] = curLength; + } + + entry += runLen; + curLength++; + if (entry > c->Entries) return VORBIS_ERR_CODEBOOK_ENTRY; + } + usedEntries = c->Entries; + } + c->NumCodewords = usedEntries; + + static int booknummm; + Platform_Log1("### BUILDING %i ###", &booknummm); + Codebook_CalcCodewords(c, codewordLens); + Platform_MemFree(&codewordLens); + booknummm++; + + c->LookupType = Vorbis_ReadBits(ctx, 4); + if (c->LookupType == 0) return 0; + if (c->LookupType > 2) return VORBIS_ERR_CODEBOOK_LOOKUP; + + c->MinValue = float32_unpack(ctx); + c->DeltaValue = float32_unpack(ctx); + Int32 valueBits = Vorbis_ReadBits(ctx, 4); valueBits++; + c->SequenceP = Vorbis_ReadBits(ctx, 1); + + UInt32 lookupValues; + if (c->LookupType == 1) { + lookupValues = lookup1_values(c->Entries, c->Dimensions); + } else { + lookupValues = c->Entries * c->Dimensions; + } + c->LookupValues = lookupValues; + + c->Multiplicands = Platform_MemAlloc(lookupValues, sizeof(UInt16), "multiplicands"); + for (i = 0; i < lookupValues; i++) { + c->Multiplicands[i] = Vorbis_ReadBits(ctx, valueBits); + } + return 0; +} + +static UInt32 Codebook_DecodeScalar(struct VorbisState* ctx, struct Codebook* c) { + UInt32 codeword = 0, shift = 31, depth, i; + /* TODO: This is so massively slow */ + for (depth = 1; depth <= 32; depth++, shift--) { + //codeword >>= 1; + UInt32 bit = Vorbis_ReadBits(ctx, 1); + codeword |= bit << shift; + for (i = 0; i < c->NumCodewords; i++) { + if (depth != c->CodewordLens[i]) continue; + if (codeword != c->Codewords[i]) continue; + + UInt32 value = c->Values[i]; + //Platform_Log2("read value %i of len %i", &value, &depth); + return value; + } + } + ErrorHandler_Fail("????????????"); + return -1; +} + +static void Codebook_DecodeVectors(struct VorbisState* ctx, struct Codebook* c, Real32* v, Int32 step) { + UInt32 lookupOffset = Codebook_DecodeScalar(ctx, c); + Real32 last = 0.0f, value; + UInt32 i, offset; + + if (c->LookupType == 1) { + UInt32 indexDivisor = 1; + for (i = 0; i < c->Dimensions; i++, v += step) { + offset = (lookupOffset / indexDivisor) % c->LookupValues; + value = c->Multiplicands[offset] * c->DeltaValue + c->MinValue + last; + + *v += value; + if (c->SequenceP) last = value; + indexDivisor *= c->LookupValues; + } + } else if (c->LookupType == 2) { + offset = lookupOffset * c->Dimensions; + for (i = 0; i < c->Dimensions; i++, offset++, v += step) { + value = c->Multiplicands[offset] * c->DeltaValue + c->MinValue + last; + + *v += value; + if (c->SequenceP) last = value; + } + } else { + ErrorHandler_Fail("???????"); + } +} + +/*########################################################################################################################* +*-----------------------------------------------------Vorbis floors-------------------------------------------------------* +*#########################################################################################################################*/ +#define FLOOR_MAX_PARTITIONS 32 +#define FLOOR_MAX_CLASSES 16 +#define FLOOR_MAX_VALUES (FLOOR_MAX_PARTITIONS * 8 + 2) +struct Floor { + UInt8 Partitions, Multiplier; Int32 Range, Values; + UInt8 PartitionClasses[FLOOR_MAX_PARTITIONS]; + UInt8 ClassDimensions[FLOOR_MAX_CLASSES]; + UInt8 ClassSubClasses[FLOOR_MAX_CLASSES]; + UInt8 ClassMasterbooks[FLOOR_MAX_CLASSES]; + Int16 SubclassBooks[FLOOR_MAX_CLASSES][8]; + Int16 XList[FLOOR_MAX_VALUES]; + UInt16 ListOrder[FLOOR_MAX_VALUES]; + Int32 YList[VORBIS_MAX_CHANS][FLOOR_MAX_VALUES]; +}; + +/* TODO: Make this thread safe */ +Int16* tmp_xlist; +UInt16* tmp_order; +static void Floor_SortXList(Int32 left, Int32 right) { + UInt16* values = tmp_order; UInt16 value; + Int16* keys = tmp_xlist; Int16 key; + while (left < right) { + Int32 i = left, j = right; + Int16 pivot = keys[(i + j) / 2]; + + /* partition the list */ + while (i <= j) { + while (pivot > keys[i]) i++; + while (pivot < keys[j]) j--; + QuickSort_Swap_KV_Maybe(); + } + /* recurse into the smaller subset */ + QuickSort_Recurse(Floor_SortXList) + } +} + +static ReturnCode Floor_DecodeSetup(struct VorbisState* ctx, struct Floor* f) { + f->Partitions = Vorbis_ReadBits(ctx, 5); + Int32 i, j, idx, maxClass = -1; + + for (i = 0; i < f->Partitions; i++) { + f->PartitionClasses[i] = Vorbis_ReadBits(ctx, 4); + maxClass = max(maxClass, f->PartitionClasses[i]); + } + + for (i = 0; i <= maxClass; i++) { + f->ClassDimensions[i] = Vorbis_ReadBits(ctx, 3); f->ClassDimensions[i]++; + f->ClassSubClasses[i] = Vorbis_ReadBits(ctx, 2); + if (f->ClassSubClasses[i]) { + f->ClassMasterbooks[i] = Vorbis_ReadBits(ctx, 8); + } + for (j = 0; j < (1 << f->ClassSubClasses[i]); j++) { + f->SubclassBooks[i][j] = Vorbis_ReadBits(ctx, 8); f->SubclassBooks[i][j]--; + } + } + + f->Multiplier = Vorbis_ReadBits(ctx, 2); f->Multiplier++; + static Int16 ranges[4] = { 256, 128, 84, 64 }; + f->Range = ranges[f->Multiplier - 1]; + + UInt32 rangeBits = Vorbis_ReadBits(ctx, 4); + f->XList[0] = 0; + f->XList[1] = 1 << rangeBits; + + for (i = 0, idx = 2; i < f->Partitions; i++) { + Int32 classNum = f->PartitionClasses[i]; + for (j = 0; j < f->ClassDimensions[classNum]; j++) { + f->XList[idx++] = Vorbis_ReadBits(ctx, rangeBits); + } + } + f->Values = idx; + + /* sort X list for curve computation later */ + Int16 xlist_sorted[FLOOR_MAX_VALUES]; + Platform_MemCpy(xlist_sorted, f->XList, idx * sizeof(Int16)); + for (i = 0; i < idx; i++) { f->ListOrder[i] = i; } + + tmp_xlist = xlist_sorted; + tmp_order = f->ListOrder; + Floor_SortXList(0, idx - 1); + return 0; +} + +static bool Floor_DecodeFrame(struct VorbisState* ctx, struct Floor* f, Int32 ch) { + Int32 nonZero = Vorbis_ReadBits(ctx, 1); + if (!nonZero) return false; + Int32* yList = f->YList[ch]; + + Int32 i, j, idx, rangeBits = iLog(f->Range - 1); + yList[0] = Vorbis_ReadBits(ctx, rangeBits); + yList[1] = Vorbis_ReadBits(ctx, rangeBits); + + for (i = 0, idx = 2; i < f->Partitions; i++) { + UInt8 class = f->PartitionClasses[i]; + UInt8 cdim = f->ClassDimensions[class]; + UInt8 cbits = f->ClassSubClasses[class]; + + UInt32 csub = (1 << cbits) - 1; + UInt32 cval = 0; + if (cbits) { + UInt8 bookNum = f->ClassMasterbooks[class]; + cval = Codebook_DecodeScalar(ctx, &ctx->Codebooks[bookNum]); + } + + for (j = 0; j < cdim; j++) { + Int16 bookNum = f->SubclassBooks[class][cval & csub]; + cval >>= cbits; + if (bookNum >= 0) { + yList[idx + j] = Codebook_DecodeScalar(ctx, &ctx->Codebooks[bookNum]); + } else { + yList[idx + j] = 0; + } + } + idx += cdim; + } + return true; +} + +static Int32 render_point(Int32 x0, Int32 y0, Int32 x1, Int32 y1, Int32 X) { + Int32 dy = y1 - y0, adx = x1 - x0; + Int32 ady = Math_AbsI(dy); + Int32 err = ady * (X - x0); + Int32 off = err / adx; + + if (dy < 0) { + return y0 - off; + } else { + return y0 + off; + } +} + +static Real32 floor1_inverse_dB_table[256]; +static void render_line(Int32 x0, Int32 y0, Int32 x1, Int32 y1, Real32* data) { + Int32 dy = y1 - y0, adx = x1 - x0; + Int32 ady = Math_AbsI(dy); + Int32 base = dy / adx, sy; + Int32 x = x0, y = y0, err = 0; + + if (dy < 0) { + sy = base - 1; + } else { + sy = base + 1; + } + + ady = ady - Math_AbsI(base) * adx; + data[x] *= floor1_inverse_dB_table[y]; + + for (x = x0 + 1; x < x1; x++) { + err = err + ady; + if (err >= adx) { + err = err - adx; + y = y + sy; + } else { + y = y + base; + } + data[x] *= floor1_inverse_dB_table[y]; + } +} + +static Int32 low_neighbor(Int16* v, Int32 x) { + Int32 n = 0, i, max = Int32_MinValue; + for (i = 0; i < x; i++) { + if (v[i] < v[x] && v[i] > max) { n = i; max = v[i]; } + } + return n; +} + +static Int32 high_neighbor(Int16* v, Int32 x) { + Int32 n = 0, i, min = Int32_MaxValue; + for (i = 0; i < x; i++) { + if (v[i] > v[x] && v[i] < min) { n = i; min = v[i]; } + } + return n; +} + +static void Floor_Synthesis(struct VorbisState* ctx, struct Floor* f, Int32 ch) { + /* amplitude value synthesis */ + Int32 YFinal[FLOOR_MAX_VALUES]; + bool Step2[FLOOR_MAX_VALUES]; + + Real32* data = Vorbis_ChanData(ctx, ch); + Int32* yList = f->YList[ch]; + + Step2[0] = true; + Step2[1] = true; + YFinal[0] = yList[0]; + YFinal[1] = yList[1]; + + Int32 i; + for (i = 2; i < f->Values; i++) { + Int32 lo_offset = low_neighbor(f->XList, i); + Int32 hi_offset = high_neighbor(f->XList, i); + + Int32 predicted = render_point(f->XList[lo_offset], YFinal[lo_offset], + f->XList[hi_offset], YFinal[hi_offset], f->XList[i]); + + Int32 val = yList[i]; + Int32 highroom = f->Range - predicted; + Int32 lowroom = predicted, room; + + if (highroom < lowroom) { + room = highroom * 2; + } else { + room = lowroom * 2; + } + + if (val) { + Step2[lo_offset] = true; + Step2[hi_offset] = true; + Step2[i] = true; + + if (val >= room) { + if (highroom > lowroom) { + YFinal[i] = val - lowroom + predicted; + } else { + YFinal[i] = predicted - val + highroom - 1; + } + } else { + if (val & 1) { + YFinal[i] = predicted - (val + 1) / 2; + } else { + YFinal[i] = predicted + val / 2; + } + } + } else { + Step2[i] = false; + YFinal[i] = predicted; + } + } + + /* curve synthesis */ + Int32 hx = 0, lx = 0, rawI; + Int32 ly = YFinal[f->ListOrder[0]] * f->Multiplier, hy = ly; + + for (rawI = 1; rawI < f->Values; rawI++) { + i = f->ListOrder[rawI]; + if (!Step2[i]) continue; + + hx = f->XList[i]; hy = YFinal[i] * f->Multiplier; + if (lx < hx) { + render_line(lx, ly, min(hx, ctx->DataSize), hy, data); + } + lx = hx; ly = hy; + } + + /* fill remainder of floor with a flat line */ + /* TODO: Is this right? should hy be 0, if Step2 is false for all */ + if (hx >= ctx->DataSize) return; + lx = hx; hx = ctx->DataSize; + Real32 value = floor1_inverse_dB_table[hy]; + for (; lx < hx; lx++) { data[lx] *= value; } +} + + +/*########################################################################################################################* +*----------------------------------------------------Vorbis residues------------------------------------------------------* +*#########################################################################################################################*/ +#define RESIDUE_MAX_CLASSIFICATIONS 65 +struct Residue { + UInt8 Type, Classifications, Classbook; + UInt32 Begin, End, PartitionSize; + UInt8 Cascade[RESIDUE_MAX_CLASSIFICATIONS]; + Int16 Books[RESIDUE_MAX_CLASSIFICATIONS][8]; +}; + +static ReturnCode Residue_DecodeSetup(struct VorbisState* ctx, struct Residue* r, UInt16 type) { + r->Type = type; + r->Begin = Vorbis_ReadBits(ctx, 24); + r->End = Vorbis_ReadBits(ctx, 24); + r->PartitionSize = Vorbis_ReadBits(ctx, 24); r->PartitionSize++; + r->Classifications = Vorbis_ReadBits(ctx, 6); r->Classifications++; + r->Classbook = Vorbis_ReadBits(ctx, 8); + + Int32 i; + for (i = 0; i < r->Classifications; i++) { + r->Cascade[i] = Vorbis_ReadBits(ctx, 3); + Int32 moreBits = Vorbis_ReadBits(ctx, 1); + if (!moreBits) continue; + Int32 bits = Vorbis_ReadBits(ctx, 5); + r->Cascade[i] |= bits << 3; + } + + Int32 j; + for (i = 0; i < r->Classifications; i++) { + for (j = 0; j < 8; j++) { + Int16 codebook = -1; + if (r->Cascade[i] & (1 << j)) { + codebook = Vorbis_ReadBits(ctx, 8); + } + r->Books[i][j] = codebook; + } + } + return 0; +} + +static void Residue_DecodeCore(struct VorbisState* ctx, struct Residue* r, UInt32 size, Int32 ch, bool* doNotDecode, Real32** data) { + UInt32 residueBeg = min(r->Begin, size); + UInt32 residueEnd = min(r->End, size); + Int32 pass, i, j, k; + + struct Codebook* classbook = &ctx->Codebooks[r->Classbook]; + UInt32 classwordsPerCodeword = classbook->Dimensions; + UInt32 nToRead = residueEnd - residueBeg; + UInt32 partitionsToRead = nToRead / r->PartitionSize; + + UInt8* classifications_raw = Platform_MemAlloc(ch * partitionsToRead * classbook->Dimensions, sizeof(UInt8), "temp classicifcations"); + UInt8* classifications[VORBIS_MAX_CHANS]; /* TODO ????? */ + for (i = 0; i < ch; i++) { + classifications[i] = classifications_raw + (i * partitionsToRead * classbook->Dimensions); + } + + if (nToRead == 0) return; + for (pass = 0; pass < 8; pass++) { + UInt32 partitionCount = 0; + while (partitionCount < partitionsToRead) { + + /* read classifications in pass 0 */ + if (pass == 0) { + for (j = 0; j < ch; j++) { + if (doNotDecode[j]) continue; + + UInt32 temp = Codebook_DecodeScalar(ctx, classbook); + for (i = classwordsPerCodeword - 1; i >= 0; i--) { + classifications[j][i + partitionCount] = temp % r->Classifications; + temp /= r->Classifications; + } + } + } + + for (i = 0; i < classwordsPerCodeword && partitionCount < partitionsToRead; i++) { + for (j = 0; j < ch; j++) { + if (doNotDecode[j]) continue; + UInt8 class = classifications[j][partitionCount]; + Int16 book = r->Books[class][pass]; + if (book < 0) continue; + + UInt32 offset = residueBeg + partitionCount * r->PartitionSize; + Real32* v = data[j] + offset; + struct Codebook* c = &ctx->Codebooks[book]; + + if (r->Type == 0) { + Int32 step = r->PartitionSize / c->Dimensions; + for (k = 0; k < step; k++) { + Codebook_DecodeVectors(ctx, c, v, step); v++; + } + } else { + for (k = 0; k < r->PartitionSize; k += c->Dimensions) { + Codebook_DecodeVectors(ctx, c, v, 1); v += c->Dimensions; + } + } + } + partitionCount++; + } + } + } +} + +static void Residue_DecodeFrame(struct VorbisState* ctx, struct Residue* r, Int32 ch, bool* doNotDecode, Real32** data) { + UInt32 size = ctx->DataSize; + if (r->Type == 2) { + bool decodeAny = false; + Int32 i, j; + + /* type 2 decodes all channel vectors 2, if at least 1 channel to decode */ + for (i = 0; i < ch; i++) { + if (!doNotDecode[i]) decodeAny = true; + } + if (!decodeAny) return; + + decodeAny = false; /* because DecodeCore expects this to be 'false' for 'do not decode' */ + Real32* interleaved = Platform_MemAllocCleared(ctx->DataSize * ctx->Channels, sizeof(Real32), "residue 2 temp"); + Residue_DecodeCore(ctx, r, size * ch, 1, &decodeAny, &interleaved); + + /* de interleave type 2 output */ + for (i = 0; i < size; i++) { + for (j = 0; j < ch; j++) { + data[j][i] = interleaved[i * ch + j]; + } + } + } else { + Residue_DecodeCore(ctx, r, size, ch, doNotDecode, data); + } +} + + +/*########################################################################################################################* +*----------------------------------------------------Vorbis mappings------------------------------------------------------* +*#########################################################################################################################*/ +#define MAPPING_MAX_COUPLINGS 256 +#define MAPPING_MAX_SUBMAPS 15 +struct Mapping { + UInt8 CouplingSteps, Submaps; + UInt8 Mux[VORBIS_MAX_CHANS]; + UInt8 FloorIdx[MAPPING_MAX_SUBMAPS]; + UInt8 ResidueIdx[MAPPING_MAX_SUBMAPS]; + UInt8 Magnitude[MAPPING_MAX_COUPLINGS]; + UInt8 Angle[MAPPING_MAX_COUPLINGS]; +}; + +static ReturnCode Mapping_DecodeSetup(struct VorbisState* ctx, struct Mapping* m) { + Int32 i, submaps = 1, submapFlag = Vorbis_ReadBits(ctx, 1); + if (submapFlag) { + submaps = Vorbis_ReadBits(ctx, 4); submaps++; + } + + Int32 couplingSteps = 0, couplingFlag = Vorbis_ReadBits(ctx, 1); + if (couplingFlag) { + couplingSteps = Vorbis_ReadBits(ctx, 8); couplingSteps++; + /* TODO: How big can couplingSteps ever really get in practice? */ + Int32 couplingBits = iLog(ctx->Channels - 1); + for (i = 0; i < couplingSteps; i++) { + m->Magnitude[i] = Vorbis_ReadBits(ctx, couplingBits); + m->Angle[i] = Vorbis_ReadBits(ctx, couplingBits); + if (m->Magnitude[i] == m->Angle[i]) return VORBIS_ERR_MAPPING_CHANS; + } + } + + Int32 reserved = Vorbis_ReadBits(ctx, 2); + if (reserved != 0) return VORBIS_ERR_MAPPING_RESERVED; + m->Submaps = submaps; + m->CouplingSteps = couplingSteps; + + if (submaps > 1) { + for (i = 0; i < ctx->Channels; i++) { + m->Mux[i] = Vorbis_ReadBits(ctx, 4); + } + } else { + for (i = 0; i < ctx->Channels; i++) { + m->Mux[i] = 0; + } + } + + for (i = 0; i < submaps; i++) { + Vorbis_ReadBits(ctx, 8); /* time value */ + m->FloorIdx[i] = Vorbis_ReadBits(ctx, 8); + m->ResidueIdx[i] = Vorbis_ReadBits(ctx, 8); + } + return 0; +} + + +/*########################################################################################################################* +*------------------------------------------------------imdct impl---------------------------------------------------------* +*#########################################################################################################################*/ +#define PI MATH_PI +void imdct(Real32* in, Real32* out, Int32 N) { + Int32 i, k; + + for (i = 0; i < 2 * N; i++) { + Real64 sum = 0; + for (k = 0; k < N; k++) { + sum += in[k] * Math_Cos((PI / N) * (i + 0.5 + N * 0.5) * (k + 0.5)); + } + out[i] = sum; + } +} + +static UInt32 ReverseBits(UInt32 v) { + v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1); + v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2); + v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4); + v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8); + v = (v >> 16) | (v << 16); + return v; +} + +static UInt32 Log2(UInt32 v) { + UInt32 r = 0; + while (v >>= 1) r++; + return r; +} + +#define VORBIS_MAX_BLOCK_SIZE 8192 +void imdct_fast(Real32* in, Real32* out, Int32 n) { + Int32 n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, n3_4 = n - n4; + Int32 k, k2, k4, i, j; + + /* Optimised algorithm from "The use of multirate filter banks for coding of high quality digital audio" */ + /* Uses a few fixes for the paper noted at http://www.nothings.org/stb_vorbis/mdct_01.txt */ + + Real32 A[VORBIS_MAX_BLOCK_SIZE / 2]; + Real32 B[VORBIS_MAX_BLOCK_SIZE / 2]; + Real32 C[VORBIS_MAX_BLOCK_SIZE / 4]; + /* setup twiddle factors */ + for (k = 0, k2 = 0; k < n4; k++, k2 += 2) { + A[k2] = Math_Cos((4*k * PI) / n); + A[k2+1] = -Math_Sin((4*k * PI) / n); + B[k2] = Math_Cos(((k2+1) * PI) / (2*n)); + B[k2+1] = Math_Sin(((k2+1) * PI) / (2*n)); + } + for (k = 0, k2 = 0; k < n8; k++, k2 += 2) { + C[k2] = Math_Cos(((k2+1) * (2*PI)) / n); + C[k2+1] = -Math_Sin(((k2+1) * (2*PI)) / n); + } + + Real32 u[VORBIS_MAX_BLOCK_SIZE]; + Real32 v[VORBIS_MAX_BLOCK_SIZE]; + Real32 w[VORBIS_MAX_BLOCK_SIZE]; + Real32 X[VORBIS_MAX_BLOCK_SIZE]; + /* spectral coefficients */ + for (k = 0; k < n2; k++) u[k] = in[k]; + for ( ; k < n; k++) u[k] = -in[n-k-1]; + + /* step 1 */ + for (k = 0, k2 = 0, k4 = 0; k < n4; k++, k2 += 2, k4 += 4) { + v[n-k4-1] = (u[k4] - u[n-k4-1]) * A[k2] - (u[k4+2] - u[n-k4-3]) * A[k2 + 1]; + v[n-k4-3] = (u[k4] - u[n-k4-1]) * A[k2+1] + (u[k4+2] - u[n-k4-3]) * A[k2]; + } + + /* step 2 */ + for (k = 0, k4 = 0; k < n8; k++, k4 += 4) { + w[n2+3+k4] = v[n2+3+k4] + v[k4+3]; + w[n2+1+k4] = v[n2+1+k4] + v[k4+1]; + + w[k4+3] = (v[n2+3+k4] - v[k4+3]) * A[n2-4-k4] - (v[n2+1+k4] - v[k4+1]) * A[n2-3-k4]; + w[k4+1] = (v[n2+1+k4] - v[k4+1]) * A[n2-4-k4] + (v[n2+3+k4] - v[k4+3]) * A[n2-3-k4]; + } + + /* step 3*/ + Int32 l, ld_n = Log2(n); + for (l = 0; l <= ld_n - 4; l++) { + Int32 k0 = n >> (l+2), k1 = 1 << (l+3); + Int32 r, r4, rMax = n >> (l+4), s2, s2Max = 1 << (l+2); + + for (r = 0, r4 = 0; r < rMax; r++, r4 += 4) { + for (s2 = 0; s2 < s2Max; s2 += 2) { + u[n-1-k0*s2-r4] = w[n-1-k0*s2-r4] + w[n-1-k0*(s2+1)-r4]; + u[n-3-k0*s2-r4] = w[n-3-k0*s2-r4] + w[n-3-k0*(s2+1)-r4]; + + u[n-1-k0*(s2+1)-r4] = (w[n-1-k0*s2-r4] - w[n-1-k0*(s2+1)-r4]) * A[r*k1] + - (w[n-3-k0*s2-r4] - w[n-3-k0*(s2+1)-r4]) * A[r*k1+1]; + u[n-3-k0*(s2+1)-r4] = (w[n-3-k0*s2-r4] - w[n-3-k0*(s2+1)-r4]) * A[r*k1] + + (w[n-1-k0*s2-r4] - w[n-1-k0*(s2+1)-r4]) * A[r*k1+1]; + } + } + + if (l+1 <= ld_n - 4) { + Platform_MemCpy(w, u, sizeof(u)); + } + } + + /* step 4 */ + for (i = 0; i < n8; i++) { + j = ReverseBits(i) >> (32-ld_n+3); + if (i == j) { + Int32 i8 = i << 3; + v[i8+1] = u[i8+1]; v[i8+3] = u[i8+3]; + v[i8+5] = u[i8+5]; v[i8+7] = u[i8+7]; + } else if (i < j) { + Int32 i8 = i << 3, j8 = j << 3; + v[j8+1] = u[i8+1]; v[i8+1] = u[j8+1]; + v[j8+3] = u[i8+3]; v[i8+3] = u[j8+3]; + v[j8+5] = u[i8+5]; v[i8+5] = u[j8+5]; + v[j8+7] = u[i8+7]; v[i8+7] = u[j8+7]; + } + } + + /* step 5 */ + for (k = 0, k2 = 0; k < n2; k++, k2 += 2) { + w[k] = v[k2+1]; + } + + /* step 6 */ + for (k = 0, k2 = 0, k4 = 0; k < n8; k++, k2 += 2, k4 += 4) { + u[n-1-k2] = w[k4]; + u[n-2-k2] = w[k4+1]; + u[n3_4-1-k2] = w[k4+2]; + u[n3_4-2-k2] = w[k4+3]; + } + + /* step 7 */ + for (k = 0, k2 = 0; k < n8; k++, k2 += 2) { + v[n2+k2] = ( u[n2+k2] + u[n-2-k2] + C[k2+1] * (u[n2+k2] - u[n-2-k2]) + C[k2] * (u[n2+k2+1] + u[n-2-k2+1])) * 0.5f; + v[n-2-k2] = ( u[n2+k2] + u[n-2-k2] - C[k2+1] * (u[n2+k2] - u[n-2-k2]) - C[k2] * (u[n2+k2+1] + u[n-2-k2+1])) * 0.5f; + v[n2+k2+1] = ( u[n2+k2+1] - u[n-1-k2] + C[k2+1] * (u[n2+1+k2] + u[n-1-k2]) - C[k2] * (u[n2+k2] - u[n-2-k2])) * 0.5f; + v[n-1-k2] = (-u[n2+k2+1] + u[n-1-k2] + C[k2+1] * (u[n2+1+k2] + u[n-1-k2]) - C[k2] * (u[n2+k2] - u[n-2-k2])) * 0.5f; + } + + /* step 8 */ + for (k = 0, k2 = 0; k < n4; k++, k2 += 2) { + X[k] = v[k2+n2] * B[k2] + v[k2+1+n2] * B[k2+1]; + X[n2-1-k] = v[k2+n2] * B[k2+1] - v[k2+1+n2] * B[k2]; + } + + /* output */ + for (k = 0; k < n4; k++) out[k] = 0.5f * X[k+n4]; + for ( ; k < n3_4; k++) out[k] = 0.5f * -X[n3_4-k-1]; + for ( ; k < n; k++) out[k] = 0.5f * -X[k-n3_4]; +} + + +/*########################################################################################################################* +*-----------------------------------------------------Vorbis setup--------------------------------------------------------* +*#########################################################################################################################*/ +struct Mode { UInt8 BlockSizeFlag, MappingIdx; }; +static ReturnCode Mode_DecodeSetup(struct VorbisState* ctx, struct Mode* m) { + m->BlockSizeFlag = Vorbis_ReadBits(ctx, 1); + UInt16 windowType = Vorbis_ReadBits(ctx, 16); + if (windowType != 0) return VORBIS_ERR_MODE_WINDOW; + + UInt16 transformType = Vorbis_ReadBits(ctx, 16); + if (transformType != 0) return VORBIS_ERR_MODE_TRANSFORM; + m->MappingIdx = Vorbis_ReadBits(ctx, 8); + return 0; +} + +void Vorbis_Init(struct VorbisState* ctx, struct Stream* source) { + Platform_MemSet(ctx, 0, sizeof(struct VorbisState)); + ctx->Source = source; +} + +/* TODO: Work out Vorbis_Free implementation */ + +static bool Vorbis_ValidBlockSize(UInt32 blockSize) { + return blockSize >= 64 && blockSize <= 8192 && Math_IsPowOf2(blockSize); +} + +static ReturnCode Vorbis_DecodeHeader(struct VorbisState* ctx, UInt8 type) { + UInt8 header[7]; + Stream_Read(ctx->Source, header, sizeof(header)); + if (header[0] != type) return VORBIS_ERR_WRONG_HEADER; + + bool OK = + header[1] == 'v' && header[2] == 'o' && header[3] == 'r' && + header[4] == 'b' && header[5] == 'i' && header[6] == 's'; + return OK ? 0 : ReturnCode_InvalidArg; +} + +static ReturnCode Vorbis_DecodeIdentifier(struct VorbisState* ctx) { + UInt8 header[23]; + Stream_Read(ctx->Source, header, sizeof(header)); + UInt32 version = Stream_GetU32_LE(&header[0]); + if (version != 0) return VORBIS_ERR_VERSION; + + ctx->Channels = header[4]; + ctx->SampleRate = Stream_GetU32_LE(&header[5]); + /* (12) bitrate_maximum, nominal, minimum */ + ctx->BlockSizes[0] = 1 << (header[21] & 0xF); + ctx->BlockSizes[1] = 1 << (header[21] >> 4); + + if (!Vorbis_ValidBlockSize(ctx->BlockSizes[0])) return VORBIS_ERR_BLOCKSIZE; + if (!Vorbis_ValidBlockSize(ctx->BlockSizes[1])) return VORBIS_ERR_BLOCKSIZE; + if (ctx->BlockSizes[0] > ctx->BlockSizes[1]) return VORBIS_ERR_BLOCKSIZE; + + if (ctx->Channels == 0 || ctx->Channels > VORBIS_MAX_CHANS) return VORBIS_ERR_CHANS; + /* check framing flag */ + return (header[22] & 1) ? 0 : VORBIS_ERR_FRAMING; +} + +static ReturnCode Vorbis_DecodeComments(struct VorbisState* ctx) { + UInt32 vendorLen = Stream_ReadU32_LE(ctx->Source); + Stream_Skip(ctx->Source, vendorLen); + + UInt32 i, comments = Stream_ReadU32_LE(ctx->Source); + for (i = 0; i < comments; i++) { + UInt32 commentLen = Stream_ReadU32_LE(ctx->Source); + Stream_Skip(ctx->Source, commentLen); + } + + /* check framing flag */ + return (Stream_ReadU8(ctx->Source) & 1) ? 0 : VORBIS_ERR_FRAMING; +} + +static ReturnCode Vorbis_DecodeSetup(struct VorbisState* ctx) { + Int32 i, count; + ReturnCode result; + + count = Vorbis_ReadBits(ctx, 8); count++; + ctx->Codebooks = Platform_MemAlloc(count, sizeof(struct Codebook), "vorbis codebooks"); + for (i = 0; i < count; i++) { + result = Codebook_DecodeSetup(ctx, &ctx->Codebooks[i]); + if (result) return result; + } + + count = Vorbis_ReadBits(ctx, 6); count++; + for (i = 0; i < count; i++) { + UInt16 time = Vorbis_ReadBits(ctx, 16); + if (time != 0) return VORBIS_ERR_TIME_TYPE; + } + + count = Vorbis_ReadBits(ctx, 6); count++; + ctx->Floors = Platform_MemAlloc(count, sizeof(struct Floor), "vorbis floors"); + for (i = 0; i < count; i++) { + UInt16 floor = Vorbis_ReadBits(ctx, 16); + if (floor != 1) return VORBIS_ERR_FLOOR_TYPE; + result = Floor_DecodeSetup(ctx, &ctx->Floors[i]); + if (result) return result; + } + + count = Vorbis_ReadBits(ctx, 6); count++; + ctx->Residues = Platform_MemAlloc(count, sizeof(struct Residue), "vorbis residues"); + for (i = 0; i < count; i++) { + UInt16 residue = Vorbis_ReadBits(ctx, 16); + if (residue > 2) return VORBIS_ERR_FLOOR_TYPE; + result = Residue_DecodeSetup(ctx, &ctx->Residues[i], residue); + if (result) return result; + } + + count = Vorbis_ReadBits(ctx, 6); count++; + ctx->Mappings = Platform_MemAlloc(count, sizeof(struct Mapping), "vorbis mappings"); + for (i = 0; i < count; i++) { + UInt16 mapping = Vorbis_ReadBits(ctx, 16); + if (mapping != 0) return VORBIS_ERR_MAPPING_TYPE; + result = Mapping_DecodeSetup(ctx, &ctx->Mappings[i]); + if (result) return result; + } + + count = Vorbis_ReadBits(ctx, 6); count++; + ctx->Modes = Platform_MemAlloc(count, sizeof(struct Mode), "vorbis modes"); + for (i = 0; i < count; i++) { + result = Mode_DecodeSetup(ctx, &ctx->Modes[i]); + if (result) return result; + } + + ctx->ModeNumBits = iLog(count - 1); /* ilog([vorbis_mode_count]-1) bits */ + UInt8 framing = Vorbis_ReadBits(ctx, 1); + Vorbis_AlignBits(ctx); + /* check framing flag */ + return (framing & 1) ? 0 : VORBIS_ERR_FRAMING; +} + +enum VORBIS_PACKET { VORBIS_IDENTIFIER = 1, VORBIS_COMMENTS = 3, VORBIS_SETUP = 5, }; +ReturnCode Vorbis_DecodeHeaders(struct VorbisState* ctx) { + ReturnCode result; + + result = Vorbis_DecodeHeader(ctx, VORBIS_IDENTIFIER); + if (result) return result; + result = Vorbis_DecodeIdentifier(ctx); + if (result) return result; + + result = Vorbis_DecodeHeader(ctx, VORBIS_COMMENTS); + if (result) return result; + result = Vorbis_DecodeComments(ctx); + if (result) return result; + + result = Vorbis_DecodeHeader(ctx, VORBIS_SETUP); + if (result) return result; + result = Vorbis_DecodeSetup(ctx); + if (result) return result; + + return 0; +} + + +/*########################################################################################################################* +*-----------------------------------------------------Vorbis frame--------------------------------------------------------* +*#########################################################################################################################*/ +ReturnCode Vorbis_DecodeFrame(struct VorbisState* ctx) { + Int32 i, j, packetType = Vorbis_ReadBits(ctx, 1); + if (packetType) return VORBIS_ERR_FRAME_TYPE; + + Int32 modeIdx = Vorbis_ReadBits(ctx, ctx->ModeNumBits); + struct Mode* mode = &ctx->Modes[modeIdx]; + struct Mapping* mapping = &ctx->Mappings[mode->MappingIdx]; + + /* decode window shape */ + ctx->CurBlockSize = ctx->BlockSizes[mode->BlockSizeFlag]; + ctx->DataSize = ctx->CurBlockSize / 2; + Int32 prev_window, next_window; + /* long window lapping*/ + if (mode->BlockSizeFlag) { + prev_window = Vorbis_ReadBits(ctx, 1); + next_window = Vorbis_ReadBits(ctx, 1); + } + + ctx->Values = Platform_MemAllocCleared(ctx->Channels * ctx->DataSize, sizeof(Real32), "audio values"); + + /* decode floor */ + bool hasFloor[VORBIS_MAX_CHANS], hasResidue[VORBIS_MAX_CHANS]; + for (i = 0; i < ctx->Channels; i++) { + Int32 submap = mapping->Mux[i]; + Int32 floorIdx = mapping->FloorIdx[submap]; + hasFloor[i] = Floor_DecodeFrame(ctx, &ctx->Floors[floorIdx], i); + hasResidue[i] = hasFloor[i]; + } + + /* non-zero vector propogate */ + for (i = 0; i < mapping->CouplingSteps; i++) { + Int32 magChannel = mapping->Magnitude[i]; + Int32 angChannel = mapping->Angle[i]; + + if (hasResidue[magChannel] || hasResidue[angChannel]) { + hasResidue[magChannel] = true; hasResidue[angChannel] = true; + } + } + + /* decode residue */ + for (i = 0; i < mapping->Submaps; i++) { + Int32 ch = 0; + bool doNotDecode[VORBIS_MAX_CHANS]; + Real32* data[VORBIS_MAX_CHANS]; /* map residue data to actual channel data */ + for (j = 0; j < ctx->Channels; j++) { + if (mapping->Mux[j] != i) continue; + + doNotDecode[ch] = !hasResidue[j]; + data[ch] = Vorbis_ChanData(ctx, j); + ch++; + } + + Int32 residueIdx = mapping->FloorIdx[i]; + Residue_DecodeFrame(ctx, &ctx->Residues[residueIdx], ch, doNotDecode, data); + } + + /* inverse coupling */ + for (i = mapping->CouplingSteps - 1; i >= 0; i--) { + Real32* magValues = Vorbis_ChanData(ctx, mapping->Magnitude[i]); + Real32* angValues = Vorbis_ChanData(ctx, mapping->Angle[i]); + + for (j = 0; j < ctx->DataSize; j++) { + Real32 m = magValues[j], a = angValues[j]; + + if (m > 0.0f) { + if (a > 0.0f) { angValues[j] = m - a; } + else { + angValues[j] = m; + magValues[j] = m + a; + } + } else { + if (a > 0.0f) { angValues[j] = m + a; } + else { + angValues[j] = m; + magValues[j] = m - a; + } + } + } + } + + /* compute dot product of floor and residue, producing audio spectrum vector */ + for (i = 0; i < ctx->Channels; i++) { + if (!hasFloor[i]) continue; + Int32 submap = mapping->Mux[i]; + Int32 floorIdx = mapping->FloorIdx[submap]; + Floor_Synthesis(ctx, &ctx->Floors[floorIdx], i); + } + + Int32 n = ctx->CurBlockSize; + Int32 window_center = n / 2; + Int32 left_window_beg, left_window_end, left_n; + Int32 right_window_beg, right_window_end, right_n; + + if (mode->BlockSizeFlag && !prev_window) { + left_window_beg = n / 4 - ctx->BlockSizes[0] / 4; + left_window_end = n / 4 + ctx->BlockSizes[0] / 4; + left_n = ctx->BlockSizes[0] / 2; + } else { + left_window_beg = 0; + left_window_end = window_center; + left_n = n / 2; + } + + if (mode->BlockSizeFlag && !next_window) { + right_window_beg = (n*3) / 4 - ctx->BlockSizes[0] / 4; + right_window_end = (n*3) / 4 + ctx->BlockSizes[0] / 4; + right_n = ctx->BlockSizes[0] / 2; + } else { + right_window_beg = window_center; + right_window_end = n; + right_n = n / 2; + } + + Real32* window = Platform_MemAlloc(n, sizeof(Real32), "temp window"); + for (i = 0; i < left_window_beg; i++) window[i] = 0; + for (i = left_window_beg; i < left_window_end; i++) { + Real64 inner = Math_Sin((i - left_window_beg + 0.5) / left_n * (PI/2)); + window[i] = Math_Sin((PI/2) * inner * inner); + } + for (i = left_window_end; i < right_window_beg; i++) window[i] = 1; + for (i = right_window_beg; i < right_window_end; i++) { + Real64 inner = Math_Sin((i - right_window_beg + 0.5) / right_n * (PI/2) + (PI/2)); + window[i] = Math_Sin((PI/2) * inner * inner); + } + for (i = right_window_end; i < n; i++) window[i] = 0; + + /* inverse monolithic transform of audio spectrum vector */ + for (i = 0; i < ctx->Channels; i++) { + if (!hasFloor[i]) { + ctx->CurOutput[i] = Platform_MemAllocCleared(ctx->CurBlockSize, sizeof(Real32), "empty output"); + continue; + } + Int32 submap = mapping->Mux[i]; + Int32 floorIdx = mapping->FloorIdx[submap]; + + Real32* data = Vorbis_ChanData(ctx, i); + Real32* output = Platform_MemAlloc(ctx->CurBlockSize, sizeof(Real32), "imdct output"); + imdct_fast(data, output, ctx->CurBlockSize); + + /* apply windowing */ + for (j = 0; j < n; j++) { output[j] *= window[j]; } + ctx->CurOutput[i] = output; + } + + /* discard remaining bits at end of packet */ + Vorbis_AlignBits(ctx); +} + +Int32 Vorbis_OutputFrame(struct VorbisState* ctx, Int16* data) { + Int32 i, j = 0, ch, size = 0; + if (ctx->PrevBlockSize == 0) goto finish; + size = (ctx->PrevBlockSize / 4) + (ctx->CurBlockSize / 4); + + Platform_LogConst("##### FRAME #####" ); + /* TODO: There's probably a nicer way of doing this.. */ + Real32* combined[VORBIS_MAX_CHANS]; + for (i = 0; i < ctx->Channels; i++) { + combined[i] = Platform_MemAllocCleared(size, sizeof(Real32), "temp combined"); + } + + Int32 prevHalf = ctx->PrevBlockSize / 2, curHalf = ctx->CurBlockSize / 2; + Int32 prevEnd = prevHalf + min(prevHalf, size); + Int32 curBeg = curHalf - min(curHalf, size); + + /* overlap and add data */ + for (i = prevHalf; i < prevEnd; i++, j++) { + for (ch = 0; ch < ctx->Channels; ch++) { + combined[ch][j] = ctx->PrevOutput[ch][i]; + } + } + + for (i = curHalf - 1, j = size - 1; i >= curBeg; i--, j--) { + for (ch = 0; ch < ctx->Channels; ch++) { + combined[ch][j] += ctx->CurOutput[ch][i]; + } + } + + for (i = 0; i < size; i++) { + for (ch = 0; ch < ctx->Channels; ch++) { + Real32 sample = combined[ch][i]; + Math_Clamp(sample, -1.0f, 1.0f); + *data++ = (Int16)(sample * 32767); + } + } + +finish: + ctx->PrevBlockSize = ctx->CurBlockSize; + for (i = 0; i < VORBIS_MAX_CHANS; i++) { + ctx->PrevOutput[i] = ctx->CurOutput[i]; + } + return size; +} + +static Real32 floor1_inverse_dB_table[256] = { + 1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f, + 1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f, + 1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f, + 2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f, + 2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f, + 3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f, + 4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f, + 6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f, + 7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f, + 1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f, + 1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f, + 1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f, + 2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f, + 2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f, + 3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f, + 4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f, + 5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f, + 7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f, + 9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f, + 1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f, + 1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f, + 2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f, + 2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f, + 3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f, + 4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f, + 5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f, + 7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f, + 9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f, + 0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f, + 0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f, + 0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f, + 0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f, + 0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f, + 0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f, + 0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f, + 0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f, + 0.00092223983f, 0.00098217216f, 0.0010459992f, 0.0011139742f, + 0.0011863665f, 0.0012634633f, 0.0013455702f, 0.0014330129f, + 0.0015261382f, 0.0016253153f, 0.0017309374f, 0.0018434235f, + 0.0019632195f, 0.0020908006f, 0.0022266726f, 0.0023713743f, + 0.0025254795f, 0.0026895994f, 0.0028643847f, 0.0030505286f, + 0.0032487691f, 0.0034598925f, 0.0036847358f, 0.0039241906f, + 0.0041792066f, 0.0044507950f, 0.0047400328f, 0.0050480668f, + 0.0053761186f, 0.0057254891f, 0.0060975636f, 0.0064938176f, + 0.0069158225f, 0.0073652516f, 0.0078438871f, 0.0083536271f, + 0.0088964928f, 0.009474637f, 0.010090352f, 0.010746080f, + 0.011444421f, 0.012188144f, 0.012980198f, 0.013823725f, + 0.014722068f, 0.015678791f, 0.016697687f, 0.017782797f, + 0.018938423f, 0.020169149f, 0.021479854f, 0.022875735f, + 0.024362330f, 0.025945531f, 0.027631618f, 0.029427276f, + 0.031339626f, 0.033376252f, 0.035545228f, 0.037855157f, + 0.040315199f, 0.042935108f, 0.045725273f, 0.048696758f, + 0.051861348f, 0.055231591f, 0.058820850f, 0.062643361f, + 0.066714279f, 0.071049749f, 0.075666962f, 0.080584227f, + 0.085821044f, 0.091398179f, 0.097337747f, 0.10366330f, + 0.11039993f, 0.11757434f, 0.12521498f, 0.13335215f, + 0.14201813f, 0.15124727f, 0.16107617f, 0.17154380f, + 0.18269168f, 0.19456402f, 0.20720788f, 0.22067342f, + 0.23501402f, 0.25028656f, 0.26655159f, 0.28387361f, + 0.30232132f, 0.32196786f, 0.34289114f, 0.36517414f, + 0.38890521f, 0.41417847f, 0.44109412f, 0.46975890f, + 0.50028648f, 0.53279791f, 0.56742212f, 0.60429640f, + 0.64356699f, 0.68538959f, 0.72993007f, 0.77736504f, + 0.82788260f, 0.88168307f, 0.9389798f, 1.00000000f, +}; diff --git a/src/Client/Vorbis.h b/src/Client/Vorbis.h new file mode 100644 index 000000000..4b2a18086 --- /dev/null +++ b/src/Client/Vorbis.h @@ -0,0 +1,49 @@ +#ifndef CC_VORBIS_H +#define CC_VORBIS_H +#include "Typedefs.h" +/* Decodes ogg vorbis audio + Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 +*/ +struct IGameComponent; +struct Stream; + +enum OGG_VER { OGG_ERR_INVALID_SIG = 2552401, OGG_ERR_VERSION }; +#define OGG_BUFFER_SIZE (255 * 256) +void Ogg_MakeStream(struct Stream* stream, UInt8* buffer, struct Stream* source); + +enum VORBIS_ERR { + VORBIS_ERR_HEADER = 5238001, VORBIS_ERR_WRONG_HEADER, VORBIS_ERR_FRAMING, + VORBIS_ERR_VERSION, VORBIS_ERR_BLOCKSIZE, VORBIS_ERR_CHANS, + VORBIS_ERR_TIME_TYPE, VORBIS_ERR_FLOOR_TYPE, VORBIS_ERR_RESIDUE_TYPE, + VORBIS_ERR_MAPPING_TYPE, VORBIS_ERR_MODE_TYPE, + VORBIS_ERR_CODEBOOK_SYNC, VORBIS_ERR_CODEBOOK_ENTRY, VORBIS_ERR_CODEBOOK_LOOKUP, + VORBIS_ERR_MODE_WINDOW, VORBIS_ERR_MODE_TRANSFORM, + VORBIS_ERR_MAPPING_CHANS, VORBIS_ERR_MAPPING_RESERVED, + VORBIS_ERR_FRAME_TYPE, +}; + +#define VORBIS_MAX_CHANS 8 +struct Codebook; struct Floor; struct Residue; struct Mapping; struct Mode; +struct VorbisState { + UInt32 Bits; /* Holds bits across byte boundaries*/ + UInt32 NumBits; /* Number of bits in Bits buffer*/ + struct Stream* Source; /* Source for filling Input buffer */ + + UInt8 Channels, ModeNumBits; + UInt16 CurBlockSize, PrevBlockSize, DataSize; + Int32 SampleRate; Int32 BlockSizes[2]; + Real32* Values; + Real32* PrevOutput[VORBIS_MAX_CHANS]; + Real32* CurOutput[VORBIS_MAX_CHANS]; + + struct Codebook* Codebooks; + struct Floor* Floors; + struct Residue* Residues; + struct Mapping* Mappings; + struct Mode* Modes; +}; +void Vorbis_Init(struct VorbisState* ctx, struct Stream* source); +ReturnCode Vorbis_DecodeHeaders(struct VorbisState* ctx); +ReturnCode Vorbis_DecodeFrame(struct VorbisState* ctx); +Int32 Vorbis_OutputFrame(struct VorbisState* ctx, Int16* data); +#endif diff --git a/src/Client/WinPlatform.c b/src/Client/WinPlatform.c index 9831adf3a..488e701a2 100644 --- a/src/Client/WinPlatform.c +++ b/src/Client/WinPlatform.c @@ -16,6 +16,7 @@ #include #include #include +#include /* Missing from some old MingW32 headers */ #define HTTP_QUERY_ETAG 54 @@ -597,4 +598,97 @@ ReturnCode Platform_HttpFreeRequest(void* handle) { ReturnCode Platform_HttpFree(void) { return InternetCloseHandle(hInternet) ? 0 : GetLastError(); } + + +struct AudioContext { + HWAVEOUT Handle; + WAVEHDR Headers[AUDIO_MAX_CHUNKS]; + struct AudioFormat Format; + UInt8 NumBuffers, PlayingAsync; +}; +struct AudioContext Audio_Contexts[20]; + +void Platform_AudioInit(Int32* handle, UInt8 buffers) { + Int32 i; + for (i = 0; i < Array_Elems(Audio_Contexts); i++) { + struct AudioContext* ctx = &Audio_Contexts[i]; + if (ctx->NumBuffers) continue; + + ctx->NumBuffers = buffers; + *handle = i; return; + } + ErrorHandler_Fail("No free audio contexts"); +} + +void Platform_AudioFree(Int32 handle) { + struct AudioContext* ctx = &Audio_Contexts[handle]; + if (!ctx->Handle) return; + + ReturnCode result = waveOutClose(ctx->Handle); + Platform_MemSet(ctx, 0, sizeof(struct AudioContext)); + ErrorHandler_CheckOrFail(result, "Audio - closing device"); +} + +void Platform_AudioSetFormat(Int32 handle, struct AudioFormat* format) { + struct AudioContext* ctx = &Audio_Contexts[handle]; + struct AudioFormat* cur = &ctx->Format; + + /* only recreate handle if we need to */ + if (AudioFormat_Eq(cur, format)) return; + if (ctx->Handle) Platform_AudioFree(handle); + + WAVEFORMATEX fmt = { 0 }; + fmt.nChannels = format->Channels; + fmt.wFormatTag = WAVE_FORMAT_PCM; + fmt.wBitsPerSample = format->BitsPerSample; + fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8; + fmt.nSamplesPerSec = format->Frequency; + fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign; + + if (waveOutGetNumDevs() == 0u) ErrorHandler_Fail("No audio devices found"); + ReturnCode result = waveOutOpen(&ctx->Handle, UInt32_MaxValue, &fmt, NULL, NULL, CALLBACK_NULL); + ErrorHandler_CheckOrFail(result, "Audio - opening device"); +} + +void Platform_AudioPlayAsync(Int32 handle, void* data, UInt32 dataSize) { + struct AudioContext* ctx = &Audio_Contexts[handle]; + WAVEHDR* hdr = &ctx->Headers[0]; + Platform_MemSet(hdr, 0, sizeof(WAVEHDR)); + + hdr->lpData = data; + hdr->dwBufferLength = dataSize; + hdr->dwLoops = 1; + ctx->PlayingAsync = true; + + ReturnCode result = waveOutPrepareHeader(ctx->Handle, hdr, sizeof(WAVEHDR)); + ErrorHandler_CheckOrFail(result, "Audio - prepare header"); + result = waveOutWrite(ctx->Handle, hdr, sizeof(WAVEHDR)); + ErrorHandler_CheckOrFail(result, "Audio - write header"); +} + +Int32 Platform_AudioNextFinishedAsync(Int32 handle) { + struct AudioContext* ctx = &Audio_Contexts[handle]; + Int32 i; + for (i = 0; i < ctx->NumBuffers; i++) { + WAVEHDR* hdr = &ctx->Headers[i]; + if (!(hdr->dwFlags & WHDR_DONE)) continue; + + if (hdr->dwFlags & WHDR_PREPARED) { + ReturnCode result = waveOutUnprepareHeader(ctx->Handle, hdr, sizeof(WAVEHDR)); + ErrorHandler_CheckOrFail(result, "Audio - unprepare header"); + } + return i; + } + return -1; +} + +bool Platform_AudioFinishedAsync(Int32 handle) { + struct AudioContext* ctx = &Audio_Contexts[handle]; + if (!ctx->PlayingAsync) return true; + Int32 index = Platform_AudioNextFinishedAsync(handle); + + if (index >= 0) return false; + ctx->PlayingAsync = false; + return true; +} #endif