From 5c70495ed377c135a6d34a9d7fdaa334f6f07ae5 Mon Sep 17 00:00:00 2001 From: dashodanger Date: Tue, 15 Oct 2024 16:50:45 -0600 Subject: [PATCH 1/6] Initial floating point mixer efforts --- docs/licenses/License Attribution.txt | 5 +- libraries/CMakeLists.txt | 2 - libraries/crsid/host/audio.c | 20 + libraries/crsid/libcRSID.h | 1 + libraries/fmmidi/CHANGES.txt | 10 - libraries/fmmidi/CMakeLists.txt | 10 - libraries/fmmidi/LICENSE.txt | 26 - libraries/fmmidi/filter.cpp | 286 - libraries/fmmidi/filter.hpp | 54 - libraries/fmmidi/midisynth.cpp | 2685 --- libraries/fmmidi/midisynth.hpp | 681 - libraries/libRAD/opal.cpp | 10 +- libraries/libRAD/opal.h | 3 +- libraries/libRAD/radmidi.cpp | 4 +- libraries/libRAD/radmidi.h | 6 +- libraries/m4p/it2drivers/sb16.c | 49 + libraries/m4p/it_music.c | 14 + libraries/m4p/it_music.h | 3 + libraries/m4p/m4p.c | 10 + libraries/m4p/m4p.h | 3 + libraries/m4p/pmp_mix.c | 69 + libraries/m4p/pmp_mix.h | 1 + libraries/minivorbis/.gitignore | 6 - libraries/minivorbis/CMakeLists.txt | 7 - libraries/minivorbis/LICENSE.txt | 29 - libraries/minivorbis/README.md | 87 - libraries/minivorbis/minivorbis.cc | 5 - libraries/minivorbis/minivorbis.h | 21776 ------------------------ libraries/stb/stb_vorbis.h | 5584 ++++++ source_files/edge/CMakeLists.txt | 3 - source_files/edge/i_movie.cc | 67 +- source_files/edge/i_sound.cc | 11 +- source_files/edge/m_option.cc | 7 +- source_files/edge/s_blit.cc | 179 +- source_files/edge/s_blit.h | 10 +- source_files/edge/s_cache.cc | 34 +- source_files/edge/s_flac.cc | 30 +- source_files/edge/s_fluid.cc | 22 +- source_files/edge/s_fmm.cc | 383 - source_files/edge/s_fmm.h | 28 - source_files/edge/s_m4p.cc | 20 +- source_files/edge/s_mp3.cc | 28 +- source_files/edge/s_music.cc | 7 +- source_files/edge/s_ogg.cc | 351 +- source_files/edge/s_opl.cc | 22 +- source_files/edge/s_rad.cc | 16 +- source_files/edge/s_sid.cc | 20 +- source_files/edge/s_sound.cc | 15 +- source_files/edge/s_wav.cc | 8 +- source_files/edge/snd_data.cc | 463 +- source_files/edge/snd_data.h | 30 +- source_files/edge/snd_gather.cc | 35 +- source_files/edge/snd_gather.h | 2 +- 53 files changed, 6066 insertions(+), 27171 deletions(-) delete mode 100644 libraries/fmmidi/CHANGES.txt delete mode 100644 libraries/fmmidi/CMakeLists.txt delete mode 100644 libraries/fmmidi/LICENSE.txt delete mode 100644 libraries/fmmidi/filter.cpp delete mode 100644 libraries/fmmidi/filter.hpp delete mode 100644 libraries/fmmidi/midisynth.cpp delete mode 100644 libraries/fmmidi/midisynth.hpp delete mode 100644 libraries/minivorbis/.gitignore delete mode 100644 libraries/minivorbis/CMakeLists.txt delete mode 100644 libraries/minivorbis/LICENSE.txt delete mode 100644 libraries/minivorbis/README.md delete mode 100644 libraries/minivorbis/minivorbis.cc delete mode 100644 libraries/minivorbis/minivorbis.h create mode 100644 libraries/stb/stb_vorbis.h delete mode 100644 source_files/edge/s_fmm.cc delete mode 100644 source_files/edge/s_fmm.h diff --git a/docs/licenses/License Attribution.txt b/docs/licenses/License Attribution.txt index fc3f6db9e..83e87e133 100644 --- a/docs/licenses/License Attribution.txt +++ b/docs/licenses/License Attribution.txt @@ -25,9 +25,6 @@ libRAD (patches.cpp/radmidi.cpp) - Copyright (c) 2021-2023 Devin Acker Mod4Play library - Copyright (c) 2024 dashodanger Copyright (c) 2022 Olav Sørensen -minivorbis library - Copyright (c) 2020 Eduardo Bart - Copyright (c) 2002-2020 Xiph.org Foundation - ZDoom FName implementation - Copyright (c) 2005-2007 Randy Heit =========================================================================================== @@ -139,7 +136,7 @@ prns.h - Marc B. Reynolds "sf_GMbank" soundfont (renamed to Default.sf2) - The Csound Developers -stb_image, stb_image_write, stb_rect_pack, stb_sprintf and stb_truetype libraries - Sean Barrett +stb_image, stb_image_write, stb_rect_pack, stb_sprintf, stb_truetype and stb_vorbis libraries - Sean Barrett =========================================================================================== SIL Open Font License diff --git a/libraries/CMakeLists.txt b/libraries/CMakeLists.txt index a5efa7654..44423d023 100644 --- a/libraries/CMakeLists.txt +++ b/libraries/CMakeLists.txt @@ -1,7 +1,6 @@ add_subdirectory(almostequals) add_subdirectory(crsid) add_subdirectory(dr_libs) -add_subdirectory(fmmidi) add_subdirectory(fluidlite) if (NOT EDGE_GL_ES2) add_subdirectory(glad) @@ -13,7 +12,6 @@ add_subdirectory(libRAD) add_subdirectory(libvwad) add_subdirectory(lua) add_subdirectory(m4p) -add_subdirectory(minivorbis) add_subdirectory(miniz) add_subdirectory(pl_mpeg) add_subdirectory(prns) diff --git a/libraries/crsid/host/audio.c b/libraries/crsid/host/audio.c index 22f3b1ae1..f1e2c45c3 100644 --- a/libraries/crsid/host/audio.c +++ b/libraries/crsid/host/audio.c @@ -1,3 +1,5 @@ +#include + void cRSID_generateSound(cRSID_C64instance *C64, unsigned char *buf, unsigned short len) { static unsigned short i; @@ -18,6 +20,24 @@ void cRSID_generateSound(cRSID_C64instance *C64, unsigned char *buf, unsigned sh } } +void cRSID_generateFloat(cRSID_C64instance *C64, float *buf, unsigned short len) +{ + static unsigned short i; + static unsigned char j; + static cRSID_Output Output; + static int16_t OutputL, OutputR; + + for (i = 0; i < len; i += 8) + { + for (j = 0; j < C64->PlaybackSpeed; ++j) + Output = cRSID_generateSample(C64); + *(uint32_t *)buf=0x43818000^((uint16_t)Output.L * C64->MainVolume / 256); + *buf++ -= 259.0f; + *(uint32_t *)buf=0x43818000^((uint16_t)Output.R * C64->MainVolume / 256); + *buf++ -= 259.0f; + } +} + static inline cRSID_Output cRSID_generateSample(cRSID_C64instance *C64) { // call this from custom buffer-filler static cRSID_Output Output; diff --git a/libraries/crsid/libcRSID.h b/libraries/crsid/libcRSID.h index a533a7c4c..f6b72a22c 100644 --- a/libraries/crsid/libcRSID.h +++ b/libraries/crsid/libcRSID.h @@ -102,6 +102,7 @@ extern "C" // host/audio.c void cRSID_generateSound(cRSID_C64instance *C64, unsigned char *buf, unsigned short len); + void cRSID_generateFloat(cRSID_C64instance *C64, float *buf, unsigned short len); struct cRSID_SIDheader { // Offset: default/info: diff --git a/libraries/fmmidi/CHANGES.txt b/libraries/fmmidi/CHANGES.txt deleted file mode 100644 index 3e23c80fe..000000000 --- a/libraries/fmmidi/CHANGES.txt +++ /dev/null @@ -1,10 +0,0 @@ -Version 1.0.2 ------- -- Use thread playing to solve sound gaps (need pthread support). - [thanks to supercatexpert for contributing] - -Version 1.0.1 ------- -- Corrected an error that under certain conditions crashed the program at the - end of playing -- Implemented Curses-based interface diff --git a/libraries/fmmidi/CMakeLists.txt b/libraries/fmmidi/CMakeLists.txt deleted file mode 100644 index 3e7aab924..000000000 --- a/libraries/fmmidi/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -########################################## -# fmmidi -########################################## - -add_library(fmmidi - filter.cpp - midisynth.cpp -) - -target_include_directories(fmmidi PUBLIC ./) \ No newline at end of file diff --git a/libraries/fmmidi/LICENSE.txt b/libraries/fmmidi/LICENSE.txt deleted file mode 100644 index 17d8fdc5d..000000000 --- a/libraries/fmmidi/LICENSE.txt +++ /dev/null @@ -1,26 +0,0 @@ -Copyright 2003-2006 yuno. -All Rights Reserved. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this list -of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, this -list of conditions and the following disclaimer in the documentation and/or other -materials provided with the distribution. - -3. The name of the author may not be used to endorse or promote products -derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED -WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN -NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED -TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/libraries/fmmidi/filter.cpp b/libraries/fmmidi/filter.cpp deleted file mode 100644 index 9a9ac556b..000000000 --- a/libraries/fmmidi/filter.cpp +++ /dev/null @@ -1,286 +0,0 @@ -#include "filter.hpp" - -#include -#include - -using namespace std; - -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - -namespace filter -{ -// n��2���Ƃ���ΐ�(�؂�̂�) -int log2(int n) -{ - assert(n >= 1); - int x = 0; - while (n) - { - ++x; - n >>= 1; - } - return x - 1; -} -// n��2���Ƃ���ΐ�(�؂�グ) -int log2_ceil(int n) -{ - assert(n >= 1); - int x = 0; - while (n > 1) - { - ++x; - n >>= 1; - } - return x; -} - -// �����t�[���G�ϊ� -void fft(complex dst[], const complex src[], int n) -{ - assert(n >= 1); - int size = pow2(n); - vector reversal(size); - for (int i = 0; i < size; ++i) - { - int a = i, b = 0; - for (int j = 0; j < n; ++j) - { - b <<= 1; - b |= a & 1; - a >>= 1; - } - reversal[i] = b; - } - vector> x0(&src[0], &src[size]); - vector> x1(size); - complex A = exp(complex(0, -2 * M_PI / size)); - for (int r = 1; r <= n; ++r) - { - int n_r = n - r; - int bit = pow2(n_r); - for (int i = 0; i < size; ++i) - { - int s = i & ~((bit << 1) - 1); // s = i / (bit * 2) * (bit * 2); - s = reversal[s]; - s <<= n_r; // s *= bit; - const complex &src1 = x0[i & ~bit]; - const complex &src2 = x0[i | bit]; - if (i & bit) - { - x1[i] = src1 - src2 * pow(A, s); - } - else - { - x1[i] = src1 + src2 * pow(A, s); - } - } - if (r < n) - { - x0.swap(x1); - } - } - for (int i = 0; i < size; ++i) - { - dst[i] = x1[reversal[i]]; - } -} - -// �t�����t�[���G�ϊ� -void ifft(complex dst[], const complex src[], int n) -{ - assert(n >= 1); - int size = pow2(n); - vector reversal(size); - for (int i = 0; i < size; ++i) - { - int a = i, b = 0; - for (int j = 0; j < n; ++j) - { - b <<= 1; - b |= a & 1; - a >>= 1; - } - reversal[i] = b; - } - vector> x0(&src[0], &src[size]); - vector> x1(size); - complex A = exp(complex(0, 2 * M_PI / size)); - for (int r = 1; r <= n; ++r) - { - int n_r = n - r; - int bit = pow2(n_r); - for (int i = 0; i < size; i++) - { - int s = i & ~((bit << 1) - 1); // s = i / (bit * 2) * (bit * 2); - s = reversal[s]; - s <<= n_r; // s *= bit; - const complex &src1 = x0[i & ~bit]; - const complex &src2 = x0[i | bit]; - if (i & bit) - { - x1[i] = src1 - src2 * pow(A, s); - } - else - { - x1[i] = src1 + src2 * pow(A, s); - } - } - if (r < n) - { - x0.swap(x1); - } - } - for (int i = 0; i < size; ++i) - { - dst[i] = x1[reversal[i]] / static_cast(size); - } -} - -// �n�j���O�� -void hanning_window(double dst[], const double src[], size_t n) -{ - double t = 2 * M_PI / n; - for (size_t i = 0; i < n; ++i) - { - dst[i] = src[i] * (0.5 - 0.5 * cos(t * i)); - } -} - -// FIR�R���X�g���N�^ -finite_impulse_response::finite_impulse_response() -{ - buffer.resize(1); - h.assign(1, 1); - pos = 0; - hlen = 1; -} -// FIR�W���ݒ� -void finite_impulse_response::set_impulse_response(const double *h_, size_t length) -{ - hlen = length; - h.resize(pow2(log2_ceil(length))); - for (size_t i = 0; i < length; ++i) - { - h[i] = static_cast(h_[i] * (1 << 12)); - } - for (size_t i = length; i < h.size(); ++i) - { - h[i] = 0; - } - while (hlen > 1 && h[hlen - 1] == 0) - { - --hlen; - } - length = h.size(); - if (buffer.size() < length) - { - size_t size = buffer.size(); - size_t d = length - size; - buffer.resize(length); - memmove(&buffer[pos + d], &buffer[pos], sizeof(buffer[0]) * (size - pos)); - memset(&buffer[pos], 0, sizeof(buffer[0]) * d); - } -} -// FIR�t�B���^�K�p -void finite_impulse_response::apply(int_least32_t *out, const int_least32_t *in, size_t length, std::size_t stride) -{ - std::size_t buflenmask = buffer.size() - 1; - while (length > 0) - { - buffer[pos] = *in; - pos = (pos + 1) & buflenmask; - size_t offset = pos + buffer.size() - hlen; - int_least32_t result = 0; - for (size_t i = 0; i < hlen; ++i) - { - result += h[i] * buffer[(offset + i) & buflenmask] >> 12; - } - *out = result; - in = reinterpret_cast(reinterpret_cast(in) + stride); - out = reinterpret_cast(reinterpret_cast(out) + stride); - --length; - } -} - -// �C�R���C�UFIR�t�B���^�쐬 -void compute_equalizer_fir(double *h, std::size_t length, double rate, const std::map &gains) -{ - for (std::size_t i = 0; i < length; ++i) - { - h[i] = 0; - } - if (gains.empty()) - { - h[0] = 1; - } - else - { - int h_bits = log2(length); - size_t length = pow2(h_bits); - size_t half_length = length / 2; - std::map gain_bounds; - std::map::const_iterator i = gains.begin(); - gain_bounds[0] = i->second; - for (;;) - { - double fL = i->first; - double gL = i->second; - ++i; - if (i == gains.end()) - { - break; - } - double fR = i->first; - double gR = i->second; - double log_fL = log(fL); - double log_fR = log(fR); - const int n = 16; - for (int i = 0; i < n; ++i) - { - double ft = (i + 0.5) / n; - double f = exp(log_fL * (1 - ft) + log_fR * ft); - double gt = static_cast(i) / n; - double g = gL * (1 - gt) + gR * gt; - gain_bounds[f] = g; - } - } - double T = 1 / rate; - for (size_t k = 0; k < half_length; ++k) - { - double kT = k * T; - double hk = 0; - i = gain_bounds.begin(); - while (i != gain_bounds.end()) - { - double gain = i->second; - double f0 = i->first; - ++i; - double f1 = i == gain_bounds.end() ? rate / 2 : i->first; - double w0 = f0 * 2 * M_PI; - double w1 = f1 * 2 * M_PI; - if (k == 0) - { - hk += gain * (w1 - w0 + (-w0) - (-w1)); - } - else - { - double w0kT = w0 * kT; - double w1kT = w1 * kT; - /* - hk += + gain * exp(complex(0, w1kT)) / complex(0, kT) - - gain * exp(complex(0, w0kT)) / complex(0, kT) - + gain * exp(complex(0, -w0kT)) / complex(0, kT) - - gain * exp(complex(0, -w1kT)) / complex(0, kT); - */ - hk += gain * (sin(w1kT) - sin(w0kT)) * 2 / kT; - } - } - hk *= T / (2 * M_PI); - h[half_length - 1 - k] = hk; - h[half_length - 1 + k] = hk; - } - } -} -} // namespace filter diff --git a/libraries/fmmidi/filter.hpp b/libraries/fmmidi/filter.hpp deleted file mode 100644 index cd4db9835..000000000 --- a/libraries/fmmidi/filter.hpp +++ /dev/null @@ -1,54 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include - -namespace filter -{ -/* -typedef long int_least32_t; -*/ - -// �v�Z -inline int pow2(int x) -{ - return 1 << x; -} - -int log2(int n); -int log2_ceil(int n); - -// �����t�[���G�ϊ� -void fft(std::complex dst[], const std::complex src[], int n); -void ifft(std::complex dst[], const std::complex src[], int n); - -// ���֐� -void hanning_window(double dst[], const double src[], std::size_t n); - -// FIR�t�B���^ -class finite_impulse_response -{ - public: - finite_impulse_response(); - void set_impulse_response(const double *h, std::size_t length); - void set_impulse_response(const std::vector &h) - { - set_impulse_response(&h[0], h.size()); - } - void apply(int_least32_t *out, const int_least32_t *in, std::size_t length, - std::size_t stride = sizeof(int_least32_t)); - - private: - std::vector h; - std::vector buffer; - std::size_t pos; - std::size_t hlen; -}; - -// �t�B���^�쐬 -void compute_equalizer_fir(double *h, std::size_t length, double rate, const std::map &gains); -} // namespace filter \ No newline at end of file diff --git a/libraries/fmmidi/midisynth.cpp b/libraries/fmmidi/midisynth.cpp deleted file mode 100644 index bf457d7bc..000000000 --- a/libraries/fmmidi/midisynth.cpp +++ /dev/null @@ -1,2685 +0,0 @@ -// �\�t�g�E�F�AMIDI�V���Z�T�C�U�B -// Copyright(c)2003-2005 yuno -#include "midisynth.hpp" - -#include -#include -#include -#include - -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - -namespace midisynth -{ -// �`�����l���R���X�g���N�^�B -channel::channel(note_factory *factory_, int bank) : factory(factory_), default_bank(bank) -{ - notes.reserve(16); - reset_all_parameters(); -} -// �`�����l���f�X�g���N�^�B -channel::~channel() -{ - all_sound_off_immediately(); -} -// �������̉�����������B -int channel::synthesize(int_least32_t *out, std::size_t samples, float rate, int_least32_t master_volume, - int master_balance) -{ - double volume = - mute ? 0.0 - : std::pow(static_cast(master_volume) * this->volume * expression / (16383.0 * 16383.0 * 16383.0), - 2) * - 16383.0; - int num_notes = 0; - std::vector::iterator i = notes.begin(); - while (i != notes.end()) - { - class note *note = i->note; - int_least32_t panpot = note->get_panpot(); - if (this->panpot <= 8192) - { - panpot = panpot * this->panpot / 8192; - } - else - { - panpot = panpot * (16384 - this->panpot) / 8192 + (this->panpot - 8192) * 2; - } - if (master_balance <= 8192) - { - panpot = panpot * master_balance / 8192; - } - else - { - panpot = panpot * (16384 - master_balance) / 8192 + (master_balance - 8192) * 2; - } - int_least32_t left = - static_cast(volume * std::cos(std::max(0, panpot - 1) * (M_PI / 2 / 16382))); - int_least32_t right = - static_cast(volume * std::sin(std::max(0, panpot - 1) * (M_PI / 2 / 16382))); - bool ret = note->synthesize(out, samples, rate, left, right); - if (ret) - { - ++i; - } - else - { - i = notes.erase(i); - delete note; - } - ++num_notes; - } - return num_notes; -} -// ���ׂẴp�����[�^��������Ԃɖ߂��B -void channel::reset_all_parameters() -{ - program = default_bank * 128; - bank = default_bank; - panpot = 8192; - volume = 12800; - fine_tuning = 8192; - coarse_tuning = 8192; - tremolo_frequency = 3; - vibrato_frequency = 3; - master_frequency_multiplier = 1; - mono = false; - mute = false; - system_mode = system_mode_default; - reset_all_controller(); -} -// �p�����[�^��������Ԃɖ߂��B -void channel::reset_all_controller() -{ - expression = 16383; - channel_pressure(0); - pitch_bend = 8192; - pitch_bend_sensitivity = 256; - update_frequency_multiplier(); - modulation_depth = 0; - modulation_depth_range = 64; - update_modulation(); - set_damper(0); - set_sostenute(0); - set_freeze(0); - RPN = 0x3FFF; - NRPN = 0x3FFF; -} -// ���ׂẲ����m�[�g�I�t����B -void channel::all_note_off() -{ - for (std::vector::iterator i = notes.begin(); i != notes.end(); ++i) - { - if (i->status == NOTE::NOTEON) - { - i->status = NOTE::NOTEOFF; - i->note->note_off(64); - } - } -} -// ���ׂẲ����T�E���h�I�t����B -void channel::all_sound_off() -{ - for (std::vector::iterator i = notes.begin(); i != notes.end(); ++i) - { - if (i->status != NOTE::SOUNDOFF) - { - i->status = NOTE::SOUNDOFF; - i->note->sound_off(); - } - } -} -// ���������B -void channel::all_sound_off_immediately() -{ - for (std::vector::iterator i = notes.begin(); i != notes.end(); ++i) - { - delete i->note; - } - notes.clear(); -} -// �m�[�g�I���B�����o���B -void channel::note_on(int note, int velocity) -{ - assert(note >= 0 && note < NUM_NOTES); - assert(velocity >= 0 && velocity <= 127); - - note_off(note, 64); - if (velocity) - { - if (mono) - { - all_sound_off(); - } - class note *p = factory->note_on(program, note, velocity, frequency_multiplier); - if (p) - { - int assign = p->get_assign(); - if (assign) - { - for (std::vector::iterator i = notes.begin(); i != notes.end(); ++i) - { - if (i->note->get_assign() == assign) - { - i->note->sound_off(); - } - } - } - if (freeze) - { - p->set_freeze(freeze); - } - if (damper) - { - p->set_damper(damper); - } - if (modulation_depth) - { - float depth = static_cast(modulation_depth) * modulation_depth_range / (16383.0 * 128.0); - p->set_vibrato(depth, vibrato_frequency); - } - if (pressure) - { - p->set_tremolo(pressure, tremolo_frequency); - } - notes.push_back(NOTE(p, note)); - } - } -} -// �m�[�g�I�t�B���������[�X�^�C���ɓ���B -void channel::note_off(int note, int velocity) -{ - assert(note >= 0 && note < NUM_NOTES); - assert(velocity >= 0 && velocity <= 127); - for (std::vector::iterator i = notes.begin(); i != notes.end(); ++i) - { - if (i->key == note && i->status == NOTE::NOTEON) - { - i->status = NOTE::NOTEOFF; - i->note->note_off(velocity); - } - } -} -// �|���t�H�j�b�N�L�[�v���b�V���B -void channel::polyphonic_key_pressure(int note, int value) -{ - assert(note >= 0 && note < NUM_NOTES); - assert(value >= 0 && value <= 127); - for (std::vector::iterator i = notes.begin(); i != notes.end(); ++i) - { - if (i->key == note && i->status == NOTE::NOTEON) - { - i->note->set_tremolo(value, tremolo_frequency); - } - } -} -// �`�����l���v���b�V���B -void channel::channel_pressure(int value) -{ - assert(value >= 0 && value <= 127); - if (pressure != value) - { - pressure = value; - for (std::vector::iterator i = notes.begin(); i != notes.end(); ++i) - { - if (i->status == NOTE::NOTEON) - { - i->note->set_tremolo(value, tremolo_frequency); - } - } - } -} -// �R���g���[���`�F���W�B -void channel::control_change(int control, int value) -{ - assert(value >= 0 && value <= 0x7F); - switch (control) - { - case 0x00: - bank_select((bank & 0x7F) | (value << 7)); - break; - case 0x01: - set_modulation_depth((modulation_depth & 0x7F) | (value << 7)); - break; - case 0x06: - set_registered_parameter((get_registered_parameter() & 0x7F) | (value << 7)); - break; - case 0x07: - volume = (volume & 0x7F) | (value << 7); - break; - case 0x0A: - panpot = (panpot & 0x7F) | (value << 7); - break; - case 0x0B: - expression = (expression & 0x7F) | (value << 7); - break; - case 0x20: - bank_select((bank & 0x7F) | (value << 7)); - break; - case 0x21: - set_modulation_depth((modulation_depth & ~0x7F) | value); - break; - case 0x26: - set_registered_parameter((get_registered_parameter() & ~0x7F) | value); - break; - case 0x27: - volume = (volume & ~0x7F) | value; - break; - case 0x2A: - panpot = (panpot & ~0x7F) | value; - break; - case 0x2B: - expression = (expression & ~0x7F) | value; - break; - case 0x40: - set_damper(value); - break; - case 0x42: - set_sostenute(value); - break; - case 0x45: - set_freeze(value); - break; - case 0x60: - set_registered_parameter(std::min(0x3FFF, get_registered_parameter() + 1)); - break; - case 0x61: - set_registered_parameter(std::max(0, get_registered_parameter() - 1)); - break; - case 0x62: - set_NRPN((NRPN & ~0x7F) | value); - break; - case 0x63: - set_NRPN((NRPN & 0x7F) | (value << 7)); - break; - case 0x64: - set_RPN((RPN & ~0x7F) | value); - break; - case 0x65: - set_RPN((RPN & 0x7F) | (value << 7)); - break; - case 0x78: - all_sound_off(); - break; - case 0x79: - reset_all_controller(); - break; - case 0x7B: - case 0x7C: - case 0x7D: - all_note_off(); - break; - case 0x7E: - mono_mode_on(); - break; - case 0x7F: - poly_mode_on(); - break; - } -} -// �o���N�Z���N�g -void channel::bank_select(int value) -{ - switch (system_mode) - { - case system_mode_gm: - break; - case system_mode_gs: - if (((bank & 0x3F80) == 0x3C00) == ((value & 0x3F80) == 0x3C00)) - { - set_bank(value); - } - break; - case system_mode_xg: - if (default_bank == 0x3C00) - { - set_bank(0x3C00 | (value & 0x7F)); - } - else if ((value & 0x3F80) == 0x3F80) - { - set_bank(0x3C00 | (value & 0x7F)); - } - else - { - set_bank(value); - } - break; - default: - if (default_bank == 0x3C00) - { - set_bank(0x3C00 | (value & 0x7F)); - } - else - { - set_bank(value); - } - break; - } -} -// �_���p�[���ʁB -void channel::set_damper(int value) -{ - if (damper != value) - { - damper = value; - for (std::vector::iterator i = notes.begin(); i != notes.end(); ++i) - { - i->note->set_damper(value); - } - } -} -// �\�X�e�k�[�g���ʁB -void channel::set_sostenute(int value) -{ - sostenute = value; - for (std::vector::iterator i = notes.begin(); i != notes.end(); ++i) - { - i->note->set_sostenute(value); - } -} -// �t���[�Y���ʁB -void channel::set_freeze(int value) -{ - if (freeze != value) - { - freeze = value; - for (std::vector::iterator i = notes.begin(); i != notes.end(); ++i) - { - i->note->set_freeze(value); - } - } -} -// RPN�擾�B -int channel::get_registered_parameter() -{ - switch (RPN) - { - case 0x0000: - return pitch_bend_sensitivity; - case 0x0001: - return fine_tuning; - case 0x0002: - return coarse_tuning; - case 0x0005: - return modulation_depth_range; - default: - return 0; - } -} -// RPN�ݒ�B -void channel::set_registered_parameter(int value) -{ - switch (RPN) - { - case 0x0000: - set_pitch_bend_sensitivity(value); - break; - case 0x0001: - set_fine_tuning(value); - break; - case 0x0002: - set_coarse_tuning(value); - break; - case 0x0005: - set_modulation_depth_range(value); - break; - default: - break; - } -} -// ���g���{�����Čv�Z���X�V����B -void channel::update_frequency_multiplier() -{ - float value = - master_frequency_multiplier * - std::pow(2, (coarse_tuning - 8192) / (128.0 * 100.0 * 12.0) + (fine_tuning - 8192) / (8192.0 * 100.0 * 12.0) + - static_cast(pitch_bend - 8192) * pitch_bend_sensitivity / (8192.0 * 128.0 * 12.0)); - if (frequency_multiplier != value) - { - frequency_multiplier = value; - for (std::vector::iterator i = notes.begin(); i != notes.end(); ++i) - { - i->note->set_frequency_multiplier(value); - } - } -} -// ���W�����[�V�����f�v�X���ʂ̍X�V�B -void channel::update_modulation() -{ - float depth = static_cast(modulation_depth) * modulation_depth_range / (16383.0 * 128.0); - for (std::vector::iterator i = notes.begin(); i != notes.end(); ++i) - { - i->note->set_vibrato(depth, vibrato_frequency); - } -} - -// �V���Z�T�C�U�R���X�g���N�^�B -synthesizer::synthesizer(note_factory *factory) -{ - for (int i = 0; i < NUM_CHANNELS; ++i) - { - channels[i] = new channel(factory, i == 9 ? 0x3C00 : 0x3C80); - } - reset_all_parameters(); -} - -synthesizer::~synthesizer() -{ - for (int i = 0; i < NUM_CHANNELS; ++i) - delete channels[i]; -} -// �`�����l���擾�B -channel *synthesizer::get_channel(int ch) -{ - assert(ch >= 0 && ch < NUM_CHANNELS); - return channels[ch]; -} -// ������������B��������Ԃ��B -int synthesizer::synthesize(int_least16_t *output, std::size_t samples, float rate) -{ - if (samples == 0) - return 0; - std::size_t n = samples * 2; - std::vector buf(n); - int num_notes = synthesize_mixing(&buf[0], samples, rate); - if (num_notes) - { - for (std::size_t i = 0; i < n; ++i) - { - int_least32_t x = buf[i]; - if (x < -32767) - { - output[i] = -32767; - } - else if (x > 32767) - { - output[i] = 32767; - } - else - { - output[i] = static_cast(x); - } - } - } - else - { - std::memset(output, 0, sizeof(int_least16_t) * n); - } - return num_notes; -} -int synthesizer::synthesize_mixing(int_least32_t *output, std::size_t samples, float rate) -{ - if (active_sensing == 0) - { - all_sound_off(); - active_sensing = -1; - } - else if (active_sensing > 0) - { - active_sensing = std::max(0.0f, active_sensing - samples / rate); - } - int_least32_t volume = static_cast(main_volume) * master_volume / 16384; - int num_notes = 0; - for (int i = 0; i < NUM_CHANNELS; ++i) - { - num_notes += channels[i]->synthesize(output, samples, rate, volume, master_balance); - } - return num_notes; -} -// �V���Z�T�C�U�����S�Ƀ��Z�b�g����B -void synthesizer::reset() -{ - all_sound_off_immediately(); - reset_all_parameters(); -} -// ���ׂẴp�����[�^��������Ԃɖ߂��B -void synthesizer::reset_all_parameters() -{ - active_sensing = -1; - main_volume = 8192; - master_volume = 16383; - master_balance = 8192; - master_fine_tuning = 8192; - master_coarse_tuning = 8192; - master_frequency_multiplier = 1; - system_mode = system_mode_default; - for (int i = 0; i < NUM_CHANNELS; ++i) - { - channels[i]->reset_all_parameters(); - } -} -// �p�����[�^��������Ԃɖ߂��B -void synthesizer::reset_all_controller() -{ - for (int i = 0; i < NUM_CHANNELS; ++i) - { - channels[i]->reset_all_controller(); - } -} -// �I�[���m�[�g�I�t�B���ׂẲ����m�[�g�I�t����B -void synthesizer::all_note_off() -{ - for (int i = 0; i < NUM_CHANNELS; ++i) - { - channels[i]->all_note_off(); - } -} -// �I�[���T�E���h�I�t�B���ׂẲ����T�E���h�I�t����B -void synthesizer::all_sound_off() -{ - for (int i = 0; i < NUM_CHANNELS; ++i) - { - channels[i]->all_sound_off(); - } -} -// ���������B -void synthesizer::all_sound_off_immediately() -{ - for (int i = 0; i < NUM_CHANNELS; ++i) - { - channels[i]->all_sound_off_immediately(); - } -} -// �V�X�e���G�N�X�N���[�V�u���b�Z�[�W�̉��ߎ��s�B -void synthesizer::sysex_message(const void *pvdata, std::size_t size) -{ - const unsigned char *data = reinterpret_cast(pvdata); - if (size == 6 && std::memcmp(data, "\xF0\x7E\x7F\x09\x01\xF7", 6) == 0) - { - /* GM system on */ - set_system_mode(system_mode_gm); - } - else if (size == 6 && std::memcmp(data, "\xF0\x7E\x7F\x09\x02\xF7", 6) == 0) - { - /* GM system off */ - set_system_mode(system_mode_gm2); - } - else if (size == 6 && std::memcmp(data, "\xF0\x7E\x7F\x09\x03\xF7", 6) == 0) - { - /* GM2 system on */ - set_system_mode(system_mode_gm2); - } - else if (size == 11 && std::memcmp(data, "\xF0\x41", 2) == 0 && - std::memcmp(data + 3, "\x42\x12\x40\x00\x7F\x00\x41\xF7", 8) == 0) - { - /* GS reset */ - set_system_mode(system_mode_gs); - } - else if (size == 9 && std::memcmp(data, "\xF0\x43", 2) == 0 && (data[2] & 0xF0) == 0x10 && - std::memcmp(data + 3, "\x4C\x00\x00\x7E\x00\xF7", 6) == 0) - { - /* XG system on */ - set_system_mode(system_mode_xg); - } - else if (size == 8 && std::memcmp(data, "\xF0\x7F\x7F\x04\x01", 5) == 0 && data[7] == 0xF7) - { - /* master volume */ - set_master_volume((data[5] & 0x7F) | ((data[6] & 0x7F) << 7)); - } - else if (size == 8 && std::memcmp(data, "\xF0\x7F\x7F\x04\x02", 5) == 0 && data[7] == 0xF7) - { - /* master balance */ - set_master_balance((data[5] & 0x7F) | ((data[6] & 0x7F) << 7)); - } - else if (size == 8 && std::memcmp(data, "\xF0\x7F\x7F\x04\x03", 5) == 0 && data[7] == 0xF7) - { - /* master fine tuning */ - set_master_fine_tuning((data[5] & 0x7F) | ((data[6] & 0x7F) << 7)); - } - else if (size == 8 && std::memcmp(data, "\xF0\x7F\x7F\x04\x04", 5) == 0 && data[7] == 0xF7) - { - /* master coarse tuning */ - set_master_coarse_tuning((data[5] & 0x7F) | ((data[6] & 0x7F) << 7)); - } - else if (size == 11 && std::memcmp(data, "\xF0\x41", 2) == 0 && (data[2] & 0xF0) == 0x10 && - std::memcmp(data + 3, "\x42\x12\x40", 3) == 0 && (data[6] & 0xF0) == 0x10 && data[7] == 0x15 && - data[10] == 0xF7) - { - /* use for rhythm part */ - int channel = data[6] & 0x0F; - int map = data[8]; - if (map == 0) - { - channels[channel]->set_bank(0x3C80); - } - else - { - channels[channel]->set_bank(0x3C00); - } - channels[channel]->program_change(0); - } -} -// MIDI�C�x���g�̉��ߎ��s�B -void synthesizer::midi_event(int event, int param1, int param2) -{ - if (event == 0xFE) - active_sensing = 0.33f; - else if (event == 0xFF) - { - all_sound_off(); - reset_all_parameters(); - } - else - { - switch (event & 0xF0) - { - case 0x80: - note_off(event & 0x0F, param1 & 0x7F, param2 & 0x7F); - break; - case 0x90: - note_on(event & 0x0F, param1 & 0x7F, param2 & 0x7F); - break; - case 0xA0: - polyphonic_key_pressure(event & 0x0F, param1 & 0x7F, param2 & 0x7F); - break; - case 0xB0: - control_change(event & 0x0F, param1 & 0x7F, param2 & 0x7F); - break; - case 0xC0: - program_change(event & 0x0F, param1 & 0x7F); - break; - case 0xD0: - channel_pressure(event & 0x0F, param1 & 0x7F); - break; - case 0xE0: - pitch_bend_change(event & 0x0F, ((param2 & 0x7F) << 7) | (param1 & 0x7F)); - break; - } - } -} -// �V�X�e�����[�h��ύX����B -void synthesizer::set_system_mode(system_mode_t mode) -{ - all_sound_off(); - reset_all_parameters(); - system_mode = mode; - for (int i = 0; i < NUM_CHANNELS; ++i) - { - channels[i]->set_system_mode(mode); - } -} -// �}�X�^�[�`���[�j���O���Čv�Z���X�V����B -void synthesizer::update_master_frequency_multiplier() -{ - float value = std::pow(2, (master_coarse_tuning - 8192) / (128.0 * 100.0 * 12.0) + - (master_fine_tuning - 8192) / (8192.0 * 100.0 * 12.0)); - if (master_frequency_multiplier != value) - { - master_frequency_multiplier = value; - for (int i = 0; i < NUM_CHANNELS; ++i) - { - channels[i]->set_master_frequency_multiplier(value); - } - } -} - -// �����e�[�u���B�����g�W�F�l���[�^�p�B -namespace -{ -class sine_table -{ - public: - enum - { - DIVISION = 4096 - }; - sine_table(); - int_least16_t get(int n) const - { - return data[n]; - } - - private: - int_least16_t data[DIVISION]; -} sine_table; - -sine_table::sine_table() -{ - for (int i = 0; i < DIVISION; ++i) - { - data[i] = static_cast(32767 * std::sin(i * 2 * M_PI / DIVISION)); - } -} -} // namespace -// �����g������R���X�g���N�^�B -inline sine_wave_generator::sine_wave_generator() : position(0), step(0) -{ -} -inline sine_wave_generator::sine_wave_generator(float cycle) : position(0) -{ - set_cycle(cycle); -} -// �����g�̎�����ύX����B -void sine_wave_generator::set_cycle(float cycle) -{ - if (cycle) - { - step = static_cast(sine_table::DIVISION * 32768.0 / cycle); - } - else - { - step = 0; - } -} -// ���W�����[�V������������B -void sine_wave_generator::add_modulation(int_least32_t x) -{ - position += static_cast(static_cast(step) * x >> 16); -} -// ���̃T���v�������o���B -inline int sine_wave_generator::get_next() -{ - return sine_table.get((position += step) / 32768 % sine_table::DIVISION); -} -// ���̃T���v�������o��(���g���ϒ��t��)�B -inline int sine_wave_generator::get_next(int_least32_t modulation) -{ - uint_least32_t m = modulation * sine_table::DIVISION / 65536; - uint_least32_t p = ((position += step) / 32768 + m) % sine_table::DIVISION; - return sine_table.get(p); -} - -// �ΐ��ϊ��e�[�u���B�G���x���[�v�W�F�l���[�^�̃f�B�P�C�ȍ~�Ŏg���B -namespace -{ -#define LOG10_32767 4.5154366811416989472479934140484 -#define LOGTABLE_SIZE 4096 -#define LOGTABLE_FACTOR (LOGTABLE_SIZE / LOG10_32767) -class log_table -{ - public: - log_table(); - uint_least16_t get(int x) const - { - return data[x]; - } - - private: - uint_least16_t data[LOGTABLE_SIZE]; -} log_table; -log_table::log_table() -{ - for (int i = 0; i < LOGTABLE_SIZE; ++i) - { - data[i] = static_cast(std::pow(10, static_cast(i) / LOGTABLE_FACTOR)); - } -} -} // namespace - -// ���[�g�e�[�u���BAR�ADR�ASR�ARR�̌v�Z�����̍������B -namespace -{ -struct envelope_table -{ - envelope_table(); - uint_least32_t TL[128]; - uint_least32_t SL[16][128]; - double AR[64][128]; - double RR[64][128]; -} const envelope_table; - -envelope_table::envelope_table() -{ - for (int t = 0; t < 128; ++t) - { - double fTL = 32767 * std::pow(10, t * -0.75 / 10); - TL[t] = static_cast(fTL); - if (TL[t] == 0) - { - TL[t] = 1; - } - for (int s = 0; s < 16; ++s) - { - double x = fTL * std::pow(10, s * -3.0 / 10); - if (x <= 1) - { - SL[s][t] = 0; - } - else - { - SL[s][t] = static_cast(65536 * LOGTABLE_FACTOR * std::log10(x)); - } - } - } - for (int x = 0; x < 64; ++x) - { - double attack_time = 15.3262 * std::pow(10, x * -0.75 / 10); - double release_time = 211.84 * std::pow(10, x * -0.75 / 10); - for (int t = 0; t < 128; ++t) - { - AR[x][t] = TL[t] / attack_time; - RR[x][t] = 65536 * LOGTABLE_FACTOR * 48.0 / 10 * TL[t] / 32767 / release_time; - } - } -} -} // namespace - -// �G���x���[�v������R���X�g���N�^�B -envelope_generator::envelope_generator(int AR_, int DR_, int SR_, int RR_, int SL, int TL_) - : state(ATTACK), AR(AR_), DR(DR_), SR(SR_), RR(RR_), TL(TL_), current(0), rate(1), hold(0), freeze(0) -{ - if (AR >= 63) - AR = 63; - if (DR >= 63) - DR = 63; - if (SR >= 63) - SR = 63; - if (RR >= 63) - RR = 63; - assert(AR >= 0); - assert(DR >= 0); - assert(SR >= 0); - assert(RR >= 0); - assert(SL >= 0 && SL <= 15); - assert(TL >= 0 && TL <= 127); - - fTL = envelope_table.TL[TL]; - fSS = fSL = envelope_table.SL[SL][TL]; - fAR = 0; - fDR = 0; - fSR = 0; - fRR = 0; - fOR = 0; - fDRR = 0; - fDSS = 0; -} -// �Đ����[�g��ݒ肷��B -inline void envelope_generator::set_rate(float rate) -{ - this->rate = rate ? rate : 1; - update_parameters(); -} -// �z�[���h(�_���p�[&�\�X�e�k�[�g)��ݒ肷��B -void envelope_generator::set_hold(float hold) -{ - if (this->hold > hold || state <= SASTAIN || current >= fSL) - { - this->hold = hold; - update_parameters(); - } -} -// �t���[�Y��ݒ肷��B -void envelope_generator::set_freeze(float freeze) -{ - if (this->freeze > freeze || state <= SASTAIN || current >= fSL) - { - this->freeze = freeze; - update_parameters(); - } -} -// �e�p�����[�^�̍X�V�B -void envelope_generator::update_parameters() -{ - double fAR = envelope_table.AR[AR][TL] / rate; - double fDR = envelope_table.RR[DR][TL] / rate; - double fSR = envelope_table.RR[SR][TL] / rate; - double fRR = envelope_table.RR[RR][TL] / rate; - - if (fRR < 1) - { - fRR = 1; - } - if (hold > 0) - { - fRR = fSR * hold + fRR * (1 - hold); - } - if (freeze > 0) - { - fDR *= (1 - freeze); - fSR *= (1 - freeze); - fRR *= (1 - freeze); - } - if (fAR < 1) - { - fAR = 1; - } - this->fAR = static_cast(fAR); - this->fDR = static_cast(fDR); - this->fSR = static_cast(fSR); - this->fRR = static_cast(fRR); - this->fOR = static_cast(envelope_table.RR[63][0] / rate); - this->fSS = std::max(this->fDR, fSL); - this->fDRR = std::max(this->fDR, this->fRR); - this->fDSS = std::max(this->fDRR, this->fSS); -} -// �L�[�I�t�B�����[�X�ɓ���B -void envelope_generator::key_off() -{ - switch (state) - { - case ATTACK: - state = ATTACK_RELEASE; - break; - case DECAY: - state = DECAY_RELEASE; - break; - case SASTAIN: - state = RELEASE; - break; - default: - break; - } -} -// �T�E���h�I�t�B�}���������[�h�ɓ���B -void envelope_generator::sound_off() -{ - switch (state) - { - case ATTACK: - case ATTACK_RELEASE: - if (current) - { - current = static_cast(65536 * LOGTABLE_FACTOR * std::log10(static_cast(current))); - } - break; - default: - break; - } - state = SOUNDOFF; -} -// �����[�X����T�E���h�I�t�Ɉڂ郌�x���B�����[�X�̒����������‚܂ł����� -// ���Ă��CPU�p���[�̖��ʂȂ̂œK���ȂƂ���Ő؂邽�߁B�����͑ΐ��Ȃ̂� -// ���ʂ��^�Ƀ[���ɂȂ�ɂ͖����̎��Ԃ�v����B���ۂɂ͐����ɐ؂�̂Ă� -// �������Ă���̂łP�����ɂȂ����疳���ɂȂ�Ƃ͂����c�B -// �s���R�łȂ����x�ɂȂ�ׂ������l�̕����p�t�H�[�}���X�����シ��B -#define SOUNDOFF_LEVEL 1024 -// ���̃T���v���𓾂�B -int envelope_generator::get_next() -{ - uint_least32_t current = this->current; - switch (state) - { - case ATTACK: - if (current < fTL) - { - return this->current = current + fAR; - } - this->current = static_cast(65536 * LOGTABLE_FACTOR * std::log10(static_cast(fTL))); - state = DECAY; - return fTL; - case DECAY: - if (current > fSS) - { - this->current = current -= fDR; - return log_table.get(current / 65536); - } - this->current = current = fSL; - state = SASTAIN; - return log_table.get(current / 65536); - case SASTAIN: - if (current > fSR) - { - this->current = current -= fSR; - int n = log_table.get(current / 65536); - if (n > 1) - { - return n; - } - } - state = FINISHED; - return 0; - case ATTACK_RELEASE: - if (current < fTL) - { - return this->current = current + fAR; - } - this->current = static_cast(65536 * LOGTABLE_FACTOR * std::log10(static_cast(fTL))); - state = DECAY_RELEASE; - return fTL; - case DECAY_RELEASE: - if (current > fDSS) - { - this->current = current -= fDRR; - return log_table.get(current / 65536); - } - this->current = current = fSL; - state = RELEASE; - return log_table.get(current / 65536); - case RELEASE: - if (current > fRR) - { - this->current = current -= fRR; - int n = log_table.get(current / 65536); - if (n > SOUNDOFF_LEVEL) - { - return n; - } - state = SOUNDOFF; - return n; - } - state = FINISHED; - return 0; - case SOUNDOFF: - if (current > fOR) - { - this->current = current -= fOR; - int n = log_table.get(current / 65536); - if (n > 1) - { - return n; - } - } - state = FINISHED; - return 0; - default: - return 0; - } -} - -namespace -{ -// �L�[�X�P�[�����O�e�[�u�� -const int keyscale_table[4][128] = { - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 4, - 4, 4, 4, 4, 4, 5, 5, 6, 6, 7, 8, 8, 8, 8, 8, 8, 8, 9, 9, 10, 10, 11, 12, 12, 12, 12, - 12, 12, 12, 13, 13, 14, 14, 15, 16, 16, 16, 16, 16, 16, 16, 17, 17, 18, 18, 19, 20, 20, 20, 20, 20, 20, - 20, 21, 21, 22, 22, 23, 24, 24, 24, 24, 24, 24, 24, 25, 25, 26, 26, 27, 28, 28, 28, 28, 28, 28, 28, 29, - 29, 30, 30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31}}; -// �f�B�`���[���e�[�u�� -const float detune_table[4][128] = { - {0}, - {0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, - 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, - 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, - 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, - 0.106, 0.106, 0.106, 0.159, 0.159, 0.159, 0.159, 0.159, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, - 0.212, 0.212, 0.212, 0.264, 0.264, 0.264, 0.264, 0.264, 0.264, 0.264, 0.264, 0.317, 0.317, 0.317, 0.317, 0.370, - 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, - 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.423}, - {0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.053, 0.053, - 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, - 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, - 0.159, 0.159, 0.159, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.264, - 0.264, 0.264, 0.264, 0.264, 0.264, 0.264, 0.264, 0.317, 0.317, 0.317, 0.317, 0.370, 0.423, 0.423, 0.423, - 0.423, 0.423, 0.423, 0.423, 0.423, 0.423, 0.476, 0.476, 0.529, 0.582, 0.582, 0.582, 0.582, 0.582, 0.582, - 0.582, 0.635, 0.635, 0.688, 0.688, 0.741, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, - 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846}, - {0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.106, 0.106, 0.106, - 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.106, 0.159, - 0.159, 0.159, 0.159, 0.159, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.212, 0.264, - 0.264, 0.264, 0.264, 0.264, 0.264, 0.264, 0.264, 0.317, 0.317, 0.317, 0.317, 0.370, 0.423, 0.423, 0.423, 0.423, - 0.423, 0.423, 0.423, 0.423, 0.423, 0.476, 0.476, 0.529, 0.582, 0.582, 0.582, 0.582, 0.582, 0.582, 0.582, 0.635, - 0.635, 0.688, 0.688, 0.741, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.846, 0.899, 0.899, 1.005, 1.005, 1.058, - 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, - 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164, 1.164}}; -// LFO�e�[�u�� -const uint_least32_t ams_table[4] = {0, static_cast(128 - 128 * std::pow(10, -1.44 / 10)), - static_cast(128 - 128 * std::pow(10, -5.9 / 10)), - static_cast(128 - 128 * std::pow(10, -11.8 / 10))}; -} // namespace - -// FM�I�y���[�^�̃R���X�g���N�^�B -fm_operator::fm_operator(int AR, int DR, int SR, int RR, int SL, int TL, int KS, int ML_, int DT_, int AMS_, int key) - : eg(AR * 2 + keyscale_table[KS][key], DR * 2 + keyscale_table[KS][key], SR * 2 + keyscale_table[KS][key], - RR * 4 + keyscale_table[KS][key] + 2, SL, TL) -{ - assert(AR >= 0 && AR <= 31); - assert(DR >= 0 && DR <= 31); - assert(SR >= 0 && SR <= 31); - assert(RR >= 0 && RR <= 15); - assert(SL >= 0); - assert(TL >= 0); - assert(KS >= 0 && KS <= 3); - assert(ML_ >= 0 && ML_ <= 15); - assert(DT_ >= 0 && DT_ <= 7); - assert(AMS_ >= 0 && AMS_ <= 3); - assert(key >= 0 && key <= 127); - - if (DT_ >= 4) - { - DT = -detune_table[DT_ - 4][key]; - } - else - { - DT = detune_table[DT_][key]; - } - if (ML_ == 0) - { - ML = 0.5; - } - else - { - ML = ML_; - } - - ams_factor = ams_table[AMS_] / 2; - ams_bias = 32768 - ams_factor * 256; -} -// �Đ����g���ݒ�B -void fm_operator::set_freq_rate(float freq, float rate) -{ - freq += DT; - freq *= ML; - swg.set_cycle(rate / freq); - eg.set_rate(rate); -} -// ���̃T���v���𓾂�B -inline int fm_operator::get_next() -{ - return static_cast(swg.get_next()) * eg.get_next() >> 15; -} -inline int fm_operator::get_next(int modulate) -{ - return static_cast(swg.get_next(modulate)) * eg.get_next() >> 15; -} -inline int fm_operator::get_next(int ams, int modulate) -{ - return (static_cast(swg.get_next(modulate)) * eg.get_next() >> 15) * (ams * ams_factor + ams_bias) >> - 15; -} - -// �r�u���[�g�e�[�u���B -namespace -{ -class vibrato_table -{ - public: - enum - { - DIVISION = 16384 - }; - vibrato_table(); - int_least32_t get(int x) const - { - return data[x + DIVISION / 2]; - } - - private: - int_least32_t data[DIVISION]; -} vibrato_table; - -vibrato_table::vibrato_table() -{ - for (int i = 0; i < DIVISION; ++i) - { - double x = (static_cast(i) / DIVISION - 0.5) * 256.0 / 12.0; - data[i] = static_cast((std::pow(2, x) - 1) * 65536.0); - } -} -} // namespace - -// FM�����R���X�g���N�^�B -fm_sound_generator::fm_sound_generator(const FMPARAMETER ¶ms, int note, float frequency_multiplier) - : op1(params.op1.AR, params.op1.DR, params.op1.SR, params.op1.RR, params.op1.SL, params.op1.TL, params.op1.KS, - params.op1.ML, params.op1.DT, params.op1.AMS, note), - op2(params.op2.AR, params.op2.DR, params.op2.SR, params.op2.RR, params.op2.SL, params.op2.TL, params.op2.KS, - params.op2.ML, params.op2.DT, params.op2.AMS, note), - op3(params.op3.AR, params.op3.DR, params.op3.SR, params.op3.RR, params.op3.SL, params.op3.TL, params.op3.KS, - params.op3.ML, params.op3.DT, params.op3.AMS, note), - op4(params.op4.AR, params.op4.DR, params.op4.SR, params.op4.RR, params.op4.SL, params.op4.TL, params.op4.KS, - params.op4.ML, params.op4.DT, params.op4.AMS, note), - ALG(params.ALG), freq(440 * std::pow(2.0, (note - 69) / 12.0)), freq_mul(frequency_multiplier), tremolo_depth(0), - tremolo_freq(1), vibrato_depth(0), vibrato_freq(1), rate(0), feedback(0), damper(0), sostenute(0) -{ - assert(ALG >= 0 && ALG <= 7); - assert(params.LFO >= 0 && params.LFO <= 7); - assert(params.FB >= 0 && params.FB <= 7); - - static const int feedbacks[8] = {31, 6, 5, 4, 3, 2, 1, 0}; - FB = feedbacks[params.FB]; - - static const float ams_table[8] = {3.98, 5.56, 6.02, 6.37, 6.88, 9.63, 48.1, 72.2}; - ams_freq = ams_table[params.LFO]; - ams_enable = (params.op1.AMS + params.op2.AMS + params.op3.AMS + params.op4.AMS != 0); -} -// �Đ����[�g�ݒ�B -void fm_sound_generator::set_rate(float rate) -{ - if (this->rate != rate) - { - this->rate = rate; - ams_lfo.set_cycle(rate / ams_freq); - vibrato_lfo.set_cycle(rate / vibrato_freq); - tremolo_lfo.set_cycle(rate / tremolo_freq); - float f = freq * freq_mul; - op1.set_freq_rate(f, rate); - op2.set_freq_rate(f, rate); - op3.set_freq_rate(f, rate); - op4.set_freq_rate(f, rate); - } -} -// ���g���{���ݒ�B -void fm_sound_generator::set_frequency_multiplier(float value) -{ - freq_mul = value; - float f = freq * freq_mul; - op1.set_freq_rate(f, rate); - op2.set_freq_rate(f, rate); - op3.set_freq_rate(f, rate); - op4.set_freq_rate(f, rate); -} -// �_���p�[���ʐݒ�B -void fm_sound_generator::set_damper(int damper) -{ - this->damper = damper; - float value = 1.0 - (1.0 - damper / 127.0) * (1.0 - sostenute / 127.0); - op1.set_hold(value); - op2.set_hold(value); - op3.set_hold(value); - op4.set_hold(value); -} -// �\�X�e�k�[�g���ʐݒ�B -void fm_sound_generator::set_sostenute(int sostenute) -{ - this->sostenute = sostenute; - float value = 1.0 - (1.0 - damper / 127.0) * (1.0 - sostenute / 127.0); - op1.set_hold(value); - op2.set_hold(value); - op3.set_hold(value); - op4.set_hold(value); -} -// �t���[�Y���ʐݒ�B -void fm_sound_generator::set_freeze(int freeze) -{ - float value = freeze / 127.0; - op1.set_freeze(value); - op2.set_freeze(value); - op3.set_freeze(value); - op4.set_freeze(value); -} -// �g���������ʐݒ�B -void fm_sound_generator::set_tremolo(int depth, float frequency) -{ - tremolo_depth = depth; - tremolo_freq = frequency; - tremolo_lfo.set_cycle(rate / frequency); -} -// �r�u���[�g���ʐݒ�B -void fm_sound_generator::set_vibrato(float depth, float frequency) -{ - vibrato_depth = static_cast(depth * (vibrato_table::DIVISION / 256.0)); - vibrato_freq = frequency; - vibrato_lfo.set_cycle(rate / frequency); -} -// �L�[�I�t�B -void fm_sound_generator::key_off() -{ - op1.key_off(); - op2.key_off(); - op3.key_off(); - op4.key_off(); -} -// �T�E���h�I�t�B -void fm_sound_generator::sound_off() -{ - op1.sound_off(); - op2.sound_off(); - op3.sound_off(); - op4.sound_off(); -} -// ���̔������I���������ǂ�����Ԃ��B -bool fm_sound_generator::is_finished() const -{ - switch (ALG) - { - case 0: - case 1: - case 2: - case 3: - return op4.is_finished(); - case 4: - return op2.is_finished() && op4.is_finished(); - case 5: - case 6: - return op2.is_finished() && op3.is_finished() && op4.is_finished(); - case 7: - return op1.is_finished() && op2.is_finished() && op3.is_finished() && op4.is_finished(); - default: - assert(!"fm_sound_generator: invalid algorithm number"); - return true; - } -} -// ���̃T���v���𓾂�B -int fm_sound_generator::get_next() -{ - if (vibrato_depth) - { - int x = static_cast(vibrato_lfo.get_next()) * vibrato_depth >> 15; - int_least32_t modulation = vibrato_table.get(x); - op1.add_modulation(modulation); - op2.add_modulation(modulation); - op3.add_modulation(modulation); - op4.add_modulation(modulation); - } - int feedback = (this->feedback << 1) >> FB; - int ret; - if (ams_enable) - { - int ams = ams_lfo.get_next() >> 7; - switch (ALG) - { - case 0: - ret = op4(ams, op3(ams, op2(ams, this->feedback = op1(ams, feedback)))); - break; - case 1: - ret = op4(ams, op3(ams, op2(ams, 0) + (this->feedback = op1(ams, feedback)))); - break; - case 2: - ret = op4(ams, op3(ams, op2(ams, 0)) + (this->feedback = op1(ams, feedback))); - break; - case 3: - ret = op4(ams, op3(ams, 0) + op2(ams, this->feedback = op1(ams, feedback))); - break; - case 4: - ret = op4(ams, op3(ams, 0)) + op2(ams, this->feedback = op1(ams, feedback)); - break; - case 5: - this->feedback = feedback = op1(ams, feedback); - ret = op4(ams, feedback) + op3(ams, feedback) + op2(ams, feedback); - break; - case 6: - ret = op4(ams, 0) + op3(ams, 0) + op2(ams, this->feedback = op1(ams, feedback)); - break; - case 7: - ret = op4(ams, 0) + op3(ams, 0) + op2(ams, 0) + (this->feedback = op1(ams, feedback)); - break; - default: - assert(!"fm_sound_generator: invalid algorithm number"); - return 0; - } - } - else - { - switch (ALG) - { - case 0: - ret = op4(op3(op2(this->feedback = op1(feedback)))); - break; - case 1: - ret = op4(op3(op2() + (this->feedback = op1(feedback)))); - break; - case 2: - ret = op4(op3(op2()) + (this->feedback = op1(feedback))); - break; - case 3: - ret = op4(op3() + op2(this->feedback = op1(feedback))); - break; - case 4: - ret = op4(op3()) + op2(this->feedback = op1(feedback)); - break; - case 5: - this->feedback = feedback = op1(feedback); - ret = op4(feedback) + op3(feedback) + op2(feedback); - break; - case 6: - ret = op4() + op3() + op2(this->feedback = op1(feedback)); - break; - case 7: - ret = op4() + op3() + op2() + (this->feedback = op1(feedback)); - break; - default: - assert(!"fm_sound_generator: invalid algorithm number"); - return 0; - } - } - if (tremolo_depth) - { - int_least32_t x = 4096 - (((static_cast(tremolo_lfo.get_next()) + 32768) * tremolo_depth) >> 11); - ret = ret * x >> 12; - } - return ret; -} - -// FM�m�[�g�̃R���X�g���N�^�B -fm_note::fm_note(const FMPARAMETER ¶ms, int note, int velocity_, int panpot, int assign, float frequency_multiplier) - : midisynth::note(assign, panpot), fm(params, note, frequency_multiplier), velocity(velocity_) -{ - assert(velocity >= 1 && velocity <= 127); - ++velocity; -} -// �g�`�o�́B -bool fm_note::synthesize(int_least32_t *buf, std::size_t samples, float rate, int_least32_t left, int_least32_t right) -{ - left = (left * velocity) >> 7; - right = (right * velocity) >> 7; - fm.set_rate(rate); - for (std::size_t i = 0; i < samples; ++i) - { - int_least32_t sample = fm.get_next(); - buf[i * 2 + 0] += (sample * left) >> 14; - buf[i * 2 + 1] += (sample * right) >> 14; - } - return !fm.is_finished(); -} -// �m�[�g�I�t�B -void fm_note::note_off(int) -{ - fm.key_off(); -} -// �T�E���h�I�t�B -void fm_note::sound_off() -{ - fm.sound_off(); -} -// ���g���{���ݒ�B -void fm_note::set_frequency_multiplier(float value) -{ - fm.set_frequency_multiplier(value); -} -// �g���������ʐݒ�B -void fm_note::set_tremolo(int depth, float freq) -{ - fm.set_tremolo(depth, freq); -} -// �r�u���[�g���ʐݒ�B -void fm_note::set_vibrato(float depth, float freq) -{ - fm.set_vibrato(depth, freq); -} -// �_���p�[���ʐݒ�B -void fm_note::set_damper(int value) -{ - fm.set_damper(value); -} -// �\�X�e�k�[�g���ʐݒ�B -void fm_note::set_sostenute(int value) -{ - fm.set_sostenute(value); -} -// �t���[�Y���ʐݒ�B -void fm_note::set_freeze(int value) -{ - fm.set_freeze(value); -} - -// FM�m�[�g�t�@�N�g���������B -fm_note_factory::fm_note_factory() -{ - clear(); - midisynth::DRUMPARAMETER p; - p.ALG = 4, p.FB = 3, p.LFO = 0, p.op1.AR = 26, p.op1.DR = 10, p.op1.SR = 1, p.op1.RR = 0, p.op1.SL = 0, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 3, p.op1.AMS = 0, p.op2.AR = 26, p.op2.DR = 10, p.op2.SR = 2, - p.op2.RR = 7, p.op2.SL = 2, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 3, p.op2.AMS = 0, p.op3.AR = 26, - p.op3.DR = 10, p.op3.SR = 2, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 7, - p.op3.AMS = 0, p.op4.AR = 18, p.op4.DR = 6, p.op4.SR = 1, p.op4.RR = 6, p.op4.SL = 4, p.op4.TL = 2, p.op4.KS = 1, - p.op4.ML = 1, p.op4.DT = 7, p.op4.AMS = 0; - set_program(0, p); - p.ALG = 4, p.FB = 5, p.LFO = 0, p.op1.AR = 26, p.op1.DR = 10, p.op1.SR = 1, p.op1.RR = 0, p.op1.SL = 0, - p.op1.TL = 5, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 3, p.op1.AMS = 0, p.op2.AR = 26, p.op2.DR = 10, p.op2.SR = 3, - p.op2.RR = 7, p.op2.SL = 1, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 3, p.op2.AMS = 0, p.op3.AR = 26, - p.op3.DR = 10, p.op3.SR = 2, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 20, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 7, - p.op3.AMS = 0, p.op4.AR = 18, p.op4.DR = 6, p.op4.SR = 2, p.op4.RR = 6, p.op4.SL = 4, p.op4.TL = 2, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 7, p.op4.AMS = 0; - set_program(1, p); - p.ALG = 4, p.FB = 5, p.LFO = 0, p.op1.AR = 26, p.op1.DR = 10, p.op1.SR = 1, p.op1.RR = 0, p.op1.SL = 0, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 3, p.op1.AMS = 0, p.op2.AR = 26, p.op2.DR = 10, p.op2.SR = 2, - p.op2.RR = 7, p.op2.SL = 2, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 3, p.op2.AMS = 0, p.op3.AR = 26, - p.op3.DR = 10, p.op3.SR = 2, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 7, - p.op3.AMS = 0, p.op4.AR = 18, p.op4.DR = 6, p.op4.SR = 1, p.op4.RR = 6, p.op4.SL = 4, p.op4.TL = 2, p.op4.KS = 1, - p.op4.ML = 1, p.op4.DT = 7, p.op4.AMS = 0; - set_program(2, p); - p.ALG = 5, p.FB = 7, p.LFO = 0, p.op1.AR = 26, p.op1.DR = 10, p.op1.SR = 1, p.op1.RR = 0, p.op1.SL = 0, - p.op1.TL = 10, p.op1.KS = 3, p.op1.ML = 6, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 26, p.op2.DR = 10, p.op2.SR = 4, - p.op2.RR = 7, p.op2.SL = 1, p.op2.TL = 1, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 7, p.op2.AMS = 0, p.op3.AR = 15, - p.op3.DR = 20, p.op3.SR = 5, p.op3.RR = 8, p.op3.SL = 1, p.op3.TL = 2, p.op3.KS = 1, p.op3.ML = 2, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 8, p.op4.DR = 15, p.op4.SR = 3, p.op4.RR = 11, p.op4.SL = 2, p.op4.TL = 3, p.op4.KS = 3, - p.op4.ML = 3, p.op4.DT = 3, p.op4.AMS = 0; - set_program(3, p); - p.ALG = 4, p.FB = 3, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 10, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 16, p.op2.SR = 0, - p.op2.RR = 12, p.op2.SL = 1, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 10, p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 20, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 22, p.op4.DR = 9, p.op4.SR = 0, p.op4.RR = 10, p.op4.SL = 4, p.op4.TL = 2, p.op4.KS = 0, - p.op4.ML = 2, p.op4.DT = 0, p.op4.AMS = 0; - set_program(4, p); - p.ALG = 6, p.FB = 4, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 12, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, - p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 3, p.op1.AMS = 0, p.op2.AR = 29, p.op2.DR = 12, p.op2.SR = 1, - p.op2.RR = 7, p.op2.SL = 1, p.op2.TL = 1, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 26, - p.op3.DR = 12, p.op3.SR = 2, p.op3.RR = 6, p.op3.SL = 2, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 3, - p.op3.AMS = 0, p.op4.AR = 26, p.op4.DR = 12, p.op4.SR = 2, p.op4.RR = 6, p.op4.SL = 2, p.op4.TL = 5, p.op4.KS = 0, - p.op4.ML = 0, p.op4.DT = 7, p.op4.AMS = 0; - set_program(5, p); - p.ALG = 2, p.FB = 4, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 16, p.op1.SR = 1, p.op1.RR = 0, p.op1.SL = 0, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 7, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 16, p.op2.SR = 2, - p.op2.RR = 8, p.op2.SL = 1, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 3, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 14, p.op3.SR = 2, p.op3.RR = 8, p.op3.SL = 0, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 3, p.op3.DT = 3, - p.op3.AMS = 0, p.op4.AR = 28, p.op4.DR = 12, p.op4.SR = 1, p.op4.RR = 7, p.op4.SL = 2, p.op4.TL = 0, p.op4.KS = 1, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(6, p); - p.ALG = 2, p.FB = 4, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 16, p.op1.SR = 1, p.op1.RR = 0, p.op1.SL = 0, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 7, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 16, p.op2.SR = 2, - p.op2.RR = 7, p.op2.SL = 1, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 3, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 14, p.op3.SR = 2, p.op3.RR = 7, p.op3.SL = 0, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 4, p.op3.DT = 3, - p.op3.AMS = 0, p.op4.AR = 28, p.op4.DR = 14, p.op4.SR = 1, p.op4.RR = 7, p.op4.SL = 2, p.op4.TL = 0, p.op4.KS = 1, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(7, p); - p.ALG = 7, p.FB = 6, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 18, p.op1.SR = 9, p.op1.RR = 6, p.op1.SL = 1, - p.op1.TL = 1, p.op1.KS = 0, p.op1.ML = 4, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 14, p.op2.SR = 8, - p.op2.RR = 5, p.op2.SL = 3, p.op2.TL = 2, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 3, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 14, p.op3.SR = 9, p.op3.RR = 6, p.op3.SL = 1, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 28, p.op4.DR = 14, p.op4.SR = 10, p.op4.RR = 6, p.op4.SL = 2, p.op4.TL = 2, p.op4.KS = 0, - p.op4.ML = 0, p.op4.DT = 7, p.op4.AMS = 0; - set_program(8, p); - p.ALG = 7, p.FB = 6, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 26, p.op1.SR = 21, p.op1.RR = 10, p.op1.SL = 2, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 7, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 20, p.op2.DR = 0, p.op2.SR = 11, - p.op2.RR = 5, p.op2.SL = 0, p.op2.TL = 7, p.op2.KS = 0, p.op2.ML = 4, p.op2.DT = 1, p.op2.AMS = 0, p.op3.AR = 31, - p.op3.DR = 0, p.op3.SR = 11, p.op3.RR = 5, p.op3.SL = 0, p.op3.TL = 3, p.op3.KS = 0, p.op3.ML = 4, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 28, p.op4.SR = 15, p.op4.RR = 7, p.op4.SL = 3, p.op4.TL = 0, p.op4.KS = 0, - p.op4.ML = 2, p.op4.DT = 0, p.op4.AMS = 0; - set_program(9, p); - p.ALG = 6, p.FB = 2, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 2, - p.op1.KS = 0, p.op1.ML = 4, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 13, p.op2.RR = 6, - p.op2.SL = 0, p.op2.TL = 2, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 25, p.op3.RR = 12, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 13, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 3, p.op4.ML = 4, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(10, p); - p.ALG = 6, p.FB = 4, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 16, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 2, - p.op1.TL = 1, p.op1.KS = 0, p.op1.ML = 7, p.op1.DT = 7, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 10, - p.op2.RR = 7, p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 3, p.op2.AMS = 0, p.op3.AR = 24, - p.op3.DR = 10, p.op3.SR = 9, p.op3.RR = 7, p.op3.SL = 1, p.op3.TL = 2, p.op3.KS = 1, p.op3.ML = 1, p.op3.DT = 7, - p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 24, p.op4.SR = 13, p.op4.RR = 9, p.op4.SL = 4, p.op4.TL = 0, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(11, p); - p.ALG = 7, p.FB = 1, p.LFO = 0, p.op1.AR = 27, p.op1.DR = 20, p.op1.SR = 25, p.op1.RR = 12, p.op1.SL = 1, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 16, p.op2.DR = 0, p.op2.SR = 13, - p.op2.RR = 6, p.op2.SL = 0, p.op2.TL = 12, p.op2.KS = 0, p.op2.ML = 4, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 16, - p.op3.DR = 0, p.op3.SR = 15, p.op3.RR = 7, p.op3.SL = 0, p.op3.TL = 2, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 27, p.op4.DR = 0, p.op4.SR = 19, p.op4.RR = 9, p.op4.SL = 0, p.op4.TL = 4, p.op4.KS = 0, - p.op4.ML = 4, p.op4.DT = 0, p.op4.AMS = 0; - set_program(12, p); - p.ALG = 7, p.FB = 0, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 11, p.op1.RR = 5, p.op1.SL = 0, - p.op1.TL = 0, p.op1.KS = 2, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 15, - p.op2.RR = 7, p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 1, p.op2.ML = 3, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, - p.op3.DR = 0, p.op3.SR = 19, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 8, p.op3.KS = 0, p.op3.ML = 6, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, - p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(13, p); - p.ALG = 6, p.FB = 3, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 2, - p.op1.KS = 0, p.op1.ML = 5, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 16, p.op2.SR = 11, p.op2.RR = 5, - p.op2.SL = 2, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 3, p.op2.AMS = 0, p.op3.AR = 24, p.op3.DR = 0, - p.op3.SR = 13, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 6, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 7, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 19, p.op4.RR = 9, p.op4.SL = 0, p.op4.TL = 1, p.op4.KS = 0, p.op4.ML = 3, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(14, p); - p.ALG = 4, p.FB = 4, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 7, - p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 24, p.op2.SR = 9, p.op2.RR = 6, - p.op2.SL = 1, p.op2.TL = 1, p.op2.KS = 1, p.op2.ML = 1, p.op2.DT = 7, p.op2.AMS = 0, p.op3.AR = 28, p.op3.DR = 0, - p.op3.SR = 9, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 7, p.op3.DT = 3, p.op3.AMS = 0, - p.op4.AR = 28, p.op4.DR = 24, p.op4.SR = 9, p.op4.RR = 6, p.op4.SL = 2, p.op4.TL = 3, p.op4.KS = 1, p.op4.ML = 1, - p.op4.DT = 3, p.op4.AMS = 0; - set_program(15, p); - p.ALG = 7, p.FB = 5, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 12, p.op1.SL = 0, - p.op1.TL = 4, p.op1.KS = 0, p.op1.ML = 0, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 0, p.op2.SR = 0, - p.op2.RR = 12, p.op2.SL = 0, p.op2.TL = 6, p.op2.KS = 0, p.op2.ML = 4, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 0, p.op3.SR = 0, p.op3.RR = 12, p.op3.SL = 0, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 28, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 12, p.op4.SL = 0, p.op4.TL = 3, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(16, p); - p.ALG = 7, p.FB = 3, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 22, p.op1.SR = 0, p.op1.RR = 12, p.op1.SL = 10, - p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 4, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 20, p.op2.SR = 0, - p.op2.RR = 12, p.op2.SL = 2, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, - p.op3.DR = 18, p.op3.SR = 0, p.op3.RR = 12, p.op3.SL = 1, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 18, p.op4.SR = 0, p.op4.RR = 12, p.op4.SL = 1, p.op4.TL = 0, p.op4.KS = 0, - p.op4.ML = 0, p.op4.DT = 0, p.op4.AMS = 0; - set_program(17, p); - p.ALG = 7, p.FB = 5, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 12, p.op1.SL = 0, - p.op1.TL = 4, p.op1.KS = 0, p.op1.ML = 0, p.op1.DT = 0, p.op1.AMS = 3, p.op2.AR = 28, p.op2.DR = 0, p.op2.SR = 0, - p.op2.RR = 12, p.op2.SL = 0, p.op2.TL = 6, p.op2.KS = 0, p.op2.ML = 4, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 0, p.op3.SR = 0, p.op3.RR = 12, p.op3.SL = 0, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 28, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 12, p.op4.SL = 0, p.op4.TL = 3, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 2; - set_program(18, p); - p.ALG = 6, p.FB = 5, p.LFO = 0, p.op1.AR = 18, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 9, p.op1.SL = 0, p.op1.TL = 2, - p.op1.KS = 0, p.op1.ML = 6, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 20, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 9, - p.op2.SL = 0, p.op2.TL = 2, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 18, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 10, p.op3.SL = 0, p.op3.TL = 3, p.op3.KS = 0, p.op3.ML = 4, p.op3.DT = 3, p.op3.AMS = 0, - p.op4.AR = 28, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 12, p.op4.SL = 0, p.op4.TL = 5, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 7, p.op4.AMS = 0; - set_program(19, p); - p.ALG = 6, p.FB = 2, p.LFO = 0, p.op1.AR = 14, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 10, p.op1.SL = 0, - p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 6, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 20, p.op2.DR = 0, p.op2.SR = 0, - p.op2.RR = 12, p.op2.SL = 0, p.op2.TL = 2, p.op2.KS = 1, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 14, - p.op3.DR = 0, p.op3.SR = 0, p.op3.RR = 8, p.op3.SL = 0, p.op3.TL = 1, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 17, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 8, p.op4.SL = 0, p.op4.TL = 8, p.op4.KS = 1, - p.op4.ML = 0, p.op4.DT = 0, p.op4.AMS = 0; - set_program(20, p); - p.ALG = 3, p.FB = 3, p.LFO = 0, p.op1.AR = 16, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 0, p.op1.ML = 4, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 12, p.op2.DR = 3, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 4, p.op2.TL = 6, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 14, p.op3.DR = 7, - p.op3.SR = 0, p.op3.RR = 8, p.op3.SL = 4, p.op3.TL = 3, p.op3.KS = 0, p.op3.ML = 3, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 22, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 8, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(21, p); - p.ALG = 4, p.FB = 5, p.LFO = 0, p.op1.AR = 16, p.op1.DR = 0, p.op1.SR = 1, p.op1.RR = 11, p.op1.SL = 0, - p.op1.TL = 3, p.op1.KS = 0, p.op1.ML = 5, p.op1.DT = 3, p.op1.AMS = 0, p.op2.AR = 20, p.op2.DR = 0, p.op2.SR = 3, - p.op2.RR = 9, p.op2.SL = 0, p.op2.TL = 2, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 3, p.op2.AMS = 0, p.op3.AR = 18, - p.op3.DR = 0, p.op3.SR = 1, p.op3.RR = 11, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 4, p.op3.DT = 7, - p.op3.AMS = 0, p.op4.AR = 24, p.op4.DR = 0, p.op4.SR = 2, p.op4.RR = 10, p.op4.SL = 0, p.op4.TL = 2, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 7, p.op4.AMS = 0; - set_program(22, p); - p.ALG = 3, p.FB = 3, p.LFO = 0, p.op1.AR = 20, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 2, - p.op1.KS = 0, p.op1.ML = 4, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 16, p.op2.DR = 3, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 4, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 18, p.op3.DR = 7, - p.op3.SR = 0, p.op3.RR = 8, p.op3.SL = 4, p.op3.TL = 3, p.op3.KS = 0, p.op3.ML = 3, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 24, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(23, p); - p.ALG = 0, p.FB = 0, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 31, p.op1.SR = 31, p.op1.RR = 15, p.op1.SL = 0, - p.op1.TL = 127, p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, - p.op2.RR = 0, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, - p.op3.DR = 0, p.op3.SR = 4, p.op3.RR = 10, p.op3.SL = 0, p.op3.TL = 2, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 29, p.op4.DR = 14, p.op4.SR = 8, p.op4.RR = 9, p.op4.SL = 2, p.op4.TL = 0, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(24, p); - p.ALG = 1, p.FB = 4, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 1, - p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 2, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 4, p.op3.RR = 10, p.op3.SL = 0, p.op3.TL = 2, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 20, p.op4.SR = 8, p.op4.RR = 9, p.op4.SL = 2, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(25, p); - p.ALG = 1, p.FB = 4, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 31, p.op1.SR = 31, p.op1.RR = 15, p.op1.SL = 0, - p.op1.TL = 127, p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, - p.op2.RR = 0, p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 3, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, - p.op3.DR = 0, p.op3.SR = 4, p.op3.RR = 10, p.op3.SL = 0, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 28, p.op4.DR = 12, p.op4.SR = 8, p.op4.RR = 9, p.op4.SL = 1, p.op4.TL = 3, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(26, p); - p.ALG = 0, p.FB = 0, p.LFO = 0, p.op1.AR = 20, p.op1.DR = 0, p.op1.SR = 2, p.op1.RR = 9, p.op1.SL = 0, - p.op1.TL = 10, p.op1.KS = 0, p.op1.ML = 12, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 26, p.op2.DR = 0, p.op2.SR = 2, - p.op2.RR = 10, p.op2.SL = 0, p.op2.TL = 2, p.op2.KS = 0, p.op2.ML = 4, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 0, p.op3.SR = 4, p.op3.RR = 10, p.op3.SL = 0, p.op3.TL = 2, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 14, p.op4.SR = 8, p.op4.RR = 9, p.op4.SL = 2, p.op4.TL = 4, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(27, p); - p.ALG = 0, p.FB = 0, p.LFO = 0, p.op1.AR = 20, p.op1.DR = 0, p.op1.SR = 2, p.op1.RR = 15, p.op1.SL = 0, - p.op1.TL = 10, p.op1.KS = 0, p.op1.ML = 4, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 26, p.op2.DR = 0, p.op2.SR = 2, - p.op2.RR = 15, p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 0, p.op3.SR = 6, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 8, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 16, p.op4.SR = 10, p.op4.RR = 15, p.op4.SL = 2, p.op4.TL = 5, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(28, p); - p.ALG = 1, p.FB = 0, p.LFO = 0, p.op1.AR = 16, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 12, p.op1.SL = 0, - p.op1.TL = 4, p.op1.KS = 0, p.op1.ML = 4, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 18, p.op2.DR = 0, p.op2.SR = 0, - p.op2.RR = 12, p.op2.SL = 0, p.op2.TL = 2, p.op2.KS = 0, p.op2.ML = 3, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 20, - p.op3.DR = 0, p.op3.SR = 0, p.op3.RR = 12, p.op3.SL = 0, p.op3.TL = 1, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 29, p.op4.DR = 4, p.op4.SR = 2, p.op4.RR = 12, p.op4.SL = 1, p.op4.TL = 2, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(29, p); - p.ALG = 5, p.FB = 0, p.LFO = 0, p.op1.AR = 20, p.op1.DR = 0, p.op1.SR = 1, p.op1.RR = 9, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 0, p.op1.ML = 5, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 14, p.op2.SR = 1, p.op2.RR = 9, - p.op2.SL = 1, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 5, p.op2.DT = 7, p.op2.AMS = 0, p.op3.AR = 28, p.op3.DR = 14, - p.op3.SR = 1, p.op3.RR = 9, p.op3.SL = 1, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 28, p.op4.DR = 14, p.op4.SR = 1, p.op4.RR = 9, p.op4.SL = 1, p.op4.TL = 4, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 3, p.op4.AMS = 0; - set_program(30, p); - p.ALG = 4, p.FB = 3, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 9, p.op1.SL = 0, p.op1.TL = 2, - p.op1.KS = 0, p.op1.ML = 8, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 12, p.op2.SR = 2, p.op2.RR = 9, - p.op2.SL = 2, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 18, - p.op3.SR = 0, p.op3.RR = 9, p.op3.SL = 5, p.op3.TL = 1, p.op3.KS = 0, p.op3.ML = 10, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 18, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 3, p.op4.TL = 2, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(31, p); - p.ALG = 4, p.FB = 5, p.LFO = 0, p.op1.AR = 24, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 2, p.op1.ML = 0, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 24, p.op2.DR = 18, p.op2.SR = 4, p.op2.RR = 8, - p.op2.SL = 1, p.op2.TL = 0, p.op2.KS = 2, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 18, p.op3.DR = 18, - p.op3.SR = 3, p.op3.RR = 7, p.op3.SL = 1, p.op3.TL = 6, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 22, p.op4.DR = 18, p.op4.SR = 3, p.op4.RR = 7, p.op4.SL = 1, p.op4.TL = 2, p.op4.KS = 0, p.op4.ML = 2, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(32, p); - p.ALG = 2, p.FB = 3, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 10, p.op1.SR = 0, p.op1.RR = 15, p.op1.SL = 1, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 26, p.op2.DR = 22, p.op2.SR = 0, - p.op2.RR = 6, p.op2.SL = 1, p.op2.TL = 1, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 8, p.op3.SR = 8, p.op3.RR = 8, p.op3.SL = 1, p.op3.TL = 1, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 22, p.op4.DR = 14, p.op4.SR = 0, p.op4.RR = 7, p.op4.SL = 1, p.op4.TL = 0, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(33, p); - p.ALG = 2, p.FB = 3, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 10, p.op1.SR = 5, p.op1.RR = 15, p.op1.SL = 1, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 26, p.op2.DR = 22, p.op2.SR = 10, - p.op2.RR = 6, p.op2.SL = 1, p.op2.TL = 1, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 8, p.op3.SR = 8, p.op3.RR = 8, p.op3.SL = 1, p.op3.TL = 1, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 28, p.op4.DR = 14, p.op4.SR = 6, p.op4.RR = 7, p.op4.SL = 1, p.op4.TL = 0, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(34, p); - p.ALG = 4, p.FB = 0, p.LFO = 0, p.op1.AR = 24, p.op1.DR = 8, p.op1.SR = 3, p.op1.RR = 13, p.op1.SL = 0, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 9, p.op2.SR = 1, - p.op2.RR = 13, p.op2.SL = 2, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, - p.op3.DR = 31, p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, - p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(35, p); - p.ALG = 2, p.FB = 0, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 10, p.op1.SR = 5, p.op1.RR = 15, p.op1.SL = 1, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 26, p.op2.DR = 18, p.op2.SR = 8, - p.op2.RR = 6, p.op2.SL = 1, p.op2.TL = 1, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 6, p.op3.SR = 6, p.op3.RR = 10, p.op3.SL = 1, p.op3.TL = 1, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 28, p.op4.DR = 4, p.op4.SR = 6, p.op4.RR = 10, p.op4.SL = 1, p.op4.TL = 2, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(36, p); - p.ALG = 2, p.FB = 0, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 10, p.op1.SR = 2, p.op1.RR = 15, p.op1.SL = 1, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 26, p.op2.DR = 18, p.op2.SR = 0, - p.op2.RR = 6, p.op2.SL = 1, p.op2.TL = 1, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 6, p.op3.SR = 2, p.op3.RR = 10, p.op3.SL = 1, p.op3.TL = 1, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 28, p.op4.DR = 4, p.op4.SR = 4, p.op4.RR = 10, p.op4.SL = 1, p.op4.TL = 2, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(37, p); - p.ALG = 2, p.FB = 4, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 16, p.op1.SR = 12, p.op1.RR = 15, p.op1.SL = 1, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 7, p.op1.AMS = 0, p.op2.AR = 26, p.op2.DR = 12, p.op2.SR = 6, - p.op2.RR = 6, p.op2.SL = 2, p.op2.TL = 1, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 13, p.op3.SR = 11, p.op3.RR = 8, p.op3.SL = 1, p.op3.TL = 1, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 3, - p.op3.AMS = 0, p.op4.AR = 28, p.op4.DR = 10, p.op4.SR = 6, p.op4.RR = 12, p.op4.SL = 3, p.op4.TL = 0, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(38, p); - p.ALG = 3, p.FB = 1, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 7, p.op1.SR = 3, p.op1.RR = 15, p.op1.SL = 1, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 10, p.op2.SR = 2, - p.op2.RR = 6, p.op2.SL = 1, p.op2.TL = 1, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 10, p.op3.SR = 1, p.op3.RR = 8, p.op3.SL = 1, p.op3.TL = 2, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 12, p.op4.SL = 1, p.op4.TL = 0, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(39, p); - p.ALG = 5, p.FB = 7, p.LFO = 0, p.op1.AR = 20, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 6, p.op1.SL = 0, - p.op1.TL = 11, p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 16, p.op2.DR = 0, p.op2.SR = 0, - p.op2.RR = 6, p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 16, - p.op3.DR = 0, p.op3.SR = 0, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 6, p.op3.KS = 0, p.op3.ML = 4, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 16, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 10, p.op4.KS = 0, - p.op4.ML = 8, p.op4.DT = 0, p.op4.AMS = 0; - set_program(40, p); - p.ALG = 3, p.FB = 7, p.LFO = 0, p.op1.AR = 20, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 8, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 16, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 6, - p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 16, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 8, p.op3.KS = 0, p.op3.ML = 3, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 16, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 4, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(41, p); - p.ALG = 2, p.FB = 4, p.LFO = 0, p.op1.AR = 20, p.op1.DR = 1, p.op1.SR = 1, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 20, p.op2.DR = 2, p.op2.SR = 2, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 20, p.op3.DR = 2, - p.op3.SR = 2, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 20, p.op4.DR = 1, p.op4.SR = 1, p.op4.RR = 1, p.op4.SL = 7, p.op4.TL = 0, p.op4.KS = 3, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(42, p); - p.ALG = 2, p.FB = 5, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 1, p.op1.SR = 1, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 1, p.op2.SR = 1, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 1, - p.op3.SR = 1, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 3, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 20, p.op4.DR = 1, p.op4.SR = 1, p.op4.RR = 7, p.op4.SL = 0, p.op4.TL = 4, p.op4.KS = 1, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(43, p); - p.ALG = 3, p.FB = 4, p.LFO = 1, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 5, - p.op1.KS = 0, p.op1.ML = 5, p.op1.DT = 0, p.op1.AMS = 1, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 20, p.op3.DR = 10, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 6, p.op3.KS = 0, p.op3.ML = 5, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 20, p.op4.DR = 14, p.op4.SR = 0, p.op4.RR = 8, p.op4.SL = 1, p.op4.TL = 2, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(44, p); - p.ALG = 5, p.FB = 4, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 0, p.op1.SR = 13, p.op1.RR = 6, p.op1.SL = 0, - p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 0, p.op2.SR = 13, - p.op2.RR = 6, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, - p.op3.DR = 0, p.op3.SR = 19, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 4, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 17, p.op4.RR = 8, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, - p.op4.ML = 2, p.op4.DT = 0, p.op4.AMS = 0; - set_program(45, p); - p.ALG = 6, p.FB = 2, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 0, p.op1.SR = 9, p.op1.RR = 6, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 1, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 16, p.op2.SR = 9, p.op2.RR = 6, - p.op2.SL = 1, p.op2.TL = 0, p.op2.KS = 1, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, p.op3.DR = 16, - p.op3.SR = 9, p.op3.RR = 6, p.op3.SL = 1, p.op3.TL = 0, p.op3.KS = 1, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(46, p); - p.ALG = 0, p.FB = 6, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 13, p.op1.RR = 6, p.op1.SL = 0, - p.op1.TL = 0, p.op1.KS = 2, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 13, - p.op2.RR = 6, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 2, p.op2.ML = 4, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, - p.op3.DR = 0, p.op3.SR = 13, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 2, p.op3.ML = 2, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 13, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 2, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(47, p); - p.ALG = 3, p.FB = 4, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 5, - p.op1.KS = 0, p.op1.ML = 5, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 24, p.op3.DR = 10, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 6, p.op3.KS = 0, p.op3.ML = 5, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 20, p.op4.DR = 14, p.op4.SR = 0, p.op4.RR = 8, p.op4.SL = 1, p.op4.TL = 2, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(48, p); - p.ALG = 3, p.FB = 4, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 5, - p.op1.KS = 0, p.op1.ML = 5, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 24, p.op3.DR = 10, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 6, p.op3.KS = 0, p.op3.ML = 5, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 12, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 4, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(49, p); - p.ALG = 3, p.FB = 4, p.LFO = 0, p.op1.AR = 14, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 1, - p.op1.KS = 0, p.op1.ML = 5, p.op1.DT = 3, p.op1.AMS = 0, p.op2.AR = 14, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 14, p.op3.DR = 10, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 6, p.op3.KS = 0, p.op3.ML = 5, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 14, p.op4.DR = 14, p.op4.SR = 0, p.op4.RR = 8, p.op4.SL = 1, p.op4.TL = 2, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(50, p); - p.ALG = 3, p.FB = 4, p.LFO = 0, p.op1.AR = 14, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 1, - p.op1.KS = 0, p.op1.ML = 5, p.op1.DT = 3, p.op1.AMS = 0, p.op2.AR = 14, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 14, p.op3.DR = 10, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 6, p.op3.KS = 0, p.op3.ML = 5, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 12, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 2, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(51, p); - p.ALG = 7, p.FB = 5, p.LFO = 0, p.op1.AR = 16, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 8, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 7, p.op1.AMS = 0, p.op2.AR = 16, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 8, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 7, p.op2.AMS = 0, p.op3.AR = 18, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 2, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 3, p.op3.AMS = 0, - p.op4.AR = 18, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 0, p.op4.TL = 3, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 3, p.op4.AMS = 0; - set_program(52, p); - p.ALG = 7, p.FB = 4, p.LFO = 0, p.op1.AR = 24, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 8, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 3, p.op1.AMS = 0, p.op2.AR = 24, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 8, - p.op2.SL = 0, p.op2.TL = 2, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 7, p.op2.AMS = 0, p.op3.AR = 24, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 8, p.op3.SL = 0, p.op3.TL = 15, p.op3.KS = 0, p.op3.ML = 4, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 24, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 8, p.op4.SL = 0, p.op4.TL = 18, p.op4.KS = 0, p.op4.ML = 0, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(53, p); - p.ALG = 7, p.FB = 3, p.LFO = 0, p.op1.AR = 17, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 7, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 3, p.op1.AMS = 0, p.op2.AR = 17, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 7, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 7, p.op2.AMS = 0, p.op3.AR = 17, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 7, p.op3.SL = 0, p.op3.TL = 8, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 17, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 7, p.op4.SL = 0, p.op4.TL = 14, p.op4.KS = 0, p.op4.ML = 4, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(54, p); - p.ALG = 5, p.FB = 5, p.LFO = 0, p.op1.AR = 16, p.op1.DR = 0, p.op1.SR = 13, p.op1.RR = 6, p.op1.SL = 0, - p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 18, p.op2.DR = 0, p.op2.SR = 13, - p.op2.RR = 6, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 0, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 20, - p.op3.DR = 0, p.op3.SR = 13, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 16, p.op4.DR = 0, p.op4.SR = 13, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, - p.op4.ML = 2, p.op4.DT = 0, p.op4.AMS = 0; - set_program(55, p); - p.ALG = 4, p.FB = 4, p.LFO = 0, p.op1.AR = 22, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 1, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 22, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 8, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 22, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 8, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 22, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 8, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(56, p); - p.ALG = 4, p.FB = 4, p.LFO = 0, p.op1.AR = 17, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 8, p.op1.SL = 0, p.op1.TL = 1, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 17, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 8, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 20, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 8, p.op3.SL = 1, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 20, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 8, p.op4.SL = 1, p.op4.TL = 12, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(57, p); - p.ALG = 4, p.FB = 3, p.LFO = 0, p.op1.AR = 17, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 8, p.op1.SL = 0, p.op1.TL = 1, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 17, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 8, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 20, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 8, p.op3.SL = 1, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 20, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 8, p.op4.SL = 1, p.op4.TL = 12, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(58, p); - p.ALG = 3, p.FB = 3, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 6, p.op3.KS = 0, p.op3.ML = 4, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 18, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(59, p); - p.ALG = 4, p.FB = 2, p.LFO = 0, p.op1.AR = 17, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 8, p.op1.SL = 0, p.op1.TL = 1, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 17, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 8, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 20, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 8, p.op3.SL = 1, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 20, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 8, p.op4.SL = 1, p.op4.TL = 12, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(60, p); - p.ALG = 4, p.FB = 5, p.LFO = 0, p.op1.AR = 17, p.op1.DR = 9, p.op1.SR = 0, p.op1.RR = 9, p.op1.SL = 1, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 17, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 9, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 17, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 17, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 0, p.op4.TL = 4, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(61, p); - p.ALG = 4, p.FB = 5, p.LFO = 0, p.op1.AR = 17, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 9, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 17, p.op2.DR = 9, p.op2.SR = 0, p.op2.RR = 9, - p.op2.SL = 2, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 17, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 0, p.op4.TL = 4, p.op4.KS = 0, p.op4.ML = 3, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(62, p); - p.ALG = 4, p.FB = 5, p.LFO = 0, p.op1.AR = 14, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 9, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 14, p.op2.DR = 7, p.op2.SR = 0, p.op2.RR = 9, - p.op2.SL = 2, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 14, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 0, p.op4.TL = 4, p.op4.KS = 0, p.op4.ML = 3, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(63, p); - p.ALG = 3, p.FB = 5, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 8, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 20, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 10, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(64, p); - p.ALG = 1, p.FB = 4, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 2, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 6, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 20, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 10, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(65, p); - p.ALG = 1, p.FB = 5, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 2, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 2, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 20, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 10, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(66, p); - p.ALG = 3, p.FB = 4, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 6, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 24, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 10, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(67, p); - p.ALG = 5, p.FB = 5, p.LFO = 0, p.op1.AR = 18, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 3, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 18, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 9, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 18, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(68, p); - p.ALG = 4, p.FB = 4, p.LFO = 0, p.op1.AR = 20, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 9, p.op1.SL = 0, p.op1.TL = 1, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 20, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 9, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 24, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 2, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 24, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 0, p.op4.TL = 4, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(69, p); - p.ALG = 3, p.FB = 2, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 7, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 2, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 6, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 22, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 10, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(70, p); - p.ALG = 4, p.FB = 5, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 5, - p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 16, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 8, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 1, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 16, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 8, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 1, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(71, p); - p.ALG = 4, p.FB = 4, p.LFO = 0, p.op1.AR = 16, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 9, p.op1.SL = 0, - p.op1.TL = 12, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 20, p.op2.DR = 0, p.op2.SR = 0, - p.op2.RR = 9, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 1, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, - p.op3.DR = 0, p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 12, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 16, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 0, p.op4.TL = 8, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(72, p); - p.ALG = 6, p.FB = 1, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 1, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 18, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 9, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 18, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 1, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 18, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 0, p.op4.TL = 3, p.op4.KS = 0, p.op4.ML = 3, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(73, p); - p.ALG = 6, p.FB = 3, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 8, - p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 20, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 9, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 20, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 28, p.op4.DR = 18, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 1, p.op4.TL = 2, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(74, p); - p.ALG = 4, p.FB = 4, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 10, p.op1.SL = 0, - p.op1.TL = 6, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 17, p.op2.DR = 0, p.op2.SR = 0, - p.op2.RR = 10, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, - p.op3.DR = 0, p.op3.SR = 0, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 6, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 17, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 10, p.op4.SL = 0, p.op4.TL = 8, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(75, p); - p.ALG = 6, p.FB = 4, p.LFO = 0, p.op1.AR = 13, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 9, p.op1.SL = 0, p.op1.TL = 8, - p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 3, p.op1.AMS = 0, p.op2.AR = 16, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 9, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 16, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 8, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 16, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 0, p.op4.TL = 16, p.op4.KS = 0, p.op4.ML = 4, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(76, p); - p.ALG = 4, p.FB = 4, p.LFO = 2, p.op1.AR = 12, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 20, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 9, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 12, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 10, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 2, - p.op4.AR = 20, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(77, p); - p.ALG = 6, p.FB = 2, p.LFO = 0, p.op1.AR = 14, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 8, - p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 18, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 9, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 18, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 3, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 18, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 0, p.op4.TL = 2, p.op4.KS = 0, p.op4.ML = 4, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(78, p); - p.ALG = 7, p.FB = 4, p.LFO = 0, p.op1.AR = 17, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 9, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 17, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 9, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 17, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(79, p); - p.ALG = 6, p.FB = 5, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 15, p.op1.SL = 0, - p.op1.TL = 4, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, - p.op2.RR = 15, p.op2.SL = 0, p.op2.TL = 1, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, - p.op3.DR = 31, p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, - p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(80, p); - p.ALG = 6, p.FB = 5, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 15, p.op1.SL = 0, - p.op1.TL = 4, p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, - p.op2.RR = 15, p.op2.SL = 0, p.op2.TL = 1, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, - p.op3.DR = 31, p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, - p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(81, p); - p.ALG = 4, p.FB = 2, p.LFO = 0, p.op1.AR = 2, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 10, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 20, p.op2.DR = 16, p.op2.SR = 0, p.op2.RR = 10, - p.op2.SL = 1, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 20, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 10, p.op3.SL = 0, p.op3.TL = 1, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 20, p.op4.DR = 16, p.op4.SR = 0, p.op4.RR = 10, p.op4.SL = 1, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(82, p); - p.ALG = 3, p.FB = 7, p.LFO = 0, p.op1.AR = 16, p.op1.DR = 0, p.op1.SR = 14, p.op1.RR = 11, p.op1.SL = 0, - p.op1.TL = 5, p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, - p.op2.RR = 0, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, - p.op3.DR = 31, p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 28, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 11, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(83, p); - p.ALG = 5, p.FB = 4, p.LFO = 0, p.op1.AR = 16, p.op1.DR = 16, p.op1.SR = 1, p.op1.RR = 9, p.op1.SL = 1, - p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 4, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 14, p.op2.SR = 1, - p.op2.RR = 9, p.op2.SL = 1, p.op2.TL = 2, p.op2.KS = 0, p.op2.ML = 4, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 14, p.op3.SR = 1, p.op3.RR = 9, p.op3.SL = 1, p.op3.TL = 2, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 28, p.op4.DR = 14, p.op4.SR = 1, p.op4.RR = 9, p.op4.SL = 1, p.op4.TL = 2, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(84, p); - p.ALG = 7, p.FB = 5, p.LFO = 0, p.op1.AR = 16, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 8, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 7, p.op1.AMS = 0, p.op2.AR = 16, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 8, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 7, p.op2.AMS = 0, p.op3.AR = 18, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 2, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 3, p.op3.AMS = 0, - p.op4.AR = 18, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 0, p.op4.TL = 3, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 3, p.op4.AMS = 0; - set_program(85, p); - p.ALG = 4, p.FB = 5, p.LFO = 0, p.op1.AR = 26, p.op1.DR = 4, p.op1.SR = 0, p.op1.RR = 9, p.op1.SL = 1, p.op1.TL = 1, - p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 26, p.op2.DR = 4, p.op2.SR = 0, p.op2.RR = 9, - p.op2.SL = 1, p.op2.TL = 1, p.op2.KS = 0, p.op2.ML = 3, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 26, p.op3.DR = 4, - p.op3.SR = 0, p.op3.RR = 9, p.op3.SL = 1, p.op3.TL = 1, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 26, p.op4.DR = 4, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 1, p.op4.TL = 1, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(86, p); - p.ALG = 5, p.FB = 5, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 16, p.op1.SR = 0, p.op1.RR = 10, p.op1.SL = 1, - p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 16, p.op2.SR = 0, - p.op2.RR = 10, p.op2.SL = 1, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 16, p.op3.SR = 0, p.op3.RR = 10, p.op3.SL = 1, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 28, p.op4.DR = 16, p.op4.SR = 0, p.op4.RR = 10, p.op4.SL = 1, p.op4.TL = 0, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(87, p); - p.ALG = 7, p.FB = 4, p.LFO = 1, p.op1.AR = 18, p.op1.DR = 0, p.op1.SR = 4, p.op1.RR = 5, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 2, p.op2.AR = 18, p.op2.DR = 0, p.op2.SR = 4, p.op2.RR = 5, - p.op2.SL = 0, p.op2.TL = 1, p.op2.KS = 1, p.op2.ML = 2, p.op2.DT = 1, p.op2.AMS = 0, p.op3.AR = 18, p.op3.DR = 0, - p.op3.SR = 4, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 2, p.op3.KS = 2, p.op3.ML = 4, p.op3.DT = 2, p.op3.AMS = 0, - p.op4.AR = 18, p.op4.DR = 0, p.op4.SR = 4, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 6, p.op4.KS = 3, p.op4.ML = 8, - p.op4.DT = 3, p.op4.AMS = 0; - set_program(88, p); - p.ALG = 6, p.FB = 3, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 1, p.op2.AR = 10, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 7, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 10, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 7, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(89, p); - p.ALG = 5, p.FB = 5, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 14, p.op1.SR = 0, p.op1.RR = 10, p.op1.SL = 1, - p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 24, p.op2.DR = 10, p.op2.SR = 0, - p.op2.RR = 6, p.op2.SL = 1, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 1, p.op2.AMS = 0, p.op3.AR = 26, - p.op3.DR = 14, p.op3.SR = 0, p.op3.RR = 9, p.op3.SL = 1, p.op3.TL = 2, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 28, p.op4.DR = 14, p.op4.SR = 0, p.op4.RR = 9, p.op4.SL = 1, p.op4.TL = 4, p.op4.KS = 0, - p.op4.ML = 4, p.op4.DT = 0, p.op4.AMS = 0; - set_program(90, p); - p.ALG = 7, p.FB = 2, p.LFO = 4, p.op1.AR = 16, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 6, p.op1.SL = 0, p.op1.TL = 2, - p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 7, p.op1.AMS = 0, p.op2.AR = 16, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 7, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 7, p.op2.AMS = 0, p.op3.AR = 18, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 2, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 3, p.op3.AMS = 0, - p.op4.AR = 18, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 7, p.op4.SL = 0, p.op4.TL = 3, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 3, p.op4.AMS = 3; - set_program(91, p); - p.ALG = 7, p.FB = 4, p.LFO = 0, p.op1.AR = 12, p.op1.DR = 8, p.op1.SR = 0, p.op1.RR = 6, p.op1.SL = 1, p.op1.TL = 1, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 15, p.op2.DR = 8, p.op2.SR = 0, p.op2.RR = 6, - p.op2.SL = 1, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 1, p.op2.AMS = 0, p.op3.AR = 16, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 8, p.op3.KS = 0, p.op3.ML = 3, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 16, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 24, p.op4.KS = 0, p.op4.ML = 6, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(92, p); - p.ALG = 6, p.FB = 6, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 6, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 11, p.op2.DR = 6, p.op2.SR = 4, p.op2.RR = 7, - p.op2.SL = 1, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 31, - p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(93, p); - p.ALG = 7, p.FB = 4, p.LFO = 2, p.op1.AR = 20, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 7, p.op1.SL = 0, p.op1.TL = 2, - p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 7, p.op1.AMS = 0, p.op2.AR = 20, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 7, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 7, p.op2.AMS = 0, p.op3.AR = 20, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 8, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 3, p.op3.AMS = 3, - p.op4.AR = 20, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 8, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 3, p.op4.AMS = 0; - set_program(94, p); - p.ALG = 6, p.FB = 4, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 12, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 6, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 1, p.op2.AMS = 0, p.op3.AR = 12, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 3, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 12, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 6, p.op4.KS = 0, p.op4.ML = 3, - p.op4.DT = 2, p.op4.AMS = 0; - set_program(95, p); - p.ALG = 4, p.FB = 4, p.LFO = 5, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 26, p.op2.DR = 16, p.op2.SR = 4, p.op2.RR = 6, - p.op2.SL = 1, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 7, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 26, p.op4.DR = 16, p.op4.SR = 5, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 16, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 3; - set_program(96, p); - p.ALG = 5, p.FB = 4, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 11, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 1, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 2, p.op1.AMS = 1, p.op2.AR = 16, p.op2.DR = 0, p.op2.SR = 0, - p.op2.RR = 6, p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 16, - p.op3.DR = 0, p.op3.SR = 0, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 3, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 16, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 8, p.op4.KS = 0, - p.op4.ML = 6, p.op4.DT = 0, p.op4.AMS = 0; - set_program(97, p); - p.ALG = 6, p.FB = 2, p.LFO = 0, p.op1.AR = 24, p.op1.DR = 0, p.op1.SR = 7, p.op1.RR = 5, p.op1.SL = 0, p.op1.TL = 8, - p.op1.KS = 0, p.op1.ML = 4, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 24, p.op2.DR = 0, p.op2.SR = 7, p.op2.RR = 5, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 31, - p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(98, p); - p.ALG = 3, p.FB = 2, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 8, - p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 16, p.op2.SR = 0, p.op2.RR = 6, - p.op2.SL = 1, p.op2.TL = 6, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, p.op3.DR = 16, - p.op3.SR = 0, p.op3.RR = 6, p.op3.SL = 1, p.op3.TL = 6, p.op3.KS = 0, p.op3.ML = 3, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 28, p.op4.DR = 16, p.op4.SR = 0, p.op4.RR = 6, p.op4.SL = 1, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(99, p); - p.ALG = 5, p.FB = 4, p.LFO = 5, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 8, p.op1.RR = 5, p.op1.SL = 0, p.op1.TL = 8, - p.op1.KS = 0, p.op1.ML = 4, p.op1.DT = 1, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 0, p.op2.SR = 8, p.op2.RR = 6, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, p.op3.DR = 0, - p.op3.SR = 8, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 2, - p.op4.AR = 28, p.op4.DR = 0, p.op4.SR = 8, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 4, p.op4.KS = 0, p.op4.ML = 3, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(100, p); - p.ALG = 7, p.FB = 2, p.LFO = 0, p.op1.AR = 10, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 6, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 10, p.op2.DR = 0, p.op2.SR = 1, p.op2.RR = 6, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 10, p.op3.DR = 0, - p.op3.SR = 2, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 8, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 10, p.op4.DR = 0, p.op4.SR = 2, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 12, p.op4.KS = 0, p.op4.ML = 3, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(101, p); - p.ALG = 4, p.FB = 3, p.LFO = 1, p.op1.AR = 22, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 7, - p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 1, p.op2.AR = 22, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 6, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 22, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 1, - p.op4.AR = 22, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(102, p); - p.ALG = 4, p.FB = 6, p.LFO = 2, p.op1.AR = 22, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 6, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 22, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 6, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 22, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 3, p.op3.DT = 0, p.op3.AMS = 1, - p.op4.AR = 22, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 8, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(103, p); - p.ALG = 5, p.FB = 4, p.LFO = 0, p.op1.AR = 22, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 22, p.op2.DR = 0, p.op2.SR = 6, p.op2.RR = 6, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 22, p.op3.DR = 0, - p.op3.SR = 6, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 3, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(104, p); - p.ALG = 7, p.FB = 5, p.LFO = 0, p.op1.AR = 24, p.op1.DR = 0, p.op1.SR = 7, p.op1.RR = 7, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 14, p.op2.SR = 14, p.op2.RR = 15, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 31, - p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(105, p); - p.ALG = 3, p.FB = 0, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 1, - p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 17, p.op3.RR = 8, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 24, p.op4.DR = 0, p.op4.SR = 10, p.op4.RR = 7, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(106, p); - p.ALG = 3, p.FB = 0, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 2, - p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 17, p.op3.RR = 8, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 24, p.op4.DR = 0, p.op4.SR = 11, p.op4.RR = 5, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(107, p); - p.ALG = 4, p.FB = 5, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 20, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 2, - p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 0, p.op2.SR = 13, - p.op2.RR = 7, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 0, p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 28, p.op4.DR = 0, p.op4.SR = 13, p.op4.RR = 7, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(108, p); - p.ALG = 3, p.FB = 2, p.LFO = 0, p.op1.AR = 18, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 7, p.op1.SL = 0, p.op1.TL = 2, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 20, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 8, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 31, - p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 20, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 8, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(109, p); - p.ALG = 3, p.FB = 0, p.LFO = 0, p.op1.AR = 18, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 22, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 0, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 18, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 8, p.op3.KS = 0, p.op3.ML = 2, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 22, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 8, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(110, p); - p.ALG = 4, p.FB = 6, p.LFO = 0, p.op1.AR = 15, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 17, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 11, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 15, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 0, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 3, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 17, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 11, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(111, p); - p.ALG = 4, p.FB = 4, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 10, p.op1.RR = 6, p.op1.SL = 0, - p.op1.TL = 2, p.op1.KS = 0, p.op1.ML = 5, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 24, p.op2.DR = 0, p.op2.SR = 10, - p.op2.RR = 6, p.op2.SL = 0, p.op2.TL = 2, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, - p.op3.DR = 0, p.op3.SR = 10, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 5, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 24, p.op4.DR = 0, p.op4.SR = 10, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 4, p.op4.KS = 0, - p.op4.ML = 4, p.op4.DT = 0, p.op4.AMS = 0; - set_program(112, p); - p.ALG = 5, p.FB = 5, p.LFO = 6, p.op1.AR = 24, p.op1.DR = 0, p.op1.SR = 15, p.op1.RR = 8, p.op1.SL = 0, - p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 4, p.op1.DT = 0, p.op1.AMS = 2, p.op2.AR = 24, p.op2.DR = 0, p.op2.SR = 15, - p.op2.RR = 8, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 22, - p.op3.DR = 0, p.op3.SR = 15, p.op3.RR = 8, p.op3.SL = 0, p.op3.TL = 2, p.op3.KS = 0, p.op3.ML = 3, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 20, p.op4.DR = 0, p.op4.SR = 15, p.op4.RR = 8, p.op4.SL = 0, p.op4.TL = 4, p.op4.KS = 0, - p.op4.ML = 5, p.op4.DT = 0, p.op4.AMS = 0; - set_program(113, p); - p.ALG = 4, p.FB = 3, p.LFO = 5, p.op1.AR = 28, p.op1.DR = 0, p.op1.SR = 11, p.op1.RR = 5, p.op1.SL = 0, - p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 0, p.op2.SR = 11, - p.op2.RR = 6, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, - p.op3.DR = 0, p.op3.SR = 11, p.op3.RR = 5, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 2, p.op4.AR = 28, p.op4.DR = 0, p.op4.SR = 11, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 5, p.op4.KS = 0, - p.op4.ML = 2, p.op4.DT = 0, p.op4.AMS = 0; - set_program(114, p); - p.ALG = 5, p.FB = 3, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 5, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 21, p.op2.RR = 10, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 21, p.op3.RR = 10, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 5, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 21, p.op4.RR = 10, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 7, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(115, p); - p.ALG = 6, p.FB = 7, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 0, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 19, p.op2.RR = 9, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 0, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, - p.op3.SR = 13, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 11, p.op4.RR = 5, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 0, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(116, p); - p.ALG = 5, p.FB = 5, p.LFO = 0, p.op1.AR = 28, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 4, - p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 28, p.op2.DR = 0, p.op2.SR = 17, p.op2.RR = 8, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 0, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 28, p.op3.DR = 0, - p.op3.SR = 19, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 0, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 28, p.op4.DR = 0, p.op4.SR = 21, p.op4.RR = 10, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 2, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(117, p); - p.ALG = 4, p.FB = 7, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 0, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 19, p.op2.RR = 9, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 0, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 14, p.op3.DR = 0, - p.op3.SR = 15, p.op3.RR = 7, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 0, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 11, p.op4.RR = 5, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 0, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(118, p); - p.ALG = 6, p.FB = 7, p.LFO = 6, p.op1.AR = 7, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 6, - p.op1.KS = 0, p.op1.ML = 4, p.op1.DT = 0, p.op1.AMS = 1, p.op2.AR = 8, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 15, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 31, - p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(119, p); - p.ALG = 7, p.FB = 0, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 31, p.op1.SR = 31, p.op1.RR = 15, p.op1.SL = 0, - p.op1.TL = 127, p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 31, - p.op2.SR = 31, p.op2.RR = 15, p.op2.SL = 0, p.op2.TL = 127, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, - p.op3.AR = 31, p.op3.DR = 31, p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, p.op3.KS = 0, - p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, - p.op4.TL = 127, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(120, p); - p.ALG = 6, p.FB = 7, p.LFO = 0, p.op1.AR = 17, p.op1.DR = 0, p.op1.SR = 17, p.op1.RR = 8, p.op1.SL = 0, - p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 15, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 17, p.op2.DR = 0, p.op2.SR = 17, - p.op2.RR = 8, p.op2.SL = 0, p.op2.TL = 16, p.op2.KS = 0, p.op2.ML = 1, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, - p.op3.DR = 31, p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, - p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, - p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(121, p); - p.ALG = 6, p.FB = 7, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 15, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 8, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 6, - p.op2.SL = 0, p.op2.TL = 2, p.op2.KS = 0, p.op2.ML = 15, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 31, - p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(122, p); - p.ALG = 5, p.FB = 2, p.LFO = 5, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 12, p.op1.DT = 0, p.op1.AMS = 1, p.op2.AR = 16, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 7, - p.op2.SL = 0, p.op2.TL = 2, p.op2.KS = 0, p.op2.ML = 3, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 16, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 7, p.op3.SL = 0, p.op3.TL = 4, p.op3.KS = 0, p.op3.ML = 9, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 16, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 7, p.op4.SL = 0, p.op4.TL = 6, p.op4.KS = 0, p.op4.ML = 12, - p.op4.DT = 0, p.op4.AMS = 0; - set_program(123, p); - p.ALG = 5, p.FB = 4, p.LFO = 5, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 13, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 8, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 2, p.op2.DT = 0, p.op2.AMS = 1, p.op3.AR = 31, p.op3.DR = 31, - p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(124, p); - p.ALG = 6, p.FB = 7, p.LFO = 5, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 15, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 8, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 0, p.op2.DT = 0, p.op2.AMS = 3, p.op3.AR = 31, p.op3.DR = 31, - p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(125, p); - p.ALG = 5, p.FB = 7, p.LFO = 6, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 15, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 8, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 6, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 4, p.op2.DT = 0, p.op2.AMS = 3, p.op3.AR = 8, p.op3.DR = 0, - p.op3.SR = 0, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 0, p.op3.KS = 0, p.op3.ML = 8, p.op3.DT = 0, p.op3.AMS = 3, - p.op4.AR = 8, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 6, p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 12, - p.op4.DT = 0, p.op4.AMS = 3; - set_program(126, p); - p.ALG = 5, p.FB = 7, p.LFO = 0, p.op1.AR = 31, p.op1.DR = 0, p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, - p.op1.KS = 0, p.op1.ML = 11, p.op1.DT = 0, p.op1.AMS = 0, p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 15, p.op2.RR = 7, - p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 11, p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 31, - p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, - p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, - p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_program(127, p); - p.ALG = 7, p.FB = 7, p.LFO = 0, p.key = 36, p.panpot = 8192, p.assign = 1, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 17, p.op1.RR = 8, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 1, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 0, p.op2.RR = 15, p.op2.SL = 0, p.op2.TL = 127, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, p.op3.SR = 0, p.op3.RR = 15, p.op3.SL = 0, p.op3.TL = 127, - p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 0, p.op4.RR = 15, - p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(-1, p); - p.ALG = 5, p.FB = 0, p.LFO = 0, p.key = 11, p.panpot = 8192, p.assign = 35, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 17, p.op1.RR = 8, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 17, p.op2.RR = 8, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, p.op3.SR = 17, p.op3.RR = 8, p.op3.SL = 0, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 17, p.op4.RR = 8, - p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(35, p); - p.ALG = 5, p.FB = 0, p.LFO = 0, p.key = 14, p.panpot = 8192, p.assign = 36, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 15, p.op1.RR = 7, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 15, p.op2.RR = 7, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, p.op3.SR = 15, p.op3.RR = 7, p.op3.SL = 0, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 15, p.op4.RR = 7, - p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(36, p); - p.ALG = 6, p.FB = 7, p.LFO = 0, p.key = 59, p.panpot = 8192, p.assign = 37, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 19, p.op1.RR = 9, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 19, p.op2.RR = 9, p.op2.SL = 0, p.op2.TL = 4, p.op2.KS = 0, p.op2.ML = 2, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 31, p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, - p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 31, - p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(37, p); - p.ALG = 5, p.FB = 7, p.LFO = 0, p.key = 48, p.panpot = 8192, p.assign = 38, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 15, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 24, p.op2.SR = 17, p.op2.RR = 8, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 24, p.op3.SR = 17, p.op3.RR = 8, p.op3.SL = 0, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 24, p.op4.SR = 17, p.op4.RR = 8, - p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(38, p); - p.ALG = 5, p.FB = 7, p.LFO = 0, p.key = 87, p.panpot = 6912, p.assign = 39, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 15, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 19, p.op2.RR = 9, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 1, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, p.op3.SR = 19, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 2, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 19, p.op4.RR = 9, - p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 3, p.op4.AMS = 0; - set_drum_program(39, p); - p.ALG = 5, p.FB = 7, p.LFO = 0, p.key = 55, p.panpot = 8192, p.assign = 40, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 15, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 19, p.op2.RR = 9, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, p.op3.SR = 19, p.op3.RR = 9, p.op3.SL = 0, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 19, p.op4.RR = 9, - p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(40, p); - p.ALG = 5, p.FB = 0, p.LFO = 0, p.key = 14, p.panpot = 4352, p.assign = 41, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 15, p.op1.RR = 7, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 15, p.op2.RR = 7, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, p.op3.SR = 15, p.op3.RR = 7, p.op3.SL = 0, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 15, p.op4.RR = 7, - p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(41, p); - p.ALG = 6, p.FB = 7, p.LFO = 0, p.key = 99, p.panpot = 10752, p.assign = 42, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 21, p.op2.RR = 10, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 31, p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, - p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 31, - p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(42, p); - p.ALG = 5, p.FB = 0, p.LFO = 0, p.key = 18, p.panpot = 5888, p.assign = 43, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 15, p.op1.RR = 7, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 15, p.op2.RR = 7, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, p.op3.SR = 15, p.op3.RR = 7, p.op3.SL = 0, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 15, p.op4.RR = 7, - p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(43, p); - p.ALG = 6, p.FB = 7, p.LFO = 0, p.key = 100, p.panpot = 10752, p.assign = 42, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 21, p.op2.RR = 10, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 31, p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, - p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 31, - p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(44, p); - p.ALG = 5, p.FB = 0, p.LFO = 0, p.key = 20, p.panpot = 7424, p.assign = 45, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 15, p.op1.RR = 7, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 15, p.op2.RR = 7, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, p.op3.SR = 15, p.op3.RR = 7, p.op3.SL = 0, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 15, p.op4.RR = 7, - p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(45, p); - p.ALG = 6, p.FB = 7, p.LFO = 0, p.key = 100, p.panpot = 10752, p.assign = 42, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 13, p.op2.RR = 6, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 31, p.op3.SR = 31, p.op3.RR = 15, p.op3.SL = 0, - p.op3.TL = 127, p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 31, - p.op4.SR = 31, p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(46, p); - p.ALG = 5, p.FB = 0, p.LFO = 0, p.key = 24, p.panpot = 8960, p.assign = 47, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 15, p.op1.RR = 7, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 15, p.op2.RR = 7, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, p.op3.SR = 15, p.op3.RR = 7, p.op3.SL = 0, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 15, p.op4.RR = 7, - p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(47, p); - p.ALG = 5, p.FB = 0, p.LFO = 0, p.key = 27, p.panpot = 10496, p.assign = 48, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 15, p.op1.RR = 7, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 15, p.op2.RR = 7, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, p.op3.SR = 15, p.op3.RR = 7, p.op3.SL = 0, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 15, p.op4.RR = 7, - p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(48, p); - p.ALG = 5, p.FB = 7, p.LFO = 3, p.key = 104, p.panpot = 10752, p.assign = 49, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 8, p.op1.DT = 0, p.op1.AMS = 1, - p.op2.AR = 31, p.op2.DR = 20, p.op2.SR = 11, p.op2.RR = 5, p.op2.SL = 1, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 2, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 20, p.op3.SR = 11, p.op3.RR = 5, p.op3.SL = 2, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 3, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 20, p.op4.SR = 11, p.op4.RR = 5, - p.op4.SL = 3, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 5, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(49, p); - p.ALG = 5, p.FB = 0, p.LFO = 0, p.key = 31, p.panpot = 12032, p.assign = 50, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 15, p.op1.RR = 7, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 3, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 15, p.op2.RR = 7, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, p.op3.SR = 15, p.op3.RR = 7, p.op3.SL = 0, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 0, p.op4.SR = 15, p.op4.RR = 7, - p.op4.SL = 0, p.op4.TL = 0, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(50, p); - p.ALG = 5, p.FB = 7, p.LFO = 0, p.key = 97, p.panpot = 5632, p.assign = 51, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 6, p.op1.KS = 0, p.op1.ML = 8, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 11, p.op2.RR = 5, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, p.op3.SR = 11, p.op3.RR = 5, p.op3.SL = 0, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, - p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(51, p); - p.ALG = 5, p.FB = 7, p.LFO = 3, p.key = 94, p.panpot = 5632, p.assign = 52, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 0, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 2, p.op1.DT = 0, p.op1.AMS = 1, - p.op2.AR = 31, p.op2.DR = 0, p.op2.SR = 11, p.op2.RR = 5, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 1, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 0, p.op3.SR = 11, p.op3.RR = 5, p.op3.SL = 0, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 1, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, p.op4.RR = 15, - p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, p.op4.ML = 1, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(52, p); - p.ALG = 5, p.FB = 4, p.LFO = 0, p.key = 72, p.panpot = 3072, p.assign = 80, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 10, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 15, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 24, p.op2.SR = 17, p.op2.RR = 8, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 15, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 24, p.op3.SR = 17, p.op3.RR = 8, p.op3.SL = 0, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 15, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, - p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, p.op4.ML = 15, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(80, p); - p.ALG = 5, p.FB = 4, p.LFO = 0, p.key = 72, p.panpot = 3072, p.assign = 80, p.op1.AR = 31, p.op1.DR = 0, - p.op1.SR = 5, p.op1.RR = 0, p.op1.SL = 0, p.op1.TL = 0, p.op1.KS = 0, p.op1.ML = 15, p.op1.DT = 0, p.op1.AMS = 0, - p.op2.AR = 31, p.op2.DR = 24, p.op2.SR = 13, p.op2.RR = 6, p.op2.SL = 0, p.op2.TL = 0, p.op2.KS = 0, p.op2.ML = 15, - p.op2.DT = 0, p.op2.AMS = 0, p.op3.AR = 31, p.op3.DR = 24, p.op3.SR = 13, p.op3.RR = 6, p.op3.SL = 0, p.op3.TL = 0, - p.op3.KS = 0, p.op3.ML = 15, p.op3.DT = 0, p.op3.AMS = 0, p.op4.AR = 31, p.op4.DR = 31, p.op4.SR = 31, - p.op4.RR = 15, p.op4.SL = 0, p.op4.TL = 127, p.op4.KS = 0, p.op4.ML = 15, p.op4.DT = 0, p.op4.AMS = 0; - set_drum_program(81, p); -} -// �N���A�B -void fm_note_factory::clear() -{ - // �f�t�H���g�̉��F(sin�g) - static const struct FMPARAMETER param = {7, - 0, - 0, // ALG FB LFO - // AR DR SR RR SL TL KS ML DT AMS - {31, 0, 0, 15, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 15, 0, 127, 0, 0, 0, 0}, - {0, 0, 0, 15, 0, 127, 0, 0, 0, 0}, - {0, 0, 0, 15, 0, 127, 0, 0, 0, 0}}; - drums.clear(); - programs.clear(); - programs[-1] = param; -} -// ���F�p�����[�^�̐��������� -namespace -{ -bool is_valid_fmparameter(const FMPARAMETER &p) -{ - return p.ALG >= 0 && p.ALG <= 7 && p.FB >= 0 && p.FB <= 7 && p.LFO >= 0 && p.LFO <= 7 && p.op1.AR >= 0 && - p.op1.AR <= 31 && p.op1.DR >= 0 && p.op1.DR <= 31 && p.op1.SR >= 0 && p.op1.SR <= 31 && p.op1.RR >= 0 && - p.op1.RR <= 15 && p.op1.SL >= 0 && p.op1.SL <= 15 && p.op1.TL >= 0 && p.op1.TL <= 127 && p.op1.KS >= 0 && - p.op1.KS <= 3 && p.op1.ML >= 0 && p.op1.ML <= 15 && p.op1.DT >= 0 && p.op1.DT <= 7 && p.op1.AMS >= 0 && - p.op1.AMS <= 3 && p.op2.AR >= 0 && p.op2.AR <= 31 && p.op2.DR >= 0 && p.op2.DR <= 31 && p.op2.SR >= 0 && - p.op2.SR <= 31 && p.op2.RR >= 0 && p.op2.RR <= 15 && p.op2.SL >= 0 && p.op2.SL <= 15 && p.op2.TL >= 0 && - p.op2.TL <= 127 && p.op2.KS >= 0 && p.op2.KS <= 3 && p.op2.ML >= 0 && p.op2.ML <= 15 && p.op2.DT >= 0 && - p.op2.DT <= 7 && p.op2.AMS >= 0 && p.op2.AMS <= 3 && p.op3.AR >= 0 && p.op3.AR <= 31 && p.op3.DR >= 0 && - p.op3.DR <= 31 && p.op3.SR >= 0 && p.op3.SR <= 31 && p.op3.RR >= 0 && p.op3.RR <= 15 && p.op3.SL >= 0 && - p.op3.SL <= 15 && p.op3.TL >= 0 && p.op3.TL <= 127 && p.op3.KS >= 0 && p.op3.KS <= 3 && p.op3.ML >= 0 && - p.op3.ML <= 15 && p.op3.DT >= 0 && p.op3.DT <= 7 && p.op3.AMS >= 0 && p.op3.AMS <= 3 && p.op4.AR >= 0 && - p.op4.AR <= 31 && p.op4.DR >= 0 && p.op4.DR <= 31 && p.op4.SR >= 0 && p.op4.SR <= 31 && p.op4.RR >= 0 && - p.op4.RR <= 15 && p.op4.SL >= 0 && p.op4.SL <= 15 && p.op4.TL >= 0 && p.op4.TL <= 127 && p.op4.KS >= 0 && - p.op4.KS <= 3 && p.op4.ML >= 0 && p.op4.ML <= 15 && p.op4.DT >= 0 && p.op4.DT <= 7 && p.op4.AMS >= 0 && - p.op4.AMS <= 3; -} -bool is_valid_drumparameter(const DRUMPARAMETER &p) -{ - return is_valid_fmparameter(p) && p.key >= 0 && p.key <= 127 && p.panpot >= 0 && p.panpot <= 16383; -} -} // namespace -// ���F�p�����[�^�擾�B -void fm_note_factory::get_program(int program, FMPARAMETER &p) -{ - if (programs.find(program) != programs.end()) - { - p = programs[program]; - } - else if (programs.find(program & 0x3FFF) != programs.end()) - { - p = programs[program & 0x3FFF]; - } - else if (programs.find(program & 0x7F) != programs.end()) - { - p = programs[program & 0x7F]; - } - else - { - p = programs[-1]; - } -} -// ���F�p�����[�^���Z�b�g�B -bool fm_note_factory::set_program(int number, const FMPARAMETER &p) -{ - if (is_valid_fmparameter(p)) - { - programs[number] = p; - return true; - } - else - { - return false; - } -} -// �h�������F�p�����[�^���Z�b�g�B -bool fm_note_factory::set_drum_program(int number, const DRUMPARAMETER &p) -{ - if (is_valid_drumparameter(p)) - { - drums[number] = p; - return true; - } - else - { - return false; - } -} -// �m�[�g�I���B -note *fm_note_factory::note_on(int_least32_t program, int note, int velocity, float frequency_multiplier) -{ - bool drum = (program >> 14) == 120; - if (drum) - { - int n = (program & 0x3FFF) * 128 + note; - struct DRUMPARAMETER *p; - if (drums.find(n) != drums.end()) - { - p = &drums[n]; - } - else if (drums.find(n & 0x3FFF) != drums.end()) - { - p = &drums[n & 0x3FFF]; - } - else if (drums.find(note) != drums.end()) - { - p = &drums[note]; - } - else if (drums.find(-1) != drums.end()) - { - p = &drums[-1]; - } - else - { - return NULL; - } - return new fm_note(*p, p->key, velocity, p->panpot, p->assign, 1); - } - else - { - struct FMPARAMETER *p; - if (programs.find(program) != programs.end()) - { - p = &programs[program]; - } - else if (programs.find(program & 0x7F) != programs.end()) - { - p = &programs[program & 0x7F]; - } - else - { - p = &programs[-1]; - } - return new fm_note(*p, note, velocity, 8192, 0, frequency_multiplier); - } -} -} // namespace midisynth diff --git a/libraries/fmmidi/midisynth.hpp b/libraries/fmmidi/midisynth.hpp deleted file mode 100644 index b97874ab4..000000000 --- a/libraries/fmmidi/midisynth.hpp +++ /dev/null @@ -1,681 +0,0 @@ -// �\�t�g�E�F�AMIDI�V���Z�T�C�U�B -// Copyright(c)2003-2005 yuno -#pragma once - -#include -#include -#include -#include - -namespace midisynth -{ -/* -typedef short int_least16_t; -typedef unsigned short uint_least16_t; -typedef long int_least32_t; -typedef unsigned long uint_least32_t; -typedef __int64 int_least64_t; -*/ -class channel; - -// �V�X�e�����[�h�񋓌^�B -enum system_mode_t -{ - system_mode_default, - system_mode_gm, - system_mode_gm2, - system_mode_gs, - system_mode_xg -}; - -// �R�s�[�s�‚̊�{�N���X�B -class uncopyable -{ - public: - uncopyable() - { - } - - private: - uncopyable(const uncopyable &) - { - } - void operator=(const uncopyable &) - { - } -}; - -// �m�[�g�B�������̉��B -class note : uncopyable -{ - public: - note(int assign_, int panpot_) : assign(assign_), panpot(panpot_) - { - } - virtual ~note() - { - } - int get_assign() const - { - return assign; - } - int get_panpot() const - { - return panpot; - } - virtual bool synthesize(int_least32_t *buf, std::size_t samples, float rate, int_least32_t left, - int_least32_t right) = 0; - virtual void note_off(int velocity) = 0; - virtual void sound_off() = 0; - virtual void set_frequency_multiplier(float value) = 0; - virtual void set_tremolo(int depth, float freq) = 0; - virtual void set_vibrato(float depth, float freq) = 0; - virtual void set_damper(int value) = 0; - virtual void set_sostenute(int value) = 0; - virtual void set_freeze(int value) = 0; - - private: - int assign; - int panpot; -}; - -// �m�[�g�t�@�N�g���B -// �m�[�g�I�����b�Z�[�W�ɑ΂��ēK�؂ȃm�[�g�����o���B -class note_factory : uncopyable -{ - public: - virtual note *note_on(int_least32_t program, int note, int velocity, float frequency_multiplier) = 0; - - protected: - ~note_factory() - { - } -}; - -// MIDI�`�����l���B -class channel : uncopyable -{ - enum - { - NUM_NOTES = 128 - }; - - public: - channel(note_factory *factory, int bank); - ~channel(); - - int synthesize(int_least32_t *out, std::size_t samples, float rate, int_least32_t master_volume, - int master_balance); - void reset_all_parameters(); - void reset_all_controller(); - void all_note_off(); - void all_sound_off(); - void all_sound_off_immediately(); - - void note_off(int note, int velocity); - void note_on(int note, int velocity); - void polyphonic_key_pressure(int note, int value); - void program_change(int value) - { - set_program(128 * bank + value); - } - void channel_pressure(int value); - void pitch_bend_change(int value) - { - pitch_bend = value; - update_frequency_multiplier(); - } - void control_change(int control, int value); - void bank_select(int value); - - void set_bank(int value) - { - bank = value; - } - void set_program(int value) - { - program = value; - } - void set_panpot(int value) - { - panpot = value; - } - void set_volume(int value) - { - volume = value; - } - void set_expression(int value) - { - expression = value; - } - void set_pitch_bend_sensitivity(int value) - { - pitch_bend_sensitivity = value; - update_frequency_multiplier(); - } - void set_modulation_depth(int value) - { - modulation_depth = value; - update_modulation(); - } - void set_modulation_depth_range(int value) - { - modulation_depth_range = value; - update_modulation(); - } - void set_damper(int value); - void set_sostenute(int value); - void set_freeze(int value); - void set_fine_tuning(int value) - { - fine_tuning = value; - update_frequency_multiplier(); - } - void set_coarse_tuning(int value) - { - coarse_tuning = value; - update_frequency_multiplier(); - } - void set_RPN(int value) - { - RPN = value; - NRPN = 0x3FFF; - } - void set_NRPN(int value) - { - NRPN = value; - RPN = 0x3FFF; - } - void set_tremolo_frequency(float value) - { - tremolo_frequency = value; - } - void set_vibrato_frequency(float value) - { - vibrato_frequency = value; - } - void set_master_frequency_multiplier(float value) - { - master_frequency_multiplier = value; - update_frequency_multiplier(); - } - void set_mute(bool mute_) - { - mute = mute_; - } - void set_system_mode(system_mode_t mode) - { - system_mode = mode; - } - void mono_mode_on() - { - all_note_off(); - mono = true; - } - void poly_mode_on() - { - all_note_off(); - mono = false; - } - - int get_program() const - { - return program; - } - int get_bank() const - { - return bank; - } - int get_panpot() const - { - return panpot; - } - int get_volume() const - { - return volume; - } - int get_expression() const - { - return expression; - } - int get_channel_pressure() const - { - return pressure; - } - int get_pitch_bend() const - { - return pitch_bend; - } - int get_pitch_bend_sensitivity() const - { - return pitch_bend_sensitivity; - } - int get_modulation_depth() const - { - return modulation_depth; - } - int get_modulation_depth_range() const - { - return modulation_depth_range; - } - int get_damper() const - { - return damper; - } - int get_sostenute() const - { - return sostenute; - } - int get_freeze() const - { - return freeze; - } - int get_fine_tuning() const - { - return fine_tuning; - } - int get_coarse_tuning() const - { - return coarse_tuning; - } - int get_RPN() const - { - return RPN; - } - int get_NRPN() const - { - return NRPN; - } - float get_tremolo_frequency() const - { - return tremolo_frequency; - } - float get_vibrato_frequency() const - { - return vibrato_frequency; - } - bool get_mute() const - { - return mute; - } - bool get_mono_mode() const - { - return mono; - } - - private: - struct NOTE - { - class note *note; - int key; - enum STATUS - { - NOTEON, - NOTEOFF, - SOUNDOFF - } status; - NOTE(class note *p, int key_) : note(p), key(key_), status(NOTEON) - { - } - }; - std::vector notes; - note_factory *factory; - int default_bank; - int program; - int bank; - int panpot; - int volume; - int expression; - int pressure; - int pitch_bend; - int pitch_bend_sensitivity; - int modulation_depth; - int modulation_depth_range; - int damper; - int sostenute; - int freeze; - int fine_tuning; - int coarse_tuning; - int RPN; - int NRPN; - bool mono; - bool mute; - float tremolo_frequency; - float vibrato_frequency; - float frequency_multiplier; - float master_frequency_multiplier; - system_mode_t system_mode; - - int get_registered_parameter(); - void set_registered_parameter(int value); - void update_frequency_multiplier(); - void update_modulation(); -}; - -// MIDI�V���Z�T�C�U�B -class synthesizer : uncopyable -{ - enum - { - NUM_CHANNELS = 16 - }; - - public: - synthesizer(note_factory *factory); - ~synthesizer(); - - channel *get_channel(int ch); - - int synthesize(int_least16_t *output, std::size_t samples, float rate); - int synthesize_mixing(int_least32_t *output, std::size_t samples, float rate); - void reset(); - void reset_all_parameters(); - void reset_all_controller(); - void all_note_off(); - void all_sound_off(); - void all_sound_off_immediately(); - - void note_on(int channel, int note, int velocity) - { - get_channel(channel)->note_on(note, velocity); - } - void note_off(int channel, int note, int velocity) - { - get_channel(channel)->note_off(note, velocity); - } - void polyphonic_key_pressure(int channel, int note, int value) - { - get_channel(channel)->polyphonic_key_pressure(note, value); - } - void control_change(int channel, int control, int value) - { - get_channel(channel)->control_change(control, value); - } - void program_change(int channel, int program) - { - get_channel(channel)->program_change(program); - } - void channel_pressure(int channel, int value) - { - get_channel(channel)->channel_pressure(value); - } - void pitch_bend_change(int channel, int value) - { - get_channel(channel)->pitch_bend_change(value); - } - void sysex_message(const void *data, std::size_t size); - void midi_event(int command, int param1, int param2); - void midi_event(uint_least32_t message) - { - midi_event(message & 0xFF, (message >> 8) & 0x7F, (message >> 16) & 0x7F); - } - - void set_main_volume(int value) - { - main_volume = value; - } - void set_master_volume(int value) - { - master_volume = value; - } - void set_master_balance(int value) - { - master_balance = value; - } - void set_master_fine_tuning(int value) - { - master_fine_tuning = value; - update_master_frequency_multiplier(); - } - void set_master_coarse_tuning(int value) - { - master_coarse_tuning = value; - update_master_frequency_multiplier(); - } - void set_system_mode(system_mode_t mode); - - int get_main_volume() const - { - return main_volume; - } - int get_master_volume() const - { - return master_volume; - } - int get_master_balance() const - { - return master_balance; - } - int get_master_fine_tuning() const - { - return master_fine_tuning; - } - int get_master_coarse_tuning() const - { - return master_coarse_tuning; - } - system_mode_t get_system_mode() const - { - return system_mode; - } - - private: - channel *channels[NUM_CHANNELS]; - float active_sensing; - int main_volume; - int master_volume; - int master_balance; - int master_fine_tuning; - int master_coarse_tuning; - float master_frequency_multiplier; - system_mode_t system_mode; - void update_master_frequency_multiplier(); -}; - -// �����g������B -// �U�� 32768 (-32767�`32767) �̐����g�𐶐�����B -class sine_wave_generator -{ - public: - sine_wave_generator(); - sine_wave_generator(float cycle); - void set_cycle(float cycle); - void add_modulation(int_least32_t x); - int get_next(); - int get_next(int_least32_t modulation); - - private: - uint_least32_t position; - uint_least32_t step; -}; - -// �G���x���[�v������B -// TL=0 �̂Ƃ� 0�`32767 �̒l�𐶐�����B -class envelope_generator -{ - public: - envelope_generator(int AR, int DR, int SR, int RR, int SL, int TL); - void set_rate(float rate); - void set_hold(float value); - void set_freeze(float value); - void key_off(); - void sound_off(); - bool is_finished() const - { - return state == FINISHED; - } - int get_next(); - - private: - enum - { - ATTACK, - ATTACK_RELEASE, - DECAY, - DECAY_RELEASE, - SASTAIN, - RELEASE, - SOUNDOFF, - FINISHED - } state; - int AR, DR, SR, RR, TL; - uint_least32_t fAR, fDR, fSR, fRR, fSL, fTL, fOR, fSS, fDRR, fDSS; - uint_least32_t current; - float rate; - float hold; - float freeze; - void update_parameters(); -}; - -// FM�I�y���[�^ (���W�����[�^����уL�����A)�B -class fm_operator -{ - public: - fm_operator(int AR, int DR, int SR, int RR, int SL, int TL, int KS, int ML, int DT, int AMS, int key); - void set_freq_rate(float freq, float rate); - void set_hold(float value) - { - eg.set_hold(value); - } - void set_freeze(float value) - { - eg.set_freeze(value); - } - void add_modulation(int_least32_t x) - { - swg.add_modulation(x); - } - void key_off() - { - eg.key_off(); - } - void sound_off() - { - eg.sound_off(); - } - bool is_finished() const - { - return eg.is_finished(); - } - int get_next(); - int get_next(int modulate); - int get_next(int lfo, int modulate); - inline int operator()() - { - return get_next(); - } - inline int operator()(int m) - { - return get_next(m); - } - inline int operator()(int lfo, int m) - { - return get_next(lfo, m); - } - - private: - sine_wave_generator swg; - envelope_generator eg; - float ML; - float DT; - int_least32_t ams_factor; - int_least32_t ams_bias; -}; - -// FM�����p�����[�^�B -struct FMPARAMETER -{ - int ALG, FB, LFO; - struct - { - int AR, DR, SR, RR, SL, TL, KS, ML, DT, AMS; - } op1, op2, op3, op4; -}; -// �h�����p�����[�^�B -struct DRUMPARAMETER : FMPARAMETER -{ - int key, panpot, assign; -}; - -// FM�T�E���h�W�F�l���[�^�B -class fm_sound_generator -{ - public: - fm_sound_generator(const FMPARAMETER ¶ms, int note, float frequency_multiplier); - void set_rate(float rate); - void set_frequency_multiplier(float value); - void set_damper(int damper); - void set_sostenute(int sostenute); - void set_freeze(int freeze); - void set_tremolo(int depth, float frequency); - void set_vibrato(float depth, float frequency); - void key_off(); - void sound_off(); - bool is_finished() const; - int get_next(); - - private: - fm_operator op1; - fm_operator op2; - fm_operator op3; - fm_operator op4; - sine_wave_generator ams_lfo; - sine_wave_generator vibrato_lfo; - sine_wave_generator tremolo_lfo; - int ALG; - int FB; - float freq; - float freq_mul; - float ams_freq; - bool ams_enable; - int tremolo_depth; - float tremolo_freq; - int vibrato_depth; - float vibrato_freq; - float rate; - int feedback; - int damper; - int sostenute; -}; - -// FM�����m�[�g�B -class fm_note : public note -{ - public: - fm_note(const FMPARAMETER ¶ms, int note, int velocity, int panpot, int assign, float frequency_multiplier); - virtual void release() - { - delete this; - } - virtual bool synthesize(int_least32_t *buf, std::size_t samples, float rate, int_least32_t left, - int_least32_t right); - virtual void note_off(int velocity); - virtual void sound_off(); - virtual void set_frequency_multiplier(float value); - virtual void set_tremolo(int depth, float freq); - virtual void set_vibrato(float depth, float freq); - virtual void set_damper(int value); - virtual void set_sostenute(int value); - virtual void set_freeze(int value); - - public: - fm_sound_generator fm; - int velocity; -}; - -// FM�����m�[�g�t�@�N�g���B -class fm_note_factory : public note_factory -{ - public: - fm_note_factory(); - void clear(); - void get_program(int number, FMPARAMETER &p); - bool set_program(int number, const FMPARAMETER &p); - bool set_drum_program(int number, const DRUMPARAMETER &p); - virtual note *note_on(int_least32_t program, int note, int velocity, float frequency_multiplier); - - private: - std::map programs; - std::map drums; -}; -} // namespace midisynth \ No newline at end of file diff --git a/libraries/libRAD/opal.cpp b/libraries/libRAD/opal.cpp index 2aa389d86..3e646a250 100644 --- a/libraries/libRAD/opal.cpp +++ b/libraries/libRAD/opal.cpp @@ -398,11 +398,11 @@ void Opal::Port(uint16_t reg_num, uint8_t val) } //================================================================================================== -// Generate sample. Every time you call this you will get two signed 16-bit samples (one for each +// Generate sample. Every time you call this you will get two floating point samples (one for each // stereo channel) which will sound correct when played back at the sample rate given when the // class was constructed. //================================================================================================== -void Opal::Sample(int16_t *left, int16_t *right) +void Opal::Sample(float *left, float *right) { // If the destination sample rate is higher than the OPL3 sample rate, we need to skip ahead @@ -419,8 +419,10 @@ void Opal::Sample(int16_t *left, int16_t *right) // Mix with the partial accumulation int32_t omblend = SampleRate - SampleAccum; - *left = (LastOutput[0] * omblend + CurrOutput[0] * SampleAccum) / SampleRate; - *right = (LastOutput[1] * omblend + CurrOutput[1] * SampleAccum) / SampleRate; + *(uint32_t *)left = 0x43818000^(uint16_t)((LastOutput[0] * omblend + CurrOutput[0] * SampleAccum) / SampleRate); + *left -= 259.0f; + *(uint32_t *)right = 0x43818000^(uint16_t)((LastOutput[1] * omblend + CurrOutput[1] * SampleAccum) / SampleRate); + *right -= 259.0f; SampleAccum += OPL3SampleRate; } diff --git a/libraries/libRAD/opal.h b/libraries/libRAD/opal.h index 0c85b07f3..2919e0c93 100644 --- a/libraries/libRAD/opal.h +++ b/libraries/libRAD/opal.h @@ -202,11 +202,12 @@ class Opal void SetSampleRate(int sample_rate); void Port(uint16_t reg_num, uint8_t val); - void Sample(int16_t *left, int16_t *right); + void Sample(float *left, float *right); protected: void Init(int sample_rate); void Output(int16_t &left, int16_t &right); + inline float OutputToFloat(int16_t in); int32_t SampleRate; int32_t SampleAccum; diff --git a/libraries/libRAD/radmidi.cpp b/libraries/libRAD/radmidi.cpp index 40e949798..9699be1cb 100644 --- a/libraries/libRAD/radmidi.cpp +++ b/libraries/libRAD/radmidi.cpp @@ -34,7 +34,7 @@ bool OPLPlayer::loadPatches() } // ---------------------------------------------------------------------------- -void OPLPlayer::generate(int16_t *data, unsigned numSamples) +void OPLPlayer::generate(float *data, unsigned numSamples) { unsigned int samp = 0; @@ -127,7 +127,7 @@ void OPLPlayer::runSamples(unsigned count) // (i.e. when forcing a voice off, changing 4op flags, etc.) while (count--) { - std::pair output; + std::pair output; m_opl3->Sample(&output.first, &output.second); m_sampleFIFO.push(output); } diff --git a/libraries/libRAD/radmidi.h b/libraries/libRAD/radmidi.h index d9ca0250b..69b7df83e 100644 --- a/libraries/libRAD/radmidi.h +++ b/libraries/libRAD/radmidi.h @@ -66,7 +66,7 @@ class OPLPlayer // load built-in instrument patches bool loadPatches(); - void generate(int16_t *data, unsigned numSamples); + void generate(float *data, unsigned numSamples); // reset OPL and midi file void reset(); @@ -146,9 +146,9 @@ class OPLPlayer Opal *m_opl3 = nullptr; int rate; - std::pair m_output; // output sample data + std::pair m_output; // output sample data // if we need to clock one of the OPLs between register writes, save the resulting sample - std::queue> m_sampleFIFO; + std::queue> m_sampleFIFO; MIDIChannel m_channels[16]; std::vector m_voices; diff --git a/libraries/m4p/it2drivers/sb16.c b/libraries/m4p/it2drivers/sb16.c index abaddb540..4955d73d0 100644 --- a/libraries/m4p/it2drivers/sb16.c +++ b/libraries/m4p/it2drivers/sb16.c @@ -272,6 +272,27 @@ static int32_t SB16_PostMix(int16_t *AudioOut16, int32_t SamplesToOutput) // 8bb return SamplesTodo; } +static int32_t SB16_PostMix_Float(float *AudioOut32, int32_t SamplesToOutput) +{ + const uint8_t SampleShiftValue = (Song.Header.Flags & ITF_STEREO) ? 13 : 14; + + int32_t SamplesTodo = (SamplesToOutput == 0) ? BytesToMix : SamplesToOutput; + for (int32_t i = 0; i < SamplesTodo * 2; i++) + { + int32_t Sample = MixBuffer[MixTransferOffset++] >> SampleShiftValue; + + if (Sample < INT16_MIN) + Sample = INT16_MIN; + else if (Sample > INT16_MAX) + Sample = INT16_MAX; + + *(uint32_t *)AudioOut32=0x43818000^((uint16_t)Sample); + *AudioOut32++ -= 259.0f; + } + + return SamplesTodo; +} + static void SB16_Mix(int32_t numSamples, int16_t *audioOut) // 8bb: added this (original SB16 driver uses IRQ callback) { int32_t SamplesLeft = numSamples; @@ -296,6 +317,30 @@ static void SB16_Mix(int32_t numSamples, int16_t *audioOut) // 8bb: added this ( } } +static void SB16_Mix_Float(int32_t numSamples, float *audioOut) +{ + int32_t SamplesLeft = numSamples; + while (SamplesLeft > 0) + { + if (MixTransferRemaining == 0) + { + Update(); + SB16_MixSamples(); + MixTransferRemaining = BytesToMix; + } + + int32_t SamplesToTransfer = SamplesLeft; + if (SamplesToTransfer > MixTransferRemaining) + SamplesToTransfer = MixTransferRemaining; + + SB16_PostMix_Float(audioOut, SamplesToTransfer); + audioOut += SamplesToTransfer * 2; + + MixTransferRemaining -= SamplesToTransfer; + SamplesLeft -= SamplesToTransfer; + } +} + /* 8bb: ** Fixes sample end bytes for interpolation (yes, we have room after the data). ** Sustain loops are always handled as non-looping during fix in IT2. @@ -361,11 +406,13 @@ static void SB16_CloseDriver(void) DriverClose = NULL; DriverMix = NULL; + DriverMixFloat = NULL; DriverSetTempo = NULL; DriverSetMixVolume = NULL; DriverFixSamples = NULL; DriverResetMixer = NULL; DriverPostMix = NULL; + DriverPostMixFloat = NULL; DriverMixSamples = NULL; } @@ -389,11 +436,13 @@ bool SB16_InitDriver(int32_t mixingFrequency) // 8bb: setup driver functions DriverClose = SB16_CloseDriver; DriverMix = SB16_Mix; // 8bb: added this (original driver uses IRQ callback) + DriverMixFloat = SB16_Mix_Float; DriverSetTempo = SB16_SetTempo; DriverSetMixVolume = SB16_SetMixVolume; DriverFixSamples = SB16_FixSamples; DriverResetMixer = SB16_ResetMixer; // 8bb: added this DriverPostMix = SB16_PostMix; // 8bb: added this + DriverPostMixFloat = SB16_PostMix_Float; DriverMixSamples = SB16_MixSamples; // 8bb: added this /* diff --git a/libraries/m4p/it_music.c b/libraries/m4p/it_music.c index 8bd2215fd..4cc7af013 100644 --- a/libraries/m4p/it_music.c +++ b/libraries/m4p/it_music.c @@ -40,8 +40,10 @@ enum // 8bb: globalized void (*DriverClose)(void) = NULL; void (*DriverMix)(int32_t, int16_t *) = NULL; +void (*DriverMixFloat)(int32_t, float *) = NULL; void (*DriverResetMixer)(void) = NULL; int32_t (*DriverPostMix)(int16_t *, int32_t) = NULL; +int32_t (*DriverPostMixFloat)(float *, int32_t) = NULL; void (*DriverMixSamples)(void) = NULL; void (*DriverSetTempo)(uint8_t) = NULL; void (*DriverSetMixVolume)(uint8_t) = NULL; @@ -1960,6 +1962,18 @@ void Music_FillAudioBuffer(int16_t *buffer, int32_t numSamples) DriverMix(numSamples, buffer); } +void Music_FillAudioBufferFloat(float *buffer, int32_t numSamples) +{ + if (!Song.Playing) + { + memset(buffer, 0, numSamples * 2 * sizeof (float)); + return; + } + + if (DriverMixFloat != NULL) + DriverMixFloat(numSamples, buffer); +} + bool Music_Init(int32_t mixingFrequency, int32_t mixingBufferSize) { if (FirstTimeInit) diff --git a/libraries/m4p/it_music.h b/libraries/m4p/it_music.h index 64c8f2b46..1c48949c5 100644 --- a/libraries/m4p/it_music.h +++ b/libraries/m4p/it_music.h @@ -36,8 +36,10 @@ enum // 8bb: globalized extern void (*DriverClose)(void); extern void (*DriverMix)(int32_t, int16_t *); +extern void (*DriverMixFloat)(int32_t, float *); extern void (*DriverResetMixer)(void); extern int32_t (*DriverPostMix)(int16_t *, int32_t); +extern int32_t (*DriverPostMixFloat)(float *, int32_t); extern void (*DriverMixSamples)(void); extern void (*DriverSetTempo)(uint8_t); extern void (*DriverSetMixVolume)(uint8_t); @@ -60,6 +62,7 @@ void PitchSlideDown(hostChn_t *hc, slaveChn_t *sc, int16_t SlideValue); void Update(void); void Music_FillAudioBuffer(int16_t *buffer, int32_t numSamples); +void Music_FillAudioBufferFloat(float *buffer, int32_t numSamples); bool Music_Init(int32_t mixingFrequency, int32_t mixingBufferSize); void Music_Close(void); // 8bb: added this diff --git a/libraries/m4p/m4p.c b/libraries/m4p/m4p.c index dc97c57df..313c8173b 100644 --- a/libraries/m4p/m4p.c +++ b/libraries/m4p/m4p.c @@ -6,6 +6,7 @@ extern bool Music_Init(int32_t mixingFrequency, int32_t mixingBufferSize); extern bool Music_LoadFromData(uint8_t *Data, uint32_t DataLen); extern void Music_PlaySong(uint16_t order); extern void Music_FillAudioBuffer(int16_t *buffer, int32_t numSamples); +extern void Music_FillAudioBufferFloat(float *buffer, int32_t numSamples); extern void Music_Close(void); extern void Music_Stop(void); extern void Music_FreeSong(void); @@ -16,6 +17,7 @@ extern bool loadMusicFromData(const uint8_t *data, uint32_t dataLength); extern void startPlaying(void); extern void stopPlaying(void); extern void mix_UpdateBuffer(int16_t *buffer, int32_t numSamples); +extern void mix_UpdateBufferFloat(float *buffer, int32_t numSamples); extern void stopMusic(); extern void freeMusic(void); @@ -104,6 +106,14 @@ void m4p_GenerateSamples(int16_t *buffer, int32_t numSamples) mix_UpdateBuffer(buffer, numSamples); } +void m4p_GenerateFloatSamples(float *buffer, int32_t numSamples) +{ + if (current_format == FORMAT_IT_S3M) + Music_FillAudioBufferFloat(buffer, numSamples); + else + mix_UpdateBufferFloat(buffer, numSamples); +} + void m4p_Stop(void) { if (current_format == FORMAT_IT_S3M) diff --git a/libraries/m4p/m4p.h b/libraries/m4p/m4p.h index 899a2539e..4f2f4aa3b 100644 --- a/libraries/m4p/m4p.h +++ b/libraries/m4p/m4p.h @@ -18,6 +18,9 @@ void m4p_PlaySong(void); // Generate samples and fill buffer void m4p_GenerateSamples(int16_t *buffer, int32_t numSamples); +// Generate samples and fill buffer +void m4p_GenerateFloatSamples(float *buffer, int32_t numSamples); + // Set replayer status to Stop void m4p_Stop(void); diff --git a/libraries/m4p/pmp_mix.c b/libraries/m4p/pmp_mix.c index 9994197fd..13bd9a157 100644 --- a/libraries/m4p/pmp_mix.c +++ b/libraries/m4p/pmp_mix.c @@ -312,6 +312,75 @@ void mix_UpdateBuffer(int16_t *buffer, int32_t numSamples) } } +void mix_UpdateBufferFloat(float *buffer, int32_t numSamples) +{ + if (numSamples <= 0) + return; + + if (musicPaused) // silence output + { + memset(buffer, 0, numSamples * (2 * sizeof (int16_t))); + return; + } + + memset(CDA_MixBuffer, 0, numSamples * (2 * sizeof (int32_t))); + + int32_t c = 0; + int32_t a = numSamples; + + while (a > 0) + { + if (PMPLeft == 0) + { + mix_SaveIPVolumes(); + mainPlayer(); + mix_UpdateChannelVolPanFrq(); + PMPLeft = speedVal; + } + + int32_t b = a; + if (b > PMPLeft) + b = PMPLeft; + + CIType *v = CI; + for (int32_t i = 0; i < song.antChn*2; i++, v++) + PMPMix32Proc(v, b, c); + + c += b; + a -= b; + PMPLeft -= b; + } + + numSamples *= 2; // 8bb: stereo + + /* 8bb: Done a bit differently since we don't use a + ** Sound Blaster with its master volume setting. + ** Instead we change the amplitude here. + */ + + if (masterVol == 256) // 8bb: max master volume, no need to change amp + { + for (int32_t i = 0; i < numSamples; i++) + { + int32_t out32 = CDA_MixBuffer[i] >> 8; + CLAMP16(out32); + *(uint32_t *)&buffer[i] = 0x43818000^((uint16_t)out32); + buffer[i] -= 259.0f; + } + } + else + { + for (int32_t i = 0; i < numSamples; i++) + { + int32_t out32 = CDA_MixBuffer[i] >> 8; + CLAMP16(out32); + out32 = (out32 * masterVol) >> 8; + *(uint32_t *)&buffer[i] = 0x43818000^((uint16_t)out32); + buffer[i] -= 259.0f; + } + } +} + bool dump_Init(int32_t frq, int32_t amp, int16_t songPos) { setPos(songPos, 0); diff --git a/libraries/m4p/pmp_mix.h b/libraries/m4p/pmp_mix.h index 9929880e7..f0e026ced 100644 --- a/libraries/m4p/pmp_mix.h +++ b/libraries/m4p/pmp_mix.h @@ -52,6 +52,7 @@ void mix_Free(void); void mix_ClearChannels(void); void mix_UpdateBuffer(int16_t *buffer, int32_t numSamples); +void mix_UpdateBufferFloat(float *buffer, int32_t numSamples); bool dump_Init(int32_t frq, int32_t amp, int16_t songPos); void dump_Close(void); diff --git a/libraries/minivorbis/.gitignore b/libraries/minivorbis/.gitignore deleted file mode 100644 index eb06dd4e4..000000000 --- a/libraries/minivorbis/.gitignore +++ /dev/null @@ -1,6 +0,0 @@ -*.tar.gz -libogg-* -libvorbis-* -miniaudio.h -miniaudio_engine.h -stb_vorbis.* diff --git a/libraries/minivorbis/CMakeLists.txt b/libraries/minivorbis/CMakeLists.txt deleted file mode 100644 index 88b17b017..000000000 --- a/libraries/minivorbis/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -########################################## -# minivorbis -########################################## - -add_library(minivorbis minivorbis.cc) - -target_include_directories(minivorbis PUBLIC ./) \ No newline at end of file diff --git a/libraries/minivorbis/LICENSE.txt b/libraries/minivorbis/LICENSE.txt deleted file mode 100644 index 90d054cea..000000000 --- a/libraries/minivorbis/LICENSE.txt +++ /dev/null @@ -1,29 +0,0 @@ -Copyright (c) 2002-2020 Xiph.org Foundation -Copyright (c) 2020 Eduardo Bart (https://github.com/edubart) - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -- Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -- Neither the name of the Xiph.org Foundation nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION -OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/libraries/minivorbis/README.md b/libraries/minivorbis/README.md deleted file mode 100644 index ecf187a0e..000000000 --- a/libraries/minivorbis/README.md +++ /dev/null @@ -1,87 +0,0 @@ -# MiniVorbis - -This is libogg 1.3.4 + libvorbis 1.3.7 contained in a single header -to be bundled in C/C++ applications with ease for decoding OGG sound files. -[Ogg Vorbis](https://en.wikipedia.org/wiki/Vorbis) is a open general-purpose compressed audio format -for mid to high quality audio and music at fixed and variable bitrates. - -## Example Usage - -```c -#define OGG_IMPL -#define VORBIS_IMPL -#include "minivorbis.h" - -int main(int argc, char** argv) -{ - if(argc < 2) { - printf("No input file.\n"); - return -1; - } - /* Open sound file. */ - FILE* fp = fopen(argv[1], "rb"); - if(!fp) { - printf("Failed to open file '%s'.", argv[1]); - return -1; - } - /* Open sound stream. */ - OggVorbis_File vorbis; - if(ov_open_callbacks(fp, &vorbis, NULL, 0, OV_CALLBACKS_DEFAULT) != 0) { - printf("Invalid Ogg file '%s'.", argv[1]); - return -1; - } - /* Print sound information. */ - vorbis_info* info = ov_info(&vorbis, -1); - printf("Ogg file %d Hz, %d channels, %d kbit/s.\n", info->rate, info->channels, info->bitrate_nominal / 1024); - /* Read the entire sound stream. */ - unsigned char buf[4096]; - while(1) { - int section = 0; - long bytes = ov_read(&vorbis, buf, sizeof(buf), 0, 2, 1, §ion); - if(bytes <= 0) /* end of file or error */ - break; - } - /* Close sound file */ - ov_clear(&vorbis); - return 0; -} -``` - -## Usage - -Copy `minivorbis.h` into your C/C++ project, include it anywhere you want to use the Vorbis API. -Then do the following in *one* C file to implement Ogg and Vorbis: -```c -#define OGG_IMPL -#define VORBIS_IMPL -#include "minivorbis.h" -``` - -Optionally provide the following defines: - - `OV_EXCLUDE_STATIC_CALLBACKS` - exclude the default static callbacks - -Note that almost no modification was made in the Ogg/Vorbis implementation code, -thus there are some C variable names that may collide with your code, -therefore it is best to declare the implementation in dedicated C file. - -## Documentation - -For documentation on how to use Ogg and Vorbis read its [official documentation](https://xiph.org/doc/). - -## Extra Miniaudio Vorbis Decoder - -You can use `miniaudio_vorbis.h` to replace Vorbis decoder of the awesome -single-header [miniaudio](https://miniaud.io/) library with minivorbis, follow the -instructions described on it. - -## Updates - -- **02-Nov-2020**: Library created, using Ogg 1.3.4 and Vorbis 1.3.7. - -## Notes - -The header is generated using the bash script `gen.sh` all modifications done is there. - -## License - -BSD-like License, same as libogg and libvorbis, see LICENSE.txt for information. diff --git a/libraries/minivorbis/minivorbis.cc b/libraries/minivorbis/minivorbis.cc deleted file mode 100644 index c2d0f076a..000000000 --- a/libraries/minivorbis/minivorbis.cc +++ /dev/null @@ -1,5 +0,0 @@ -#define OV_EXCLUDE_STATIC_CALLBACKS -#define OGG_IMPL -#define VORBIS_IMPL - -#include "minivorbis.h" \ No newline at end of file diff --git a/libraries/minivorbis/minivorbis.h b/libraries/minivorbis/minivorbis.h deleted file mode 100644 index 6b63f4158..000000000 --- a/libraries/minivorbis/minivorbis.h +++ /dev/null @@ -1,21776 +0,0 @@ -/* - minivorbis.h -- libvorbis decoder in a single header - Project URL: https://github.com/edubart/minivorbis - - This is libogg 1.3.4 + libvorbis 1.3.7 contained in a single header - to be bundled in C/C++ applications with ease for decoding OGG sound files. - Ogg Vorbis is a open general-purpose compressed audio format - for mid to high quality audio and music at fixed and variable bitrates. - - Do the following in *one* C file to implement Ogg and Vorbis: - #define OGG_IMPL - #define VORBIS_IMPL - - Optionally provide the following defines: - OV_EXCLUDE_STATIC_CALLBACKS - exclude the default static callbacks - - Note that almost no modification was made in the Ogg/Vorbis implementation code, - thus there are some C variable names that may collide with your code, - therefore it is best to declare the implementation in dedicated C file. - - LICENSE - BSD-like License, same as libogg and libvorbis, see end of file. -*/ -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 * - * by the Xiph.Org Foundation http://www.xiph.org/ * - * * - ******************************************************************** - - function: Define a consistent set of types on each platform. - - ********************************************************************/ -#ifndef _OS_TYPES_H -#define _OS_TYPES_H - -#ifdef _MSC_VER - #pragma warning( push ) - // disable declaration of 'i' hides previous local declaration - #pragma warning( disable : 4456 ) - // disable declaration of 'mask' hides global declaration - #pragma warning( disable : 4459 ) - // disable assignment within conditional expression - #pragma warning( disable : 4706 ) - // disable declaration of 'a' hides function parameter - #pragma warning( disable : 4457 ) -#endif - - -/* make it easy on the folks that want to compile the libs with a - different malloc than stdlib */ -#define _ogg_malloc malloc -#define _ogg_calloc calloc -#define _ogg_realloc realloc -#define _ogg_free free - -#if defined(_WIN32) - -# if defined(__CYGWIN__) -# include - typedef int16_t ogg_int16_t; - typedef uint16_t ogg_uint16_t; - typedef int32_t ogg_int32_t; - typedef uint32_t ogg_uint32_t; - typedef int64_t ogg_int64_t; - typedef uint64_t ogg_uint64_t; -# elif defined(__MINGW32__) -# include - typedef short ogg_int16_t; - typedef unsigned short ogg_uint16_t; - typedef int ogg_int32_t; - typedef unsigned int ogg_uint32_t; - typedef long long ogg_int64_t; - typedef unsigned long long ogg_uint64_t; -# elif defined(__MWERKS__) - typedef long long ogg_int64_t; - typedef unsigned long long ogg_uint64_t; - typedef int ogg_int32_t; - typedef unsigned int ogg_uint32_t; - typedef short ogg_int16_t; - typedef unsigned short ogg_uint16_t; -# else -# if defined(_MSC_VER) && (_MSC_VER >= 1800) /* MSVC 2013 and newer */ -# include - typedef int16_t ogg_int16_t; - typedef uint16_t ogg_uint16_t; - typedef int32_t ogg_int32_t; - typedef uint32_t ogg_uint32_t; - typedef int64_t ogg_int64_t; - typedef uint64_t ogg_uint64_t; -# else - /* MSVC/Borland */ - typedef __int64 ogg_int64_t; - typedef __int32 ogg_int32_t; - typedef unsigned __int32 ogg_uint32_t; - typedef unsigned __int64 ogg_uint64_t; - typedef __int16 ogg_int16_t; - typedef unsigned __int16 ogg_uint16_t; -# endif -# endif - -#elif (defined(__APPLE__) && defined(__MACH__)) /* MacOS X Framework build */ - -# include - typedef int16_t ogg_int16_t; - typedef uint16_t ogg_uint16_t; - typedef int32_t ogg_int32_t; - typedef uint32_t ogg_uint32_t; - typedef int64_t ogg_int64_t; - typedef uint64_t ogg_uint64_t; - -#elif defined(__HAIKU__) - - /* Haiku */ -# include - typedef short ogg_int16_t; - typedef unsigned short ogg_uint16_t; - typedef int ogg_int32_t; - typedef unsigned int ogg_uint32_t; - typedef long long ogg_int64_t; - typedef unsigned long long ogg_uint64_t; - -#elif defined(__BEOS__) - - /* Be */ -# include - typedef int16_t ogg_int16_t; - typedef uint16_t ogg_uint16_t; - typedef int32_t ogg_int32_t; - typedef uint32_t ogg_uint32_t; - typedef int64_t ogg_int64_t; - typedef uint64_t ogg_uint64_t; - -#elif defined (__EMX__) - - /* OS/2 GCC */ - typedef short ogg_int16_t; - typedef unsigned short ogg_uint16_t; - typedef int ogg_int32_t; - typedef unsigned int ogg_uint32_t; - typedef long long ogg_int64_t; - typedef unsigned long long ogg_uint64_t; - - -#elif defined (DJGPP) - - /* DJGPP */ - typedef short ogg_int16_t; - typedef int ogg_int32_t; - typedef unsigned int ogg_uint32_t; - typedef long long ogg_int64_t; - typedef unsigned long long ogg_uint64_t; - -#elif defined(R5900) - - /* PS2 EE */ - typedef long ogg_int64_t; - typedef unsigned long ogg_uint64_t; - typedef int ogg_int32_t; - typedef unsigned ogg_uint32_t; - typedef short ogg_int16_t; - -#elif defined(__SYMBIAN32__) - - /* Symbian GCC */ - typedef signed short ogg_int16_t; - typedef unsigned short ogg_uint16_t; - typedef signed int ogg_int32_t; - typedef unsigned int ogg_uint32_t; - typedef long long int ogg_int64_t; - typedef unsigned long long int ogg_uint64_t; - -#elif defined(__TMS320C6X__) - - /* TI C64x compiler */ - typedef signed short ogg_int16_t; - typedef unsigned short ogg_uint16_t; - typedef signed int ogg_int32_t; - typedef unsigned int ogg_uint32_t; - typedef long long int ogg_int64_t; - typedef unsigned long long int ogg_uint64_t; - -#else - -/* config_types.h */ -#include -typedef int16_t ogg_int16_t; -typedef uint16_t ogg_uint16_t; -typedef int32_t ogg_int32_t; -typedef uint32_t ogg_uint32_t; -typedef int64_t ogg_int64_t; -typedef uint64_t ogg_uint64_t; - -#endif - -#endif /* _OS_TYPES_H */ -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 * - * by the Xiph.Org Foundation http://www.xiph.org/ * - * * - ******************************************************************** - - function: toplevel libogg include - - ********************************************************************/ -#ifndef _OGG_H -#define _OGG_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include -/*#include */ - -typedef struct { - void *iov_base; - size_t iov_len; -} ogg_iovec_t; - -typedef struct { - long endbyte; - int endbit; - - unsigned char *buffer; - unsigned char *ptr; - long storage; -} oggpack_buffer; - -/* ogg_page is used to encapsulate the data in one Ogg bitstream page *****/ - -typedef struct { - unsigned char *header; - long header_len; - unsigned char *body; - long body_len; -} ogg_page; - -/* ogg_stream_state contains the current encode/decode state of a logical - Ogg bitstream **********************************************************/ - -typedef struct { - unsigned char *body_data; /* bytes from packet bodies */ - long body_storage; /* storage elements allocated */ - long body_fill; /* elements stored; fill mark */ - long body_returned; /* elements of fill returned */ - - - int *lacing_vals; /* The values that will go to the segment table */ - ogg_int64_t *granule_vals; /* granulepos values for headers. Not compact - this way, but it is simple coupled to the - lacing fifo */ - long lacing_storage; - long lacing_fill; - long lacing_packet; - long lacing_returned; - - unsigned char header[282]; /* working space for header encode */ - int header_fill; - - int e_o_s; /* set when we have buffered the last packet in the - logical bitstream */ - int b_o_s; /* set after we've written the initial page - of a logical bitstream */ - long serialno; - long pageno; - ogg_int64_t packetno; /* sequence number for decode; the framing - knows where there's a hole in the data, - but we need coupling so that the codec - (which is in a separate abstraction - layer) also knows about the gap */ - ogg_int64_t granulepos; - -} ogg_stream_state; - -/* ogg_packet is used to encapsulate the data and metadata belonging - to a single raw Ogg/Vorbis packet *************************************/ - -typedef struct { - unsigned char *packet; - long bytes; - long b_o_s; - long e_o_s; - - ogg_int64_t granulepos; - - ogg_int64_t packetno; /* sequence number for decode; the framing - knows where there's a hole in the data, - but we need coupling so that the codec - (which is in a separate abstraction - layer) also knows about the gap */ -} ogg_packet; - -typedef struct { - unsigned char *data; - int storage; - int fill; - int returned; - - int unsynced; - int headerbytes; - int bodybytes; -} ogg_sync_state; - -/* Ogg BITSTREAM PRIMITIVES: bitstream ************************/ - -extern void oggpack_writeinit(oggpack_buffer *b); -extern int oggpack_writecheck(oggpack_buffer *b); -extern void oggpack_writetrunc(oggpack_buffer *b,long bits); -extern void oggpack_writealign(oggpack_buffer *b); -extern void oggpack_writecopy(oggpack_buffer *b,void *source,long bits); -extern void oggpack_reset(oggpack_buffer *b); -extern void oggpack_writeclear(oggpack_buffer *b); -extern void oggpack_readinit(oggpack_buffer *b,unsigned char *buf,int bytes); -extern void oggpack_write(oggpack_buffer *b,unsigned long value,int bits); -extern long oggpack_look(oggpack_buffer *b,int bits); -extern long oggpack_look1(oggpack_buffer *b); -extern void oggpack_adv(oggpack_buffer *b,int bits); -extern void oggpack_adv1(oggpack_buffer *b); -extern long oggpack_read(oggpack_buffer *b,int bits); -extern long oggpack_read1(oggpack_buffer *b); -extern long oggpack_bytes(oggpack_buffer *b); -extern long oggpack_bits(oggpack_buffer *b); -extern unsigned char *oggpack_get_buffer(oggpack_buffer *b); - -extern void oggpackB_writeinit(oggpack_buffer *b); -extern int oggpackB_writecheck(oggpack_buffer *b); -extern void oggpackB_writetrunc(oggpack_buffer *b,long bits); -extern void oggpackB_writealign(oggpack_buffer *b); -extern void oggpackB_writecopy(oggpack_buffer *b,void *source,long bits); -extern void oggpackB_reset(oggpack_buffer *b); -extern void oggpackB_writeclear(oggpack_buffer *b); -extern void oggpackB_readinit(oggpack_buffer *b,unsigned char *buf,int bytes); -extern void oggpackB_write(oggpack_buffer *b,unsigned long value,int bits); -extern long oggpackB_look(oggpack_buffer *b,int bits); -extern long oggpackB_look1(oggpack_buffer *b); -extern void oggpackB_adv(oggpack_buffer *b,int bits); -extern void oggpackB_adv1(oggpack_buffer *b); -extern long oggpackB_read(oggpack_buffer *b,int bits); -extern long oggpackB_read1(oggpack_buffer *b); -extern long oggpackB_bytes(oggpack_buffer *b); -extern long oggpackB_bits(oggpack_buffer *b); -extern unsigned char *oggpackB_get_buffer(oggpack_buffer *b); - -/* Ogg BITSTREAM PRIMITIVES: encoding **************************/ - -extern int ogg_stream_packetin(ogg_stream_state *os, ogg_packet *op); -extern int ogg_stream_iovecin(ogg_stream_state *os, ogg_iovec_t *iov, - int count, long e_o_s, ogg_int64_t granulepos); -extern int ogg_stream_pageout(ogg_stream_state *os, ogg_page *og); -extern int ogg_stream_pageout_fill(ogg_stream_state *os, ogg_page *og, int nfill); -extern int ogg_stream_flush(ogg_stream_state *os, ogg_page *og); -extern int ogg_stream_flush_fill(ogg_stream_state *os, ogg_page *og, int nfill); - -/* Ogg BITSTREAM PRIMITIVES: decoding **************************/ - -extern int ogg_sync_init(ogg_sync_state *oy); -extern int ogg_sync_clear(ogg_sync_state *oy); -extern int ogg_sync_reset(ogg_sync_state *oy); -extern int ogg_sync_destroy(ogg_sync_state *oy); -extern int ogg_sync_check(ogg_sync_state *oy); - -extern char *ogg_sync_buffer(ogg_sync_state *oy, long size); -extern int ogg_sync_wrote(ogg_sync_state *oy, long bytes); -extern long ogg_sync_pageseek(ogg_sync_state *oy,ogg_page *og); -extern int ogg_sync_pageout(ogg_sync_state *oy, ogg_page *og); -extern int ogg_stream_pagein(ogg_stream_state *os, ogg_page *og); -extern int ogg_stream_packetout(ogg_stream_state *os,ogg_packet *op); -extern int ogg_stream_packetpeek(ogg_stream_state *os,ogg_packet *op); - -/* Ogg BITSTREAM PRIMITIVES: general ***************************/ - -extern int ogg_stream_init(ogg_stream_state *os,int serialno); -extern int ogg_stream_clear(ogg_stream_state *os); -extern int ogg_stream_reset(ogg_stream_state *os); -extern int ogg_stream_reset_serialno(ogg_stream_state *os,int serialno); -extern int ogg_stream_destroy(ogg_stream_state *os); -extern int ogg_stream_check(ogg_stream_state *os); -extern int ogg_stream_eos(ogg_stream_state *os); - -extern void ogg_page_checksum_set(ogg_page *og); - -extern int ogg_page_version(const ogg_page *og); -extern int ogg_page_continued(const ogg_page *og); -extern int ogg_page_bos(const ogg_page *og); -extern int ogg_page_eos(const ogg_page *og); -extern ogg_int64_t ogg_page_granulepos(const ogg_page *og); -extern int ogg_page_serialno(const ogg_page *og); -extern long ogg_page_pageno(const ogg_page *og); -extern int ogg_page_packets(const ogg_page *og); - -extern void ogg_packet_clear(ogg_packet *op); - - -#ifdef __cplusplus -} -#endif - -#endif /* _OGG_H */ -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 * - * by the Xiph.Org Foundation https://xiph.org/ * - - ******************************************************************** - - function: libvorbis codec headers - - ********************************************************************/ - -#ifndef _vorbis_codec_h_ -#define _vorbis_codec_h_ - -#ifdef __cplusplus -extern "C" -{ -#endif /* __cplusplus */ - -/*#include */ - -typedef struct vorbis_info{ - int version; - int channels; - long rate; - - /* The below bitrate declarations are *hints*. - Combinations of the three values carry the following implications: - - all three set to the same value: - implies a fixed rate bitstream - only nominal set: - implies a VBR stream that averages the nominal bitrate. No hard - upper/lower limit - upper and or lower set: - implies a VBR bitstream that obeys the bitrate limits. nominal - may also be set to give a nominal rate. - none set: - the coder does not care to speculate. - */ - - long bitrate_upper; - long bitrate_nominal; - long bitrate_lower; - long bitrate_window; - - void *codec_setup; -} vorbis_info; - -/* vorbis_dsp_state buffers the current vorbis audio - analysis/synthesis state. The DSP state belongs to a specific - logical bitstream ****************************************************/ -typedef struct vorbis_dsp_state{ - int analysisp; - vorbis_info *vi; - - float **pcm; - float **pcmret; - int pcm_storage; - int pcm_current; - int pcm_returned; - - int preextrapolate; - int eofflag; - - long lW; - long W; - long nW; - long centerW; - - ogg_int64_t granulepos; - ogg_int64_t sequence; - - ogg_int64_t glue_bits; - ogg_int64_t time_bits; - ogg_int64_t floor_bits; - ogg_int64_t res_bits; - - void *backend_state; -} vorbis_dsp_state; - -typedef struct vorbis_block{ - /* necessary stream state for linking to the framing abstraction */ - float **pcm; /* this is a pointer into local storage */ - oggpack_buffer opb; - - long lW; - long W; - long nW; - int pcmend; - int mode; - - int eofflag; - ogg_int64_t granulepos; - ogg_int64_t sequence; - vorbis_dsp_state *vd; /* For read-only access of configuration */ - - /* local storage to avoid remallocing; it's up to the mapping to - structure it */ - void *localstore; - long localtop; - long localalloc; - long totaluse; - struct alloc_chain *reap; - - /* bitmetrics for the frame */ - long glue_bits; - long time_bits; - long floor_bits; - long res_bits; - - void *internal; - -} vorbis_block; - -/* vorbis_block is a single block of data to be processed as part of -the analysis/synthesis stream; it belongs to a specific logical -bitstream, but is independent from other vorbis_blocks belonging to -that logical bitstream. *************************************************/ - -struct alloc_chain{ - void *ptr; - struct alloc_chain *next; -}; - -/* vorbis_info contains all the setup information specific to the - specific compression/decompression mode in progress (eg, - psychoacoustic settings, channel setup, options, codebook - etc). vorbis_info and substructures are in backends.h. -*********************************************************************/ - -/* the comments are not part of vorbis_info so that vorbis_info can be - static storage */ -typedef struct vorbis_comment{ - /* unlimited user comment fields. libvorbis writes 'libvorbis' - whatever vendor is set to in encode */ - char **user_comments; - int *comment_lengths; - int comments; - char *vendor; - -} vorbis_comment; - - -/* libvorbis encodes in two abstraction layers; first we perform DSP - and produce a packet (see docs/analysis.txt). The packet is then - coded into a framed OggSquish bitstream by the second layer (see - docs/framing.txt). Decode is the reverse process; we sync/frame - the bitstream and extract individual packets, then decode the - packet back into PCM audio. - - The extra framing/packetizing is used in streaming formats, such as - files. Over the net (such as with UDP), the framing and - packetization aren't necessary as they're provided by the transport - and the streaming layer is not used */ - -/* Vorbis PRIMITIVES: general ***************************************/ - -extern void vorbis_info_init(vorbis_info *vi); -extern void vorbis_info_clear(vorbis_info *vi); -extern int vorbis_info_blocksize(vorbis_info *vi,int zo); -extern void vorbis_comment_init(vorbis_comment *vc); -extern void vorbis_comment_add(vorbis_comment *vc, const char *comment); -extern void vorbis_comment_add_tag(vorbis_comment *vc, - const char *tag, const char *contents); -extern char *vorbis_comment_query(vorbis_comment *vc, const char *tag, int count); -extern int vorbis_comment_query_count(vorbis_comment *vc, const char *tag); -extern void vorbis_comment_clear(vorbis_comment *vc); - -extern int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb); -extern int vorbis_block_clear(vorbis_block *vb); -extern void vorbis_dsp_clear(vorbis_dsp_state *v); -extern double vorbis_granule_time(vorbis_dsp_state *v, - ogg_int64_t granulepos); - -extern const char *vorbis_version_string(void); - -/* Vorbis PRIMITIVES: analysis/DSP layer ****************************/ - -extern int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi); -extern int vorbis_commentheader_out(vorbis_comment *vc, ogg_packet *op); -extern int vorbis_analysis_headerout(vorbis_dsp_state *v, - vorbis_comment *vc, - ogg_packet *op, - ogg_packet *op_comm, - ogg_packet *op_code); -extern float **vorbis_analysis_buffer(vorbis_dsp_state *v,int vals); -extern int vorbis_analysis_wrote(vorbis_dsp_state *v,int vals); -extern int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb); -extern int vorbis_analysis(vorbis_block *vb,ogg_packet *op); - -extern int vorbis_bitrate_addblock(vorbis_block *vb); -extern int vorbis_bitrate_flushpacket(vorbis_dsp_state *vd, - ogg_packet *op); - -/* Vorbis PRIMITIVES: synthesis layer *******************************/ -extern int vorbis_synthesis_idheader(ogg_packet *op); -extern int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc, - ogg_packet *op); - -extern int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi); -extern int vorbis_synthesis_restart(vorbis_dsp_state *v); -extern int vorbis_synthesis(vorbis_block *vb,ogg_packet *op); -extern int vorbis_synthesis_trackonly(vorbis_block *vb,ogg_packet *op); -extern int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb); -extern int vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm); -extern int vorbis_synthesis_lapout(vorbis_dsp_state *v,float ***pcm); -extern int vorbis_synthesis_read(vorbis_dsp_state *v,int samples); -extern long vorbis_packet_blocksize(vorbis_info *vi,ogg_packet *op); - -extern int vorbis_synthesis_halfrate(vorbis_info *v,int flag); -extern int vorbis_synthesis_halfrate_p(vorbis_info *v); - -/* Vorbis ERRORS and return codes ***********************************/ - -#define OV_FALSE -1 -#define OV_EOF -2 -#define OV_HOLE -3 - -#define OV_EREAD -128 -#define OV_EFAULT -129 -#define OV_EIMPL -130 -#define OV_EINVAL -131 -#define OV_ENOTVORBIS -132 -#define OV_EBADHEADER -133 -#define OV_EVERSION -134 -#define OV_ENOTAUDIO -135 -#define OV_EBADPACKET -136 -#define OV_EBADLINK -137 -#define OV_ENOSEEK -138 - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif - -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: stdio-based convenience library for opening/seeking/decoding - - ********************************************************************/ - -#ifndef _OV_FILE_H_ -#define _OV_FILE_H_ - -#ifdef __cplusplus -extern "C" -{ -#endif /* __cplusplus */ - -#include -/*#include "codec.h"*/ - -/* The function prototypes for the callbacks are basically the same as for - * the stdio functions fread, fseek, fclose, ftell. - * The one difference is that the FILE * arguments have been replaced with - * a void * - this is to be used as a pointer to whatever internal data these - * functions might need. In the stdio case, it's just a FILE * cast to a void * - * - * If you use other functions, check the docs for these functions and return - * the right values. For seek_func(), you *MUST* return -1 if the stream is - * unseekable - */ -typedef struct { - size_t (*read_func) (void *ptr, size_t size, size_t nmemb, void *datasource); - int (*seek_func) (void *datasource, ogg_int64_t offset, int whence); - int (*close_func) (void *datasource); - long (*tell_func) (void *datasource); -} ov_callbacks; - -#ifndef OV_EXCLUDE_STATIC_CALLBACKS - -/* a few sets of convenient callbacks, especially for use under - * Windows where ov_open_callbacks() should always be used instead of - * ov_open() to avoid problems with incompatible crt.o version linking - * issues. */ - -static int _ov_header_fseek_wrap(FILE *f,ogg_int64_t off,int whence){ - if(f==NULL)return(-1); - -#ifdef __MINGW32__ - return fseeko64(f,off,whence); -#elif defined (_WIN32) - return _fseeki64(f,off,whence); -#else - return fseek(f,off,whence); -#endif -} - -/* These structs below (OV_CALLBACKS_DEFAULT etc) are defined here as - * static data. That means that every file which includes this header - * will get its own copy of these structs whether it uses them or - * not unless it #defines OV_EXCLUDE_STATIC_CALLBACKS. - * These static symbols are essential on platforms such as Windows on - * which several different versions of stdio support may be linked to - * by different DLLs, and we need to be certain we know which one - * we're using (the same one as the main application). - */ - -static ov_callbacks OV_CALLBACKS_DEFAULT = { - (size_t (*)(void *, size_t, size_t, void *)) fread, - (int (*)(void *, ogg_int64_t, int)) _ov_header_fseek_wrap, - (int (*)(void *)) fclose, - (long (*)(void *)) ftell -}; - -static ov_callbacks OV_CALLBACKS_NOCLOSE = { - (size_t (*)(void *, size_t, size_t, void *)) fread, - (int (*)(void *, ogg_int64_t, int)) _ov_header_fseek_wrap, - (int (*)(void *)) NULL, - (long (*)(void *)) ftell -}; - -static ov_callbacks OV_CALLBACKS_STREAMONLY = { - (size_t (*)(void *, size_t, size_t, void *)) fread, - (int (*)(void *, ogg_int64_t, int)) NULL, - (int (*)(void *)) fclose, - (long (*)(void *)) NULL -}; - -static ov_callbacks OV_CALLBACKS_STREAMONLY_NOCLOSE = { - (size_t (*)(void *, size_t, size_t, void *)) fread, - (int (*)(void *, ogg_int64_t, int)) NULL, - (int (*)(void *)) NULL, - (long (*)(void *)) NULL -}; - -#endif - -#define NOTOPEN 0 -#define PARTOPEN 1 -#define OPENED 2 -#define STREAMSET 3 -#define INITSET 4 - -typedef struct OggVorbis_File { - void *datasource; /* Pointer to a FILE *, etc. */ - int seekable; - ogg_int64_t offset; - ogg_int64_t end; - ogg_sync_state oy; - - /* If the FILE handle isn't seekable (eg, a pipe), only the current - stream appears */ - int links; - ogg_int64_t *offsets; - ogg_int64_t *dataoffsets; - long *serialnos; - ogg_int64_t *pcmlengths; /* overloaded to maintain binary - compatibility; x2 size, stores both - beginning and end values */ - vorbis_info *vi; - vorbis_comment *vc; - - /* Decoding working state local storage */ - ogg_int64_t pcm_offset; - int ready_state; - long current_serialno; - int current_link; - - double bittrack; - double samptrack; - - ogg_stream_state os; /* take physical pages, weld into a logical - stream of packets */ - vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */ - vorbis_block vb; /* local working space for packet->PCM decode */ - - ov_callbacks callbacks; - -} OggVorbis_File; - - -extern int ov_clear(OggVorbis_File *vf); -extern int ov_fopen(const char *path,OggVorbis_File *vf); -extern int ov_open(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes); -extern int ov_open_callbacks(void *datasource, OggVorbis_File *vf, - const char *initial, long ibytes, ov_callbacks callbacks); - -extern int ov_test(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes); -extern int ov_test_callbacks(void *datasource, OggVorbis_File *vf, - const char *initial, long ibytes, ov_callbacks callbacks); -extern int ov_test_open(OggVorbis_File *vf); - -extern long ov_bitrate(OggVorbis_File *vf,int i); -extern long ov_bitrate_instant(OggVorbis_File *vf); -extern long ov_streams(OggVorbis_File *vf); -extern long ov_seekable(OggVorbis_File *vf); -extern long ov_serialnumber(OggVorbis_File *vf,int i); - -extern ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i); -extern ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i); -extern double ov_time_total(OggVorbis_File *vf,int i); - -extern int ov_raw_seek(OggVorbis_File *vf,ogg_int64_t pos); -extern int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos); -extern int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos); -extern int ov_time_seek(OggVorbis_File *vf,double pos); -extern int ov_time_seek_page(OggVorbis_File *vf,double pos); - -extern int ov_raw_seek_lap(OggVorbis_File *vf,ogg_int64_t pos); -extern int ov_pcm_seek_lap(OggVorbis_File *vf,ogg_int64_t pos); -extern int ov_pcm_seek_page_lap(OggVorbis_File *vf,ogg_int64_t pos); -extern int ov_time_seek_lap(OggVorbis_File *vf,double pos); -extern int ov_time_seek_page_lap(OggVorbis_File *vf,double pos); - -extern ogg_int64_t ov_raw_tell(OggVorbis_File *vf); -extern ogg_int64_t ov_pcm_tell(OggVorbis_File *vf); -extern double ov_time_tell(OggVorbis_File *vf); - -extern vorbis_info *ov_info(OggVorbis_File *vf,int link); -extern vorbis_comment *ov_comment(OggVorbis_File *vf,int link); - -extern long ov_read_float(OggVorbis_File *vf,float ***pcm_channels,int samples, - int *bitstream); -extern long ov_read_filter(OggVorbis_File *vf,char *buffer,int length, - int bigendianp,int word,int sgned,int *bitstream, - void (*filter)(float **pcm,long channels,long samples,void *filter_param),void *filter_param); -extern long ov_read(OggVorbis_File *vf,char *buffer,int length, - int bigendianp,int word,int sgned,int *bitstream); -extern int ov_crosslap(OggVorbis_File *vf1,OggVorbis_File *vf2); - -extern int ov_halfrate(OggVorbis_File *vf,int flag); -extern int ov_halfrate_p(OggVorbis_File *vf); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif - -#ifdef OGG_IMPL -#ifdef __cplusplus -extern "C" { -#endif -/******************************************************************** - * * - * THIS FILE IS PART OF THE Ogg CONTAINER SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2014 * - * by the Xiph.Org Foundation http://www.xiph.org/ * - * * - ******************************************************************** - - function: packing variable sized words into an octet stream - - ********************************************************************/ - -/* We're 'LSb' endian; if we write a word but read individual bits, - then we'll read the lsb first */ - -#include -#include -#include -/*#include */ - -#define BUFFER_INCREMENT 256 - -static const unsigned long mask[]= -{0x00000000,0x00000001,0x00000003,0x00000007,0x0000000f, - 0x0000001f,0x0000003f,0x0000007f,0x000000ff,0x000001ff, - 0x000003ff,0x000007ff,0x00000fff,0x00001fff,0x00003fff, - 0x00007fff,0x0000ffff,0x0001ffff,0x0003ffff,0x0007ffff, - 0x000fffff,0x001fffff,0x003fffff,0x007fffff,0x00ffffff, - 0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,0x1fffffff, - 0x3fffffff,0x7fffffff,0xffffffff }; - -static const unsigned int mask8B[]= -{0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff}; - -void oggpack_writeinit(oggpack_buffer *b){ - memset(b,0,sizeof(*b)); - b->ptr=b->buffer=(unsigned char *)_ogg_malloc(BUFFER_INCREMENT); - b->buffer[0]='\0'; - b->storage=BUFFER_INCREMENT; -} - -void oggpackB_writeinit(oggpack_buffer *b){ - oggpack_writeinit(b); -} - -int oggpack_writecheck(oggpack_buffer *b){ - if(!b->ptr || !b->storage)return -1; - return 0; -} - -int oggpackB_writecheck(oggpack_buffer *b){ - return oggpack_writecheck(b); -} - -void oggpack_writetrunc(oggpack_buffer *b,long bits){ - long bytes=bits>>3; - if(b->ptr){ - bits-=bytes*8; - b->ptr=b->buffer+bytes; - b->endbit=bits; - b->endbyte=bytes; - *b->ptr&=mask[bits]; - } -} - -void oggpackB_writetrunc(oggpack_buffer *b,long bits){ - long bytes=bits>>3; - if(b->ptr){ - bits-=bytes*8; - b->ptr=b->buffer+bytes; - b->endbit=bits; - b->endbyte=bytes; - *b->ptr&=mask8B[bits]; - } -} - -/* Takes only up to 32 bits. */ -void oggpack_write(oggpack_buffer *b,unsigned long value,int bits){ - if(bits<0 || bits>32) goto err; - if(b->endbyte>=b->storage-4){ - void *ret; - if(!b->ptr)return; - if(b->storage>LONG_MAX-BUFFER_INCREMENT) goto err; - ret=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT); - if(!ret) goto err; - b->buffer=(unsigned char *)ret; - b->storage+=BUFFER_INCREMENT; - b->ptr=b->buffer+b->endbyte; - } - - value&=mask[bits]; - bits+=b->endbit; - - b->ptr[0]|=value<endbit; - - if(bits>=8){ - b->ptr[1]=(unsigned char)(value>>(8-b->endbit)); - if(bits>=16){ - b->ptr[2]=(unsigned char)(value>>(16-b->endbit)); - if(bits>=24){ - b->ptr[3]=(unsigned char)(value>>(24-b->endbit)); - if(bits>=32){ - if(b->endbit) - b->ptr[4]=(unsigned char)(value>>(32-b->endbit)); - else - b->ptr[4]=0; - } - } - } - } - - b->endbyte+=bits/8; - b->ptr+=bits/8; - b->endbit=bits&7; - return; - err: - oggpack_writeclear(b); -} - -/* Takes only up to 32 bits. */ -void oggpackB_write(oggpack_buffer *b,unsigned long value,int bits){ - if(bits<0 || bits>32) goto err; - if(b->endbyte>=b->storage-4){ - void *ret; - if(!b->ptr)return; - if(b->storage>LONG_MAX-BUFFER_INCREMENT) goto err; - ret=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT); - if(!ret) goto err; - b->buffer=(unsigned char *)ret; - b->storage+=BUFFER_INCREMENT; - b->ptr=b->buffer+b->endbyte; - } - - value=(value&mask[bits])<<(32-bits); - bits+=b->endbit; - - b->ptr[0]|=value>>(24+b->endbit); - - if(bits>=8){ - b->ptr[1]=(unsigned char)(value>>(16+b->endbit)); - if(bits>=16){ - b->ptr[2]=(unsigned char)(value>>(8+b->endbit)); - if(bits>=24){ - b->ptr[3]=(unsigned char)(value>>(b->endbit)); - if(bits>=32){ - if(b->endbit) - b->ptr[4]=(unsigned char)(value<<(8-b->endbit)); - else - b->ptr[4]=0; - } - } - } - } - - b->endbyte+=bits/8; - b->ptr+=bits/8; - b->endbit=bits&7; - return; - err: - oggpack_writeclear(b); -} - -void oggpack_writealign(oggpack_buffer *b){ - int bits=8-b->endbit; - if(bits<8) - oggpack_write(b,0,bits); -} - -void oggpackB_writealign(oggpack_buffer *b){ - int bits=8-b->endbit; - if(bits<8) - oggpackB_write(b,0,bits); -} - -static void oggpack_writecopy_helper(oggpack_buffer *b, - void *source, - long bits, - void (*w)(oggpack_buffer *, - unsigned long, - int), - int msb){ - unsigned char *ptr=(unsigned char *)source; - - long bytes=bits/8; - long pbytes=(b->endbit+bits)/8; - bits-=bytes*8; - - /* expand storage up-front */ - if(b->endbyte+pbytes>=b->storage){ - void *ret; - if(!b->ptr) goto err; - if(b->storage>b->endbyte+pbytes+BUFFER_INCREMENT) goto err; - b->storage=b->endbyte+pbytes+BUFFER_INCREMENT; - ret=_ogg_realloc(b->buffer,b->storage); - if(!ret) goto err; - b->buffer=(unsigned char *)ret; - b->ptr=b->buffer+b->endbyte; - } - - /* copy whole octets */ - if(b->endbit){ - int i; - /* unaligned copy. Do it the hard way. */ - for(i=0;iptr,source,bytes); - b->ptr+=bytes; - b->endbyte+=bytes; - *b->ptr=0; - } - - /* copy trailing bits */ - if(bits){ - if(msb) - w(b,(unsigned long)(ptr[bytes]>>(8-bits)),bits); - else - w(b,(unsigned long)(ptr[bytes]),bits); - } - return; - err: - oggpack_writeclear(b); -} - -void oggpack_writecopy(oggpack_buffer *b,void *source,long bits){ - oggpack_writecopy_helper(b,source,bits,oggpack_write,0); -} - -void oggpackB_writecopy(oggpack_buffer *b,void *source,long bits){ - oggpack_writecopy_helper(b,source,bits,oggpackB_write,1); -} - -void oggpack_reset(oggpack_buffer *b){ - if(!b->ptr)return; - b->ptr=b->buffer; - b->buffer[0]=0; - b->endbit=b->endbyte=0; -} - -void oggpackB_reset(oggpack_buffer *b){ - oggpack_reset(b); -} - -void oggpack_writeclear(oggpack_buffer *b){ - if(b->buffer)_ogg_free(b->buffer); - memset(b,0,sizeof(*b)); -} - -void oggpackB_writeclear(oggpack_buffer *b){ - oggpack_writeclear(b); -} - -void oggpack_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){ - memset(b,0,sizeof(*b)); - b->buffer=b->ptr=buf; - b->storage=bytes; -} - -void oggpackB_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){ - oggpack_readinit(b,buf,bytes); -} - -/* Read in bits without advancing the bitptr; bits <= 32 */ -long oggpack_look(oggpack_buffer *b,int bits){ - unsigned long ret; - unsigned long m; - - if(bits<0 || bits>32) return -1; - m=mask[bits]; - bits+=b->endbit; - - if(b->endbyte >= b->storage-4){ - /* not the main path */ - if(b->endbyte > b->storage-((bits+7)>>3)) return -1; - /* special case to avoid reading b->ptr[0], which might be past the end of - the buffer; also skips some useless accounting */ - else if(!bits)return(0L); - } - - ret=b->ptr[0]>>b->endbit; - if(bits>8){ - ret|=b->ptr[1]<<(8-b->endbit); - if(bits>16){ - ret|=b->ptr[2]<<(16-b->endbit); - if(bits>24){ - ret|=b->ptr[3]<<(24-b->endbit); - if(bits>32 && b->endbit) - ret|=b->ptr[4]<<(32-b->endbit); - } - } - } - return(m&ret); -} - -/* Read in bits without advancing the bitptr; bits <= 32 */ -long oggpackB_look(oggpack_buffer *b,int bits){ - unsigned long ret; - int m=32-bits; - - if(m<0 || m>32) return -1; - bits+=b->endbit; - - if(b->endbyte >= b->storage-4){ - /* not the main path */ - if(b->endbyte > b->storage-((bits+7)>>3)) return -1; - /* special case to avoid reading b->ptr[0], which might be past the end of - the buffer; also skips some useless accounting */ - else if(!bits)return(0L); - } - - ret=b->ptr[0]<<(24+b->endbit); - if(bits>8){ - ret|=b->ptr[1]<<(16+b->endbit); - if(bits>16){ - ret|=b->ptr[2]<<(8+b->endbit); - if(bits>24){ - ret|=b->ptr[3]<<(b->endbit); - if(bits>32 && b->endbit) - ret|=b->ptr[4]>>(8-b->endbit); - } - } - } - return ((ret&0xffffffff)>>(m>>1))>>((m+1)>>1); -} - -long oggpack_look1(oggpack_buffer *b){ - if(b->endbyte>=b->storage)return(-1); - return((b->ptr[0]>>b->endbit)&1); -} - -long oggpackB_look1(oggpack_buffer *b){ - if(b->endbyte>=b->storage)return(-1); - return((b->ptr[0]>>(7-b->endbit))&1); -} - -void oggpack_adv(oggpack_buffer *b,int bits){ - bits+=b->endbit; - - if(b->endbyte > b->storage-((bits+7)>>3)) goto overflow; - - b->ptr+=bits/8; - b->endbyte+=bits/8; - b->endbit=bits&7; - return; - - overflow: - b->ptr=NULL; - b->endbyte=b->storage; - b->endbit=1; -} - -void oggpackB_adv(oggpack_buffer *b,int bits){ - oggpack_adv(b,bits); -} - -void oggpack_adv1(oggpack_buffer *b){ - if(++(b->endbit)>7){ - b->endbit=0; - b->ptr++; - b->endbyte++; - } -} - -void oggpackB_adv1(oggpack_buffer *b){ - oggpack_adv1(b); -} - -/* bits <= 32 */ -long oggpack_read(oggpack_buffer *b,int bits){ - long ret; - unsigned long m; - - if(bits<0 || bits>32) goto err; - m=mask[bits]; - bits+=b->endbit; - - if(b->endbyte >= b->storage-4){ - /* not the main path */ - if(b->endbyte > b->storage-((bits+7)>>3)) goto overflow; - /* special case to avoid reading b->ptr[0], which might be past the end of - the buffer; also skips some useless accounting */ - else if(!bits)return(0L); - } - - ret=b->ptr[0]>>b->endbit; - if(bits>8){ - ret|=b->ptr[1]<<(8-b->endbit); - if(bits>16){ - ret|=b->ptr[2]<<(16-b->endbit); - if(bits>24){ - ret|=b->ptr[3]<<(24-b->endbit); - if(bits>32 && b->endbit){ - ret|=b->ptr[4]<<(32-b->endbit); - } - } - } - } - ret&=m; - b->ptr+=bits/8; - b->endbyte+=bits/8; - b->endbit=bits&7; - return ret; - - overflow: - err: - b->ptr=NULL; - b->endbyte=b->storage; - b->endbit=1; - return -1L; -} - -/* bits <= 32 */ -long oggpackB_read(oggpack_buffer *b,int bits){ - long ret; - long m=32-bits; - - if(m<0 || m>32) goto err; - bits+=b->endbit; - - if(b->endbyte+4>=b->storage){ - /* not the main path */ - if(b->endbyte > b->storage-((bits+7)>>3)) goto overflow; - /* special case to avoid reading b->ptr[0], which might be past the end of - the buffer; also skips some useless accounting */ - else if(!bits)return(0L); - } - - ret=b->ptr[0]<<(24+b->endbit); - if(bits>8){ - ret|=b->ptr[1]<<(16+b->endbit); - if(bits>16){ - ret|=b->ptr[2]<<(8+b->endbit); - if(bits>24){ - ret|=b->ptr[3]<<(b->endbit); - if(bits>32 && b->endbit) - ret|=b->ptr[4]>>(8-b->endbit); - } - } - } - ret=((ret&0xffffffffUL)>>(m>>1))>>((m+1)>>1); - - b->ptr+=bits/8; - b->endbyte+=bits/8; - b->endbit=bits&7; - return ret; - - overflow: - err: - b->ptr=NULL; - b->endbyte=b->storage; - b->endbit=1; - return -1L; -} - -long oggpack_read1(oggpack_buffer *b){ - long ret; - - if(b->endbyte >= b->storage) goto overflow; - ret=(b->ptr[0]>>b->endbit)&1; - - b->endbit++; - if(b->endbit>7){ - b->endbit=0; - b->ptr++; - b->endbyte++; - } - return ret; - - overflow: - b->ptr=NULL; - b->endbyte=b->storage; - b->endbit=1; - return -1L; -} - -long oggpackB_read1(oggpack_buffer *b){ - long ret; - - if(b->endbyte >= b->storage) goto overflow; - ret=(b->ptr[0]>>(7-b->endbit))&1; - - b->endbit++; - if(b->endbit>7){ - b->endbit=0; - b->ptr++; - b->endbyte++; - } - return ret; - - overflow: - b->ptr=NULL; - b->endbyte=b->storage; - b->endbit=1; - return -1L; -} - -long oggpack_bytes(oggpack_buffer *b){ - return(b->endbyte+(b->endbit+7)/8); -} - -long oggpack_bits(oggpack_buffer *b){ - return(b->endbyte*8+b->endbit); -} - -long oggpackB_bytes(oggpack_buffer *b){ - return oggpack_bytes(b); -} - -long oggpackB_bits(oggpack_buffer *b){ - return oggpack_bits(b); -} - -unsigned char *oggpack_get_buffer(oggpack_buffer *b){ - return(b->buffer); -} - -unsigned char *oggpackB_get_buffer(oggpack_buffer *b){ - return oggpack_get_buffer(b); -} - -/* Self test of the bitwise routines; everything else is based on - them, so they damned well better be solid. */ - -#ifdef _V_SELFTEST -#include - -static int ilog(unsigned int v){ - int ret=0; - while(v){ - ret++; - v>>=1; - } - return(ret); -} - -oggpack_buffer o; -oggpack_buffer r; - -void report(char *in){ - fprintf(stderr,"%s",in); - exit(1); -} - -void cliptest(unsigned long *b,int vals,int bits,int *comp,int compsize){ - long bytes,i; - unsigned char *buffer; - - oggpack_reset(&o); - for(i=0;i -#include -#include -/*#include */ - -/* A complete description of Ogg framing exists in docs/framing.html */ - -int ogg_page_version(const ogg_page *og){ - return((int)(og->header[4])); -} - -int ogg_page_continued(const ogg_page *og){ - return((int)(og->header[5]&0x01)); -} - -int ogg_page_bos(const ogg_page *og){ - return((int)(og->header[5]&0x02)); -} - -int ogg_page_eos(const ogg_page *og){ - return((int)(og->header[5]&0x04)); -} - -ogg_int64_t ogg_page_granulepos(const ogg_page *og){ - unsigned char *page=og->header; - ogg_uint64_t granulepos=page[13]&(0xff); - granulepos= (granulepos<<8)|(page[12]&0xff); - granulepos= (granulepos<<8)|(page[11]&0xff); - granulepos= (granulepos<<8)|(page[10]&0xff); - granulepos= (granulepos<<8)|(page[9]&0xff); - granulepos= (granulepos<<8)|(page[8]&0xff); - granulepos= (granulepos<<8)|(page[7]&0xff); - granulepos= (granulepos<<8)|(page[6]&0xff); - return((ogg_int64_t)granulepos); -} - -int ogg_page_serialno(const ogg_page *og){ - return((int)((ogg_uint32_t)og->header[14]) | - ((ogg_uint32_t)og->header[15]<<8) | - ((ogg_uint32_t)og->header[16]<<16) | - ((ogg_uint32_t)og->header[17]<<24)); -} - -long ogg_page_pageno(const ogg_page *og){ - return((long)((ogg_uint32_t)og->header[18]) | - ((ogg_uint32_t)og->header[19]<<8) | - ((ogg_uint32_t)og->header[20]<<16) | - ((ogg_uint32_t)og->header[21]<<24)); -} - - - -/* returns the number of packets that are completed on this page (if - the leading packet is begun on a previous page, but ends on this - page, it's counted */ - -/* NOTE: - If a page consists of a packet begun on a previous page, and a new - packet begun (but not completed) on this page, the return will be: - ogg_page_packets(page) ==1, - ogg_page_continued(page) !=0 - - If a page happens to be a single packet that was begun on a - previous page, and spans to the next page (in the case of a three or - more page packet), the return will be: - ogg_page_packets(page) ==0, - ogg_page_continued(page) !=0 -*/ - -int ogg_page_packets(const ogg_page *og){ - int i,n=og->header[26],count=0; - for(i=0;iheader[27+i]<255)count++; - return(count); -} - - -#if 0 -/* helper to initialize lookup for direct-table CRC (illustrative; we - use the static init in crctable.h) */ - -static void _ogg_crc_init(){ - int i, j; - ogg_uint32_t polynomial, crc; - polynomial = 0x04c11db7; /* The same as the ethernet generator - polynomial, although we use an - unreflected alg and an init/final - of 0, not 0xffffffff */ - for (i = 0; i <= 0xFF; i++){ - crc = i << 24; - - for (j = 0; j < 8; j++) - crc = (crc << 1) ^ (crc & (1 << 31) ? polynomial : 0); - - crc_lookup[0][i] = crc; - } - - for (i = 0; i <= 0xFF; i++) - for (j = 1; j < 8; j++) - crc_lookup[j][i] = crc_lookup[0][(crc_lookup[j - 1][i] >> 24) & 0xFF] ^ (crc_lookup[j - 1][i] << 8); -} -#endif - -/*#include "crctable.h"*/ -/******************************************************************** - * * - * THIS FILE IS PART OF THE Ogg CONTAINER SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2018 * - * by the Xiph.Org Foundation http://www.xiph.org/ * - * * - ********************************************************************/ - -/*#include */ - -static const ogg_uint32_t crc_lookup[8][256]={ -{0x00000000,0x04c11db7,0x09823b6e,0x0d4326d9,0x130476dc,0x17c56b6b,0x1a864db2,0x1e475005, - 0x2608edb8,0x22c9f00f,0x2f8ad6d6,0x2b4bcb61,0x350c9b64,0x31cd86d3,0x3c8ea00a,0x384fbdbd, - 0x4c11db70,0x48d0c6c7,0x4593e01e,0x4152fda9,0x5f15adac,0x5bd4b01b,0x569796c2,0x52568b75, - 0x6a1936c8,0x6ed82b7f,0x639b0da6,0x675a1011,0x791d4014,0x7ddc5da3,0x709f7b7a,0x745e66cd, - 0x9823b6e0,0x9ce2ab57,0x91a18d8e,0x95609039,0x8b27c03c,0x8fe6dd8b,0x82a5fb52,0x8664e6e5, - 0xbe2b5b58,0xbaea46ef,0xb7a96036,0xb3687d81,0xad2f2d84,0xa9ee3033,0xa4ad16ea,0xa06c0b5d, - 0xd4326d90,0xd0f37027,0xddb056fe,0xd9714b49,0xc7361b4c,0xc3f706fb,0xceb42022,0xca753d95, - 0xf23a8028,0xf6fb9d9f,0xfbb8bb46,0xff79a6f1,0xe13ef6f4,0xe5ffeb43,0xe8bccd9a,0xec7dd02d, - 0x34867077,0x30476dc0,0x3d044b19,0x39c556ae,0x278206ab,0x23431b1c,0x2e003dc5,0x2ac12072, - 0x128e9dcf,0x164f8078,0x1b0ca6a1,0x1fcdbb16,0x018aeb13,0x054bf6a4,0x0808d07d,0x0cc9cdca, - 0x7897ab07,0x7c56b6b0,0x71159069,0x75d48dde,0x6b93dddb,0x6f52c06c,0x6211e6b5,0x66d0fb02, - 0x5e9f46bf,0x5a5e5b08,0x571d7dd1,0x53dc6066,0x4d9b3063,0x495a2dd4,0x44190b0d,0x40d816ba, - 0xaca5c697,0xa864db20,0xa527fdf9,0xa1e6e04e,0xbfa1b04b,0xbb60adfc,0xb6238b25,0xb2e29692, - 0x8aad2b2f,0x8e6c3698,0x832f1041,0x87ee0df6,0x99a95df3,0x9d684044,0x902b669d,0x94ea7b2a, - 0xe0b41de7,0xe4750050,0xe9362689,0xedf73b3e,0xf3b06b3b,0xf771768c,0xfa325055,0xfef34de2, - 0xc6bcf05f,0xc27dede8,0xcf3ecb31,0xcbffd686,0xd5b88683,0xd1799b34,0xdc3abded,0xd8fba05a, - 0x690ce0ee,0x6dcdfd59,0x608edb80,0x644fc637,0x7a089632,0x7ec98b85,0x738aad5c,0x774bb0eb, - 0x4f040d56,0x4bc510e1,0x46863638,0x42472b8f,0x5c007b8a,0x58c1663d,0x558240e4,0x51435d53, - 0x251d3b9e,0x21dc2629,0x2c9f00f0,0x285e1d47,0x36194d42,0x32d850f5,0x3f9b762c,0x3b5a6b9b, - 0x0315d626,0x07d4cb91,0x0a97ed48,0x0e56f0ff,0x1011a0fa,0x14d0bd4d,0x19939b94,0x1d528623, - 0xf12f560e,0xf5ee4bb9,0xf8ad6d60,0xfc6c70d7,0xe22b20d2,0xe6ea3d65,0xeba91bbc,0xef68060b, - 0xd727bbb6,0xd3e6a601,0xdea580d8,0xda649d6f,0xc423cd6a,0xc0e2d0dd,0xcda1f604,0xc960ebb3, - 0xbd3e8d7e,0xb9ff90c9,0xb4bcb610,0xb07daba7,0xae3afba2,0xaafbe615,0xa7b8c0cc,0xa379dd7b, - 0x9b3660c6,0x9ff77d71,0x92b45ba8,0x9675461f,0x8832161a,0x8cf30bad,0x81b02d74,0x857130c3, - 0x5d8a9099,0x594b8d2e,0x5408abf7,0x50c9b640,0x4e8ee645,0x4a4ffbf2,0x470cdd2b,0x43cdc09c, - 0x7b827d21,0x7f436096,0x7200464f,0x76c15bf8,0x68860bfd,0x6c47164a,0x61043093,0x65c52d24, - 0x119b4be9,0x155a565e,0x18197087,0x1cd86d30,0x029f3d35,0x065e2082,0x0b1d065b,0x0fdc1bec, - 0x3793a651,0x3352bbe6,0x3e119d3f,0x3ad08088,0x2497d08d,0x2056cd3a,0x2d15ebe3,0x29d4f654, - 0xc5a92679,0xc1683bce,0xcc2b1d17,0xc8ea00a0,0xd6ad50a5,0xd26c4d12,0xdf2f6bcb,0xdbee767c, - 0xe3a1cbc1,0xe760d676,0xea23f0af,0xeee2ed18,0xf0a5bd1d,0xf464a0aa,0xf9278673,0xfde69bc4, - 0x89b8fd09,0x8d79e0be,0x803ac667,0x84fbdbd0,0x9abc8bd5,0x9e7d9662,0x933eb0bb,0x97ffad0c, - 0xafb010b1,0xab710d06,0xa6322bdf,0xa2f33668,0xbcb4666d,0xb8757bda,0xb5365d03,0xb1f740b4}, - -{0x00000000,0xd219c1dc,0xa0f29e0f,0x72eb5fd3,0x452421a9,0x973de075,0xe5d6bfa6,0x37cf7e7a, - 0x8a484352,0x5851828e,0x2abadd5d,0xf8a31c81,0xcf6c62fb,0x1d75a327,0x6f9efcf4,0xbd873d28, - 0x10519b13,0xc2485acf,0xb0a3051c,0x62bac4c0,0x5575baba,0x876c7b66,0xf58724b5,0x279ee569, - 0x9a19d841,0x4800199d,0x3aeb464e,0xe8f28792,0xdf3df9e8,0x0d243834,0x7fcf67e7,0xadd6a63b, - 0x20a33626,0xf2baf7fa,0x8051a829,0x524869f5,0x6587178f,0xb79ed653,0xc5758980,0x176c485c, - 0xaaeb7574,0x78f2b4a8,0x0a19eb7b,0xd8002aa7,0xefcf54dd,0x3dd69501,0x4f3dcad2,0x9d240b0e, - 0x30f2ad35,0xe2eb6ce9,0x9000333a,0x4219f2e6,0x75d68c9c,0xa7cf4d40,0xd5241293,0x073dd34f, - 0xbabaee67,0x68a32fbb,0x1a487068,0xc851b1b4,0xff9ecfce,0x2d870e12,0x5f6c51c1,0x8d75901d, - 0x41466c4c,0x935fad90,0xe1b4f243,0x33ad339f,0x04624de5,0xd67b8c39,0xa490d3ea,0x76891236, - 0xcb0e2f1e,0x1917eec2,0x6bfcb111,0xb9e570cd,0x8e2a0eb7,0x5c33cf6b,0x2ed890b8,0xfcc15164, - 0x5117f75f,0x830e3683,0xf1e56950,0x23fca88c,0x1433d6f6,0xc62a172a,0xb4c148f9,0x66d88925, - 0xdb5fb40d,0x094675d1,0x7bad2a02,0xa9b4ebde,0x9e7b95a4,0x4c625478,0x3e890bab,0xec90ca77, - 0x61e55a6a,0xb3fc9bb6,0xc117c465,0x130e05b9,0x24c17bc3,0xf6d8ba1f,0x8433e5cc,0x562a2410, - 0xebad1938,0x39b4d8e4,0x4b5f8737,0x994646eb,0xae893891,0x7c90f94d,0x0e7ba69e,0xdc626742, - 0x71b4c179,0xa3ad00a5,0xd1465f76,0x035f9eaa,0x3490e0d0,0xe689210c,0x94627edf,0x467bbf03, - 0xfbfc822b,0x29e543f7,0x5b0e1c24,0x8917ddf8,0xbed8a382,0x6cc1625e,0x1e2a3d8d,0xcc33fc51, - 0x828cd898,0x50951944,0x227e4697,0xf067874b,0xc7a8f931,0x15b138ed,0x675a673e,0xb543a6e2, - 0x08c49bca,0xdadd5a16,0xa83605c5,0x7a2fc419,0x4de0ba63,0x9ff97bbf,0xed12246c,0x3f0be5b0, - 0x92dd438b,0x40c48257,0x322fdd84,0xe0361c58,0xd7f96222,0x05e0a3fe,0x770bfc2d,0xa5123df1, - 0x189500d9,0xca8cc105,0xb8679ed6,0x6a7e5f0a,0x5db12170,0x8fa8e0ac,0xfd43bf7f,0x2f5a7ea3, - 0xa22feebe,0x70362f62,0x02dd70b1,0xd0c4b16d,0xe70bcf17,0x35120ecb,0x47f95118,0x95e090c4, - 0x2867adec,0xfa7e6c30,0x889533e3,0x5a8cf23f,0x6d438c45,0xbf5a4d99,0xcdb1124a,0x1fa8d396, - 0xb27e75ad,0x6067b471,0x128ceba2,0xc0952a7e,0xf75a5404,0x254395d8,0x57a8ca0b,0x85b10bd7, - 0x383636ff,0xea2ff723,0x98c4a8f0,0x4add692c,0x7d121756,0xaf0bd68a,0xdde08959,0x0ff94885, - 0xc3cab4d4,0x11d37508,0x63382adb,0xb121eb07,0x86ee957d,0x54f754a1,0x261c0b72,0xf405caae, - 0x4982f786,0x9b9b365a,0xe9706989,0x3b69a855,0x0ca6d62f,0xdebf17f3,0xac544820,0x7e4d89fc, - 0xd39b2fc7,0x0182ee1b,0x7369b1c8,0xa1707014,0x96bf0e6e,0x44a6cfb2,0x364d9061,0xe45451bd, - 0x59d36c95,0x8bcaad49,0xf921f29a,0x2b383346,0x1cf74d3c,0xceee8ce0,0xbc05d333,0x6e1c12ef, - 0xe36982f2,0x3170432e,0x439b1cfd,0x9182dd21,0xa64da35b,0x74546287,0x06bf3d54,0xd4a6fc88, - 0x6921c1a0,0xbb38007c,0xc9d35faf,0x1bca9e73,0x2c05e009,0xfe1c21d5,0x8cf77e06,0x5eeebfda, - 0xf33819e1,0x2121d83d,0x53ca87ee,0x81d34632,0xb61c3848,0x6405f994,0x16eea647,0xc4f7679b, - 0x79705ab3,0xab699b6f,0xd982c4bc,0x0b9b0560,0x3c547b1a,0xee4dbac6,0x9ca6e515,0x4ebf24c9}, - -{0x00000000,0x01d8ac87,0x03b1590e,0x0269f589,0x0762b21c,0x06ba1e9b,0x04d3eb12,0x050b4795, - 0x0ec56438,0x0f1dc8bf,0x0d743d36,0x0cac91b1,0x09a7d624,0x087f7aa3,0x0a168f2a,0x0bce23ad, - 0x1d8ac870,0x1c5264f7,0x1e3b917e,0x1fe33df9,0x1ae87a6c,0x1b30d6eb,0x19592362,0x18818fe5, - 0x134fac48,0x129700cf,0x10fef546,0x112659c1,0x142d1e54,0x15f5b2d3,0x179c475a,0x1644ebdd, - 0x3b1590e0,0x3acd3c67,0x38a4c9ee,0x397c6569,0x3c7722fc,0x3daf8e7b,0x3fc67bf2,0x3e1ed775, - 0x35d0f4d8,0x3408585f,0x3661add6,0x37b90151,0x32b246c4,0x336aea43,0x31031fca,0x30dbb34d, - 0x269f5890,0x2747f417,0x252e019e,0x24f6ad19,0x21fdea8c,0x2025460b,0x224cb382,0x23941f05, - 0x285a3ca8,0x2982902f,0x2beb65a6,0x2a33c921,0x2f388eb4,0x2ee02233,0x2c89d7ba,0x2d517b3d, - 0x762b21c0,0x77f38d47,0x759a78ce,0x7442d449,0x714993dc,0x70913f5b,0x72f8cad2,0x73206655, - 0x78ee45f8,0x7936e97f,0x7b5f1cf6,0x7a87b071,0x7f8cf7e4,0x7e545b63,0x7c3daeea,0x7de5026d, - 0x6ba1e9b0,0x6a794537,0x6810b0be,0x69c81c39,0x6cc35bac,0x6d1bf72b,0x6f7202a2,0x6eaaae25, - 0x65648d88,0x64bc210f,0x66d5d486,0x670d7801,0x62063f94,0x63de9313,0x61b7669a,0x606fca1d, - 0x4d3eb120,0x4ce61da7,0x4e8fe82e,0x4f5744a9,0x4a5c033c,0x4b84afbb,0x49ed5a32,0x4835f6b5, - 0x43fbd518,0x4223799f,0x404a8c16,0x41922091,0x44996704,0x4541cb83,0x47283e0a,0x46f0928d, - 0x50b47950,0x516cd5d7,0x5305205e,0x52dd8cd9,0x57d6cb4c,0x560e67cb,0x54679242,0x55bf3ec5, - 0x5e711d68,0x5fa9b1ef,0x5dc04466,0x5c18e8e1,0x5913af74,0x58cb03f3,0x5aa2f67a,0x5b7a5afd, - 0xec564380,0xed8eef07,0xefe71a8e,0xee3fb609,0xeb34f19c,0xeaec5d1b,0xe885a892,0xe95d0415, - 0xe29327b8,0xe34b8b3f,0xe1227eb6,0xe0fad231,0xe5f195a4,0xe4293923,0xe640ccaa,0xe798602d, - 0xf1dc8bf0,0xf0042777,0xf26dd2fe,0xf3b57e79,0xf6be39ec,0xf766956b,0xf50f60e2,0xf4d7cc65, - 0xff19efc8,0xfec1434f,0xfca8b6c6,0xfd701a41,0xf87b5dd4,0xf9a3f153,0xfbca04da,0xfa12a85d, - 0xd743d360,0xd69b7fe7,0xd4f28a6e,0xd52a26e9,0xd021617c,0xd1f9cdfb,0xd3903872,0xd24894f5, - 0xd986b758,0xd85e1bdf,0xda37ee56,0xdbef42d1,0xdee40544,0xdf3ca9c3,0xdd555c4a,0xdc8df0cd, - 0xcac91b10,0xcb11b797,0xc978421e,0xc8a0ee99,0xcdaba90c,0xcc73058b,0xce1af002,0xcfc25c85, - 0xc40c7f28,0xc5d4d3af,0xc7bd2626,0xc6658aa1,0xc36ecd34,0xc2b661b3,0xc0df943a,0xc10738bd, - 0x9a7d6240,0x9ba5cec7,0x99cc3b4e,0x981497c9,0x9d1fd05c,0x9cc77cdb,0x9eae8952,0x9f7625d5, - 0x94b80678,0x9560aaff,0x97095f76,0x96d1f3f1,0x93dab464,0x920218e3,0x906bed6a,0x91b341ed, - 0x87f7aa30,0x862f06b7,0x8446f33e,0x859e5fb9,0x8095182c,0x814db4ab,0x83244122,0x82fceda5, - 0x8932ce08,0x88ea628f,0x8a839706,0x8b5b3b81,0x8e507c14,0x8f88d093,0x8de1251a,0x8c39899d, - 0xa168f2a0,0xa0b05e27,0xa2d9abae,0xa3010729,0xa60a40bc,0xa7d2ec3b,0xa5bb19b2,0xa463b535, - 0xafad9698,0xae753a1f,0xac1ccf96,0xadc46311,0xa8cf2484,0xa9178803,0xab7e7d8a,0xaaa6d10d, - 0xbce23ad0,0xbd3a9657,0xbf5363de,0xbe8bcf59,0xbb8088cc,0xba58244b,0xb831d1c2,0xb9e97d45, - 0xb2275ee8,0xb3fff26f,0xb19607e6,0xb04eab61,0xb545ecf4,0xb49d4073,0xb6f4b5fa,0xb72c197d}, - -{0x00000000,0xdc6d9ab7,0xbc1a28d9,0x6077b26e,0x7cf54c05,0xa098d6b2,0xc0ef64dc,0x1c82fe6b, - 0xf9ea980a,0x258702bd,0x45f0b0d3,0x999d2a64,0x851fd40f,0x59724eb8,0x3905fcd6,0xe5686661, - 0xf7142da3,0x2b79b714,0x4b0e057a,0x97639fcd,0x8be161a6,0x578cfb11,0x37fb497f,0xeb96d3c8, - 0x0efeb5a9,0xd2932f1e,0xb2e49d70,0x6e8907c7,0x720bf9ac,0xae66631b,0xce11d175,0x127c4bc2, - 0xeae946f1,0x3684dc46,0x56f36e28,0x8a9ef49f,0x961c0af4,0x4a719043,0x2a06222d,0xf66bb89a, - 0x1303defb,0xcf6e444c,0xaf19f622,0x73746c95,0x6ff692fe,0xb39b0849,0xd3ecba27,0x0f812090, - 0x1dfd6b52,0xc190f1e5,0xa1e7438b,0x7d8ad93c,0x61082757,0xbd65bde0,0xdd120f8e,0x017f9539, - 0xe417f358,0x387a69ef,0x580ddb81,0x84604136,0x98e2bf5d,0x448f25ea,0x24f89784,0xf8950d33, - 0xd1139055,0x0d7e0ae2,0x6d09b88c,0xb164223b,0xade6dc50,0x718b46e7,0x11fcf489,0xcd916e3e, - 0x28f9085f,0xf49492e8,0x94e32086,0x488eba31,0x540c445a,0x8861deed,0xe8166c83,0x347bf634, - 0x2607bdf6,0xfa6a2741,0x9a1d952f,0x46700f98,0x5af2f1f3,0x869f6b44,0xe6e8d92a,0x3a85439d, - 0xdfed25fc,0x0380bf4b,0x63f70d25,0xbf9a9792,0xa31869f9,0x7f75f34e,0x1f024120,0xc36fdb97, - 0x3bfad6a4,0xe7974c13,0x87e0fe7d,0x5b8d64ca,0x470f9aa1,0x9b620016,0xfb15b278,0x277828cf, - 0xc2104eae,0x1e7dd419,0x7e0a6677,0xa267fcc0,0xbee502ab,0x6288981c,0x02ff2a72,0xde92b0c5, - 0xcceefb07,0x108361b0,0x70f4d3de,0xac994969,0xb01bb702,0x6c762db5,0x0c019fdb,0xd06c056c, - 0x3504630d,0xe969f9ba,0x891e4bd4,0x5573d163,0x49f12f08,0x959cb5bf,0xf5eb07d1,0x29869d66, - 0xa6e63d1d,0x7a8ba7aa,0x1afc15c4,0xc6918f73,0xda137118,0x067eebaf,0x660959c1,0xba64c376, - 0x5f0ca517,0x83613fa0,0xe3168dce,0x3f7b1779,0x23f9e912,0xff9473a5,0x9fe3c1cb,0x438e5b7c, - 0x51f210be,0x8d9f8a09,0xede83867,0x3185a2d0,0x2d075cbb,0xf16ac60c,0x911d7462,0x4d70eed5, - 0xa81888b4,0x74751203,0x1402a06d,0xc86f3ada,0xd4edc4b1,0x08805e06,0x68f7ec68,0xb49a76df, - 0x4c0f7bec,0x9062e15b,0xf0155335,0x2c78c982,0x30fa37e9,0xec97ad5e,0x8ce01f30,0x508d8587, - 0xb5e5e3e6,0x69887951,0x09ffcb3f,0xd5925188,0xc910afe3,0x157d3554,0x750a873a,0xa9671d8d, - 0xbb1b564f,0x6776ccf8,0x07017e96,0xdb6ce421,0xc7ee1a4a,0x1b8380fd,0x7bf43293,0xa799a824, - 0x42f1ce45,0x9e9c54f2,0xfeebe69c,0x22867c2b,0x3e048240,0xe26918f7,0x821eaa99,0x5e73302e, - 0x77f5ad48,0xab9837ff,0xcbef8591,0x17821f26,0x0b00e14d,0xd76d7bfa,0xb71ac994,0x6b775323, - 0x8e1f3542,0x5272aff5,0x32051d9b,0xee68872c,0xf2ea7947,0x2e87e3f0,0x4ef0519e,0x929dcb29, - 0x80e180eb,0x5c8c1a5c,0x3cfba832,0xe0963285,0xfc14ccee,0x20795659,0x400ee437,0x9c637e80, - 0x790b18e1,0xa5668256,0xc5113038,0x197caa8f,0x05fe54e4,0xd993ce53,0xb9e47c3d,0x6589e68a, - 0x9d1cebb9,0x4171710e,0x2106c360,0xfd6b59d7,0xe1e9a7bc,0x3d843d0b,0x5df38f65,0x819e15d2, - 0x64f673b3,0xb89be904,0xd8ec5b6a,0x0481c1dd,0x18033fb6,0xc46ea501,0xa419176f,0x78748dd8, - 0x6a08c61a,0xb6655cad,0xd612eec3,0x0a7f7474,0x16fd8a1f,0xca9010a8,0xaae7a2c6,0x768a3871, - 0x93e25e10,0x4f8fc4a7,0x2ff876c9,0xf395ec7e,0xef171215,0x337a88a2,0x530d3acc,0x8f60a07b}, - -{0x00000000,0x490d678d,0x921acf1a,0xdb17a897,0x20f48383,0x69f9e40e,0xb2ee4c99,0xfbe32b14, - 0x41e90706,0x08e4608b,0xd3f3c81c,0x9afeaf91,0x611d8485,0x2810e308,0xf3074b9f,0xba0a2c12, - 0x83d20e0c,0xcadf6981,0x11c8c116,0x58c5a69b,0xa3268d8f,0xea2bea02,0x313c4295,0x78312518, - 0xc23b090a,0x8b366e87,0x5021c610,0x192ca19d,0xe2cf8a89,0xabc2ed04,0x70d54593,0x39d8221e, - 0x036501af,0x4a686622,0x917fceb5,0xd872a938,0x2391822c,0x6a9ce5a1,0xb18b4d36,0xf8862abb, - 0x428c06a9,0x0b816124,0xd096c9b3,0x999bae3e,0x6278852a,0x2b75e2a7,0xf0624a30,0xb96f2dbd, - 0x80b70fa3,0xc9ba682e,0x12adc0b9,0x5ba0a734,0xa0438c20,0xe94eebad,0x3259433a,0x7b5424b7, - 0xc15e08a5,0x88536f28,0x5344c7bf,0x1a49a032,0xe1aa8b26,0xa8a7ecab,0x73b0443c,0x3abd23b1, - 0x06ca035e,0x4fc764d3,0x94d0cc44,0xddddabc9,0x263e80dd,0x6f33e750,0xb4244fc7,0xfd29284a, - 0x47230458,0x0e2e63d5,0xd539cb42,0x9c34accf,0x67d787db,0x2edae056,0xf5cd48c1,0xbcc02f4c, - 0x85180d52,0xcc156adf,0x1702c248,0x5e0fa5c5,0xa5ec8ed1,0xece1e95c,0x37f641cb,0x7efb2646, - 0xc4f10a54,0x8dfc6dd9,0x56ebc54e,0x1fe6a2c3,0xe40589d7,0xad08ee5a,0x761f46cd,0x3f122140, - 0x05af02f1,0x4ca2657c,0x97b5cdeb,0xdeb8aa66,0x255b8172,0x6c56e6ff,0xb7414e68,0xfe4c29e5, - 0x444605f7,0x0d4b627a,0xd65ccaed,0x9f51ad60,0x64b28674,0x2dbfe1f9,0xf6a8496e,0xbfa52ee3, - 0x867d0cfd,0xcf706b70,0x1467c3e7,0x5d6aa46a,0xa6898f7e,0xef84e8f3,0x34934064,0x7d9e27e9, - 0xc7940bfb,0x8e996c76,0x558ec4e1,0x1c83a36c,0xe7608878,0xae6deff5,0x757a4762,0x3c7720ef, - 0x0d9406bc,0x44996131,0x9f8ec9a6,0xd683ae2b,0x2d60853f,0x646de2b2,0xbf7a4a25,0xf6772da8, - 0x4c7d01ba,0x05706637,0xde67cea0,0x976aa92d,0x6c898239,0x2584e5b4,0xfe934d23,0xb79e2aae, - 0x8e4608b0,0xc74b6f3d,0x1c5cc7aa,0x5551a027,0xaeb28b33,0xe7bfecbe,0x3ca84429,0x75a523a4, - 0xcfaf0fb6,0x86a2683b,0x5db5c0ac,0x14b8a721,0xef5b8c35,0xa656ebb8,0x7d41432f,0x344c24a2, - 0x0ef10713,0x47fc609e,0x9cebc809,0xd5e6af84,0x2e058490,0x6708e31d,0xbc1f4b8a,0xf5122c07, - 0x4f180015,0x06156798,0xdd02cf0f,0x940fa882,0x6fec8396,0x26e1e41b,0xfdf64c8c,0xb4fb2b01, - 0x8d23091f,0xc42e6e92,0x1f39c605,0x5634a188,0xadd78a9c,0xe4daed11,0x3fcd4586,0x76c0220b, - 0xccca0e19,0x85c76994,0x5ed0c103,0x17dda68e,0xec3e8d9a,0xa533ea17,0x7e244280,0x3729250d, - 0x0b5e05e2,0x4253626f,0x9944caf8,0xd049ad75,0x2baa8661,0x62a7e1ec,0xb9b0497b,0xf0bd2ef6, - 0x4ab702e4,0x03ba6569,0xd8adcdfe,0x91a0aa73,0x6a438167,0x234ee6ea,0xf8594e7d,0xb15429f0, - 0x888c0bee,0xc1816c63,0x1a96c4f4,0x539ba379,0xa878886d,0xe175efe0,0x3a624777,0x736f20fa, - 0xc9650ce8,0x80686b65,0x5b7fc3f2,0x1272a47f,0xe9918f6b,0xa09ce8e6,0x7b8b4071,0x328627fc, - 0x083b044d,0x413663c0,0x9a21cb57,0xd32cacda,0x28cf87ce,0x61c2e043,0xbad548d4,0xf3d82f59, - 0x49d2034b,0x00df64c6,0xdbc8cc51,0x92c5abdc,0x692680c8,0x202be745,0xfb3c4fd2,0xb231285f, - 0x8be90a41,0xc2e46dcc,0x19f3c55b,0x50fea2d6,0xab1d89c2,0xe210ee4f,0x390746d8,0x700a2155, - 0xca000d47,0x830d6aca,0x581ac25d,0x1117a5d0,0xeaf48ec4,0xa3f9e949,0x78ee41de,0x31e32653}, - -{0x00000000,0x1b280d78,0x36501af0,0x2d781788,0x6ca035e0,0x77883898,0x5af02f10,0x41d82268, - 0xd9406bc0,0xc26866b8,0xef107130,0xf4387c48,0xb5e05e20,0xaec85358,0x83b044d0,0x989849a8, - 0xb641ca37,0xad69c74f,0x8011d0c7,0x9b39ddbf,0xdae1ffd7,0xc1c9f2af,0xecb1e527,0xf799e85f, - 0x6f01a1f7,0x7429ac8f,0x5951bb07,0x4279b67f,0x03a19417,0x1889996f,0x35f18ee7,0x2ed9839f, - 0x684289d9,0x736a84a1,0x5e129329,0x453a9e51,0x04e2bc39,0x1fcab141,0x32b2a6c9,0x299aabb1, - 0xb102e219,0xaa2aef61,0x8752f8e9,0x9c7af591,0xdda2d7f9,0xc68ada81,0xebf2cd09,0xf0dac071, - 0xde0343ee,0xc52b4e96,0xe853591e,0xf37b5466,0xb2a3760e,0xa98b7b76,0x84f36cfe,0x9fdb6186, - 0x0743282e,0x1c6b2556,0x311332de,0x2a3b3fa6,0x6be31dce,0x70cb10b6,0x5db3073e,0x469b0a46, - 0xd08513b2,0xcbad1eca,0xe6d50942,0xfdfd043a,0xbc252652,0xa70d2b2a,0x8a753ca2,0x915d31da, - 0x09c57872,0x12ed750a,0x3f956282,0x24bd6ffa,0x65654d92,0x7e4d40ea,0x53355762,0x481d5a1a, - 0x66c4d985,0x7decd4fd,0x5094c375,0x4bbcce0d,0x0a64ec65,0x114ce11d,0x3c34f695,0x271cfbed, - 0xbf84b245,0xa4acbf3d,0x89d4a8b5,0x92fca5cd,0xd32487a5,0xc80c8add,0xe5749d55,0xfe5c902d, - 0xb8c79a6b,0xa3ef9713,0x8e97809b,0x95bf8de3,0xd467af8b,0xcf4fa2f3,0xe237b57b,0xf91fb803, - 0x6187f1ab,0x7aaffcd3,0x57d7eb5b,0x4cffe623,0x0d27c44b,0x160fc933,0x3b77debb,0x205fd3c3, - 0x0e86505c,0x15ae5d24,0x38d64aac,0x23fe47d4,0x622665bc,0x790e68c4,0x54767f4c,0x4f5e7234, - 0xd7c63b9c,0xccee36e4,0xe196216c,0xfabe2c14,0xbb660e7c,0xa04e0304,0x8d36148c,0x961e19f4, - 0xa5cb3ad3,0xbee337ab,0x939b2023,0x88b32d5b,0xc96b0f33,0xd243024b,0xff3b15c3,0xe41318bb, - 0x7c8b5113,0x67a35c6b,0x4adb4be3,0x51f3469b,0x102b64f3,0x0b03698b,0x267b7e03,0x3d53737b, - 0x138af0e4,0x08a2fd9c,0x25daea14,0x3ef2e76c,0x7f2ac504,0x6402c87c,0x497adff4,0x5252d28c, - 0xcaca9b24,0xd1e2965c,0xfc9a81d4,0xe7b28cac,0xa66aaec4,0xbd42a3bc,0x903ab434,0x8b12b94c, - 0xcd89b30a,0xd6a1be72,0xfbd9a9fa,0xe0f1a482,0xa12986ea,0xba018b92,0x97799c1a,0x8c519162, - 0x14c9d8ca,0x0fe1d5b2,0x2299c23a,0x39b1cf42,0x7869ed2a,0x6341e052,0x4e39f7da,0x5511faa2, - 0x7bc8793d,0x60e07445,0x4d9863cd,0x56b06eb5,0x17684cdd,0x0c4041a5,0x2138562d,0x3a105b55, - 0xa28812fd,0xb9a01f85,0x94d8080d,0x8ff00575,0xce28271d,0xd5002a65,0xf8783ded,0xe3503095, - 0x754e2961,0x6e662419,0x431e3391,0x58363ee9,0x19ee1c81,0x02c611f9,0x2fbe0671,0x34960b09, - 0xac0e42a1,0xb7264fd9,0x9a5e5851,0x81765529,0xc0ae7741,0xdb867a39,0xf6fe6db1,0xedd660c9, - 0xc30fe356,0xd827ee2e,0xf55ff9a6,0xee77f4de,0xafafd6b6,0xb487dbce,0x99ffcc46,0x82d7c13e, - 0x1a4f8896,0x016785ee,0x2c1f9266,0x37379f1e,0x76efbd76,0x6dc7b00e,0x40bfa786,0x5b97aafe, - 0x1d0ca0b8,0x0624adc0,0x2b5cba48,0x3074b730,0x71ac9558,0x6a849820,0x47fc8fa8,0x5cd482d0, - 0xc44ccb78,0xdf64c600,0xf21cd188,0xe934dcf0,0xa8ecfe98,0xb3c4f3e0,0x9ebce468,0x8594e910, - 0xab4d6a8f,0xb06567f7,0x9d1d707f,0x86357d07,0xc7ed5f6f,0xdcc55217,0xf1bd459f,0xea9548e7, - 0x720d014f,0x69250c37,0x445d1bbf,0x5f7516c7,0x1ead34af,0x058539d7,0x28fd2e5f,0x33d52327}, - -{0x00000000,0x4f576811,0x9eaed022,0xd1f9b833,0x399cbdf3,0x76cbd5e2,0xa7326dd1,0xe86505c0, - 0x73397be6,0x3c6e13f7,0xed97abc4,0xa2c0c3d5,0x4aa5c615,0x05f2ae04,0xd40b1637,0x9b5c7e26, - 0xe672f7cc,0xa9259fdd,0x78dc27ee,0x378b4fff,0xdfee4a3f,0x90b9222e,0x41409a1d,0x0e17f20c, - 0x954b8c2a,0xda1ce43b,0x0be55c08,0x44b23419,0xacd731d9,0xe38059c8,0x3279e1fb,0x7d2e89ea, - 0xc824f22f,0x87739a3e,0x568a220d,0x19dd4a1c,0xf1b84fdc,0xbeef27cd,0x6f169ffe,0x2041f7ef, - 0xbb1d89c9,0xf44ae1d8,0x25b359eb,0x6ae431fa,0x8281343a,0xcdd65c2b,0x1c2fe418,0x53788c09, - 0x2e5605e3,0x61016df2,0xb0f8d5c1,0xffafbdd0,0x17cab810,0x589dd001,0x89646832,0xc6330023, - 0x5d6f7e05,0x12381614,0xc3c1ae27,0x8c96c636,0x64f3c3f6,0x2ba4abe7,0xfa5d13d4,0xb50a7bc5, - 0x9488f9e9,0xdbdf91f8,0x0a2629cb,0x457141da,0xad14441a,0xe2432c0b,0x33ba9438,0x7cedfc29, - 0xe7b1820f,0xa8e6ea1e,0x791f522d,0x36483a3c,0xde2d3ffc,0x917a57ed,0x4083efde,0x0fd487cf, - 0x72fa0e25,0x3dad6634,0xec54de07,0xa303b616,0x4b66b3d6,0x0431dbc7,0xd5c863f4,0x9a9f0be5, - 0x01c375c3,0x4e941dd2,0x9f6da5e1,0xd03acdf0,0x385fc830,0x7708a021,0xa6f11812,0xe9a67003, - 0x5cac0bc6,0x13fb63d7,0xc202dbe4,0x8d55b3f5,0x6530b635,0x2a67de24,0xfb9e6617,0xb4c90e06, - 0x2f957020,0x60c21831,0xb13ba002,0xfe6cc813,0x1609cdd3,0x595ea5c2,0x88a71df1,0xc7f075e0, - 0xbadefc0a,0xf589941b,0x24702c28,0x6b274439,0x834241f9,0xcc1529e8,0x1dec91db,0x52bbf9ca, - 0xc9e787ec,0x86b0effd,0x574957ce,0x181e3fdf,0xf07b3a1f,0xbf2c520e,0x6ed5ea3d,0x2182822c, - 0x2dd0ee65,0x62878674,0xb37e3e47,0xfc295656,0x144c5396,0x5b1b3b87,0x8ae283b4,0xc5b5eba5, - 0x5ee99583,0x11befd92,0xc04745a1,0x8f102db0,0x67752870,0x28224061,0xf9dbf852,0xb68c9043, - 0xcba219a9,0x84f571b8,0x550cc98b,0x1a5ba19a,0xf23ea45a,0xbd69cc4b,0x6c907478,0x23c71c69, - 0xb89b624f,0xf7cc0a5e,0x2635b26d,0x6962da7c,0x8107dfbc,0xce50b7ad,0x1fa90f9e,0x50fe678f, - 0xe5f41c4a,0xaaa3745b,0x7b5acc68,0x340da479,0xdc68a1b9,0x933fc9a8,0x42c6719b,0x0d91198a, - 0x96cd67ac,0xd99a0fbd,0x0863b78e,0x4734df9f,0xaf51da5f,0xe006b24e,0x31ff0a7d,0x7ea8626c, - 0x0386eb86,0x4cd18397,0x9d283ba4,0xd27f53b5,0x3a1a5675,0x754d3e64,0xa4b48657,0xebe3ee46, - 0x70bf9060,0x3fe8f871,0xee114042,0xa1462853,0x49232d93,0x06744582,0xd78dfdb1,0x98da95a0, - 0xb958178c,0xf60f7f9d,0x27f6c7ae,0x68a1afbf,0x80c4aa7f,0xcf93c26e,0x1e6a7a5d,0x513d124c, - 0xca616c6a,0x8536047b,0x54cfbc48,0x1b98d459,0xf3fdd199,0xbcaab988,0x6d5301bb,0x220469aa, - 0x5f2ae040,0x107d8851,0xc1843062,0x8ed35873,0x66b65db3,0x29e135a2,0xf8188d91,0xb74fe580, - 0x2c139ba6,0x6344f3b7,0xb2bd4b84,0xfdea2395,0x158f2655,0x5ad84e44,0x8b21f677,0xc4769e66, - 0x717ce5a3,0x3e2b8db2,0xefd23581,0xa0855d90,0x48e05850,0x07b73041,0xd64e8872,0x9919e063, - 0x02459e45,0x4d12f654,0x9ceb4e67,0xd3bc2676,0x3bd923b6,0x748e4ba7,0xa577f394,0xea209b85, - 0x970e126f,0xd8597a7e,0x09a0c24d,0x46f7aa5c,0xae92af9c,0xe1c5c78d,0x303c7fbe,0x7f6b17af, - 0xe4376989,0xab600198,0x7a99b9ab,0x35ced1ba,0xddabd47a,0x92fcbc6b,0x43050458,0x0c526c49}, - -{0x00000000,0x5ba1dcca,0xb743b994,0xece2655e,0x6a466e9f,0x31e7b255,0xdd05d70b,0x86a40bc1, - 0xd48cdd3e,0x8f2d01f4,0x63cf64aa,0x386eb860,0xbecab3a1,0xe56b6f6b,0x09890a35,0x5228d6ff, - 0xadd8a7cb,0xf6797b01,0x1a9b1e5f,0x413ac295,0xc79ec954,0x9c3f159e,0x70dd70c0,0x2b7cac0a, - 0x79547af5,0x22f5a63f,0xce17c361,0x95b61fab,0x1312146a,0x48b3c8a0,0xa451adfe,0xfff07134, - 0x5f705221,0x04d18eeb,0xe833ebb5,0xb392377f,0x35363cbe,0x6e97e074,0x8275852a,0xd9d459e0, - 0x8bfc8f1f,0xd05d53d5,0x3cbf368b,0x671eea41,0xe1bae180,0xba1b3d4a,0x56f95814,0x0d5884de, - 0xf2a8f5ea,0xa9092920,0x45eb4c7e,0x1e4a90b4,0x98ee9b75,0xc34f47bf,0x2fad22e1,0x740cfe2b, - 0x262428d4,0x7d85f41e,0x91679140,0xcac64d8a,0x4c62464b,0x17c39a81,0xfb21ffdf,0xa0802315, - 0xbee0a442,0xe5417888,0x09a31dd6,0x5202c11c,0xd4a6cadd,0x8f071617,0x63e57349,0x3844af83, - 0x6a6c797c,0x31cda5b6,0xdd2fc0e8,0x868e1c22,0x002a17e3,0x5b8bcb29,0xb769ae77,0xecc872bd, - 0x13380389,0x4899df43,0xa47bba1d,0xffda66d7,0x797e6d16,0x22dfb1dc,0xce3dd482,0x959c0848, - 0xc7b4deb7,0x9c15027d,0x70f76723,0x2b56bbe9,0xadf2b028,0xf6536ce2,0x1ab109bc,0x4110d576, - 0xe190f663,0xba312aa9,0x56d34ff7,0x0d72933d,0x8bd698fc,0xd0774436,0x3c952168,0x6734fda2, - 0x351c2b5d,0x6ebdf797,0x825f92c9,0xd9fe4e03,0x5f5a45c2,0x04fb9908,0xe819fc56,0xb3b8209c, - 0x4c4851a8,0x17e98d62,0xfb0be83c,0xa0aa34f6,0x260e3f37,0x7dafe3fd,0x914d86a3,0xcaec5a69, - 0x98c48c96,0xc365505c,0x2f873502,0x7426e9c8,0xf282e209,0xa9233ec3,0x45c15b9d,0x1e608757, - 0x79005533,0x22a189f9,0xce43eca7,0x95e2306d,0x13463bac,0x48e7e766,0xa4058238,0xffa45ef2, - 0xad8c880d,0xf62d54c7,0x1acf3199,0x416eed53,0xc7cae692,0x9c6b3a58,0x70895f06,0x2b2883cc, - 0xd4d8f2f8,0x8f792e32,0x639b4b6c,0x383a97a6,0xbe9e9c67,0xe53f40ad,0x09dd25f3,0x527cf939, - 0x00542fc6,0x5bf5f30c,0xb7179652,0xecb64a98,0x6a124159,0x31b39d93,0xdd51f8cd,0x86f02407, - 0x26700712,0x7dd1dbd8,0x9133be86,0xca92624c,0x4c36698d,0x1797b547,0xfb75d019,0xa0d40cd3, - 0xf2fcda2c,0xa95d06e6,0x45bf63b8,0x1e1ebf72,0x98bab4b3,0xc31b6879,0x2ff90d27,0x7458d1ed, - 0x8ba8a0d9,0xd0097c13,0x3ceb194d,0x674ac587,0xe1eece46,0xba4f128c,0x56ad77d2,0x0d0cab18, - 0x5f247de7,0x0485a12d,0xe867c473,0xb3c618b9,0x35621378,0x6ec3cfb2,0x8221aaec,0xd9807626, - 0xc7e0f171,0x9c412dbb,0x70a348e5,0x2b02942f,0xada69fee,0xf6074324,0x1ae5267a,0x4144fab0, - 0x136c2c4f,0x48cdf085,0xa42f95db,0xff8e4911,0x792a42d0,0x228b9e1a,0xce69fb44,0x95c8278e, - 0x6a3856ba,0x31998a70,0xdd7bef2e,0x86da33e4,0x007e3825,0x5bdfe4ef,0xb73d81b1,0xec9c5d7b, - 0xbeb48b84,0xe515574e,0x09f73210,0x5256eeda,0xd4f2e51b,0x8f5339d1,0x63b15c8f,0x38108045, - 0x9890a350,0xc3317f9a,0x2fd31ac4,0x7472c60e,0xf2d6cdcf,0xa9771105,0x4595745b,0x1e34a891, - 0x4c1c7e6e,0x17bda2a4,0xfb5fc7fa,0xa0fe1b30,0x265a10f1,0x7dfbcc3b,0x9119a965,0xcab875af, - 0x3548049b,0x6ee9d851,0x820bbd0f,0xd9aa61c5,0x5f0e6a04,0x04afb6ce,0xe84dd390,0xb3ec0f5a, - 0xe1c4d9a5,0xba65056f,0x56876031,0x0d26bcfb,0x8b82b73a,0xd0236bf0,0x3cc10eae,0x6760d264}}; - -/* init the encode/decode logical stream state */ - -int ogg_stream_init(ogg_stream_state *os,int serialno){ - if(os){ - memset(os,0,sizeof(*os)); - os->body_storage=16*1024; - os->lacing_storage=1024; - - os->body_data=(unsigned char *)_ogg_malloc(os->body_storage*sizeof(*os->body_data)); - os->lacing_vals=(int *)_ogg_malloc(os->lacing_storage*sizeof(*os->lacing_vals)); - os->granule_vals=(ogg_int64_t *)_ogg_malloc(os->lacing_storage*sizeof(*os->granule_vals)); - - if(!os->body_data || !os->lacing_vals || !os->granule_vals){ - ogg_stream_clear(os); - return -1; - } - - os->serialno=serialno; - - return(0); - } - return(-1); -} - -/* async/delayed error detection for the ogg_stream_state */ -int ogg_stream_check(ogg_stream_state *os){ - if(!os || !os->body_data) return -1; - return 0; -} - -/* _clear does not free os, only the non-flat storage within */ -int ogg_stream_clear(ogg_stream_state *os){ - if(os){ - if(os->body_data)_ogg_free(os->body_data); - if(os->lacing_vals)_ogg_free(os->lacing_vals); - if(os->granule_vals)_ogg_free(os->granule_vals); - - memset(os,0,sizeof(*os)); - } - return(0); -} - -int ogg_stream_destroy(ogg_stream_state *os){ - if(os){ - ogg_stream_clear(os); - _ogg_free(os); - } - return(0); -} - -/* Helpers for ogg_stream_encode; this keeps the structure and - what's happening fairly clear */ - -static int _os_body_expand(ogg_stream_state *os,long needed){ - if(os->body_storage-needed<=os->body_fill){ - long body_storage; - void *ret; - if(os->body_storage>LONG_MAX-needed){ - ogg_stream_clear(os); - return -1; - } - body_storage=os->body_storage+needed; - if(body_storagebody_data,body_storage*sizeof(*os->body_data)); - if(!ret){ - ogg_stream_clear(os); - return -1; - } - os->body_storage=body_storage; - os->body_data=(unsigned char *)ret; - } - return 0; -} - -static int _os_lacing_expand(ogg_stream_state *os,long needed){ - if(os->lacing_storage-needed<=os->lacing_fill){ - long lacing_storage; - void *ret; - if(os->lacing_storage>LONG_MAX-needed){ - ogg_stream_clear(os); - return -1; - } - lacing_storage=os->lacing_storage+needed; - if(lacing_storagelacing_vals,lacing_storage*sizeof(*os->lacing_vals)); - if(!ret){ - ogg_stream_clear(os); - return -1; - } - os->lacing_vals=(int *)ret; - ret=_ogg_realloc(os->granule_vals,lacing_storage* - sizeof(*os->granule_vals)); - if(!ret){ - ogg_stream_clear(os); - return -1; - } - os->granule_vals=(ogg_int64_t *)ret; - os->lacing_storage=lacing_storage; - } - return 0; -} - -/* checksum the page */ -/* Direct table CRC; note that this will be faster in the future if we - perform the checksum simultaneously with other copies */ - -static ogg_uint32_t _os_update_crc(ogg_uint32_t crc, unsigned char *buffer, int size){ - while (size>=8){ - crc^=((ogg_uint32_t)buffer[0]<<24)|((ogg_uint32_t)buffer[1]<<16)|((ogg_uint32_t)buffer[2]<<8)|((ogg_uint32_t)buffer[3]); - - crc=crc_lookup[7][ crc>>24 ]^crc_lookup[6][(crc>>16)&0xFF]^ - crc_lookup[5][(crc>> 8)&0xFF]^crc_lookup[4][ crc &0xFF]^ - crc_lookup[3][buffer[4] ]^crc_lookup[2][buffer[5] ]^ - crc_lookup[1][buffer[6] ]^crc_lookup[0][buffer[7] ]; - - buffer+=8; - size-=8; - } - - while (size--) - crc=(crc<<8)^crc_lookup[0][((crc >> 24)&0xff)^*buffer++]; - return crc; -} - -void ogg_page_checksum_set(ogg_page *og){ - if(og){ - ogg_uint32_t crc_reg=0; - - /* safety; needed for API behavior, but not framing code */ - og->header[22]=0; - og->header[23]=0; - og->header[24]=0; - og->header[25]=0; - - crc_reg=_os_update_crc(crc_reg,og->header,og->header_len); - crc_reg=_os_update_crc(crc_reg,og->body,og->body_len); - - og->header[22]=(unsigned char)(crc_reg&0xff); - og->header[23]=(unsigned char)((crc_reg>>8)&0xff); - og->header[24]=(unsigned char)((crc_reg>>16)&0xff); - og->header[25]=(unsigned char)((crc_reg>>24)&0xff); - } -} - -/* submit data to the internal buffer of the framing engine */ -int ogg_stream_iovecin(ogg_stream_state *os, ogg_iovec_t *iov, int count, - long e_o_s, ogg_int64_t granulepos){ - - long bytes = 0, lacing_vals; - int i; - - if(ogg_stream_check(os)) return -1; - if(!iov) return 0; - - for (i = 0; i < count; ++i){ - if(iov[i].iov_len>LONG_MAX) return -1; - if(bytes>LONG_MAX-(long)iov[i].iov_len) return -1; - bytes += (long)iov[i].iov_len; - } - lacing_vals=bytes/255+1; - - if(os->body_returned){ - /* advance packet data according to the body_returned pointer. We - had to keep it around to return a pointer into the buffer last - call */ - - os->body_fill-=os->body_returned; - if(os->body_fill) - memmove(os->body_data,os->body_data+os->body_returned, - os->body_fill); - os->body_returned=0; - } - - /* make sure we have the buffer storage */ - if(_os_body_expand(os,bytes) || _os_lacing_expand(os,lacing_vals)) - return -1; - - /* Copy in the submitted packet. Yes, the copy is a waste; this is - the liability of overly clean abstraction for the time being. It - will actually be fairly easy to eliminate the extra copy in the - future */ - - for (i = 0; i < count; ++i) { - memcpy(os->body_data+os->body_fill, iov[i].iov_base, iov[i].iov_len); - os->body_fill += (int)iov[i].iov_len; - } - - /* Store lacing vals for this packet */ - for(i=0;ilacing_vals[os->lacing_fill+i]=255; - os->granule_vals[os->lacing_fill+i]=os->granulepos; - } - os->lacing_vals[os->lacing_fill+i]=bytes%255; - os->granulepos=os->granule_vals[os->lacing_fill+i]=granulepos; - - /* flag the first segment as the beginning of the packet */ - os->lacing_vals[os->lacing_fill]|= 0x100; - - os->lacing_fill+=lacing_vals; - - /* for the sake of completeness */ - os->packetno++; - - if(e_o_s)os->e_o_s=1; - - return(0); -} - -int ogg_stream_packetin(ogg_stream_state *os,ogg_packet *op){ - ogg_iovec_t iov; - iov.iov_base = op->packet; - iov.iov_len = op->bytes; - return ogg_stream_iovecin(os, &iov, 1, op->e_o_s, op->granulepos); -} - -/* Conditionally flush a page; force==0 will only flush nominal-size - pages, force==1 forces us to flush a page regardless of page size - so long as there's any data available at all. */ -static int ogg_stream_flush_i(ogg_stream_state *os,ogg_page *og, int force, int nfill){ - int i; - int vals=0; - int maxvals=(os->lacing_fill>255?255:os->lacing_fill); - int bytes=0; - long acc=0; - ogg_int64_t granule_pos=-1; - - if(ogg_stream_check(os)) return(0); - if(maxvals==0) return(0); - - /* construct a page */ - /* decide how many segments to include */ - - /* If this is the initial header case, the first page must only include - the initial header packet */ - if(os->b_o_s==0){ /* 'initial header page' case */ - granule_pos=0; - for(vals=0;valslacing_vals[vals]&0x0ff)<255){ - vals++; - break; - } - } - }else{ - - /* The extra packets_done, packet_just_done logic here attempts to do two things: - 1) Don't unnecessarily span pages. - 2) Unless necessary, don't flush pages if there are less than four packets on - them; this expands page size to reduce unnecessary overhead if incoming packets - are large. - These are not necessary behaviors, just 'always better than naive flushing' - without requiring an application to explicitly request a specific optimized - behavior. We'll want an explicit behavior setup pathway eventually as well. */ - - int packets_done=0; - int packet_just_done=0; - for(vals=0;valsnfill && packet_just_done>=4){ - force=1; - break; - } - acc+=os->lacing_vals[vals]&0x0ff; - if((os->lacing_vals[vals]&0xff)<255){ - granule_pos=os->granule_vals[vals]; - packet_just_done=++packets_done; - }else - packet_just_done=0; - } - if(vals==255)force=1; - } - - if(!force) return(0); - - /* construct the header in temp storage */ - memcpy(os->header,"OggS",4); - - /* stream structure version */ - os->header[4]=0x00; - - /* continued packet flag? */ - os->header[5]=0x00; - if((os->lacing_vals[0]&0x100)==0)os->header[5]|=0x01; - /* first page flag? */ - if(os->b_o_s==0)os->header[5]|=0x02; - /* last page flag? */ - if(os->e_o_s && os->lacing_fill==vals)os->header[5]|=0x04; - os->b_o_s=1; - - /* 64 bits of PCM position */ - for(i=6;i<14;i++){ - os->header[i]=(unsigned char)(granule_pos&0xff); - granule_pos>>=8; - } - - /* 32 bits of stream serial number */ - { - long serialno=os->serialno; - for(i=14;i<18;i++){ - os->header[i]=(unsigned char)(serialno&0xff); - serialno>>=8; - } - } - - /* 32 bits of page counter (we have both counter and page header - because this val can roll over) */ - if(os->pageno==-1)os->pageno=0; /* because someone called - stream_reset; this would be a - strange thing to do in an - encode stream, but it has - plausible uses */ - { - long pageno=os->pageno++; - for(i=18;i<22;i++){ - os->header[i]=(unsigned char)(pageno&0xff); - pageno>>=8; - } - } - - /* zero for computation; filled in later */ - os->header[22]=0; - os->header[23]=0; - os->header[24]=0; - os->header[25]=0; - - /* segment table */ - os->header[26]=(unsigned char)(vals&0xff); - for(i=0;iheader[i+27]=(unsigned char)(os->lacing_vals[i]&0xff); - - /* set pointers in the ogg_page struct */ - og->header=os->header; - og->header_len=os->header_fill=vals+27; - og->body=os->body_data+os->body_returned; - og->body_len=bytes; - - /* advance the lacing data and set the body_returned pointer */ - - os->lacing_fill-=vals; - memmove(os->lacing_vals,os->lacing_vals+vals,os->lacing_fill*sizeof(*os->lacing_vals)); - memmove(os->granule_vals,os->granule_vals+vals,os->lacing_fill*sizeof(*os->granule_vals)); - os->body_returned+=bytes; - - /* calculate the checksum */ - - ogg_page_checksum_set(og); - - /* done */ - return(1); -} - -/* This will flush remaining packets into a page (returning nonzero), - even if there is not enough data to trigger a flush normally - (undersized page). If there are no packets or partial packets to - flush, ogg_stream_flush returns 0. Note that ogg_stream_flush will - try to flush a normal sized page like ogg_stream_pageout; a call to - ogg_stream_flush does not guarantee that all packets have flushed. - Only a return value of 0 from ogg_stream_flush indicates all packet - data is flushed into pages. - - since ogg_stream_flush will flush the last page in a stream even if - it's undersized, you almost certainly want to use ogg_stream_pageout - (and *not* ogg_stream_flush) unless you specifically need to flush - a page regardless of size in the middle of a stream. */ - -int ogg_stream_flush(ogg_stream_state *os,ogg_page *og){ - return ogg_stream_flush_i(os,og,1,4096); -} - -/* Like the above, but an argument is provided to adjust the nominal - page size for applications which are smart enough to provide their - own delay based flushing */ - -int ogg_stream_flush_fill(ogg_stream_state *os,ogg_page *og, int nfill){ - return ogg_stream_flush_i(os,og,1,nfill); -} - -/* This constructs pages from buffered packet segments. The pointers -returned are to static buffers; do not free. The returned buffers are -good only until the next call (using the same ogg_stream_state) */ - -int ogg_stream_pageout(ogg_stream_state *os, ogg_page *og){ - int force=0; - if(ogg_stream_check(os)) return 0; - - if((os->e_o_s&&os->lacing_fill) || /* 'were done, now flush' case */ - (os->lacing_fill&&!os->b_o_s)) /* 'initial header page' case */ - force=1; - - return(ogg_stream_flush_i(os,og,force,4096)); -} - -/* Like the above, but an argument is provided to adjust the nominal -page size for applications which are smart enough to provide their -own delay based flushing */ - -int ogg_stream_pageout_fill(ogg_stream_state *os, ogg_page *og, int nfill){ - int force=0; - if(ogg_stream_check(os)) return 0; - - if((os->e_o_s&&os->lacing_fill) || /* 'were done, now flush' case */ - (os->lacing_fill&&!os->b_o_s)) /* 'initial header page' case */ - force=1; - - return(ogg_stream_flush_i(os,og,force,nfill)); -} - -int ogg_stream_eos(ogg_stream_state *os){ - if(ogg_stream_check(os)) return 1; - return os->e_o_s; -} - -/* DECODING PRIMITIVES: packet streaming layer **********************/ - -/* This has two layers to place more of the multi-serialno and paging - control in the application's hands. First, we expose a data buffer - using ogg_sync_buffer(). The app either copies into the - buffer, or passes it directly to read(), etc. We then call - ogg_sync_wrote() to tell how many bytes we just added. - - Pages are returned (pointers into the buffer in ogg_sync_state) - by ogg_sync_pageout(). The page is then submitted to - ogg_stream_pagein() along with the appropriate - ogg_stream_state* (ie, matching serialno). We then get raw - packets out calling ogg_stream_packetout() with a - ogg_stream_state. */ - -/* initialize the struct to a known state */ -int ogg_sync_init(ogg_sync_state *oy){ - if(oy){ - oy->storage = -1; /* used as a readiness flag */ - memset(oy,0,sizeof(*oy)); - } - return(0); -} - -/* clear non-flat storage within */ -int ogg_sync_clear(ogg_sync_state *oy){ - if(oy){ - if(oy->data)_ogg_free(oy->data); - memset(oy,0,sizeof(*oy)); - } - return(0); -} - -int ogg_sync_destroy(ogg_sync_state *oy){ - if(oy){ - ogg_sync_clear(oy); - _ogg_free(oy); - } - return(0); -} - -int ogg_sync_check(ogg_sync_state *oy){ - if(oy->storage<0) return -1; - return 0; -} - -char *ogg_sync_buffer(ogg_sync_state *oy, long size){ - if(ogg_sync_check(oy)) return NULL; - - /* first, clear out any space that has been previously returned */ - if(oy->returned){ - oy->fill-=oy->returned; - if(oy->fill>0) - memmove(oy->data,oy->data+oy->returned,oy->fill); - oy->returned=0; - } - - if(size>oy->storage-oy->fill){ - /* We need to extend the internal buffer */ - long newsize=size+oy->fill+4096; /* an extra page to be nice */ - void *ret; - - if(oy->data) - ret=_ogg_realloc(oy->data,newsize); - else - ret=_ogg_malloc(newsize); - if(!ret){ - ogg_sync_clear(oy); - return NULL; - } - oy->data=(unsigned char *)ret; - oy->storage=newsize; - } - - /* expose a segment at least as large as requested at the fill mark */ - return((char *)oy->data+oy->fill); -} - -int ogg_sync_wrote(ogg_sync_state *oy, long bytes){ - if(ogg_sync_check(oy))return -1; - if(oy->fill+bytes>oy->storage)return -1; - oy->fill+=bytes; - return(0); -} - -/* sync the stream. This is meant to be useful for finding page - boundaries. - - return values for this: - -n) skipped n bytes - 0) page not ready; more data (no bytes skipped) - n) page synced at current location; page length n bytes - -*/ - -long ogg_sync_pageseek(ogg_sync_state *oy,ogg_page *og){ - unsigned char *page=oy->data+oy->returned; - unsigned char *next; - long bytes=oy->fill-oy->returned; - - if(ogg_sync_check(oy))return 0; - - if(oy->headerbytes==0){ - int headerbytes,i; - if(bytes<27)return(0); /* not enough for a header */ - - /* verify capture pattern */ - if(memcmp(page,"OggS",4))goto sync_fail; - - headerbytes=page[26]+27; - if(bytesbodybytes+=page[27+i]; - oy->headerbytes=headerbytes; - } - - if(oy->bodybytes+oy->headerbytes>bytes)return(0); - - /* The whole test page is buffered. Verify the checksum */ - { - /* Grab the checksum bytes, set the header field to zero */ - char chksum[4]; - ogg_page log; - - memcpy(chksum,page+22,4); - memset(page+22,0,4); - - /* set up a temp page struct and recompute the checksum */ - log.header=page; - log.header_len=oy->headerbytes; - log.body=page+oy->headerbytes; - log.body_len=oy->bodybytes; - ogg_page_checksum_set(&log); - - /* Compare */ - if(memcmp(chksum,page+22,4)){ - /* D'oh. Mismatch! Corrupt page (or miscapture and not a page - at all) */ - /* replace the computed checksum with the one actually read in */ - memcpy(page+22,chksum,4); - -#ifndef DISABLE_CRC - /* Bad checksum. Lose sync */ - goto sync_fail; -#endif - } - } - - /* yes, have a whole page all ready to go */ - { - if(og){ - og->header=page; - og->header_len=oy->headerbytes; - og->body=page+oy->headerbytes; - og->body_len=oy->bodybytes; - } - - oy->unsynced=0; - oy->returned+=(bytes=oy->headerbytes+oy->bodybytes); - oy->headerbytes=0; - oy->bodybytes=0; - return(bytes); - } - - sync_fail: - - oy->headerbytes=0; - oy->bodybytes=0; - - /* search for possible capture */ - next=(unsigned char *)memchr(page+1,'O',bytes-1); - if(!next) - next=oy->data+oy->fill; - - oy->returned=(int)(next-oy->data); - return((long)-(next-page)); -} - -/* sync the stream and get a page. Keep trying until we find a page. - Suppress 'sync errors' after reporting the first. - - return values: - -1) recapture (hole in data) - 0) need more data - 1) page returned - - Returns pointers into buffered data; invalidated by next call to - _stream, _clear, _init, or _buffer */ - -int ogg_sync_pageout(ogg_sync_state *oy, ogg_page *og){ - - if(ogg_sync_check(oy))return 0; - - /* all we need to do is verify a page at the head of the stream - buffer. If it doesn't verify, we look for the next potential - frame */ - - for(;;){ - long ret=ogg_sync_pageseek(oy,og); - if(ret>0){ - /* have a page */ - return(1); - } - if(ret==0){ - /* need more data */ - return(0); - } - - /* head did not start a synced page... skipped some bytes */ - if(!oy->unsynced){ - oy->unsynced=1; - return(-1); - } - - /* loop. keep looking */ - - } -} - -/* add the incoming page to the stream state; we decompose the page - into packet segments here as well. */ - -int ogg_stream_pagein(ogg_stream_state *os, ogg_page *og){ - unsigned char *header=og->header; - unsigned char *body=og->body; - long bodysize=og->body_len; - int segptr=0; - - int version=ogg_page_version(og); - int continued=ogg_page_continued(og); - int bos=ogg_page_bos(og); - int eos=ogg_page_eos(og); - ogg_int64_t granulepos=ogg_page_granulepos(og); - int serialno=ogg_page_serialno(og); - long pageno=ogg_page_pageno(og); - int segments=header[26]; - - if(ogg_stream_check(os)) return -1; - - /* clean up 'returned data' */ - { - long lr=os->lacing_returned; - long br=os->body_returned; - - /* body data */ - if(br){ - os->body_fill-=br; - if(os->body_fill) - memmove(os->body_data,os->body_data+br,os->body_fill); - os->body_returned=0; - } - - if(lr){ - /* segment table */ - if(os->lacing_fill-lr){ - memmove(os->lacing_vals,os->lacing_vals+lr, - (os->lacing_fill-lr)*sizeof(*os->lacing_vals)); - memmove(os->granule_vals,os->granule_vals+lr, - (os->lacing_fill-lr)*sizeof(*os->granule_vals)); - } - os->lacing_fill-=lr; - os->lacing_packet-=lr; - os->lacing_returned=0; - } - } - - /* check the serial number */ - if(serialno!=os->serialno)return(-1); - if(version>0)return(-1); - - if(_os_lacing_expand(os,segments+1)) return -1; - - /* are we in sequence? */ - if(pageno!=os->pageno){ - int i; - - /* unroll previous partial packet (if any) */ - for(i=os->lacing_packet;ilacing_fill;i++) - os->body_fill-=os->lacing_vals[i]&0xff; - os->lacing_fill=os->lacing_packet; - - /* make a note of dropped data in segment table */ - if(os->pageno!=-1){ - os->lacing_vals[os->lacing_fill++]=0x400; - os->lacing_packet++; - } - } - - /* are we a 'continued packet' page? If so, we may need to skip - some segments */ - if(continued){ - if(os->lacing_fill<1 || - (os->lacing_vals[os->lacing_fill-1]&0xff)<255 || - os->lacing_vals[os->lacing_fill-1]==0x400){ - bos=0; - for(;segptrbody_data+os->body_fill,body,bodysize); - os->body_fill+=bodysize; - } - - { - int saved=-1; - while(segptrlacing_vals[os->lacing_fill]=val; - os->granule_vals[os->lacing_fill]=-1; - - if(bos){ - os->lacing_vals[os->lacing_fill]|=0x100; - bos=0; - } - - if(val<255)saved=os->lacing_fill; - - os->lacing_fill++; - segptr++; - - if(val<255)os->lacing_packet=os->lacing_fill; - } - - /* set the granulepos on the last granuleval of the last full packet */ - if(saved!=-1){ - os->granule_vals[saved]=granulepos; - } - - } - - if(eos){ - os->e_o_s=1; - if(os->lacing_fill>0) - os->lacing_vals[os->lacing_fill-1]|=0x200; - } - - os->pageno=pageno+1; - - return(0); -} - -/* clear things to an initial state. Good to call, eg, before seeking */ -int ogg_sync_reset(ogg_sync_state *oy){ - if(ogg_sync_check(oy))return -1; - - oy->fill=0; - oy->returned=0; - oy->unsynced=0; - oy->headerbytes=0; - oy->bodybytes=0; - return(0); -} - -int ogg_stream_reset(ogg_stream_state *os){ - if(ogg_stream_check(os)) return -1; - - os->body_fill=0; - os->body_returned=0; - - os->lacing_fill=0; - os->lacing_packet=0; - os->lacing_returned=0; - - os->header_fill=0; - - os->e_o_s=0; - os->b_o_s=0; - os->pageno=-1; - os->packetno=0; - os->granulepos=0; - - return(0); -} - -int ogg_stream_reset_serialno(ogg_stream_state *os,int serialno){ - if(ogg_stream_check(os)) return -1; - ogg_stream_reset(os); - os->serialno=serialno; - return(0); -} - -static int _packetout(ogg_stream_state *os,ogg_packet *op,int adv){ - - /* The last part of decode. We have the stream broken into packet - segments. Now we need to group them into packets (or return the - out of sync markers) */ - - int ptr=os->lacing_returned; - - if(os->lacing_packet<=ptr)return(0); - - if(os->lacing_vals[ptr]&0x400){ - /* we need to tell the codec there's a gap; it might need to - handle previous packet dependencies. */ - os->lacing_returned++; - os->packetno++; - return(-1); - } - - if(!op && !adv)return(1); /* just using peek as an inexpensive way - to ask if there's a whole packet - waiting */ - - /* Gather the whole packet. We'll have no holes or a partial packet */ - { - int size=os->lacing_vals[ptr]&0xff; - long bytes=size; - int eos=os->lacing_vals[ptr]&0x200; /* last packet of the stream? */ - int bos=os->lacing_vals[ptr]&0x100; /* first packet of the stream? */ - - while(size==255){ - int val=os->lacing_vals[++ptr]; - size=val&0xff; - if(val&0x200)eos=0x200; - bytes+=size; - } - - if(op){ - op->e_o_s=eos; - op->b_o_s=bos; - op->packet=os->body_data+os->body_returned; - op->packetno=os->packetno; - op->granulepos=os->granule_vals[ptr]; - op->bytes=bytes; - } - - if(adv){ - os->body_returned+=bytes; - os->lacing_returned=ptr+1; - os->packetno++; - } - } - return(1); -} - -int ogg_stream_packetout(ogg_stream_state *os,ogg_packet *op){ - if(ogg_stream_check(os)) return 0; - return _packetout(os,op,1); -} - -int ogg_stream_packetpeek(ogg_stream_state *os,ogg_packet *op){ - if(ogg_stream_check(os)) return 0; - return _packetout(os,op,0); -} - -void ogg_packet_clear(ogg_packet *op) { - _ogg_free(op->packet); - memset(op, 0, sizeof(*op)); -} - -#ifdef _V_SELFTEST -#include - -ogg_stream_state os_en, os_de; -ogg_sync_state oy; - -void checkpacket(ogg_packet *op,long len, int no, long pos){ - long j; - static int sequence=0; - static int lastno=0; - - if(op->bytes!=len){ - fprintf(stderr,"incorrect packet length (%ld != %ld)!\n",op->bytes,len); - exit(1); - } - if(op->granulepos!=pos){ - fprintf(stderr,"incorrect packet granpos (%ld != %ld)!\n",(long)op->granulepos,pos); - exit(1); - } - - /* packet number just follows sequence/gap; adjust the input number - for that */ - if(no==0){ - sequence=0; - }else{ - sequence++; - if(no>lastno+1) - sequence++; - } - lastno=no; - if(op->packetno!=sequence){ - fprintf(stderr,"incorrect packet sequence %ld != %d\n", - (long)(op->packetno),sequence); - exit(1); - } - - /* Test data */ - for(j=0;jbytes;j++) - if(op->packet[j]!=((j+no)&0xff)){ - fprintf(stderr,"body data mismatch (1) at pos %ld: %x!=%lx!\n\n", - j,op->packet[j],(j+no)&0xff); - exit(1); - } -} - -void check_page(unsigned char *data,const int *header,ogg_page *og){ - long j; - /* Test data */ - for(j=0;jbody_len;j++) - if(og->body[j]!=data[j]){ - fprintf(stderr,"body data mismatch (2) at pos %ld: %x!=%x!\n\n", - j,data[j],og->body[j]); - exit(1); - } - - /* Test header */ - for(j=0;jheader_len;j++){ - if(og->header[j]!=header[j]){ - fprintf(stderr,"header content mismatch at pos %ld:\n",j); - for(j=0;jheader[j]); - fprintf(stderr,"\n"); - exit(1); - } - } - if(og->header_len!=header[26]+27){ - fprintf(stderr,"header length incorrect! (%ld!=%d)\n", - og->header_len,header[26]+27); - exit(1); - } -} - -void print_header(ogg_page *og){ - int j; - fprintf(stderr,"\nHEADER:\n"); - fprintf(stderr," capture: %c %c %c %c version: %d flags: %x\n", - og->header[0],og->header[1],og->header[2],og->header[3], - (int)og->header[4],(int)og->header[5]); - - fprintf(stderr," granulepos: %d serialno: %d pageno: %ld\n", - (og->header[9]<<24)|(og->header[8]<<16)| - (og->header[7]<<8)|og->header[6], - (og->header[17]<<24)|(og->header[16]<<16)| - (og->header[15]<<8)|og->header[14], - ((long)(og->header[21])<<24)|(og->header[20]<<16)| - (og->header[19]<<8)|og->header[18]); - - fprintf(stderr," checksum: %02x:%02x:%02x:%02x\n segments: %d (", - (int)og->header[22],(int)og->header[23], - (int)og->header[24],(int)og->header[25], - (int)og->header[26]); - - for(j=27;jheader_len;j++) - fprintf(stderr,"%d ",(int)og->header[j]); - fprintf(stderr,")\n\n"); -} - -void copy_page(ogg_page *og){ - unsigned char *temp=_ogg_malloc(og->header_len); - memcpy(temp,og->header,og->header_len); - og->header=temp; - - temp=_ogg_malloc(og->body_len); - memcpy(temp,og->body,og->body_len); - og->body=temp; -} - -void free_page(ogg_page *og){ - _ogg_free (og->header); - _ogg_free (og->body); -} - -void error(void){ - fprintf(stderr,"error!\n"); - exit(1); -} - -/* 17 only */ -const int head1_0[] = {0x4f,0x67,0x67,0x53,0,0x06, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,0,0,0,0, - 0x15,0xed,0xec,0x91, - 1, - 17}; - -/* 17, 254, 255, 256, 500, 510, 600 byte, pad */ -const int head1_1[] = {0x4f,0x67,0x67,0x53,0,0x02, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,0,0,0,0, - 0x59,0x10,0x6c,0x2c, - 1, - 17}; -const int head2_1[] = {0x4f,0x67,0x67,0x53,0,0x04, - 0x07,0x18,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,1,0,0,0, - 0x89,0x33,0x85,0xce, - 13, - 254,255,0,255,1,255,245,255,255,0, - 255,255,90}; - -/* nil packets; beginning,middle,end */ -const int head1_2[] = {0x4f,0x67,0x67,0x53,0,0x02, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,0,0,0,0, - 0xff,0x7b,0x23,0x17, - 1, - 0}; -const int head2_2[] = {0x4f,0x67,0x67,0x53,0,0x04, - 0x07,0x28,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,1,0,0,0, - 0x5c,0x3f,0x66,0xcb, - 17, - 17,254,255,0,0,255,1,0,255,245,255,255,0, - 255,255,90,0}; - -/* large initial packet */ -const int head1_3[] = {0x4f,0x67,0x67,0x53,0,0x02, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,0,0,0,0, - 0x01,0x27,0x31,0xaa, - 18, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,10}; - -const int head2_3[] = {0x4f,0x67,0x67,0x53,0,0x04, - 0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,1,0,0,0, - 0x7f,0x4e,0x8a,0xd2, - 4, - 255,4,255,0}; - - -/* continuing packet test */ -const int head1_4[] = {0x4f,0x67,0x67,0x53,0,0x02, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,0,0,0,0, - 0xff,0x7b,0x23,0x17, - 1, - 0}; - -const int head2_4[] = {0x4f,0x67,0x67,0x53,0,0x00, - 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, - 0x01,0x02,0x03,0x04,1,0,0,0, - 0xf8,0x3c,0x19,0x79, - 255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255}; - -const int head3_4[] = {0x4f,0x67,0x67,0x53,0,0x05, - 0x07,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,2,0,0,0, - 0x38,0xe6,0xb6,0x28, - 6, - 255,220,255,4,255,0}; - - -/* spill expansion test */ -const int head1_4b[] = {0x4f,0x67,0x67,0x53,0,0x02, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,0,0,0,0, - 0xff,0x7b,0x23,0x17, - 1, - 0}; - -const int head2_4b[] = {0x4f,0x67,0x67,0x53,0,0x00, - 0x07,0x10,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,1,0,0,0, - 0xce,0x8f,0x17,0x1a, - 23, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255,10,255,4,255,0,0}; - - -const int head3_4b[] = {0x4f,0x67,0x67,0x53,0,0x04, - 0x07,0x14,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,2,0,0,0, - 0x9b,0xb2,0x50,0xa1, - 1, - 0}; - -/* page with the 255 segment limit */ -const int head1_5[] = {0x4f,0x67,0x67,0x53,0,0x02, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,0,0,0,0, - 0xff,0x7b,0x23,0x17, - 1, - 0}; - -const int head2_5[] = {0x4f,0x67,0x67,0x53,0,0x00, - 0x07,0xfc,0x03,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,1,0,0,0, - 0xed,0x2a,0x2e,0xa7, - 255, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10}; - -const int head3_5[] = {0x4f,0x67,0x67,0x53,0,0x04, - 0x07,0x00,0x04,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,2,0,0,0, - 0x6c,0x3b,0x82,0x3d, - 1, - 50}; - - -/* packet that overspans over an entire page */ -const int head1_6[] = {0x4f,0x67,0x67,0x53,0,0x02, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,0,0,0,0, - 0xff,0x7b,0x23,0x17, - 1, - 0}; - -const int head2_6[] = {0x4f,0x67,0x67,0x53,0,0x00, - 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,1,0,0,0, - 0x68,0x22,0x7c,0x3d, - 255, - 100, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255}; - -const int head3_6[] = {0x4f,0x67,0x67,0x53,0,0x01, - 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, - 0x01,0x02,0x03,0x04,2,0,0,0, - 0xf4,0x87,0xba,0xf3, - 255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255}; - -const int head4_6[] = {0x4f,0x67,0x67,0x53,0,0x05, - 0x07,0x10,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,3,0,0,0, - 0xf7,0x2f,0x6c,0x60, - 5, - 254,255,4,255,0}; - -/* packet that overspans over an entire page */ -const int head1_7[] = {0x4f,0x67,0x67,0x53,0,0x02, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,0,0,0,0, - 0xff,0x7b,0x23,0x17, - 1, - 0}; - -const int head2_7[] = {0x4f,0x67,0x67,0x53,0,0x00, - 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,1,0,0,0, - 0x68,0x22,0x7c,0x3d, - 255, - 100, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255}; - -const int head3_7[] = {0x4f,0x67,0x67,0x53,0,0x05, - 0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00, - 0x01,0x02,0x03,0x04,2,0,0,0, - 0xd4,0xe0,0x60,0xe5, - 1, - 0}; - -int compare_packet(const ogg_packet *op1, const ogg_packet *op2){ - if(op1->packet!=op2->packet){ - fprintf(stderr,"op1->packet != op2->packet\n"); - return(1); - } - if(op1->bytes!=op2->bytes){ - fprintf(stderr,"op1->bytes != op2->bytes\n"); - return(1); - } - if(op1->b_o_s!=op2->b_o_s){ - fprintf(stderr,"op1->b_o_s != op2->b_o_s\n"); - return(1); - } - if(op1->e_o_s!=op2->e_o_s){ - fprintf(stderr,"op1->e_o_s != op2->e_o_s\n"); - return(1); - } - if(op1->granulepos!=op2->granulepos){ - fprintf(stderr,"op1->granulepos != op2->granulepos\n"); - return(1); - } - if(op1->packetno!=op2->packetno){ - fprintf(stderr,"op1->packetno != op2->packetno\n"); - return(1); - } - return(0); -} - -void test_pack(const int *pl, const int **headers, int byteskip, - int pageskip, int packetskip){ - unsigned char *data=_ogg_malloc(1024*1024); /* for scripted test cases only */ - long inptr=0; - long outptr=0; - long deptr=0; - long depacket=0; - long granule_pos=7,pageno=0; - int i,j,packets,pageout=pageskip; - int eosflag=0; - int bosflag=0; - - int byteskipcount=0; - - ogg_stream_reset(&os_en); - ogg_stream_reset(&os_de); - ogg_sync_reset(&oy); - - for(packets=0;packetsbyteskip){ - memcpy(next,og.header,byteskipcount-byteskip); - next+=byteskipcount-byteskip; - byteskipcount=byteskip; - } - - byteskipcount+=og.body_len; - if(byteskipcount>byteskip){ - memcpy(next,og.body,byteskipcount-byteskip); - next+=byteskipcount-byteskip; - byteskipcount=byteskip; - } - - ogg_sync_wrote(&oy,next-buf); - - while(1){ - int ret=ogg_sync_pageout(&oy,&og_de); - if(ret==0)break; - if(ret<0)continue; - /* got a page. Happy happy. Verify that it's good. */ - - fprintf(stderr,"(%d), ",pageout); - - check_page(data+deptr,headers[pageout],&og_de); - deptr+=og_de.body_len; - pageout++; - - /* submit it to deconstitution */ - ogg_stream_pagein(&os_de,&og_de); - - /* packets out? */ - while(ogg_stream_packetpeek(&os_de,&op_de2)>0){ - ogg_stream_packetpeek(&os_de,NULL); - ogg_stream_packetout(&os_de,&op_de); /* just catching them all */ - - /* verify peek and out match */ - if(compare_packet(&op_de,&op_de2)){ - fprintf(stderr,"packetout != packetpeek! pos=%ld\n", - depacket); - exit(1); - } - - /* verify the packet! */ - /* check data */ - if(memcmp(data+depacket,op_de.packet,op_de.bytes)){ - fprintf(stderr,"packet data mismatch in decode! pos=%ld\n", - depacket); - exit(1); - } - /* check bos flag */ - if(bosflag==0 && op_de.b_o_s==0){ - fprintf(stderr,"b_o_s flag not set on packet!\n"); - exit(1); - } - if(bosflag && op_de.b_o_s){ - fprintf(stderr,"b_o_s flag incorrectly set on packet!\n"); - exit(1); - } - bosflag=1; - depacket+=op_de.bytes; - - /* check eos flag */ - if(eosflag){ - fprintf(stderr,"Multiple decoded packets with eos flag!\n"); - exit(1); - } - - if(op_de.e_o_s)eosflag=1; - - /* check granulepos flag */ - if(op_de.granulepos!=-1){ - fprintf(stderr," granule:%ld ",(long)op_de.granulepos); - } - } - } - } - } - } - } - _ogg_free(data); - if(headers[pageno]!=NULL){ - fprintf(stderr,"did not write last page!\n"); - exit(1); - } - if(headers[pageout]!=NULL){ - fprintf(stderr,"did not decode last page!\n"); - exit(1); - } - if(inptr!=outptr){ - fprintf(stderr,"encoded page data incomplete!\n"); - exit(1); - } - if(inptr!=deptr){ - fprintf(stderr,"decoded page data incomplete!\n"); - exit(1); - } - if(inptr!=depacket){ - fprintf(stderr,"decoded packet data incomplete!\n"); - exit(1); - } - if(!eosflag){ - fprintf(stderr,"Never got a packet with EOS set!\n"); - exit(1); - } - fprintf(stderr,"ok.\n"); -} - -int main(void){ - - ogg_stream_init(&os_en,0x04030201); - ogg_stream_init(&os_de,0x04030201); - ogg_sync_init(&oy); - - /* Exercise each code path in the framing code. Also verify that - the checksums are working. */ - - { - /* 17 only */ - const int packets[]={17, -1}; - const int *headret[]={head1_0,NULL}; - - fprintf(stderr,"testing single page encoding... "); - test_pack(packets,headret,0,0,0); - } - - { - /* 17, 254, 255, 256, 500, 510, 600 byte, pad */ - const int packets[]={17, 254, 255, 256, 500, 510, 600, -1}; - const int *headret[]={head1_1,head2_1,NULL}; - - fprintf(stderr,"testing basic page encoding... "); - test_pack(packets,headret,0,0,0); - } - - { - /* nil packets; beginning,middle,end */ - const int packets[]={0,17, 254, 255, 0, 256, 0, 500, 510, 600, 0, -1}; - const int *headret[]={head1_2,head2_2,NULL}; - - fprintf(stderr,"testing basic nil packets... "); - test_pack(packets,headret,0,0,0); - } - - { - /* large initial packet */ - const int packets[]={4345,259,255,-1}; - const int *headret[]={head1_3,head2_3,NULL}; - - fprintf(stderr,"testing initial-packet lacing > 4k... "); - test_pack(packets,headret,0,0,0); - } - - { - /* continuing packet test; with page spill expansion, we have to - overflow the lacing table. */ - const int packets[]={0,65500,259,255,-1}; - const int *headret[]={head1_4,head2_4,head3_4,NULL}; - - fprintf(stderr,"testing single packet page span... "); - test_pack(packets,headret,0,0,0); - } - - { - /* spill expand packet test */ - const int packets[]={0,4345,259,255,0,0,-1}; - const int *headret[]={head1_4b,head2_4b,head3_4b,NULL}; - - fprintf(stderr,"testing page spill expansion... "); - test_pack(packets,headret,0,0,0); - } - - /* page with the 255 segment limit */ - { - - const int packets[]={0,10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,10, - 10,10,10,10,10,10,10,50,-1}; - const int *headret[]={head1_5,head2_5,head3_5,NULL}; - - fprintf(stderr,"testing max packet segments... "); - test_pack(packets,headret,0,0,0); - } - - { - /* packet that overspans over an entire page */ - const int packets[]={0,100,130049,259,255,-1}; - const int *headret[]={head1_6,head2_6,head3_6,head4_6,NULL}; - - fprintf(stderr,"testing very large packets... "); - test_pack(packets,headret,0,0,0); - } - -#ifndef DISABLE_CRC - { - /* test for the libogg 1.1.1 resync in large continuation bug - found by Josh Coalson) */ - const int packets[]={0,100,130049,259,255,-1}; - const int *headret[]={head1_6,head2_6,head3_6,head4_6,NULL}; - - fprintf(stderr,"testing continuation resync in very large packets... "); - test_pack(packets,headret,100,2,3); - } -#else - fprintf(stderr,"Skipping continuation resync test due to --disable-crc\n"); -#endif - - { - /* term only page. why not? */ - const int packets[]={0,100,64770,-1}; - const int *headret[]={head1_7,head2_7,head3_7,NULL}; - - fprintf(stderr,"testing zero data page (1 nil packet)... "); - test_pack(packets,headret,0,0,0); - } - - - - { - /* build a bunch of pages for testing */ - unsigned char *data=_ogg_malloc(1024*1024); - int pl[]={0, 1,1,98,4079, 1,1,2954,2057, 76,34,912,0,234,1000,1000, 1000,300,-1}; - int inptr=0,i,j; - ogg_page og[5]; - - ogg_stream_reset(&os_en); - - for(i=0;pl[i]!=-1;i++){ - ogg_packet op; - int len=pl[i]; - - op.packet=data+inptr; - op.bytes=len; - op.e_o_s=(pl[i+1]<0?1:0); - op.granulepos=(i+1)*1000; - - for(j=0;j0)error(); - - /* Test fractional page inputs: incomplete fixed header */ - memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+3, - 20); - ogg_sync_wrote(&oy,20); - if(ogg_sync_pageout(&oy,&og_de)>0)error(); - - /* Test fractional page inputs: incomplete header */ - memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+23, - 5); - ogg_sync_wrote(&oy,5); - if(ogg_sync_pageout(&oy,&og_de)>0)error(); - - /* Test fractional page inputs: incomplete body */ - - memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+28, - og[1].header_len-28); - ogg_sync_wrote(&oy,og[1].header_len-28); - if(ogg_sync_pageout(&oy,&og_de)>0)error(); - - memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,1000); - ogg_sync_wrote(&oy,1000); - if(ogg_sync_pageout(&oy,&og_de)>0)error(); - - memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body+1000, - og[1].body_len-1000); - ogg_sync_wrote(&oy,og[1].body_len-1000); - if(ogg_sync_pageout(&oy,&og_de)<=0)error(); - - fprintf(stderr,"ok.\n"); - } - - /* Test fractional page inputs: page + incomplete capture */ - { - ogg_page og_de; - fprintf(stderr,"Testing sync on 1+partial inputs... "); - ogg_sync_reset(&oy); - - memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header, - og[1].header_len); - ogg_sync_wrote(&oy,og[1].header_len); - - memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body, - og[1].body_len); - ogg_sync_wrote(&oy,og[1].body_len); - - memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header, - 20); - ogg_sync_wrote(&oy,20); - if(ogg_sync_pageout(&oy,&og_de)<=0)error(); - if(ogg_sync_pageout(&oy,&og_de)>0)error(); - - memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+20, - og[1].header_len-20); - ogg_sync_wrote(&oy,og[1].header_len-20); - memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body, - og[1].body_len); - ogg_sync_wrote(&oy,og[1].body_len); - if(ogg_sync_pageout(&oy,&og_de)<=0)error(); - - fprintf(stderr,"ok.\n"); - } - - /* Test recapture: garbage + page */ - { - ogg_page og_de; - fprintf(stderr,"Testing search for capture... "); - ogg_sync_reset(&oy); - - /* 'garbage' */ - memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body, - og[1].body_len); - ogg_sync_wrote(&oy,og[1].body_len); - - memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header, - og[1].header_len); - ogg_sync_wrote(&oy,og[1].header_len); - - memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body, - og[1].body_len); - ogg_sync_wrote(&oy,og[1].body_len); - - memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header, - 20); - ogg_sync_wrote(&oy,20); - if(ogg_sync_pageout(&oy,&og_de)>0)error(); - if(ogg_sync_pageout(&oy,&og_de)<=0)error(); - if(ogg_sync_pageout(&oy,&og_de)>0)error(); - - memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header+20, - og[2].header_len-20); - ogg_sync_wrote(&oy,og[2].header_len-20); - memcpy(ogg_sync_buffer(&oy,og[2].body_len),og[2].body, - og[2].body_len); - ogg_sync_wrote(&oy,og[2].body_len); - if(ogg_sync_pageout(&oy,&og_de)<=0)error(); - - fprintf(stderr,"ok.\n"); - } - -#ifndef DISABLE_CRC - /* Test recapture: page + garbage + page */ - { - ogg_page og_de; - fprintf(stderr,"Testing recapture... "); - ogg_sync_reset(&oy); - - memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header, - og[1].header_len); - ogg_sync_wrote(&oy,og[1].header_len); - - memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body, - og[1].body_len); - ogg_sync_wrote(&oy,og[1].body_len); - - memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header, - og[2].header_len); - ogg_sync_wrote(&oy,og[2].header_len); - - memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header, - og[2].header_len); - ogg_sync_wrote(&oy,og[2].header_len); - - if(ogg_sync_pageout(&oy,&og_de)<=0)error(); - - memcpy(ogg_sync_buffer(&oy,og[2].body_len),og[2].body, - og[2].body_len-5); - ogg_sync_wrote(&oy,og[2].body_len-5); - - memcpy(ogg_sync_buffer(&oy,og[3].header_len),og[3].header, - og[3].header_len); - ogg_sync_wrote(&oy,og[3].header_len); - - memcpy(ogg_sync_buffer(&oy,og[3].body_len),og[3].body, - og[3].body_len); - ogg_sync_wrote(&oy,og[3].body_len); - - if(ogg_sync_pageout(&oy,&og_de)>0)error(); - if(ogg_sync_pageout(&oy,&og_de)<=0)error(); - - fprintf(stderr,"ok.\n"); - } -#else - fprintf(stderr,"Skipping recapture test due to --disable-crc\n"); -#endif - - /* Free page data that was previously copied */ - { - for(i=0;i<5;i++){ - free_page(&og[i]); - } - } - } - ogg_sync_clear(&oy); - ogg_stream_clear(&os_en); - ogg_stream_clear(&os_de); - - return(0); -} - -#endif -#ifdef __cplusplus -} -#endif -#endif /* OGG_IMPL */ -#ifdef VORBIS_IMPL -#ifdef __cplusplus -extern "C" { -#endif -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: miscellaneous prototypes - - ********************************************************************/ - -#ifndef _V_RANDOM_H_ -#define _V_RANDOM_H_ -/*#include "vorbis/codec.h"*/ - -extern void *_vorbis_block_alloc(vorbis_block *vb,long bytes); -extern void _vorbis_block_ripcord(vorbis_block *vb); -extern int ov_ilog(ogg_uint32_t v); - -#ifdef ANALYSIS -extern int analysis_noisy; -extern void _analysis_output(char *base,int i,float *v,int n,int bark,int dB, - ogg_int64_t off); -extern void _analysis_output_always(char *base,int i,float *v,int n,int bark,int dB, - ogg_int64_t off); -#endif - -#ifdef DEBUG_MALLOC - -#define _VDBG_GRAPHFILE "malloc.m" -#undef _VDBG_GRAPHFILE -extern void *_VDBG_malloc(void *ptr,long bytes,char *file,long line); -extern void _VDBG_free(void *ptr,char *file,long line); - -#ifndef MISC_C -#undef _ogg_malloc -#undef _ogg_calloc -#undef _ogg_realloc -#undef _ogg_free - -#define _ogg_malloc(x) _VDBG_malloc(NULL,(x),__FILE__,__LINE__) -#define _ogg_calloc(x,y) _VDBG_malloc(NULL,(x)*(y),__FILE__,__LINE__) -#define _ogg_realloc(x,y) _VDBG_malloc((x),(y),__FILE__,__LINE__) -#define _ogg_free(x) _VDBG_free((x),__FILE__,__LINE__) -#endif -#endif - -#endif - - - - -#ifndef _OS_H -#define _OS_H -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: #ifdef jail to whip a few platforms into the UNIX ideal. - - ********************************************************************/ - -#ifdef HAVE_CONFIG_H -/*#include "config.h"*/ -#endif - -#include -/*#include */ - -/*#include "misc.h"*/ - -#ifndef _V_IFDEFJAIL_H_ -# define _V_IFDEFJAIL_H_ - -# ifdef __GNUC__ -# define STIN static __inline__ -# elif defined(_WIN32) -# define STIN static __inline -# else -# define STIN static -# endif - -#ifdef DJGPP -# define rint(x) (floor((x)+0.5f)) -#endif - -#ifndef M_PI -# define M_PI (3.1415926536f) -#endif - -#if defined(_WIN32) && !defined(__SYMBIAN32__) -# include -# define rint(x) (floor((x)+0.5f)) -# define NO_FLOAT_MATH_LIB -# define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b)) -#endif - -#if defined(__SYMBIAN32__) && defined(__WINS__) -void *_alloca(size_t size); -# define alloca _alloca -#endif - -#ifndef FAST_HYPOT -# define FAST_HYPOT hypot -#endif - -#endif /* _V_IFDEFJAIL_H_ */ - -#ifdef HAVE_ALLOCA_H -# include -#endif - -#ifdef USE_MEMORY_H -# include -#endif - -#ifndef min -# define min(x,y) ((x)>(y)?(y):(x)) -#endif - -#ifndef max -# define max(x,y) ((x)<(y)?(y):(x)) -#endif - - -/* Special i386 GCC implementation */ -#if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__) && !defined(__SSE2_MATH__) -# define VORBIS_FPU_CONTROL -/* both GCC and MSVC are kinda stupid about rounding/casting to int. - Because of encapsulation constraints (GCC can't see inside the asm - block and so we end up doing stupid things like a store/load that - is collectively a noop), we do it this way */ - -/* we must set up the fpu before this works!! */ - -typedef ogg_int16_t vorbis_fpu_control; - -static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){ - ogg_int16_t ret; - ogg_int16_t temp; - __asm__ __volatile__("fnstcw %0\n\t" - "movw %0,%%dx\n\t" - "andw $62463,%%dx\n\t" - "movw %%dx,%1\n\t" - "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx"); - *fpu=ret; -} - -static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){ - __asm__ __volatile__("fldcw %0":: "m"(fpu)); -} - -/* assumes the FPU is in round mode! */ -static inline int vorbis_ftoi(double f){ /* yes, double! Otherwise, - we get extra fst/fld to - truncate precision */ - int i; - __asm__("fistl %0": "=m"(i) : "t"(f)); - return(i); -} -#endif /* Special i386 GCC implementation */ - - -/* MSVC inline assembly. 32 bit only; inline ASM isn't implemented in the - * 64 bit compiler and doesn't work on arm. */ -#if defined(_MSC_VER) && defined(_M_IX86) && !defined(_WIN32_WCE) -# define VORBIS_FPU_CONTROL - -typedef ogg_int16_t vorbis_fpu_control; - -static __inline int vorbis_ftoi(double f){ - int i; - __asm{ - fld f - fistp i - } - return i; -} - -static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){ - (void)fpu; -} - -static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){ - (void)fpu; -} - -#endif /* Special MSVC 32 bit implementation */ - - -/* Optimized code path for x86_64 builds. Uses SSE2 intrinsics. This can be - done safely because all x86_64 CPUs supports SSE2. */ -#if (defined(_MSC_VER) && defined(_M_X64)) || (defined(__GNUC__) && defined (__SSE2_MATH__)) -# define VORBIS_FPU_CONTROL - -typedef ogg_int16_t vorbis_fpu_control; - -#include -static __inline int vorbis_ftoi(double f){ - return _mm_cvtsd_si32(_mm_load_sd(&f)); -} - -static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){ - (void)fpu; -} - -static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){ - (void)fpu; -} - -#endif /* Special MSVC x64 implementation */ - - -/* If no special implementation was found for the current compiler / platform, - use the default implementation here: */ -#ifndef VORBIS_FPU_CONTROL - -typedef int vorbis_fpu_control; - -STIN int vorbis_ftoi(double f){ - /* Note: MSVC and GCC (at least on some systems) round towards zero, thus, - the floor() call is required to ensure correct roudning of - negative numbers */ - return (int)floor(f+.5); -} - -/* We don't have special code for this compiler/arch, so do it the slow way */ -# define vorbis_fpu_setround(vorbis_fpu_control) {} -# define vorbis_fpu_restore(vorbis_fpu_control) {} - -#endif /* default implementation */ - -#endif /* _OS_H */ -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: modified discrete cosine transform prototypes - - ********************************************************************/ - -#ifndef _OGG_mdct_H_ -#define _OGG_mdct_H_ - -/*#include "vorbis/codec.h"*/ - - - - - -/*#define MDCT_INTEGERIZED <- be warned there could be some hurt left here*/ -#ifdef MDCT_INTEGERIZED - -#define DATA_TYPE int -#define REG_TYPE register int -#define TRIGBITS 14 -#define cPI3_8 6270 -#define cPI2_8 11585 -#define cPI1_8 15137 - -#define FLOAT_CONV(x) ((int)((x)*(1<>TRIGBITS) -#define HALVE(x) ((x)>>1) - -#else - -#define DATA_TYPE float -#define REG_TYPE float -#define cPI3_8 .38268343236508977175F -#define cPI2_8 .70710678118654752441F -#define cPI1_8 .92387953251128675613F - -#define FLOAT_CONV(x) (x) -#define MULT_NORM(x) (x) -#define HALVE(x) ((x)*.5f) - -#endif - - -typedef struct { - int n; - int log2n; - - DATA_TYPE *trig; - int *bitrev; - - DATA_TYPE scale; -} mdct_lookup; - -extern void mdct_init(mdct_lookup *lookup,int n); -extern void mdct_clear(mdct_lookup *l); -extern void mdct_forward(mdct_lookup *init, DATA_TYPE *in, DATA_TYPE *out); -extern void mdct_backward(mdct_lookup *init, DATA_TYPE *in, DATA_TYPE *out); - -#endif -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: PCM data envelope analysis and manipulation - - ********************************************************************/ - -#ifndef _V_ENVELOPE_ -#define _V_ENVELOPE_ - -/*#include "mdct.h"*/ - -#define VE_PRE 16 -#define VE_WIN 4 -#define VE_POST 2 -#define VE_AMP (VE_PRE+VE_POST-1) - -#define VE_BANDS 7 -#define VE_NEARDC 15 - -#define VE_MINSTRETCH 2 /* a bit less than short block */ -#define VE_MAXSTRETCH 12 /* one-third full block */ - -typedef struct { - float ampbuf[VE_AMP]; - int ampptr; - - float nearDC[VE_NEARDC]; - float nearDC_acc; - float nearDC_partialacc; - int nearptr; - -} envelope_filter_state; - -typedef struct { - int begin; - int end; - float *window; - float total; -} envelope_band; - -typedef struct { - int ch; - int winlength; - int searchstep; - float minenergy; - - mdct_lookup mdct; - float *mdct_win; - - envelope_band band[VE_BANDS]; - envelope_filter_state *filter; - int stretch; - - int *mark; - - long storage; - long current; - long curmark; - long cursor; -} envelope_lookup; - -extern void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi); -extern void _ve_envelope_clear(envelope_lookup *e); -extern long _ve_envelope_search(vorbis_dsp_state *v); -extern void _ve_envelope_shift(envelope_lookup *e,long shift); -extern int _ve_envelope_mark(vorbis_dsp_state *v); - - -#endif -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: basic shared codebook operations - - ********************************************************************/ - -#ifndef _V_CODEBOOK_H_ -#define _V_CODEBOOK_H_ - -/*#include */ - -/* This structure encapsulates huffman and VQ style encoding books; it - doesn't do anything specific to either. - - valuelist/quantlist are nonNULL (and q_* significant) only if - there's entry->value mapping to be done. - - If encode-side mapping must be done (and thus the entry needs to be - hunted), the auxiliary encode pointer will point to a decision - tree. This is true of both VQ and huffman, but is mostly useful - with VQ. - -*/ - -typedef struct static_codebook{ - long dim; /* codebook dimensions (elements per vector) */ - long entries; /* codebook entries */ - char *lengthlist; /* codeword lengths in bits */ - - /* mapping ***************************************************************/ - int maptype; /* 0=none - 1=implicitly populated values from map column - 2=listed arbitrary values */ - - /* The below does a linear, single monotonic sequence mapping. */ - long q_min; /* packed 32 bit float; quant value 0 maps to minval */ - long q_delta; /* packed 32 bit float; val 1 - val 0 == delta */ - int q_quant; /* bits: 0 < quant <= 16 */ - int q_sequencep; /* bitflag */ - - long *quantlist; /* map == 1: (int)(entries^(1/dim)) element column map - map == 2: list of dim*entries quantized entry vals - */ - int allocedp; -} static_codebook; - -typedef struct codebook{ - long dim; /* codebook dimensions (elements per vector) */ - long entries; /* codebook entries */ - long used_entries; /* populated codebook entries */ - const static_codebook *c; - - /* for encode, the below are entry-ordered, fully populated */ - /* for decode, the below are ordered by _bitreversed codeword and only - used entries are populated */ - float *valuelist; /* list of dim*entries actual entry values */ - ogg_uint32_t *codelist; /* list of bitstream codewords for each entry */ - - int *dec_index; /* only used if sparseness collapsed */ - char *dec_codelengths; - ogg_uint32_t *dec_firsttable; - int dec_firsttablen; - int dec_maxlength; - - /* The current encoder uses only centered, integer-only lattice books. */ - int quantvals; - int minval; - int delta; -} codebook; - -extern void vorbis_staticbook_destroy(static_codebook *b); -extern int vorbis_book_init_encode(codebook *dest,const static_codebook *source); -extern int vorbis_book_init_decode(codebook *dest,const static_codebook *source); -extern void vorbis_book_clear(codebook *b); - -extern float *_book_unquantize(const static_codebook *b,int n,int *map); -extern float *_book_logdist(const static_codebook *b,float *vals); -extern float _float32_unpack(long val); -extern long _float32_pack(float val); -extern int _best(codebook *book, float *a, int step); -extern long _book_maptype1_quantvals(const static_codebook *b); - -extern int vorbis_book_besterror(codebook *book,float *a,int step,int addmul); -extern long vorbis_book_codeword(codebook *book,int entry); -extern long vorbis_book_codelen(codebook *book,int entry); - - - -extern int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *b); -extern static_codebook *vorbis_staticbook_unpack(oggpack_buffer *b); - -extern int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b); - -extern long vorbis_book_decode(codebook *book, oggpack_buffer *b); -extern long vorbis_book_decodevs_add(codebook *book, float *a, - oggpack_buffer *b,int n); -extern long vorbis_book_decodev_set(codebook *book, float *a, - oggpack_buffer *b,int n); -extern long vorbis_book_decodev_add(codebook *book, float *a, - oggpack_buffer *b,int n); -extern long vorbis_book_decodevv_add(codebook *book, float **a, - long off,int ch, - oggpack_buffer *b,int n); - - - -#endif -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: fft transform - - ********************************************************************/ - -#ifndef _V_SMFT_H_ -#define _V_SMFT_H_ - -/*#include "vorbis/codec.h"*/ - -typedef struct { - int n; - float *trigcache; - int *splitcache; -} drft_lookup; - -extern void drft_forward(drft_lookup *l,float *data); -extern void drft_backward(drft_lookup *l,float *data); -extern void drft_init(drft_lookup *l,int n); -extern void drft_clear(drft_lookup *l); - -#endif -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: libvorbis codec headers - - ********************************************************************/ - -#ifndef _V_CODECI_H_ -#define _V_CODECI_H_ - -/*#include "envelope.h"*/ -/*#include "codebook.h"*/ - -#define BLOCKTYPE_IMPULSE 0 -#define BLOCKTYPE_PADDING 1 -#define BLOCKTYPE_TRANSITION 0 -#define BLOCKTYPE_LONG 1 - -#define PACKETBLOBS 15 - -typedef struct vorbis_block_internal{ - float **pcmdelay; /* this is a pointer into local storage */ - float ampmax; - int blocktype; - - oggpack_buffer *packetblob[PACKETBLOBS]; /* initialized, must be freed; - blob [PACKETBLOBS/2] points to - the oggpack_buffer in the - main vorbis_block */ -} vorbis_block_internal; - -typedef void vorbis_look_floor; -typedef void vorbis_look_residue; -typedef void vorbis_look_transform; - -/* mode ************************************************************/ -typedef struct { - int blockflag; - int windowtype; - int transformtype; - int mapping; -} vorbis_info_mode; - -typedef void vorbis_info_floor; -typedef void vorbis_info_residue; -typedef void vorbis_info_mapping; - -/*#include "psy.h"*/ -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: random psychoacoustics (not including preecho) - - ********************************************************************/ - -#ifndef _V_PSY_H_ -#define _V_PSY_H_ -/*#include "smallft.h"*/ - -/*#include "backends.h"*/ -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: libvorbis backend and mapping structures; needed for - static mode headers - - ********************************************************************/ - -/* this is exposed up here because we need it for static modes. - Lookups for each backend aren't exposed because there's no reason - to do so */ - -#ifndef _vorbis_backend_h_ -#define _vorbis_backend_h_ - -/*#include "codec_internal.h"*/ - -/* this would all be simpler/shorter with templates, but.... */ -/* Floor backend generic *****************************************/ -typedef struct{ - void (*pack) (vorbis_info_floor *,oggpack_buffer *); - vorbis_info_floor *(*unpack)(vorbis_info *,oggpack_buffer *); - vorbis_look_floor *(*look) (vorbis_dsp_state *,vorbis_info_floor *); - void (*free_info) (vorbis_info_floor *); - void (*free_look) (vorbis_look_floor *); - void *(*inverse1) (struct vorbis_block *,vorbis_look_floor *); - int (*inverse2) (struct vorbis_block *,vorbis_look_floor *, - void *buffer,float *); -} vorbis_func_floor; - -typedef struct{ - int order; - long rate; - long barkmap; - - int ampbits; - int ampdB; - - int numbooks; /* <= 16 */ - int books[16]; - - float lessthan; /* encode-only config setting hacks for libvorbis */ - float greaterthan; /* encode-only config setting hacks for libvorbis */ - -} vorbis_info_floor0; - - -#define VIF_POSIT 63 -#define VIF_CLASS 16 -#define VIF_PARTS 31 -typedef struct{ - int partitions; /* 0 to 31 */ - int partitionclass[VIF_PARTS]; /* 0 to 15 */ - - int class_dim[VIF_CLASS]; /* 1 to 8 */ - int class_subs[VIF_CLASS]; /* 0,1,2,3 (bits: 1< dB, Bark and Mel scales - - ********************************************************************/ - -#ifndef _V_SCALES_H_ -#define _V_SCALES_H_ - -#include -/*#include "os.h"*/ - -#ifdef _MSC_VER -/* MS Visual Studio doesn't have C99 inline keyword. */ -#define inline __inline -#endif - -/* 20log10(x) */ -#define VORBIS_IEEE_FLOAT32 1 -#ifdef VORBIS_IEEE_FLOAT32 - -static inline float unitnorm(float x){ - union { - ogg_uint32_t i; - float f; - } ix; - ix.f = x; - ix.i = (ix.i & 0x80000000U) | (0x3f800000U); - return ix.f; -} - -/* Segher was off (too high) by ~ .3 decibel. Center the conversion correctly. */ -static inline float todB(const float *x){ - union { - ogg_uint32_t i; - float f; - } ix; - ix.f = *x; - ix.i = ix.i&0x7fffffff; - return (float)(ix.i * 7.17711438e-7f -764.6161886f); -} - -#define todB_nn(x) todB(x) - -#else - -static float unitnorm(float x){ - if(x<0)return(-1.f); - return(1.f); -} - -#define todB(x) (*(x)==0?-400.f:log(*(x)**(x))*4.34294480f) -#define todB_nn(x) (*(x)==0.f?-400.f:log(*(x))*8.6858896f) - -#endif - -#define fromdB(x) (exp((x)*.11512925f)) - -/* The bark scale equations are approximations, since the original - table was somewhat hand rolled. The below are chosen to have the - best possible fit to the rolled tables, thus their somewhat odd - appearance (these are more accurate and over a longer range than - the oft-quoted bark equations found in the texts I have). The - approximations are valid from 0 - 30kHz (nyquist) or so. - - all f in Hz, z in Bark */ - -#define toBARK(n) (13.1f*atan(.00074f*(n))+2.24f*atan((n)*(n)*1.85e-8f)+1e-4f*(n)) -#define fromBARK(z) (102.f*(z)-2.f*pow(z,2.f)+.4f*pow(z,3.f)+pow(1.46f,z)-1.f) -#define toMEL(n) (log(1.f+(n)*.001f)*1442.695f) -#define fromMEL(m) (1000.f*exp((m)/1442.695f)-1000.f) - -/* Frequency to octave. We arbitrarily declare 63.5 Hz to be octave - 0.0 */ - -#define toOC(n) (log(n)*1.442695f-5.965784f) -#define fromOC(o) (exp(((o)+5.965784f)*.693147f)) - -#endif -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: window functions - - ********************************************************************/ - -#ifndef _V_WINDOW_ -#define _V_WINDOW_ - -extern const float *_vorbis_window_get(int n); -extern void _vorbis_apply_window(float *d,int *winno,long *blocksizes, - int lW,int W,int nW); - - -#endif -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: normalized modified discrete cosine transform - power of two length transform only [64 <= n ] - - Original algorithm adapted long ago from _The use of multirate filter - banks for coding of high quality digital audio_, by T. Sporer, - K. Brandenburg and B. Edler, collection of the European Signal - Processing Conference (EUSIPCO), Amsterdam, June 1992, Vol.1, pp - 211-214 - - The below code implements an algorithm that no longer looks much like - that presented in the paper, but the basic structure remains if you - dig deep enough to see it. - - This module DOES NOT INCLUDE code to generate/apply the window - function. Everybody has their own weird favorite including me... I - happen to like the properties of y=sin(.5PI*sin^2(x)), but others may - vehemently disagree. - - ********************************************************************/ - -/* this can also be run as an integer transform by uncommenting a - define in mdct.h; the integerization is a first pass and although - it's likely stable for Vorbis, the dynamic range is constrained and - roundoff isn't done (so it's noisy). Consider it functional, but - only a starting point. There's no point on a machine with an FPU */ - -#include -#include -#include -#include -/*#include "vorbis/codec.h"*/ -/*#include "mdct.h"*/ -/*#include "os.h"*/ -/*#include "misc.h"*/ - -/* build lookups for trig functions; also pre-figure scaling and - some window function algebra. */ - -void mdct_init(mdct_lookup *lookup,int n){ - int *bitrev=(int *)_ogg_malloc(sizeof(*bitrev)*(n/4)); - DATA_TYPE *T=(float *)_ogg_malloc(sizeof(*T)*(n+n/4)); - - int i; - int n2=n>>1; - int log2n=lookup->log2n=rint(log((float)n)/log(2.f)); - lookup->n=n; - lookup->trig=T; - lookup->bitrev=bitrev; - -/* trig lookups... */ - - for(i=0;i>j;j++) - if((msb>>j)&i)acc|=1<scale=FLOAT_CONV(4.f/n); -} - -/* 8 point butterfly (in place, 4 register) */ -STIN void mdct_butterfly_8(DATA_TYPE *x){ - REG_TYPE r0 = x[6] + x[2]; - REG_TYPE r1 = x[6] - x[2]; - REG_TYPE r2 = x[4] + x[0]; - REG_TYPE r3 = x[4] - x[0]; - - x[6] = r0 + r2; - x[4] = r0 - r2; - - r0 = x[5] - x[1]; - r2 = x[7] - x[3]; - x[0] = r1 + r0; - x[2] = r1 - r0; - - r0 = x[5] + x[1]; - r1 = x[7] + x[3]; - x[3] = r2 + r3; - x[1] = r2 - r3; - x[7] = r1 + r0; - x[5] = r1 - r0; - -} - -/* 16 point butterfly (in place, 4 register) */ -STIN void mdct_butterfly_16(DATA_TYPE *x){ - REG_TYPE r0 = x[1] - x[9]; - REG_TYPE r1 = x[0] - x[8]; - - x[8] += x[0]; - x[9] += x[1]; - x[0] = MULT_NORM((r0 + r1) * cPI2_8); - x[1] = MULT_NORM((r0 - r1) * cPI2_8); - - r0 = x[3] - x[11]; - r1 = x[10] - x[2]; - x[10] += x[2]; - x[11] += x[3]; - x[2] = r0; - x[3] = r1; - - r0 = x[12] - x[4]; - r1 = x[13] - x[5]; - x[12] += x[4]; - x[13] += x[5]; - x[4] = MULT_NORM((r0 - r1) * cPI2_8); - x[5] = MULT_NORM((r0 + r1) * cPI2_8); - - r0 = x[14] - x[6]; - r1 = x[15] - x[7]; - x[14] += x[6]; - x[15] += x[7]; - x[6] = r0; - x[7] = r1; - - mdct_butterfly_8(x); - mdct_butterfly_8(x+8); -} - -/* 32 point butterfly (in place, 4 register) */ -STIN void mdct_butterfly_32(DATA_TYPE *x){ - REG_TYPE r0 = x[30] - x[14]; - REG_TYPE r1 = x[31] - x[15]; - - x[30] += x[14]; - x[31] += x[15]; - x[14] = r0; - x[15] = r1; - - r0 = x[28] - x[12]; - r1 = x[29] - x[13]; - x[28] += x[12]; - x[29] += x[13]; - x[12] = MULT_NORM( r0 * cPI1_8 - r1 * cPI3_8 ); - x[13] = MULT_NORM( r0 * cPI3_8 + r1 * cPI1_8 ); - - r0 = x[26] - x[10]; - r1 = x[27] - x[11]; - x[26] += x[10]; - x[27] += x[11]; - x[10] = MULT_NORM(( r0 - r1 ) * cPI2_8); - x[11] = MULT_NORM(( r0 + r1 ) * cPI2_8); - - r0 = x[24] - x[8]; - r1 = x[25] - x[9]; - x[24] += x[8]; - x[25] += x[9]; - x[8] = MULT_NORM( r0 * cPI3_8 - r1 * cPI1_8 ); - x[9] = MULT_NORM( r1 * cPI3_8 + r0 * cPI1_8 ); - - r0 = x[22] - x[6]; - r1 = x[7] - x[23]; - x[22] += x[6]; - x[23] += x[7]; - x[6] = r1; - x[7] = r0; - - r0 = x[4] - x[20]; - r1 = x[5] - x[21]; - x[20] += x[4]; - x[21] += x[5]; - x[4] = MULT_NORM( r1 * cPI1_8 + r0 * cPI3_8 ); - x[5] = MULT_NORM( r1 * cPI3_8 - r0 * cPI1_8 ); - - r0 = x[2] - x[18]; - r1 = x[3] - x[19]; - x[18] += x[2]; - x[19] += x[3]; - x[2] = MULT_NORM(( r1 + r0 ) * cPI2_8); - x[3] = MULT_NORM(( r1 - r0 ) * cPI2_8); - - r0 = x[0] - x[16]; - r1 = x[1] - x[17]; - x[16] += x[0]; - x[17] += x[1]; - x[0] = MULT_NORM( r1 * cPI3_8 + r0 * cPI1_8 ); - x[1] = MULT_NORM( r1 * cPI1_8 - r0 * cPI3_8 ); - - mdct_butterfly_16(x); - mdct_butterfly_16(x+16); - -} - -/* N point first stage butterfly (in place, 2 register) */ -STIN void mdct_butterfly_first(DATA_TYPE *T, - DATA_TYPE *x, - int points){ - - DATA_TYPE *x1 = x + points - 8; - DATA_TYPE *x2 = x + (points>>1) - 8; - REG_TYPE r0; - REG_TYPE r1; - - do{ - - r0 = x1[6] - x2[6]; - r1 = x1[7] - x2[7]; - x1[6] += x2[6]; - x1[7] += x2[7]; - x2[6] = MULT_NORM(r1 * T[1] + r0 * T[0]); - x2[7] = MULT_NORM(r1 * T[0] - r0 * T[1]); - - r0 = x1[4] - x2[4]; - r1 = x1[5] - x2[5]; - x1[4] += x2[4]; - x1[5] += x2[5]; - x2[4] = MULT_NORM(r1 * T[5] + r0 * T[4]); - x2[5] = MULT_NORM(r1 * T[4] - r0 * T[5]); - - r0 = x1[2] - x2[2]; - r1 = x1[3] - x2[3]; - x1[2] += x2[2]; - x1[3] += x2[3]; - x2[2] = MULT_NORM(r1 * T[9] + r0 * T[8]); - x2[3] = MULT_NORM(r1 * T[8] - r0 * T[9]); - - r0 = x1[0] - x2[0]; - r1 = x1[1] - x2[1]; - x1[0] += x2[0]; - x1[1] += x2[1]; - x2[0] = MULT_NORM(r1 * T[13] + r0 * T[12]); - x2[1] = MULT_NORM(r1 * T[12] - r0 * T[13]); - - x1-=8; - x2-=8; - T+=16; - - }while(x2>=x); -} - -/* N/stage point generic N stage butterfly (in place, 2 register) */ -STIN void mdct_butterfly_generic(DATA_TYPE *T, - DATA_TYPE *x, - int points, - int trigint){ - - DATA_TYPE *x1 = x + points - 8; - DATA_TYPE *x2 = x + (points>>1) - 8; - REG_TYPE r0; - REG_TYPE r1; - - do{ - - r0 = x1[6] - x2[6]; - r1 = x1[7] - x2[7]; - x1[6] += x2[6]; - x1[7] += x2[7]; - x2[6] = MULT_NORM(r1 * T[1] + r0 * T[0]); - x2[7] = MULT_NORM(r1 * T[0] - r0 * T[1]); - - T+=trigint; - - r0 = x1[4] - x2[4]; - r1 = x1[5] - x2[5]; - x1[4] += x2[4]; - x1[5] += x2[5]; - x2[4] = MULT_NORM(r1 * T[1] + r0 * T[0]); - x2[5] = MULT_NORM(r1 * T[0] - r0 * T[1]); - - T+=trigint; - - r0 = x1[2] - x2[2]; - r1 = x1[3] - x2[3]; - x1[2] += x2[2]; - x1[3] += x2[3]; - x2[2] = MULT_NORM(r1 * T[1] + r0 * T[0]); - x2[3] = MULT_NORM(r1 * T[0] - r0 * T[1]); - - T+=trigint; - - r0 = x1[0] - x2[0]; - r1 = x1[1] - x2[1]; - x1[0] += x2[0]; - x1[1] += x2[1]; - x2[0] = MULT_NORM(r1 * T[1] + r0 * T[0]); - x2[1] = MULT_NORM(r1 * T[0] - r0 * T[1]); - - T+=trigint; - x1-=8; - x2-=8; - - }while(x2>=x); -} - -STIN void mdct_butterflies(mdct_lookup *init, - DATA_TYPE *x, - int points){ - - DATA_TYPE *T=init->trig; - int stages=init->log2n-5; - int i,j; - - if(--stages>0){ - mdct_butterfly_first(T,x,points); - } - - for(i=1;--stages>0;i++){ - for(j=0;j<(1<>i)*j,points>>i,4<trig)_ogg_free(l->trig); - if(l->bitrev)_ogg_free(l->bitrev); - memset(l,0,sizeof(*l)); - } -} - -STIN void mdct__bitreverse(mdct_lookup *init, - DATA_TYPE *x){ - int n = init->n; - int *bit = init->bitrev; - DATA_TYPE *w0 = x; - DATA_TYPE *w1 = x = w0+(n>>1); - DATA_TYPE *T = init->trig+n; - - do{ - DATA_TYPE *x0 = x+bit[0]; - DATA_TYPE *x1 = x+bit[1]; - - REG_TYPE r0 = x0[1] - x1[1]; - REG_TYPE r1 = x0[0] + x1[0]; - REG_TYPE r2 = MULT_NORM(r1 * T[0] + r0 * T[1]); - REG_TYPE r3 = MULT_NORM(r1 * T[1] - r0 * T[0]); - - w1 -= 4; - - r0 = HALVE(x0[1] + x1[1]); - r1 = HALVE(x0[0] - x1[0]); - - w0[0] = r0 + r2; - w1[2] = r0 - r2; - w0[1] = r1 + r3; - w1[3] = r3 - r1; - - x0 = x+bit[2]; - x1 = x+bit[3]; - - r0 = x0[1] - x1[1]; - r1 = x0[0] + x1[0]; - r2 = MULT_NORM(r1 * T[2] + r0 * T[3]); - r3 = MULT_NORM(r1 * T[3] - r0 * T[2]); - - r0 = HALVE(x0[1] + x1[1]); - r1 = HALVE(x0[0] - x1[0]); - - w0[2] = r0 + r2; - w1[0] = r0 - r2; - w0[3] = r1 + r3; - w1[1] = r3 - r1; - - T += 4; - bit += 4; - w0 += 4; - - }while(w0n; - int n2=n>>1; - int n4=n>>2; - - /* rotate */ - - DATA_TYPE *iX = in+n2-7; - DATA_TYPE *oX = out+n2+n4; - DATA_TYPE *T = init->trig+n4; - - do{ - oX -= 4; - oX[0] = MULT_NORM(-iX[2] * T[3] - iX[0] * T[2]); - oX[1] = MULT_NORM (iX[0] * T[3] - iX[2] * T[2]); - oX[2] = MULT_NORM(-iX[6] * T[1] - iX[4] * T[0]); - oX[3] = MULT_NORM (iX[4] * T[1] - iX[6] * T[0]); - iX -= 8; - T += 4; - }while(iX>=in); - - iX = in+n2-8; - oX = out+n2+n4; - T = init->trig+n4; - - do{ - T -= 4; - oX[0] = MULT_NORM (iX[4] * T[3] + iX[6] * T[2]); - oX[1] = MULT_NORM (iX[4] * T[2] - iX[6] * T[3]); - oX[2] = MULT_NORM (iX[0] * T[1] + iX[2] * T[0]); - oX[3] = MULT_NORM (iX[0] * T[0] - iX[2] * T[1]); - iX -= 8; - oX += 4; - }while(iX>=in); - - mdct_butterflies(init,out+n2,n2); - mdct__bitreverse(init,out); - - /* roatate + window */ - - { - DATA_TYPE *oX1=out+n2+n4; - DATA_TYPE *oX2=out+n2+n4; - DATA_TYPE *iX =out; - T =init->trig+n2; - - do{ - oX1-=4; - - oX1[3] = MULT_NORM (iX[0] * T[1] - iX[1] * T[0]); - oX2[0] = -MULT_NORM (iX[0] * T[0] + iX[1] * T[1]); - - oX1[2] = MULT_NORM (iX[2] * T[3] - iX[3] * T[2]); - oX2[1] = -MULT_NORM (iX[2] * T[2] + iX[3] * T[3]); - - oX1[1] = MULT_NORM (iX[4] * T[5] - iX[5] * T[4]); - oX2[2] = -MULT_NORM (iX[4] * T[4] + iX[5] * T[5]); - - oX1[0] = MULT_NORM (iX[6] * T[7] - iX[7] * T[6]); - oX2[3] = -MULT_NORM (iX[6] * T[6] + iX[7] * T[7]); - - oX2+=4; - iX += 8; - T += 8; - }while(iXoX2); - } -} - -void mdct_forward(mdct_lookup *init, DATA_TYPE *in, DATA_TYPE *out){ - int n=init->n; - int n2=n>>1; - int n4=n>>2; - int n8=n>>3; - DATA_TYPE *w=(float *)alloca(n*sizeof(*w)); /* forward needs working space */ - DATA_TYPE *w2=w+n2; - - /* rotate */ - - /* window + rotate + step 1 */ - - REG_TYPE r0; - REG_TYPE r1; - DATA_TYPE *x0=in+n2+n4; - DATA_TYPE *x1=x0+1; - DATA_TYPE *T=init->trig+n2; - - int i=0; - - for(i=0;itrig+n2; - x0=out+n2; - - for(i=0;iscale); - x0[0] =MULT_NORM((w[0]*T[1]-w[1]*T[0])*init->scale); - w+=2; - T+=2; - } -} -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: *unnormalized* fft transform - - ********************************************************************/ - -/* FFT implementation from OggSquish, minus cosine transforms, - * minus all but radix 2/4 case. In Vorbis we only need this - * cut-down version. - * - * To do more than just power-of-two sized vectors, see the full - * version I wrote for NetLib. - * - * Note that the packing is a little strange; rather than the FFT r/i - * packing following R_0, I_n, R_1, I_1, R_2, I_2 ... R_n-1, I_n-1, - * it follows R_0, R_1, I_1, R_2, I_2 ... R_n-1, I_n-1, I_n like the - * FORTRAN version - */ - -#include -#include -#include -/*#include "smallft.h"*/ -/*#include "os.h"*/ -/*#include "misc.h"*/ - -static void drfti1(int n, float *wa, int *ifac){ - static int ntryh[4] = { 4,2,3,5 }; - static float tpi = 6.28318530717958648f; - float arg,argh,argld,fi; - int ntry=0,i,j=-1; - int k1, l1, l2, ib; - int ld, ii, ip, is, nq, nr; - int ido, ipm, nfm1; - int nl=n; - int nf=0; - - L101: - j++; - if (j < 4) - ntry=ntryh[j]; - else - ntry+=2; - - L104: - nq=nl/ntry; - nr=nl-ntry*nq; - if (nr!=0) goto L101; - - nf++; - ifac[nf+1]=ntry; - nl=nq; - if(ntry!=2)goto L107; - if(nf==1)goto L107; - - for (i=1;i>1; - ipp2=ip; - idp2=ido; - nbd=(ido-1)>>1; - t0=l1*ido; - t10=ip*ido; - - if(ido==1)goto L119; - for(ik=0;ikl1){ - for(j=1;j>1; - ipp2=ip; - ipph=(ip+1)>>1; - if(idol1)goto L139; - - is= -ido-1; - t1=0; - for(j=1;jn==1)return; - drftf1(l->n,data,l->trigcache,l->trigcache+l->n,l->splitcache); -} - -void drft_backward(drft_lookup *l,float *data){ - if (l->n==1)return; - drftb1(l->n,data,l->trigcache,l->trigcache+l->n,l->splitcache); -} - -void drft_init(drft_lookup *l,int n){ - l->n=n; - l->trigcache=(float *)_ogg_calloc(3*n,sizeof(*l->trigcache)); - l->splitcache=(int *)_ogg_calloc(32,sizeof(*l->splitcache)); - fdrffti(n, l->trigcache, l->splitcache); -} - -void drft_clear(drft_lookup *l){ - if(l){ - if(l->trigcache)_ogg_free(l->trigcache); - if(l->splitcache)_ogg_free(l->splitcache); - memset(l,0,sizeof(*l)); - } -} -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: PCM data vector blocking, windowing and dis/reassembly - - Handle windowing, overlap-add, etc of the PCM vectors. This is made - more amusing by Vorbis' current two allowed block sizes. - - ********************************************************************/ - -#include -#include -#include -/*#include */ -/*#include "vorbis/codec.h"*/ -/*#include "codec_internal.h"*/ - -/*#include "window.h"*/ -/*#include "mdct.h"*/ -/*#include "lpc.h"*/ -/*#include "registry.h"*/ -/*#include "misc.h"*/ - -/* pcm accumulator examples (not exhaustive): - - <-------------- lW ----------------> - <--------------- W ----------------> -: .....|..... _______________ | -: .''' | '''_--- | |\ | -:.....''' |_____--- '''......| | \_______| -:.................|__________________|_______|__|______| - |<------ Sl ------>| > Sr < |endW - |beginSl |endSl | |endSr - |beginW |endlW |beginSr - - - |< lW >| - <--------------- W ----------------> - | | .. ______________ | - | | ' `/ | ---_ | - |___.'___/`. | ---_____| - |_______|__|_______|_________________| - | >|Sl|< |<------ Sr ----->|endW - | | |endSl |beginSr |endSr - |beginW | |endlW - mult[0] |beginSl mult[n] - - <-------------- lW -----------------> - |<--W-->| -: .............. ___ | | -: .''' |`/ \ | | -:.....''' |/`....\|...| -:.........................|___|___|___| - |Sl |Sr |endW - | | |endSr - | |beginSr - | |endSl - |beginSl - |beginW -*/ - -/* block abstraction setup *********************************************/ - -#ifndef WORD_ALIGN -#define WORD_ALIGN 8 -#endif - -int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){ - int i; - memset(vb,0,sizeof(*vb)); - vb->vd=v; - vb->localalloc=0; - vb->localstore=NULL; - if(v->analysisp){ - vorbis_block_internal *vbi= - (vorbis_block_internal *)(vb->internal=_ogg_calloc(1,sizeof(vorbis_block_internal))); - vbi->ampmax=-9999; - - for(i=0;ipacketblob[i]=&vb->opb; - }else{ - vbi->packetblob[i]= - (oggpack_buffer *)_ogg_calloc(1,sizeof(oggpack_buffer)); - } - oggpack_writeinit(vbi->packetblob[i]); - } - } - - return(0); -} - -void *_vorbis_block_alloc(vorbis_block *vb,long bytes){ - bytes=(bytes+(WORD_ALIGN-1)) & ~(WORD_ALIGN-1); - if(bytes+vb->localtop>vb->localalloc){ - /* can't just _ogg_realloc... there are outstanding pointers */ - if(vb->localstore){ - struct alloc_chain *link=(alloc_chain *)_ogg_malloc(sizeof(*link)); - vb->totaluse+=vb->localtop; - link->next=vb->reap; - link->ptr=vb->localstore; - vb->reap=link; - } - /* highly conservative */ - vb->localalloc=bytes; - vb->localstore=_ogg_malloc(vb->localalloc); - vb->localtop=0; - } - { - void *ret=(void *)(((char *)vb->localstore)+vb->localtop); - vb->localtop+=bytes; - return ret; - } -} - -/* reap the chain, pull the ripcord */ -void _vorbis_block_ripcord(vorbis_block *vb){ - /* reap the chain */ - struct alloc_chain *reap=vb->reap; - while(reap){ - struct alloc_chain *next=reap->next; - _ogg_free(reap->ptr); - memset(reap,0,sizeof(*reap)); - _ogg_free(reap); - reap=next; - } - /* consolidate storage */ - if(vb->totaluse){ - vb->localstore=_ogg_realloc(vb->localstore,vb->totaluse+vb->localalloc); - vb->localalloc+=vb->totaluse; - vb->totaluse=0; - } - - /* pull the ripcord */ - vb->localtop=0; - vb->reap=NULL; -} - -int vorbis_block_clear(vorbis_block *vb){ - int i; - vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal; - - _vorbis_block_ripcord(vb); - if(vb->localstore)_ogg_free(vb->localstore); - - if(vbi){ - for(i=0;ipacketblob[i]); - if(i!=PACKETBLOBS/2)_ogg_free(vbi->packetblob[i]); - } - _ogg_free(vbi); - } - memset(vb,0,sizeof(*vb)); - return(0); -} - -/* Analysis side code, but directly related to blocking. Thus it's - here and not in analysis.c (which is for analysis transforms only). - The init is here because some of it is shared */ - -static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi,int encp){ - int i; - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - private_state *b=NULL; - int hs; - - if(ci==NULL|| - ci->modes<=0|| - ci->blocksizes[0]<64|| - ci->blocksizes[1]blocksizes[0]){ - return 1; - } - hs=ci->halfrate_flag; - - memset(v,0,sizeof(*v)); - b=(private_state *)(v->backend_state=_ogg_calloc(1,sizeof(*b))); - - v->vi=vi; - b->modebits=ov_ilog(ci->modes-1); - - b->transform[0]=(vorbis_look_transform **)_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[0])); - b->transform[1]=(vorbis_look_transform **)_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[1])); - - /* MDCT is tranform 0 */ - - b->transform[0][0]=_ogg_calloc(1,sizeof(mdct_lookup)); - b->transform[1][0]=_ogg_calloc(1,sizeof(mdct_lookup)); - mdct_init((mdct_lookup *)b->transform[0][0],ci->blocksizes[0]>>hs); - mdct_init((mdct_lookup *)b->transform[1][0],ci->blocksizes[1]>>hs); - - /* Vorbis I uses only window type 0 */ - /* note that the correct computation below is technically: - b->window[0]=ov_ilog(ci->blocksizes[0]-1)-6; - b->window[1]=ov_ilog(ci->blocksizes[1]-1)-6; - but since blocksizes are always powers of two, - the below is equivalent. - */ - b->window[0]=ov_ilog(ci->blocksizes[0])-7; - b->window[1]=ov_ilog(ci->blocksizes[1])-7; - - if(encp){ /* encode/decode differ here */ - - /* analysis always needs an fft */ - drft_init(&b->fft_look[0],ci->blocksizes[0]); - drft_init(&b->fft_look[1],ci->blocksizes[1]); - - /* finish the codebooks */ - if(!ci->fullbooks){ - ci->fullbooks=(codebook *)_ogg_calloc(ci->books,sizeof(*ci->fullbooks)); - for(i=0;ibooks;i++) - vorbis_book_init_encode(ci->fullbooks+i,ci->book_param[i]); - } - - b->psy=(vorbis_look_psy *)_ogg_calloc(ci->psys,sizeof(*b->psy)); - for(i=0;ipsys;i++){ - _vp_psy_init(b->psy+i, - ci->psy_param[i], - &ci->psy_g_param, - ci->blocksizes[ci->psy_param[i]->blockflag]/2, - vi->rate); - } - - v->analysisp=1; - }else{ - /* finish the codebooks */ - if(!ci->fullbooks){ - ci->fullbooks=(codebook *)_ogg_calloc(ci->books,sizeof(*ci->fullbooks)); - for(i=0;ibooks;i++){ - if(ci->book_param[i]==NULL) - goto abort_books; - if(vorbis_book_init_decode(ci->fullbooks+i,ci->book_param[i])) - goto abort_books; - /* decode codebooks are now standalone after init */ - vorbis_staticbook_destroy(ci->book_param[i]); - ci->book_param[i]=NULL; - } - } - } - - /* initialize the storage vectors. blocksize[1] is small for encode, - but the correct size for decode */ - v->pcm_storage=ci->blocksizes[1]; - v->pcm=(float **)_ogg_malloc(vi->channels*sizeof(*v->pcm)); - v->pcmret=(float **)_ogg_malloc(vi->channels*sizeof(*v->pcmret)); - { - int i; - for(i=0;ichannels;i++) - v->pcm[i]=(float *)_ogg_calloc(v->pcm_storage,sizeof(*v->pcm[i])); - } - - /* all 1 (large block) or 0 (small block) */ - /* explicitly set for the sake of clarity */ - v->lW=0; /* previous window size */ - v->W=0; /* current window size */ - - /* all vector indexes */ - v->centerW=ci->blocksizes[1]/2; - - v->pcm_current=v->centerW; - - /* initialize all the backend lookups */ - b->flr=(vorbis_look_floor **)_ogg_calloc(ci->floors,sizeof(*b->flr)); - b->residue=(vorbis_look_residue **)_ogg_calloc(ci->residues,sizeof(*b->residue)); - - for(i=0;ifloors;i++) - b->flr[i]=_floor_P[ci->floor_type[i]]-> - look(v,ci->floor_param[i]); - - for(i=0;iresidues;i++) - b->residue[i]=_residue_P[ci->residue_type[i]]-> - look(v,ci->residue_param[i]); - - return 0; - abort_books: - for(i=0;ibooks;i++){ - if(ci->book_param[i]!=NULL){ - vorbis_staticbook_destroy(ci->book_param[i]); - ci->book_param[i]=NULL; - } - } - vorbis_dsp_clear(v); - return -1; -} - -/* arbitrary settings and spec-mandated numbers get filled in here */ -int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){ - private_state *b=NULL; - - if(_vds_shared_init(v,vi,1))return 1; - b=(private_state *)v->backend_state; - b->psy_g_look=_vp_global_look(vi); - - /* Initialize the envelope state storage */ - b->ve=(envelope_lookup *)_ogg_calloc(1,sizeof(*b->ve)); - _ve_envelope_init(b->ve,vi); - - vorbis_bitrate_init(vi,&b->bms); - - /* compressed audio packets start after the headers - with sequence number 3 */ - v->sequence=3; - - return(0); -} - -void vorbis_dsp_clear(vorbis_dsp_state *v){ - int i; - if(v){ - vorbis_info *vi=v->vi; - codec_setup_info *ci=(codec_setup_info *)(vi?vi->codec_setup:NULL); - private_state *b=(private_state *)v->backend_state; - - if(b){ - - if(b->ve){ - _ve_envelope_clear(b->ve); - _ogg_free(b->ve); - } - - if(b->transform[0]){ - mdct_clear((mdct_lookup *)b->transform[0][0]); - _ogg_free(b->transform[0][0]); - _ogg_free(b->transform[0]); - } - if(b->transform[1]){ - mdct_clear((mdct_lookup *)b->transform[1][0]); - _ogg_free(b->transform[1][0]); - _ogg_free(b->transform[1]); - } - - if(b->flr){ - if(ci) - for(i=0;ifloors;i++) - _floor_P[ci->floor_type[i]]-> - free_look(b->flr[i]); - _ogg_free(b->flr); - } - if(b->residue){ - if(ci) - for(i=0;iresidues;i++) - _residue_P[ci->residue_type[i]]-> - free_look(b->residue[i]); - _ogg_free(b->residue); - } - if(b->psy){ - if(ci) - for(i=0;ipsys;i++) - _vp_psy_clear(b->psy+i); - _ogg_free(b->psy); - } - - if(b->psy_g_look)_vp_global_free(b->psy_g_look); - vorbis_bitrate_clear(&b->bms); - - drft_clear(&b->fft_look[0]); - drft_clear(&b->fft_look[1]); - - } - - if(v->pcm){ - if(vi) - for(i=0;ichannels;i++) - if(v->pcm[i])_ogg_free(v->pcm[i]); - _ogg_free(v->pcm); - if(v->pcmret)_ogg_free(v->pcmret); - } - - if(b){ - /* free header, header1, header2 */ - if(b->header)_ogg_free(b->header); - if(b->header1)_ogg_free(b->header1); - if(b->header2)_ogg_free(b->header2); - _ogg_free(b); - } - - memset(v,0,sizeof(*v)); - } -} - -float **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){ - int i; - vorbis_info *vi=v->vi; - private_state *b=(private_state *)v->backend_state; - - /* free header, header1, header2 */ - if(b->header)_ogg_free(b->header); - b->header=NULL; - if(b->header1)_ogg_free(b->header1); - b->header1=NULL; - if(b->header2)_ogg_free(b->header2); - b->header2=NULL; - - /* Do we have enough storage space for the requested buffer? If not, - expand the PCM (and envelope) storage */ - - if(v->pcm_current+vals>=v->pcm_storage){ - v->pcm_storage=v->pcm_current+vals*2; - - for(i=0;ichannels;i++){ - v->pcm[i]=(float *)_ogg_realloc(v->pcm[i],v->pcm_storage*sizeof(*v->pcm[i])); - } - } - - for(i=0;ichannels;i++) - v->pcmret[i]=v->pcm[i]+v->pcm_current; - - return(v->pcmret); -} - -static void _preextrapolate_helper(vorbis_dsp_state *v){ - int i; - int order=16; - float *lpc=(float *)alloca(order*sizeof(*lpc)); - float *work=(float *)alloca(v->pcm_current*sizeof(*work)); - long j; - v->preextrapolate=1; - - if(v->pcm_current-v->centerW>order*2){ /* safety */ - for(i=0;ivi->channels;i++){ - /* need to run the extrapolation in reverse! */ - for(j=0;jpcm_current;j++) - work[j]=v->pcm[i][v->pcm_current-j-1]; - - /* prime as above */ - vorbis_lpc_from_data(work,lpc,v->pcm_current-v->centerW,order); - -#if 0 - if(v->vi->channels==2){ - if(i==0) - _analysis_output("predataL",0,work,v->pcm_current-v->centerW,0,0,0); - else - _analysis_output("predataR",0,work,v->pcm_current-v->centerW,0,0,0); - }else{ - _analysis_output("predata",0,work,v->pcm_current-v->centerW,0,0,0); - } -#endif - - /* run the predictor filter */ - vorbis_lpc_predict(lpc,work+v->pcm_current-v->centerW-order, - order, - work+v->pcm_current-v->centerW, - v->centerW); - - for(j=0;jpcm_current;j++) - v->pcm[i][v->pcm_current-j-1]=work[j]; - - } - } -} - - -/* call with val<=0 to set eof */ - -int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){ - vorbis_info *vi=v->vi; - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - - if(vals<=0){ - int order=32; - int i; - float *lpc=(float *)alloca(order*sizeof(*lpc)); - - /* if it wasn't done earlier (very short sample) */ - if(!v->preextrapolate) - _preextrapolate_helper(v); - - /* We're encoding the end of the stream. Just make sure we have - [at least] a few full blocks of zeroes at the end. */ - /* actually, we don't want zeroes; that could drop a large - amplitude off a cliff, creating spread spectrum noise that will - suck to encode. Extrapolate for the sake of cleanliness. */ - - vorbis_analysis_buffer(v,ci->blocksizes[1]*3); - v->eofflag=v->pcm_current; - v->pcm_current+=ci->blocksizes[1]*3; - - for(i=0;ichannels;i++){ - if(v->eofflag>order*2){ - /* extrapolate with LPC to fill in */ - long n; - - /* make a predictor filter */ - n=v->eofflag; - if(n>ci->blocksizes[1])n=ci->blocksizes[1]; - vorbis_lpc_from_data(v->pcm[i]+v->eofflag-n,lpc,n,order); - - /* run the predictor filter */ - vorbis_lpc_predict(lpc,v->pcm[i]+v->eofflag-order,order, - v->pcm[i]+v->eofflag,v->pcm_current-v->eofflag); - }else{ - /* not enough data to extrapolate (unlikely to happen due to - guarding the overlap, but bulletproof in case that - assumtion goes away). zeroes will do. */ - memset(v->pcm[i]+v->eofflag,0, - (v->pcm_current-v->eofflag)*sizeof(*v->pcm[i])); - - } - } - }else{ - - if(v->pcm_current+vals>v->pcm_storage) - return(OV_EINVAL); - - v->pcm_current+=vals; - - /* we may want to reverse extrapolate the beginning of a stream - too... in case we're beginning on a cliff! */ - /* clumsy, but simple. It only runs once, so simple is good. */ - if(!v->preextrapolate && v->pcm_current-v->centerW>ci->blocksizes[1]) - _preextrapolate_helper(v); - - } - return(0); -} - -/* do the deltas, envelope shaping, pre-echo and determine the size of - the next block on which to continue analysis */ -int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){ - int i; - vorbis_info *vi=v->vi; - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - private_state *b=(private_state *)v->backend_state; - vorbis_look_psy_global *g=b->psy_g_look; - long beginW=v->centerW-ci->blocksizes[v->W]/2,centerNext; - vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal; - - /* check to see if we're started... */ - if(!v->preextrapolate)return(0); - - /* check to see if we're done... */ - if(v->eofflag==-1)return(0); - - /* By our invariant, we have lW, W and centerW set. Search for - the next boundary so we can determine nW (the next window size) - which lets us compute the shape of the current block's window */ - - /* we do an envelope search even on a single blocksize; we may still - be throwing more bits at impulses, and envelope search handles - marking impulses too. */ - { - long bp=_ve_envelope_search(v); - if(bp==-1){ - - if(v->eofflag==0)return(0); /* not enough data currently to search for a - full long block */ - v->nW=0; - }else{ - - if(ci->blocksizes[0]==ci->blocksizes[1]) - v->nW=0; - else - v->nW=bp; - } - } - - centerNext=v->centerW+ci->blocksizes[v->W]/4+ci->blocksizes[v->nW]/4; - - { - /* center of next block + next block maximum right side. */ - - long blockbound=centerNext+ci->blocksizes[v->nW]/2; - if(v->pcm_currentlW=v->lW; - vb->W=v->W; - vb->nW=v->nW; - - if(v->W){ - if(!v->lW || !v->nW){ - vbi->blocktype=BLOCKTYPE_TRANSITION; - /*fprintf(stderr,"-");*/ - }else{ - vbi->blocktype=BLOCKTYPE_LONG; - /*fprintf(stderr,"_");*/ - } - }else{ - if(_ve_envelope_mark(v)){ - vbi->blocktype=BLOCKTYPE_IMPULSE; - /*fprintf(stderr,"|");*/ - - }else{ - vbi->blocktype=BLOCKTYPE_PADDING; - /*fprintf(stderr,".");*/ - - } - } - - vb->vd=v; - vb->sequence=v->sequence++; - vb->granulepos=v->granulepos; - vb->pcmend=ci->blocksizes[v->W]; - - /* copy the vectors; this uses the local storage in vb */ - - /* this tracks 'strongest peak' for later psychoacoustics */ - /* moved to the global psy state; clean this mess up */ - if(vbi->ampmax>g->ampmax)g->ampmax=vbi->ampmax; - g->ampmax=_vp_ampmax_decay(g->ampmax,v); - vbi->ampmax=g->ampmax; - - vb->pcm=(float **)_vorbis_block_alloc(vb,sizeof(*vb->pcm)*vi->channels); - vbi->pcmdelay=(float **)_vorbis_block_alloc(vb,sizeof(*vbi->pcmdelay)*vi->channels); - for(i=0;ichannels;i++){ - vbi->pcmdelay[i]= - (float *)_vorbis_block_alloc(vb,(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i])); - memcpy(vbi->pcmdelay[i],v->pcm[i],(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i])); - vb->pcm[i]=vbi->pcmdelay[i]+beginW; - - /* before we added the delay - vb->pcm[i]=_vorbis_block_alloc(vb,vb->pcmend*sizeof(*vb->pcm[i])); - memcpy(vb->pcm[i],v->pcm[i]+beginW,ci->blocksizes[v->W]*sizeof(*vb->pcm[i])); - */ - - } - - /* handle eof detection: eof==0 means that we've not yet received EOF - eof>0 marks the last 'real' sample in pcm[] - eof<0 'no more to do'; doesn't get here */ - - if(v->eofflag){ - if(v->centerW>=v->eofflag){ - v->eofflag=-1; - vb->eofflag=1; - return(1); - } - } - - /* advance storage vectors and clean up */ - { - int new_centerNext=ci->blocksizes[1]/2; - int movementW=centerNext-new_centerNext; - - if(movementW>0){ - - _ve_envelope_shift(b->ve,movementW); - v->pcm_current-=movementW; - - for(i=0;ichannels;i++) - memmove(v->pcm[i],v->pcm[i]+movementW, - v->pcm_current*sizeof(*v->pcm[i])); - - - v->lW=v->W; - v->W=v->nW; - v->centerW=new_centerNext; - - if(v->eofflag){ - v->eofflag-=movementW; - if(v->eofflag<=0)v->eofflag=-1; - /* do not add padding to end of stream! */ - if(v->centerW>=v->eofflag){ - v->granulepos+=movementW-(v->centerW-v->eofflag); - }else{ - v->granulepos+=movementW; - } - }else{ - v->granulepos+=movementW; - } - } - } - - /* done */ - return(1); -} - -int vorbis_synthesis_restart(vorbis_dsp_state *v){ - vorbis_info *vi=v->vi; - codec_setup_info *ci; - int hs; - - if(!v->backend_state)return -1; - if(!vi)return -1; - ci=(codec_setup_info *)vi->codec_setup; - if(!ci)return -1; - hs=ci->halfrate_flag; - - v->centerW=ci->blocksizes[1]>>(hs+1); - v->pcm_current=v->centerW>>hs; - - v->pcm_returned=-1; - v->granulepos=-1; - v->sequence=-1; - v->eofflag=0; - ((private_state *)(v->backend_state))->sample_count=-1; - - return(0); -} - -int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){ - if(_vds_shared_init(v,vi,0)){ - vorbis_dsp_clear(v); - return 1; - } - vorbis_synthesis_restart(v); - return 0; -} - -/* Unlike in analysis, the window is only partially applied for each - block. The time domain envelope is not yet handled at the point of - calling (as it relies on the previous block). */ - -int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){ - vorbis_info *vi=v->vi; - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - private_state *b=(private_state *)v->backend_state; - int hs=ci->halfrate_flag; - int i,j; - - if(!vb)return(OV_EINVAL); - if(v->pcm_current>v->pcm_returned && v->pcm_returned!=-1)return(OV_EINVAL); - - v->lW=v->W; - v->W=vb->W; - v->nW=-1; - - if((v->sequence==-1)|| - (v->sequence+1 != vb->sequence)){ - v->granulepos=-1; /* out of sequence; lose count */ - b->sample_count=-1; - } - - v->sequence=vb->sequence; - - if(vb->pcm){ /* no pcm to process if vorbis_synthesis_trackonly - was called on block */ - int n=ci->blocksizes[v->W]>>(hs+1); - int n0=ci->blocksizes[0]>>(hs+1); - int n1=ci->blocksizes[1]>>(hs+1); - - int thisCenter; - int prevCenter; - - v->glue_bits+=vb->glue_bits; - v->time_bits+=vb->time_bits; - v->floor_bits+=vb->floor_bits; - v->res_bits+=vb->res_bits; - - if(v->centerW){ - thisCenter=n1; - prevCenter=0; - }else{ - thisCenter=0; - prevCenter=n1; - } - - /* v->pcm is now used like a two-stage double buffer. We don't want - to have to constantly shift *or* adjust memory usage. Don't - accept a new block until the old is shifted out */ - - for(j=0;jchannels;j++){ - /* the overlap/add section */ - if(v->lW){ - if(v->W){ - /* large/large */ - const float *w=_vorbis_window_get(b->window[1]-hs); - float *pcm=v->pcm[j]+prevCenter; - float *p=vb->pcm[j]; - for(i=0;iwindow[0]-hs); - float *pcm=v->pcm[j]+prevCenter+n1/2-n0/2; - float *p=vb->pcm[j]; - for(i=0;iW){ - /* small/large */ - const float *w=_vorbis_window_get(b->window[0]-hs); - float *pcm=v->pcm[j]+prevCenter; - float *p=vb->pcm[j]+n1/2-n0/2; - for(i=0;iwindow[0]-hs); - float *pcm=v->pcm[j]+prevCenter; - float *p=vb->pcm[j]; - for(i=0;ipcm[j]+thisCenter; - float *p=vb->pcm[j]+n; - for(i=0;icenterW) - v->centerW=0; - else - v->centerW=n1; - - /* deal with initial packet state; we do this using the explicit - pcm_returned==-1 flag otherwise we're sensitive to first block - being short or long */ - - if(v->pcm_returned==-1){ - v->pcm_returned=thisCenter; - v->pcm_current=thisCenter; - }else{ - v->pcm_returned=prevCenter; - v->pcm_current=prevCenter+ - ((ci->blocksizes[v->lW]/4+ - ci->blocksizes[v->W]/4)>>hs); - } - - } - - /* track the frame number... This is for convenience, but also - making sure our last packet doesn't end with added padding. If - the last packet is partial, the number of samples we'll have to - return will be past the vb->granulepos. - - This is not foolproof! It will be confused if we begin - decoding at the last page after a seek or hole. In that case, - we don't have a starting point to judge where the last frame - is. For this reason, vorbisfile will always try to make sure - it reads the last two marked pages in proper sequence */ - - if(b->sample_count==-1){ - b->sample_count=0; - }else{ - b->sample_count+=ci->blocksizes[v->lW]/4+ci->blocksizes[v->W]/4; - } - - if(v->granulepos==-1){ - if(vb->granulepos!=-1){ /* only set if we have a position to set to */ - - v->granulepos=vb->granulepos; - - /* is this a short page? */ - if(b->sample_count>v->granulepos){ - /* corner case; if this is both the first and last audio page, - then spec says the end is cut, not beginning */ - long extra=b->sample_count-vb->granulepos; - - /* we use ogg_int64_t for granule positions because a - uint64 isn't universally available. Unfortunately, - that means granposes can be 'negative' and result in - extra being negative */ - if(extra<0) - extra=0; - - if(vb->eofflag){ - /* trim the end */ - /* no preceding granulepos; assume we started at zero (we'd - have to in a short single-page stream) */ - /* granulepos could be -1 due to a seek, but that would result - in a long count, not short count */ - - /* Guard against corrupt/malicious frames that set EOP and - a backdated granpos; don't rewind more samples than we - actually have */ - if(extra > (v->pcm_current - v->pcm_returned)<pcm_current - v->pcm_returned)<pcm_current-=extra>>hs; - }else{ - /* trim the beginning */ - v->pcm_returned+=extra>>hs; - if(v->pcm_returned>v->pcm_current) - v->pcm_returned=v->pcm_current; - } - - } - - } - }else{ - v->granulepos+=ci->blocksizes[v->lW]/4+ci->blocksizes[v->W]/4; - if(vb->granulepos!=-1 && v->granulepos!=vb->granulepos){ - - if(v->granulepos>vb->granulepos){ - long extra=v->granulepos-vb->granulepos; - - if(extra) - if(vb->eofflag){ - /* partial last frame. Strip the extra samples off */ - - /* Guard against corrupt/malicious frames that set EOP and - a backdated granpos; don't rewind more samples than we - actually have */ - if(extra > (v->pcm_current - v->pcm_returned)<pcm_current - v->pcm_returned)<pcm_current-=extra>>hs; - } /* else {Shouldn't happen *unless* the bitstream is out of - spec. Either way, believe the bitstream } */ - } /* else {Shouldn't happen *unless* the bitstream is out of - spec. Either way, believe the bitstream } */ - v->granulepos=vb->granulepos; - } - } - - /* Update, cleanup */ - - if(vb->eofflag)v->eofflag=1; - return(0); - -} - -/* pcm==NULL indicates we just want the pending samples, no more */ -int vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm){ - vorbis_info *vi=v->vi; - - if(v->pcm_returned>-1 && v->pcm_returnedpcm_current){ - if(pcm){ - int i; - for(i=0;ichannels;i++) - v->pcmret[i]=v->pcm[i]+v->pcm_returned; - *pcm=v->pcmret; - } - return(v->pcm_current-v->pcm_returned); - } - return(0); -} - -int vorbis_synthesis_read(vorbis_dsp_state *v,int n){ - if(n && v->pcm_returned+n>v->pcm_current)return(OV_EINVAL); - v->pcm_returned+=n; - return(0); -} - -/* intended for use with a specific vorbisfile feature; we want access - to the [usually synthetic/postextrapolated] buffer and lapping at - the end of a decode cycle, specifically, a half-short-block worth. - This funtion works like pcmout above, except it will also expose - this implicit buffer data not normally decoded. */ -int vorbis_synthesis_lapout(vorbis_dsp_state *v,float ***pcm){ - vorbis_info *vi=v->vi; - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - int hs=ci->halfrate_flag; - - int n=ci->blocksizes[v->W]>>(hs+1); - int n0=ci->blocksizes[0]>>(hs+1); - int n1=ci->blocksizes[1]>>(hs+1); - int i,j; - - if(v->pcm_returned<0)return 0; - - /* our returned data ends at pcm_returned; because the synthesis pcm - buffer is a two-fragment ring, that means our data block may be - fragmented by buffering, wrapping or a short block not filling - out a buffer. To simplify things, we unfragment if it's at all - possibly needed. Otherwise, we'd need to call lapout more than - once as well as hold additional dsp state. Opt for - simplicity. */ - - /* centerW was advanced by blockin; it would be the center of the - *next* block */ - if(v->centerW==n1){ - /* the data buffer wraps; swap the halves */ - /* slow, sure, small */ - for(j=0;jchannels;j++){ - float *p=v->pcm[j]; - for(i=0;ipcm_current-=n1; - v->pcm_returned-=n1; - v->centerW=0; - } - - /* solidify buffer into contiguous space */ - if((v->lW^v->W)==1){ - /* long/short or short/long */ - for(j=0;jchannels;j++){ - float *s=v->pcm[j]; - float *d=v->pcm[j]+(n1-n0)/2; - for(i=(n1+n0)/2-1;i>=0;--i) - d[i]=s[i]; - } - v->pcm_returned+=(n1-n0)/2; - v->pcm_current+=(n1-n0)/2; - }else{ - if(v->lW==0){ - /* short/short */ - for(j=0;jchannels;j++){ - float *s=v->pcm[j]; - float *d=v->pcm[j]+n1-n0; - for(i=n0-1;i>=0;--i) - d[i]=s[i]; - } - v->pcm_returned+=n1-n0; - v->pcm_current+=n1-n0; - } - } - - if(pcm){ - int i; - for(i=0;ichannels;i++) - v->pcmret[i]=v->pcm[i]+v->pcm_returned; - *pcm=v->pcmret; - } - - return(n1+n-v->pcm_returned); - -} - -const float *vorbis_window(vorbis_dsp_state *v,int W){ - vorbis_info *vi=v->vi; - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - int hs=ci->halfrate_flag; - private_state *b=(private_state *)v->backend_state; - - if(b->window[W]-1<0)return NULL; - return _vorbis_window_get(b->window[W]-hs); -} -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: PCM data envelope analysis - - ********************************************************************/ - -#include -#include -#include -#include -/*#include */ -/*#include "vorbis/codec.h"*/ -/*#include "codec_internal.h"*/ - -/*#include "os.h"*/ -/*#include "scales.h"*/ -/*#include "envelope.h"*/ -/*#include "mdct.h"*/ -/*#include "misc.h"*/ - -void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){ - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - vorbis_info_psy_global *gi=&ci->psy_g_param; - int ch=vi->channels; - int i,j; - int n=e->winlength=128; - e->searchstep=64; /* not random */ - - e->minenergy=gi->preecho_minenergy; - e->ch=ch; - e->storage=128; - e->cursor=ci->blocksizes[1]/2; - e->mdct_win=(float *)_ogg_calloc(n,sizeof(*e->mdct_win)); - mdct_init(&e->mdct,n); - - for(i=0;imdct_win[i]=sin(i/(n-1.)*M_PI); - e->mdct_win[i]*=e->mdct_win[i]; - } - - /* magic follows */ - e->band[0].begin=2; e->band[0].end=4; - e->band[1].begin=4; e->band[1].end=5; - e->band[2].begin=6; e->band[2].end=6; - e->band[3].begin=9; e->band[3].end=8; - e->band[4].begin=13; e->band[4].end=8; - e->band[5].begin=17; e->band[5].end=8; - e->band[6].begin=22; e->band[6].end=8; - - for(j=0;jband[j].end; - e->band[j].window=(float *)_ogg_malloc(n*sizeof(*e->band[0].window)); - for(i=0;iband[j].window[i]=sin((i+.5)/n*M_PI); - e->band[j].total+=e->band[j].window[i]; - } - e->band[j].total=1./e->band[j].total; - } - - e->filter=(envelope_filter_state *)_ogg_calloc(VE_BANDS*ch,sizeof(*e->filter)); - e->mark=(int *)_ogg_calloc(e->storage,sizeof(*e->mark)); - -} - -void _ve_envelope_clear(envelope_lookup *e){ - int i; - mdct_clear(&e->mdct); - for(i=0;iband[i].window); - _ogg_free(e->mdct_win); - _ogg_free(e->filter); - _ogg_free(e->mark); - memset(e,0,sizeof(*e)); -} - -/* fairly straight threshhold-by-band based until we find something - that works better and isn't patented. */ - -static int _ve_amp(envelope_lookup *ve, - vorbis_info_psy_global *gi, - float *data, - envelope_band *bands, - envelope_filter_state *filters){ - long n=ve->winlength; - int ret=0; - long i,j; - float decay; - - /* we want to have a 'minimum bar' for energy, else we're just - basing blocks on quantization noise that outweighs the signal - itself (for low power signals) */ - - float minV=ve->minenergy; - float *vec=(float *)alloca(n*sizeof(*vec)); - - /* stretch is used to gradually lengthen the number of windows - considered prevoius-to-potential-trigger */ - int stretch=max(VE_MINSTRETCH,ve->stretch/2); - float penalty=gi->stretch_penalty-(ve->stretch/2-VE_MINSTRETCH); - if(penalty<0.f)penalty=0.f; - if(penalty>gi->stretch_penalty)penalty=gi->stretch_penalty; - - /*_analysis_output_always("lpcm",seq2,data,n,0,0, - totalshift+pos*ve->searchstep);*/ - - /* window and transform */ - for(i=0;imdct_win[i]; - mdct_forward(&ve->mdct,vec,vec); - - /*_analysis_output_always("mdct",seq2,vec,n/2,0,1,0); */ - - /* near-DC spreading function; this has nothing to do with - psychoacoustics, just sidelobe leakage and window size */ - { - float temp=vec[0]*vec[0]+.7*vec[1]*vec[1]+.2*vec[2]*vec[2]; - int ptr=filters->nearptr; - - /* the accumulation is regularly refreshed from scratch to avoid - floating point creep */ - if(ptr==0){ - decay=filters->nearDC_acc=filters->nearDC_partialacc+temp; - filters->nearDC_partialacc=temp; - }else{ - decay=filters->nearDC_acc+=temp; - filters->nearDC_partialacc+=temp; - } - filters->nearDC_acc-=filters->nearDC[ptr]; - filters->nearDC[ptr]=temp; - - decay*=(1./(VE_NEARDC+1)); - filters->nearptr++; - if(filters->nearptr>=VE_NEARDC)filters->nearptr=0; - decay=todB(&decay)*.5-15.f; - } - - /* perform spreading and limiting, also smooth the spectrum. yes, - the MDCT results in all real coefficients, but it still *behaves* - like real/imaginary pairs */ - for(i=0;i>1]=val; - decay-=8.; - } - - /*_analysis_output_always("spread",seq2++,vec,n/4,0,0,0);*/ - - /* perform preecho/postecho triggering by band */ - for(j=0;j=VE_AMP)filters[j].ampptr=0; - } - - /* look at min/max, decide trigger */ - if(valmax>gi->preecho_thresh[j]+penalty){ - ret|=1; - ret|=4; - } - if(valminpostecho_thresh[j]-penalty)ret|=2; - } - - return(ret); -} - -#if 0 -static int seq=0; -static ogg_int64_t totalshift=-1024; -#endif - -long _ve_envelope_search(vorbis_dsp_state *v){ - vorbis_info *vi=v->vi; - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - vorbis_info_psy_global *gi=&ci->psy_g_param; - envelope_lookup *ve=((private_state *)(v->backend_state))->ve; - long i,j; - - int first=ve->current/ve->searchstep; - int last=v->pcm_current/ve->searchstep-VE_WIN; - if(first<0)first=0; - - /* make sure we have enough storage to match the PCM */ - if(last+VE_WIN+VE_POST>ve->storage){ - ve->storage=last+VE_WIN+VE_POST; /* be sure */ - ve->mark=(int *)_ogg_realloc(ve->mark,ve->storage*sizeof(*ve->mark)); - } - - for(j=first;jstretch++; - if(ve->stretch>VE_MAXSTRETCH*2) - ve->stretch=VE_MAXSTRETCH*2; - - for(i=0;ich;i++){ - float *pcm=v->pcm[i]+ve->searchstep*(j); - ret|=_ve_amp(ve,gi,pcm,ve->band,ve->filter+i*VE_BANDS); - } - - ve->mark[j+VE_POST]=0; - if(ret&1){ - ve->mark[j]=1; - ve->mark[j+1]=1; - } - - if(ret&2){ - ve->mark[j]=1; - if(j>0)ve->mark[j-1]=1; - } - - if(ret&4)ve->stretch=-1; - } - - ve->current=last*ve->searchstep; - - { - long centerW=v->centerW; - long testW= - centerW+ - ci->blocksizes[v->W]/4+ - ci->blocksizes[1]/2+ - ci->blocksizes[0]/4; - - j=ve->cursor; - - while(jcurrent-(ve->searchstep)){/* account for postecho - working back one window */ - if(j>=testW)return(1); - - ve->cursor=j; - - if(ve->mark[j/ve->searchstep]){ - if(j>centerW){ - -#if 0 - if(j>ve->curmark){ - float *marker=alloca(v->pcm_current*sizeof(*marker)); - int l,m; - memset(marker,0,sizeof(*marker)*v->pcm_current); - fprintf(stderr,"mark! seq=%d, cursor:%fs time:%fs\n", - seq, - (totalshift+ve->cursor)/44100., - (totalshift+j)/44100.); - _analysis_output_always("pcmL",seq,v->pcm[0],v->pcm_current,0,0,totalshift); - _analysis_output_always("pcmR",seq,v->pcm[1],v->pcm_current,0,0,totalshift); - - _analysis_output_always("markL",seq,v->pcm[0],j,0,0,totalshift); - _analysis_output_always("markR",seq,v->pcm[1],j,0,0,totalshift); - - for(m=0;msearchstep]=ve->filter[m].markers[l]*.1; - _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift); - } - - for(m=0;msearchstep]=ve->filter[m+VE_BANDS].markers[l]*.1; - _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift); - } - - for(l=0;lsearchstep]=ve->mark[l]*.4; - _analysis_output_always("mark",seq,marker,v->pcm_current,0,0,totalshift); - - - seq++; - - } -#endif - - ve->curmark=j; - if(j>=testW)return(1); - return(0); - } - } - j+=ve->searchstep; - } - } - - return(-1); -} - -int _ve_envelope_mark(vorbis_dsp_state *v){ - envelope_lookup *ve=((private_state *)(v->backend_state))->ve; - vorbis_info *vi=v->vi; - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - long centerW=v->centerW; - long beginW=centerW-ci->blocksizes[v->W]/4; - long endW=centerW+ci->blocksizes[v->W]/4; - if(v->W){ - beginW-=ci->blocksizes[v->lW]/4; - endW+=ci->blocksizes[v->nW]/4; - }else{ - beginW-=ci->blocksizes[0]/4; - endW+=ci->blocksizes[0]/4; - } - - if(ve->curmark>=beginW && ve->curmarksearchstep; - long last=endW/ve->searchstep; - long i; - for(i=first;imark[i])return(1); - } - return(0); -} - -void _ve_envelope_shift(envelope_lookup *e,long shift){ - int smallsize=e->current/e->searchstep+VE_POST; /* adjust for placing marks - ahead of ve->current */ - int smallshift=shift/e->searchstep; - - memmove(e->mark,e->mark+smallshift,(smallsize-smallshift)*sizeof(*e->mark)); - -#if 0 - for(i=0;ich;i++) - memmove(e->filter[i].markers, - e->filter[i].markers+smallshift, - (1024-smallshift)*sizeof(*(*e->filter).markers)); - totalshift+=shift; -#endif - - e->current-=shift; - if(e->curmark>=0) - e->curmark-=shift; - e->cursor-=shift; -} -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: window functions - - ********************************************************************/ - -#include -#include -/*#include "os.h"*/ -/*#include "misc.h"*/ -/*#include "window.h"*/ - -static const float vwin64[32] = { - 0.0009460463F, 0.0085006468F, 0.0235352254F, 0.0458950567F, - 0.0753351908F, 0.1115073077F, 0.1539457973F, 0.2020557475F, - 0.2551056759F, 0.3122276645F, 0.3724270287F, 0.4346027792F, - 0.4975789974F, 0.5601459521F, 0.6211085051F, 0.6793382689F, - 0.7338252629F, 0.7837245849F, 0.8283939355F, 0.8674186656F, - 0.9006222429F, 0.9280614787F, 0.9500073081F, 0.9669131782F, - 0.9793740220F, 0.9880792941F, 0.9937636139F, 0.9971582668F, - 0.9989462667F, 0.9997230082F, 0.9999638688F, 0.9999995525F, -}; - -static const float vwin128[64] = { - 0.0002365472F, 0.0021280687F, 0.0059065254F, 0.0115626550F, - 0.0190823442F, 0.0284463735F, 0.0396300935F, 0.0526030430F, - 0.0673285281F, 0.0837631763F, 0.1018564887F, 0.1215504095F, - 0.1427789367F, 0.1654677960F, 0.1895342001F, 0.2148867160F, - 0.2414252576F, 0.2690412240F, 0.2976177952F, 0.3270303960F, - 0.3571473350F, 0.3878306189F, 0.4189369387F, 0.4503188188F, - 0.4818259135F, 0.5133064334F, 0.5446086751F, 0.5755826278F, - 0.6060816248F, 0.6359640047F, 0.6650947483F, 0.6933470543F, - 0.7206038179F, 0.7467589810F, 0.7717187213F, 0.7954024542F, - 0.8177436264F, 0.8386902831F, 0.8582053981F, 0.8762669622F, - 0.8928678298F, 0.9080153310F, 0.9217306608F, 0.9340480615F, - 0.9450138200F, 0.9546851041F, 0.9631286621F, 0.9704194171F, - 0.9766389810F, 0.9818741197F, 0.9862151938F, 0.9897546035F, - 0.9925852598F, 0.9947991032F, 0.9964856900F, 0.9977308602F, - 0.9986155015F, 0.9992144193F, 0.9995953200F, 0.9998179155F, - 0.9999331503F, 0.9999825563F, 0.9999977357F, 0.9999999720F, -}; - -static const float vwin256[128] = { - 0.0000591390F, 0.0005321979F, 0.0014780301F, 0.0028960636F, - 0.0047854363F, 0.0071449926F, 0.0099732775F, 0.0132685298F, - 0.0170286741F, 0.0212513119F, 0.0259337111F, 0.0310727950F, - 0.0366651302F, 0.0427069140F, 0.0491939614F, 0.0561216907F, - 0.0634851102F, 0.0712788035F, 0.0794969160F, 0.0881331402F, - 0.0971807028F, 0.1066323515F, 0.1164803426F, 0.1267164297F, - 0.1373318534F, 0.1483173323F, 0.1596630553F, 0.1713586755F, - 0.1833933062F, 0.1957555184F, 0.2084333404F, 0.2214142599F, - 0.2346852280F, 0.2482326664F, 0.2620424757F, 0.2761000481F, - 0.2903902813F, 0.3048975959F, 0.3196059553F, 0.3344988887F, - 0.3495595160F, 0.3647705766F, 0.3801144597F, 0.3955732382F, - 0.4111287047F, 0.4267624093F, 0.4424557009F, 0.4581897696F, - 0.4739456913F, 0.4897044744F, 0.5054471075F, 0.5211546088F, - 0.5368080763F, 0.5523887395F, 0.5678780103F, 0.5832575361F, - 0.5985092508F, 0.6136154277F, 0.6285587300F, 0.6433222619F, - 0.6578896175F, 0.6722449294F, 0.6863729144F, 0.7002589187F, - 0.7138889597F, 0.7272497662F, 0.7403288154F, 0.7531143679F, - 0.7655954985F, 0.7777621249F, 0.7896050322F, 0.8011158947F, - 0.8122872932F, 0.8231127294F, 0.8335866365F, 0.8437043850F, - 0.8534622861F, 0.8628575905F, 0.8718884835F, 0.8805540765F, - 0.8888543947F, 0.8967903616F, 0.9043637797F, 0.9115773078F, - 0.9184344360F, 0.9249394562F, 0.9310974312F, 0.9369141608F, - 0.9423961446F, 0.9475505439F, 0.9523851406F, 0.9569082947F, - 0.9611289005F, 0.9650563408F, 0.9687004405F, 0.9720714191F, - 0.9751798427F, 0.9780365753F, 0.9806527301F, 0.9830396204F, - 0.9852087111F, 0.9871715701F, 0.9889398207F, 0.9905250941F, - 0.9919389832F, 0.9931929973F, 0.9942985174F, 0.9952667537F, - 0.9961087037F, 0.9968351119F, 0.9974564312F, 0.9979827858F, - 0.9984239359F, 0.9987892441F, 0.9990876435F, 0.9993276081F, - 0.9995171241F, 0.9996636648F, 0.9997741654F, 0.9998550016F, - 0.9999119692F, 0.9999502656F, 0.9999744742F, 0.9999885497F, - 0.9999958064F, 0.9999989077F, 0.9999998584F, 0.9999999983F, -}; - -static const float vwin512[256] = { - 0.0000147849F, 0.0001330607F, 0.0003695946F, 0.0007243509F, - 0.0011972759F, 0.0017882983F, 0.0024973285F, 0.0033242588F, - 0.0042689632F, 0.0053312973F, 0.0065110982F, 0.0078081841F, - 0.0092223540F, 0.0107533880F, 0.0124010466F, 0.0141650703F, - 0.0160451800F, 0.0180410758F, 0.0201524373F, 0.0223789233F, - 0.0247201710F, 0.0271757958F, 0.0297453914F, 0.0324285286F, - 0.0352247556F, 0.0381335972F, 0.0411545545F, 0.0442871045F, - 0.0475306997F, 0.0508847676F, 0.0543487103F, 0.0579219038F, - 0.0616036982F, 0.0653934164F, 0.0692903546F, 0.0732937809F, - 0.0774029356F, 0.0816170305F, 0.0859352485F, 0.0903567428F, - 0.0948806375F, 0.0995060259F, 0.1042319712F, 0.1090575056F, - 0.1139816300F, 0.1190033137F, 0.1241214941F, 0.1293350764F, - 0.1346429333F, 0.1400439046F, 0.1455367974F, 0.1511203852F, - 0.1567934083F, 0.1625545735F, 0.1684025537F, 0.1743359881F, - 0.1803534820F, 0.1864536069F, 0.1926349000F, 0.1988958650F, - 0.2052349715F, 0.2116506555F, 0.2181413191F, 0.2247053313F, - 0.2313410275F, 0.2380467105F, 0.2448206500F, 0.2516610835F, - 0.2585662164F, 0.2655342226F, 0.2725632448F, 0.2796513950F, - 0.2867967551F, 0.2939973773F, 0.3012512852F, 0.3085564739F, - 0.3159109111F, 0.3233125375F, 0.3307592680F, 0.3382489922F, - 0.3457795756F, 0.3533488602F, 0.3609546657F, 0.3685947904F, - 0.3762670121F, 0.3839690896F, 0.3916987634F, 0.3994537572F, - 0.4072317788F, 0.4150305215F, 0.4228476653F, 0.4306808783F, - 0.4385278181F, 0.4463861329F, 0.4542534630F, 0.4621274424F, - 0.4700057001F, 0.4778858615F, 0.4857655502F, 0.4936423891F, - 0.5015140023F, 0.5093780165F, 0.5172320626F, 0.5250737772F, - 0.5329008043F, 0.5407107971F, 0.5485014192F, 0.5562703465F, - 0.5640152688F, 0.5717338914F, 0.5794239366F, 0.5870831457F, - 0.5947092801F, 0.6023001235F, 0.6098534829F, 0.6173671907F, - 0.6248391059F, 0.6322671161F, 0.6396491384F, 0.6469831217F, - 0.6542670475F, 0.6614989319F, 0.6686768267F, 0.6757988210F, - 0.6828630426F, 0.6898676592F, 0.6968108799F, 0.7036909564F, - 0.7105061843F, 0.7172549043F, 0.7239355032F, 0.7305464154F, - 0.7370861235F, 0.7435531598F, 0.7499461068F, 0.7562635986F, - 0.7625043214F, 0.7686670148F, 0.7747504721F, 0.7807535410F, - 0.7866751247F, 0.7925141825F, 0.7982697296F, 0.8039408387F, - 0.8095266395F, 0.8150263196F, 0.8204391248F, 0.8257643590F, - 0.8310013848F, 0.8361496236F, 0.8412085555F, 0.8461777194F, - 0.8510567129F, 0.8558451924F, 0.8605428730F, 0.8651495278F, - 0.8696649882F, 0.8740891432F, 0.8784219392F, 0.8826633797F, - 0.8868135244F, 0.8908724888F, 0.8948404441F, 0.8987176157F, - 0.9025042831F, 0.9062007791F, 0.9098074886F, 0.9133248482F, - 0.9167533451F, 0.9200935163F, 0.9233459472F, 0.9265112712F, - 0.9295901680F, 0.9325833632F, 0.9354916263F, 0.9383157705F, - 0.9410566504F, 0.9437151618F, 0.9462922398F, 0.9487888576F, - 0.9512060252F, 0.9535447882F, 0.9558062262F, 0.9579914516F, - 0.9601016078F, 0.9621378683F, 0.9641014348F, 0.9659935361F, - 0.9678154261F, 0.9695683830F, 0.9712537071F, 0.9728727198F, - 0.9744267618F, 0.9759171916F, 0.9773453842F, 0.9787127293F, - 0.9800206298F, 0.9812705006F, 0.9824637665F, 0.9836018613F, - 0.9846862258F, 0.9857183066F, 0.9866995544F, 0.9876314227F, - 0.9885153662F, 0.9893528393F, 0.9901452948F, 0.9908941823F, - 0.9916009470F, 0.9922670279F, 0.9928938570F, 0.9934828574F, - 0.9940354423F, 0.9945530133F, 0.9950369595F, 0.9954886562F, - 0.9959094633F, 0.9963007242F, 0.9966637649F, 0.9969998925F, - 0.9973103939F, 0.9975965351F, 0.9978595598F, 0.9981006885F, - 0.9983211172F, 0.9985220166F, 0.9987045311F, 0.9988697776F, - 0.9990188449F, 0.9991527924F, 0.9992726499F, 0.9993794157F, - 0.9994740570F, 0.9995575079F, 0.9996306699F, 0.9996944099F, - 0.9997495605F, 0.9997969190F, 0.9998372465F, 0.9998712678F, - 0.9998996704F, 0.9999231041F, 0.9999421807F, 0.9999574732F, - 0.9999695157F, 0.9999788026F, 0.9999857885F, 0.9999908879F, - 0.9999944746F, 0.9999968817F, 0.9999984010F, 0.9999992833F, - 0.9999997377F, 0.9999999317F, 0.9999999911F, 0.9999999999F, -}; - -static const float vwin1024[512] = { - 0.0000036962F, 0.0000332659F, 0.0000924041F, 0.0001811086F, - 0.0002993761F, 0.0004472021F, 0.0006245811F, 0.0008315063F, - 0.0010679699F, 0.0013339631F, 0.0016294757F, 0.0019544965F, - 0.0023090133F, 0.0026930125F, 0.0031064797F, 0.0035493989F, - 0.0040217533F, 0.0045235250F, 0.0050546946F, 0.0056152418F, - 0.0062051451F, 0.0068243817F, 0.0074729278F, 0.0081507582F, - 0.0088578466F, 0.0095941655F, 0.0103596863F, 0.0111543789F, - 0.0119782122F, 0.0128311538F, 0.0137131701F, 0.0146242260F, - 0.0155642855F, 0.0165333111F, 0.0175312640F, 0.0185581042F, - 0.0196137903F, 0.0206982797F, 0.0218115284F, 0.0229534910F, - 0.0241241208F, 0.0253233698F, 0.0265511886F, 0.0278075263F, - 0.0290923308F, 0.0304055484F, 0.0317471241F, 0.0331170013F, - 0.0345151222F, 0.0359414274F, 0.0373958560F, 0.0388783456F, - 0.0403888325F, 0.0419272511F, 0.0434935347F, 0.0450876148F, - 0.0467094213F, 0.0483588828F, 0.0500359261F, 0.0517404765F, - 0.0534724575F, 0.0552317913F, 0.0570183983F, 0.0588321971F, - 0.0606731048F, 0.0625410369F, 0.0644359070F, 0.0663576272F, - 0.0683061077F, 0.0702812571F, 0.0722829821F, 0.0743111878F, - 0.0763657775F, 0.0784466526F, 0.0805537129F, 0.0826868561F, - 0.0848459782F, 0.0870309736F, 0.0892417345F, 0.0914781514F, - 0.0937401128F, 0.0960275056F, 0.0983402145F, 0.1006781223F, - 0.1030411101F, 0.1054290568F, 0.1078418397F, 0.1102793336F, - 0.1127414119F, 0.1152279457F, 0.1177388042F, 0.1202738544F, - 0.1228329618F, 0.1254159892F, 0.1280227980F, 0.1306532471F, - 0.1333071937F, 0.1359844927F, 0.1386849970F, 0.1414085575F, - 0.1441550230F, 0.1469242403F, 0.1497160539F, 0.1525303063F, - 0.1553668381F, 0.1582254875F, 0.1611060909F, 0.1640084822F, - 0.1669324936F, 0.1698779549F, 0.1728446939F, 0.1758325362F, - 0.1788413055F, 0.1818708232F, 0.1849209084F, 0.1879913785F, - 0.1910820485F, 0.1941927312F, 0.1973232376F, 0.2004733764F, - 0.2036429541F, 0.2068317752F, 0.2100396421F, 0.2132663552F, - 0.2165117125F, 0.2197755102F, 0.2230575422F, 0.2263576007F, - 0.2296754753F, 0.2330109540F, 0.2363638225F, 0.2397338646F, - 0.2431208619F, 0.2465245941F, 0.2499448389F, 0.2533813719F, - 0.2568339669F, 0.2603023956F, 0.2637864277F, 0.2672858312F, - 0.2708003718F, 0.2743298135F, 0.2778739186F, 0.2814324472F, - 0.2850051576F, 0.2885918065F, 0.2921921485F, 0.2958059366F, - 0.2994329219F, 0.3030728538F, 0.3067254799F, 0.3103905462F, - 0.3140677969F, 0.3177569747F, 0.3214578205F, 0.3251700736F, - 0.3288934718F, 0.3326277513F, 0.3363726468F, 0.3401278914F, - 0.3438932168F, 0.3476683533F, 0.3514530297F, 0.3552469734F, - 0.3590499106F, 0.3628615659F, 0.3666816630F, 0.3705099239F, - 0.3743460698F, 0.3781898204F, 0.3820408945F, 0.3858990095F, - 0.3897638820F, 0.3936352274F, 0.3975127601F, 0.4013961936F, - 0.4052852405F, 0.4091796123F, 0.4130790198F, 0.4169831732F, - 0.4208917815F, 0.4248045534F, 0.4287211965F, 0.4326414181F, - 0.4365649248F, 0.4404914225F, 0.4444206167F, 0.4483522125F, - 0.4522859146F, 0.4562214270F, 0.4601584538F, 0.4640966984F, - 0.4680358644F, 0.4719756548F, 0.4759157726F, 0.4798559209F, - 0.4837958024F, 0.4877351199F, 0.4916735765F, 0.4956108751F, - 0.4995467188F, 0.5034808109F, 0.5074128550F, 0.5113425550F, - 0.5152696149F, 0.5191937395F, 0.5231146336F, 0.5270320028F, - 0.5309455530F, 0.5348549910F, 0.5387600239F, 0.5426603597F, - 0.5465557070F, 0.5504457754F, 0.5543302752F, 0.5582089175F, - 0.5620814145F, 0.5659474793F, 0.5698068262F, 0.5736591704F, - 0.5775042283F, 0.5813417176F, 0.5851713571F, 0.5889928670F, - 0.5928059689F, 0.5966103856F, 0.6004058415F, 0.6041920626F, - 0.6079687761F, 0.6117357113F, 0.6154925986F, 0.6192391705F, - 0.6229751612F, 0.6267003064F, 0.6304143441F, 0.6341170137F, - 0.6378080569F, 0.6414872173F, 0.6451542405F, 0.6488088741F, - 0.6524508681F, 0.6560799742F, 0.6596959469F, 0.6632985424F, - 0.6668875197F, 0.6704626398F, 0.6740236662F, 0.6775703649F, - 0.6811025043F, 0.6846198554F, 0.6881221916F, 0.6916092892F, - 0.6950809269F, 0.6985368861F, 0.7019769510F, 0.7054009085F, - 0.7088085484F, 0.7121996632F, 0.7155740484F, 0.7189315023F, - 0.7222718263F, 0.7255948245F, 0.7289003043F, 0.7321880760F, - 0.7354579530F, 0.7387097518F, 0.7419432921F, 0.7451583966F, - 0.7483548915F, 0.7515326059F, 0.7546913723F, 0.7578310265F, - 0.7609514077F, 0.7640523581F, 0.7671337237F, 0.7701953535F, - 0.7732371001F, 0.7762588195F, 0.7792603711F, 0.7822416178F, - 0.7852024259F, 0.7881426654F, 0.7910622097F, 0.7939609356F, - 0.7968387237F, 0.7996954579F, 0.8025310261F, 0.8053453193F, - 0.8081382324F, 0.8109096638F, 0.8136595156F, 0.8163876936F, - 0.8190941071F, 0.8217786690F, 0.8244412960F, 0.8270819086F, - 0.8297004305F, 0.8322967896F, 0.8348709171F, 0.8374227481F, - 0.8399522213F, 0.8424592789F, 0.8449438672F, 0.8474059356F, - 0.8498454378F, 0.8522623306F, 0.8546565748F, 0.8570281348F, - 0.8593769787F, 0.8617030779F, 0.8640064080F, 0.8662869477F, - 0.8685446796F, 0.8707795899F, 0.8729916682F, 0.8751809079F, - 0.8773473059F, 0.8794908626F, 0.8816115819F, 0.8837094713F, - 0.8857845418F, 0.8878368079F, 0.8898662874F, 0.8918730019F, - 0.8938569760F, 0.8958182380F, 0.8977568194F, 0.8996727552F, - 0.9015660837F, 0.9034368465F, 0.9052850885F, 0.9071108577F, - 0.9089142057F, 0.9106951869F, 0.9124538591F, 0.9141902832F, - 0.9159045233F, 0.9175966464F, 0.9192667228F, 0.9209148257F, - 0.9225410313F, 0.9241454187F, 0.9257280701F, 0.9272890704F, - 0.9288285075F, 0.9303464720F, 0.9318430576F, 0.9333183603F, - 0.9347724792F, 0.9362055158F, 0.9376175745F, 0.9390087622F, - 0.9403791881F, 0.9417289644F, 0.9430582055F, 0.9443670283F, - 0.9456555521F, 0.9469238986F, 0.9481721917F, 0.9494005577F, - 0.9506091252F, 0.9517980248F, 0.9529673894F, 0.9541173540F, - 0.9552480557F, 0.9563596334F, 0.9574522282F, 0.9585259830F, - 0.9595810428F, 0.9606175542F, 0.9616356656F, 0.9626355274F, - 0.9636172915F, 0.9645811114F, 0.9655271425F, 0.9664555414F, - 0.9673664664F, 0.9682600774F, 0.9691365355F, 0.9699960034F, - 0.9708386448F, 0.9716646250F, 0.9724741103F, 0.9732672685F, - 0.9740442683F, 0.9748052795F, 0.9755504729F, 0.9762800205F, - 0.9769940950F, 0.9776928703F, 0.9783765210F, 0.9790452223F, - 0.9796991504F, 0.9803384823F, 0.9809633954F, 0.9815740679F, - 0.9821706784F, 0.9827534063F, 0.9833224312F, 0.9838779332F, - 0.9844200928F, 0.9849490910F, 0.9854651087F, 0.9859683274F, - 0.9864589286F, 0.9869370940F, 0.9874030054F, 0.9878568447F, - 0.9882987937F, 0.9887290343F, 0.9891477481F, 0.9895551169F, - 0.9899513220F, 0.9903365446F, 0.9907109658F, 0.9910747662F, - 0.9914281260F, 0.9917712252F, 0.9921042433F, 0.9924273593F, - 0.9927407516F, 0.9930445982F, 0.9933390763F, 0.9936243626F, - 0.9939006331F, 0.9941680631F, 0.9944268269F, 0.9946770982F, - 0.9949190498F, 0.9951528537F, 0.9953786808F, 0.9955967011F, - 0.9958070836F, 0.9960099963F, 0.9962056061F, 0.9963940787F, - 0.9965755786F, 0.9967502693F, 0.9969183129F, 0.9970798704F, - 0.9972351013F, 0.9973841640F, 0.9975272151F, 0.9976644103F, - 0.9977959036F, 0.9979218476F, 0.9980423932F, 0.9981576901F, - 0.9982678862F, 0.9983731278F, 0.9984735596F, 0.9985693247F, - 0.9986605645F, 0.9987474186F, 0.9988300248F, 0.9989085193F, - 0.9989830364F, 0.9990537085F, 0.9991206662F, 0.9991840382F, - 0.9992439513F, 0.9993005303F, 0.9993538982F, 0.9994041757F, - 0.9994514817F, 0.9994959330F, 0.9995376444F, 0.9995767286F, - 0.9996132960F, 0.9996474550F, 0.9996793121F, 0.9997089710F, - 0.9997365339F, 0.9997621003F, 0.9997857677F, 0.9998076311F, - 0.9998277836F, 0.9998463156F, 0.9998633155F, 0.9998788692F, - 0.9998930603F, 0.9999059701F, 0.9999176774F, 0.9999282586F, - 0.9999377880F, 0.9999463370F, 0.9999539749F, 0.9999607685F, - 0.9999667820F, 0.9999720773F, 0.9999767136F, 0.9999807479F, - 0.9999842344F, 0.9999872249F, 0.9999897688F, 0.9999919127F, - 0.9999937009F, 0.9999951749F, 0.9999963738F, 0.9999973342F, - 0.9999980900F, 0.9999986724F, 0.9999991103F, 0.9999994297F, - 0.9999996543F, 0.9999998049F, 0.9999999000F, 0.9999999552F, - 0.9999999836F, 0.9999999957F, 0.9999999994F, 1.0000000000F, -}; - -static const float vwin2048[1024] = { - 0.0000009241F, 0.0000083165F, 0.0000231014F, 0.0000452785F, - 0.0000748476F, 0.0001118085F, 0.0001561608F, 0.0002079041F, - 0.0002670379F, 0.0003335617F, 0.0004074748F, 0.0004887765F, - 0.0005774661F, 0.0006735427F, 0.0007770054F, 0.0008878533F, - 0.0010060853F, 0.0011317002F, 0.0012646969F, 0.0014050742F, - 0.0015528307F, 0.0017079650F, 0.0018704756F, 0.0020403610F, - 0.0022176196F, 0.0024022497F, 0.0025942495F, 0.0027936173F, - 0.0030003511F, 0.0032144490F, 0.0034359088F, 0.0036647286F, - 0.0039009061F, 0.0041444391F, 0.0043953253F, 0.0046535621F, - 0.0049191472F, 0.0051920781F, 0.0054723520F, 0.0057599664F, - 0.0060549184F, 0.0063572052F, 0.0066668239F, 0.0069837715F, - 0.0073080449F, 0.0076396410F, 0.0079785566F, 0.0083247884F, - 0.0086783330F, 0.0090391871F, 0.0094073470F, 0.0097828092F, - 0.0101655700F, 0.0105556258F, 0.0109529726F, 0.0113576065F, - 0.0117695237F, 0.0121887200F, 0.0126151913F, 0.0130489335F, - 0.0134899422F, 0.0139382130F, 0.0143937415F, 0.0148565233F, - 0.0153265536F, 0.0158038279F, 0.0162883413F, 0.0167800889F, - 0.0172790660F, 0.0177852675F, 0.0182986882F, 0.0188193231F, - 0.0193471668F, 0.0198822141F, 0.0204244594F, 0.0209738974F, - 0.0215305225F, 0.0220943289F, 0.0226653109F, 0.0232434627F, - 0.0238287784F, 0.0244212519F, 0.0250208772F, 0.0256276481F, - 0.0262415582F, 0.0268626014F, 0.0274907711F, 0.0281260608F, - 0.0287684638F, 0.0294179736F, 0.0300745833F, 0.0307382859F, - 0.0314090747F, 0.0320869424F, 0.0327718819F, 0.0334638860F, - 0.0341629474F, 0.0348690586F, 0.0355822122F, 0.0363024004F, - 0.0370296157F, 0.0377638502F, 0.0385050960F, 0.0392533451F, - 0.0400085896F, 0.0407708211F, 0.0415400315F, 0.0423162123F, - 0.0430993552F, 0.0438894515F, 0.0446864926F, 0.0454904698F, - 0.0463013742F, 0.0471191969F, 0.0479439288F, 0.0487755607F, - 0.0496140836F, 0.0504594879F, 0.0513117642F, 0.0521709031F, - 0.0530368949F, 0.0539097297F, 0.0547893979F, 0.0556758894F, - 0.0565691941F, 0.0574693019F, 0.0583762026F, 0.0592898858F, - 0.0602103410F, 0.0611375576F, 0.0620715250F, 0.0630122324F, - 0.0639596688F, 0.0649138234F, 0.0658746848F, 0.0668422421F, - 0.0678164838F, 0.0687973985F, 0.0697849746F, 0.0707792005F, - 0.0717800645F, 0.0727875547F, 0.0738016591F, 0.0748223656F, - 0.0758496620F, 0.0768835359F, 0.0779239751F, 0.0789709668F, - 0.0800244985F, 0.0810845574F, 0.0821511306F, 0.0832242052F, - 0.0843037679F, 0.0853898056F, 0.0864823050F, 0.0875812525F, - 0.0886866347F, 0.0897984378F, 0.0909166480F, 0.0920412513F, - 0.0931722338F, 0.0943095813F, 0.0954532795F, 0.0966033140F, - 0.0977596702F, 0.0989223336F, 0.1000912894F, 0.1012665227F, - 0.1024480185F, 0.1036357616F, 0.1048297369F, 0.1060299290F, - 0.1072363224F, 0.1084489014F, 0.1096676504F, 0.1108925534F, - 0.1121235946F, 0.1133607577F, 0.1146040267F, 0.1158533850F, - 0.1171088163F, 0.1183703040F, 0.1196378312F, 0.1209113812F, - 0.1221909370F, 0.1234764815F, 0.1247679974F, 0.1260654674F, - 0.1273688740F, 0.1286781995F, 0.1299934263F, 0.1313145365F, - 0.1326415121F, 0.1339743349F, 0.1353129866F, 0.1366574490F, - 0.1380077035F, 0.1393637315F, 0.1407255141F, 0.1420930325F, - 0.1434662677F, 0.1448452004F, 0.1462298115F, 0.1476200814F, - 0.1490159906F, 0.1504175195F, 0.1518246482F, 0.1532373569F, - 0.1546556253F, 0.1560794333F, 0.1575087606F, 0.1589435866F, - 0.1603838909F, 0.1618296526F, 0.1632808509F, 0.1647374648F, - 0.1661994731F, 0.1676668546F, 0.1691395880F, 0.1706176516F, - 0.1721010238F, 0.1735896829F, 0.1750836068F, 0.1765827736F, - 0.1780871610F, 0.1795967468F, 0.1811115084F, 0.1826314234F, - 0.1841564689F, 0.1856866221F, 0.1872218600F, 0.1887621595F, - 0.1903074974F, 0.1918578503F, 0.1934131947F, 0.1949735068F, - 0.1965387630F, 0.1981089393F, 0.1996840117F, 0.2012639560F, - 0.2028487479F, 0.2044383630F, 0.2060327766F, 0.2076319642F, - 0.2092359007F, 0.2108445614F, 0.2124579211F, 0.2140759545F, - 0.2156986364F, 0.2173259411F, 0.2189578432F, 0.2205943168F, - 0.2222353361F, 0.2238808751F, 0.2255309076F, 0.2271854073F, - 0.2288443480F, 0.2305077030F, 0.2321754457F, 0.2338475493F, - 0.2355239869F, 0.2372047315F, 0.2388897560F, 0.2405790329F, - 0.2422725350F, 0.2439702347F, 0.2456721043F, 0.2473781159F, - 0.2490882418F, 0.2508024539F, 0.2525207240F, 0.2542430237F, - 0.2559693248F, 0.2576995986F, 0.2594338166F, 0.2611719498F, - 0.2629139695F, 0.2646598466F, 0.2664095520F, 0.2681630564F, - 0.2699203304F, 0.2716813445F, 0.2734460691F, 0.2752144744F, - 0.2769865307F, 0.2787622079F, 0.2805414760F, 0.2823243047F, - 0.2841106637F, 0.2859005227F, 0.2876938509F, 0.2894906179F, - 0.2912907928F, 0.2930943447F, 0.2949012426F, 0.2967114554F, - 0.2985249520F, 0.3003417009F, 0.3021616708F, 0.3039848301F, - 0.3058111471F, 0.3076405901F, 0.3094731273F, 0.3113087266F, - 0.3131473560F, 0.3149889833F, 0.3168335762F, 0.3186811024F, - 0.3205315294F, 0.3223848245F, 0.3242409552F, 0.3260998886F, - 0.3279615918F, 0.3298260319F, 0.3316931758F, 0.3335629903F, - 0.3354354423F, 0.3373104982F, 0.3391881247F, 0.3410682882F, - 0.3429509551F, 0.3448360917F, 0.3467236642F, 0.3486136387F, - 0.3505059811F, 0.3524006575F, 0.3542976336F, 0.3561968753F, - 0.3580983482F, 0.3600020179F, 0.3619078499F, 0.3638158096F, - 0.3657258625F, 0.3676379737F, 0.3695521086F, 0.3714682321F, - 0.3733863094F, 0.3753063055F, 0.3772281852F, 0.3791519134F, - 0.3810774548F, 0.3830047742F, 0.3849338362F, 0.3868646053F, - 0.3887970459F, 0.3907311227F, 0.3926667998F, 0.3946040417F, - 0.3965428125F, 0.3984830765F, 0.4004247978F, 0.4023679403F, - 0.4043124683F, 0.4062583455F, 0.4082055359F, 0.4101540034F, - 0.4121037117F, 0.4140546246F, 0.4160067058F, 0.4179599190F, - 0.4199142277F, 0.4218695956F, 0.4238259861F, 0.4257833627F, - 0.4277416888F, 0.4297009279F, 0.4316610433F, 0.4336219983F, - 0.4355837562F, 0.4375462803F, 0.4395095337F, 0.4414734797F, - 0.4434380815F, 0.4454033021F, 0.4473691046F, 0.4493354521F, - 0.4513023078F, 0.4532696345F, 0.4552373954F, 0.4572055533F, - 0.4591740713F, 0.4611429123F, 0.4631120393F, 0.4650814151F, - 0.4670510028F, 0.4690207650F, 0.4709906649F, 0.4729606651F, - 0.4749307287F, 0.4769008185F, 0.4788708972F, 0.4808409279F, - 0.4828108732F, 0.4847806962F, 0.4867503597F, 0.4887198264F, - 0.4906890593F, 0.4926580213F, 0.4946266753F, 0.4965949840F, - 0.4985629105F, 0.5005304176F, 0.5024974683F, 0.5044640255F, - 0.5064300522F, 0.5083955114F, 0.5103603659F, 0.5123245790F, - 0.5142881136F, 0.5162509328F, 0.5182129997F, 0.5201742774F, - 0.5221347290F, 0.5240943178F, 0.5260530070F, 0.5280107598F, - 0.5299675395F, 0.5319233095F, 0.5338780330F, 0.5358316736F, - 0.5377841946F, 0.5397355596F, 0.5416857320F, 0.5436346755F, - 0.5455823538F, 0.5475287304F, 0.5494737691F, 0.5514174337F, - 0.5533596881F, 0.5553004962F, 0.5572398218F, 0.5591776291F, - 0.5611138821F, 0.5630485449F, 0.5649815818F, 0.5669129570F, - 0.5688426349F, 0.5707705799F, 0.5726967564F, 0.5746211290F, - 0.5765436624F, 0.5784643212F, 0.5803830702F, 0.5822998743F, - 0.5842146984F, 0.5861275076F, 0.5880382669F, 0.5899469416F, - 0.5918534968F, 0.5937578981F, 0.5956601107F, 0.5975601004F, - 0.5994578326F, 0.6013532732F, 0.6032463880F, 0.6051371429F, - 0.6070255039F, 0.6089114372F, 0.6107949090F, 0.6126758856F, - 0.6145543334F, 0.6164302191F, 0.6183035092F, 0.6201741706F, - 0.6220421700F, 0.6239074745F, 0.6257700513F, 0.6276298674F, - 0.6294868903F, 0.6313410873F, 0.6331924262F, 0.6350408745F, - 0.6368864001F, 0.6387289710F, 0.6405685552F, 0.6424051209F, - 0.6442386364F, 0.6460690702F, 0.6478963910F, 0.6497205673F, - 0.6515415682F, 0.6533593625F, 0.6551739194F, 0.6569852082F, - 0.6587931984F, 0.6605978593F, 0.6623991609F, 0.6641970728F, - 0.6659915652F, 0.6677826081F, 0.6695701718F, 0.6713542268F, - 0.6731347437F, 0.6749116932F, 0.6766850461F, 0.6784547736F, - 0.6802208469F, 0.6819832374F, 0.6837419164F, 0.6854968559F, - 0.6872480275F, 0.6889954034F, 0.6907389556F, 0.6924786566F, - 0.6942144788F, 0.6959463950F, 0.6976743780F, 0.6993984008F, - 0.7011184365F, 0.7028344587F, 0.7045464407F, 0.7062543564F, - 0.7079581796F, 0.7096578844F, 0.7113534450F, 0.7130448359F, - 0.7147320316F, 0.7164150070F, 0.7180937371F, 0.7197681970F, - 0.7214383620F, 0.7231042077F, 0.7247657098F, 0.7264228443F, - 0.7280755871F, 0.7297239147F, 0.7313678035F, 0.7330072301F, - 0.7346421715F, 0.7362726046F, 0.7378985069F, 0.7395198556F, - 0.7411366285F, 0.7427488034F, 0.7443563584F, 0.7459592717F, - 0.7475575218F, 0.7491510873F, 0.7507399471F, 0.7523240803F, - 0.7539034661F, 0.7554780839F, 0.7570479136F, 0.7586129349F, - 0.7601731279F, 0.7617284730F, 0.7632789506F, 0.7648245416F, - 0.7663652267F, 0.7679009872F, 0.7694318044F, 0.7709576599F, - 0.7724785354F, 0.7739944130F, 0.7755052749F, 0.7770111035F, - 0.7785118815F, 0.7800075916F, 0.7814982170F, 0.7829837410F, - 0.7844641472F, 0.7859394191F, 0.7874095408F, 0.7888744965F, - 0.7903342706F, 0.7917888476F, 0.7932382124F, 0.7946823501F, - 0.7961212460F, 0.7975548855F, 0.7989832544F, 0.8004063386F, - 0.8018241244F, 0.8032365981F, 0.8046437463F, 0.8060455560F, - 0.8074420141F, 0.8088331080F, 0.8102188253F, 0.8115991536F, - 0.8129740810F, 0.8143435957F, 0.8157076861F, 0.8170663409F, - 0.8184195489F, 0.8197672994F, 0.8211095817F, 0.8224463853F, - 0.8237777001F, 0.8251035161F, 0.8264238235F, 0.8277386129F, - 0.8290478750F, 0.8303516008F, 0.8316497814F, 0.8329424083F, - 0.8342294731F, 0.8355109677F, 0.8367868841F, 0.8380572148F, - 0.8393219523F, 0.8405810893F, 0.8418346190F, 0.8430825345F, - 0.8443248294F, 0.8455614974F, 0.8467925323F, 0.8480179285F, - 0.8492376802F, 0.8504517822F, 0.8516602292F, 0.8528630164F, - 0.8540601391F, 0.8552515928F, 0.8564373733F, 0.8576174766F, - 0.8587918990F, 0.8599606368F, 0.8611236868F, 0.8622810460F, - 0.8634327113F, 0.8645786802F, 0.8657189504F, 0.8668535195F, - 0.8679823857F, 0.8691055472F, 0.8702230025F, 0.8713347503F, - 0.8724407896F, 0.8735411194F, 0.8746357394F, 0.8757246489F, - 0.8768078479F, 0.8778853364F, 0.8789571146F, 0.8800231832F, - 0.8810835427F, 0.8821381942F, 0.8831871387F, 0.8842303777F, - 0.8852679127F, 0.8862997456F, 0.8873258784F, 0.8883463132F, - 0.8893610527F, 0.8903700994F, 0.8913734562F, 0.8923711263F, - 0.8933631129F, 0.8943494196F, 0.8953300500F, 0.8963050083F, - 0.8972742985F, 0.8982379249F, 0.8991958922F, 0.9001482052F, - 0.9010948688F, 0.9020358883F, 0.9029712690F, 0.9039010165F, - 0.9048251367F, 0.9057436357F, 0.9066565195F, 0.9075637946F, - 0.9084654678F, 0.9093615456F, 0.9102520353F, 0.9111369440F, - 0.9120162792F, 0.9128900484F, 0.9137582595F, 0.9146209204F, - 0.9154780394F, 0.9163296248F, 0.9171756853F, 0.9180162296F, - 0.9188512667F, 0.9196808057F, 0.9205048559F, 0.9213234270F, - 0.9221365285F, 0.9229441704F, 0.9237463629F, 0.9245431160F, - 0.9253344404F, 0.9261203465F, 0.9269008453F, 0.9276759477F, - 0.9284456648F, 0.9292100080F, 0.9299689889F, 0.9307226190F, - 0.9314709103F, 0.9322138747F, 0.9329515245F, 0.9336838721F, - 0.9344109300F, 0.9351327108F, 0.9358492275F, 0.9365604931F, - 0.9372665208F, 0.9379673239F, 0.9386629160F, 0.9393533107F, - 0.9400385220F, 0.9407185637F, 0.9413934501F, 0.9420631954F, - 0.9427278141F, 0.9433873208F, 0.9440417304F, 0.9446910576F, - 0.9453353176F, 0.9459745255F, 0.9466086968F, 0.9472378469F, - 0.9478619915F, 0.9484811463F, 0.9490953274F, 0.9497045506F, - 0.9503088323F, 0.9509081888F, 0.9515026365F, 0.9520921921F, - 0.9526768723F, 0.9532566940F, 0.9538316742F, 0.9544018300F, - 0.9549671786F, 0.9555277375F, 0.9560835241F, 0.9566345562F, - 0.9571808513F, 0.9577224275F, 0.9582593027F, 0.9587914949F, - 0.9593190225F, 0.9598419038F, 0.9603601571F, 0.9608738012F, - 0.9613828546F, 0.9618873361F, 0.9623872646F, 0.9628826591F, - 0.9633735388F, 0.9638599227F, 0.9643418303F, 0.9648192808F, - 0.9652922939F, 0.9657608890F, 0.9662250860F, 0.9666849046F, - 0.9671403646F, 0.9675914861F, 0.9680382891F, 0.9684807937F, - 0.9689190202F, 0.9693529890F, 0.9697827203F, 0.9702082347F, - 0.9706295529F, 0.9710466953F, 0.9714596828F, 0.9718685362F, - 0.9722732762F, 0.9726739240F, 0.9730705005F, 0.9734630267F, - 0.9738515239F, 0.9742360134F, 0.9746165163F, 0.9749930540F, - 0.9753656481F, 0.9757343198F, 0.9760990909F, 0.9764599829F, - 0.9768170175F, 0.9771702164F, 0.9775196013F, 0.9778651941F, - 0.9782070167F, 0.9785450909F, 0.9788794388F, 0.9792100824F, - 0.9795370437F, 0.9798603449F, 0.9801800080F, 0.9804960554F, - 0.9808085092F, 0.9811173916F, 0.9814227251F, 0.9817245318F, - 0.9820228343F, 0.9823176549F, 0.9826090160F, 0.9828969402F, - 0.9831814498F, 0.9834625674F, 0.9837403156F, 0.9840147169F, - 0.9842857939F, 0.9845535692F, 0.9848180654F, 0.9850793052F, - 0.9853373113F, 0.9855921062F, 0.9858437127F, 0.9860921535F, - 0.9863374512F, 0.9865796287F, 0.9868187085F, 0.9870547136F, - 0.9872876664F, 0.9875175899F, 0.9877445067F, 0.9879684396F, - 0.9881894112F, 0.9884074444F, 0.9886225619F, 0.9888347863F, - 0.9890441404F, 0.9892506468F, 0.9894543284F, 0.9896552077F, - 0.9898533074F, 0.9900486502F, 0.9902412587F, 0.9904311555F, - 0.9906183633F, 0.9908029045F, 0.9909848019F, 0.9911640779F, - 0.9913407550F, 0.9915148557F, 0.9916864025F, 0.9918554179F, - 0.9920219241F, 0.9921859437F, 0.9923474989F, 0.9925066120F, - 0.9926633054F, 0.9928176012F, 0.9929695218F, 0.9931190891F, - 0.9932663254F, 0.9934112527F, 0.9935538932F, 0.9936942686F, - 0.9938324012F, 0.9939683126F, 0.9941020248F, 0.9942335597F, - 0.9943629388F, 0.9944901841F, 0.9946153170F, 0.9947383593F, - 0.9948593325F, 0.9949782579F, 0.9950951572F, 0.9952100516F, - 0.9953229625F, 0.9954339111F, 0.9955429186F, 0.9956500062F, - 0.9957551948F, 0.9958585056F, 0.9959599593F, 0.9960595769F, - 0.9961573792F, 0.9962533869F, 0.9963476206F, 0.9964401009F, - 0.9965308483F, 0.9966198833F, 0.9967072261F, 0.9967928971F, - 0.9968769164F, 0.9969593041F, 0.9970400804F, 0.9971192651F, - 0.9971968781F, 0.9972729391F, 0.9973474680F, 0.9974204842F, - 0.9974920074F, 0.9975620569F, 0.9976306521F, 0.9976978122F, - 0.9977635565F, 0.9978279039F, 0.9978908736F, 0.9979524842F, - 0.9980127547F, 0.9980717037F, 0.9981293499F, 0.9981857116F, - 0.9982408073F, 0.9982946554F, 0.9983472739F, 0.9983986810F, - 0.9984488947F, 0.9984979328F, 0.9985458132F, 0.9985925534F, - 0.9986381711F, 0.9986826838F, 0.9987261086F, 0.9987684630F, - 0.9988097640F, 0.9988500286F, 0.9988892738F, 0.9989275163F, - 0.9989647727F, 0.9990010597F, 0.9990363938F, 0.9990707911F, - 0.9991042679F, 0.9991368404F, 0.9991685244F, 0.9991993358F, - 0.9992292905F, 0.9992584038F, 0.9992866914F, 0.9993141686F, - 0.9993408506F, 0.9993667526F, 0.9993918895F, 0.9994162761F, - 0.9994399273F, 0.9994628576F, 0.9994850815F, 0.9995066133F, - 0.9995274672F, 0.9995476574F, 0.9995671978F, 0.9995861021F, - 0.9996043841F, 0.9996220573F, 0.9996391352F, 0.9996556310F, - 0.9996715579F, 0.9996869288F, 0.9997017568F, 0.9997160543F, - 0.9997298342F, 0.9997431088F, 0.9997558905F, 0.9997681914F, - 0.9997800236F, 0.9997913990F, 0.9998023292F, 0.9998128261F, - 0.9998229009F, 0.9998325650F, 0.9998418296F, 0.9998507058F, - 0.9998592044F, 0.9998673362F, 0.9998751117F, 0.9998825415F, - 0.9998896358F, 0.9998964047F, 0.9999028584F, 0.9999090066F, - 0.9999148590F, 0.9999204253F, 0.9999257148F, 0.9999307368F, - 0.9999355003F, 0.9999400144F, 0.9999442878F, 0.9999483293F, - 0.9999521472F, 0.9999557499F, 0.9999591457F, 0.9999623426F, - 0.9999653483F, 0.9999681708F, 0.9999708175F, 0.9999732959F, - 0.9999756132F, 0.9999777765F, 0.9999797928F, 0.9999816688F, - 0.9999834113F, 0.9999850266F, 0.9999865211F, 0.9999879009F, - 0.9999891721F, 0.9999903405F, 0.9999914118F, 0.9999923914F, - 0.9999932849F, 0.9999940972F, 0.9999948336F, 0.9999954989F, - 0.9999960978F, 0.9999966349F, 0.9999971146F, 0.9999975411F, - 0.9999979185F, 0.9999982507F, 0.9999985414F, 0.9999987944F, - 0.9999990129F, 0.9999992003F, 0.9999993596F, 0.9999994939F, - 0.9999996059F, 0.9999996981F, 0.9999997732F, 0.9999998333F, - 0.9999998805F, 0.9999999170F, 0.9999999444F, 0.9999999643F, - 0.9999999784F, 0.9999999878F, 0.9999999937F, 0.9999999972F, - 0.9999999990F, 0.9999999997F, 1.0000000000F, 1.0000000000F, -}; - -static const float vwin4096[2048] = { - 0.0000002310F, 0.0000020791F, 0.0000057754F, 0.0000113197F, - 0.0000187121F, 0.0000279526F, 0.0000390412F, 0.0000519777F, - 0.0000667623F, 0.0000833949F, 0.0001018753F, 0.0001222036F, - 0.0001443798F, 0.0001684037F, 0.0001942754F, 0.0002219947F, - 0.0002515616F, 0.0002829761F, 0.0003162380F, 0.0003513472F, - 0.0003883038F, 0.0004271076F, 0.0004677584F, 0.0005102563F, - 0.0005546011F, 0.0006007928F, 0.0006488311F, 0.0006987160F, - 0.0007504474F, 0.0008040251F, 0.0008594490F, 0.0009167191F, - 0.0009758351F, 0.0010367969F, 0.0010996044F, 0.0011642574F, - 0.0012307558F, 0.0012990994F, 0.0013692880F, 0.0014413216F, - 0.0015151998F, 0.0015909226F, 0.0016684898F, 0.0017479011F, - 0.0018291565F, 0.0019122556F, 0.0019971983F, 0.0020839845F, - 0.0021726138F, 0.0022630861F, 0.0023554012F, 0.0024495588F, - 0.0025455588F, 0.0026434008F, 0.0027430847F, 0.0028446103F, - 0.0029479772F, 0.0030531853F, 0.0031602342F, 0.0032691238F, - 0.0033798538F, 0.0034924239F, 0.0036068338F, 0.0037230833F, - 0.0038411721F, 0.0039610999F, 0.0040828664F, 0.0042064714F, - 0.0043319145F, 0.0044591954F, 0.0045883139F, 0.0047192696F, - 0.0048520622F, 0.0049866914F, 0.0051231569F, 0.0052614583F, - 0.0054015953F, 0.0055435676F, 0.0056873748F, 0.0058330166F, - 0.0059804926F, 0.0061298026F, 0.0062809460F, 0.0064339226F, - 0.0065887320F, 0.0067453738F, 0.0069038476F, 0.0070641531F, - 0.0072262899F, 0.0073902575F, 0.0075560556F, 0.0077236838F, - 0.0078931417F, 0.0080644288F, 0.0082375447F, 0.0084124891F, - 0.0085892615F, 0.0087678614F, 0.0089482885F, 0.0091305422F, - 0.0093146223F, 0.0095005281F, 0.0096882592F, 0.0098778153F, - 0.0100691958F, 0.0102624002F, 0.0104574281F, 0.0106542791F, - 0.0108529525F, 0.0110534480F, 0.0112557651F, 0.0114599032F, - 0.0116658618F, 0.0118736405F, 0.0120832387F, 0.0122946560F, - 0.0125078917F, 0.0127229454F, 0.0129398166F, 0.0131585046F, - 0.0133790090F, 0.0136013292F, 0.0138254647F, 0.0140514149F, - 0.0142791792F, 0.0145087572F, 0.0147401481F, 0.0149733515F, - 0.0152083667F, 0.0154451932F, 0.0156838304F, 0.0159242777F, - 0.0161665345F, 0.0164106001F, 0.0166564741F, 0.0169041557F, - 0.0171536443F, 0.0174049393F, 0.0176580401F, 0.0179129461F, - 0.0181696565F, 0.0184281708F, 0.0186884883F, 0.0189506084F, - 0.0192145303F, 0.0194802535F, 0.0197477772F, 0.0200171008F, - 0.0202882236F, 0.0205611449F, 0.0208358639F, 0.0211123801F, - 0.0213906927F, 0.0216708011F, 0.0219527043F, 0.0222364019F, - 0.0225218930F, 0.0228091769F, 0.0230982529F, 0.0233891203F, - 0.0236817782F, 0.0239762259F, 0.0242724628F, 0.0245704880F, - 0.0248703007F, 0.0251719002F, 0.0254752858F, 0.0257804565F, - 0.0260874117F, 0.0263961506F, 0.0267066722F, 0.0270189760F, - 0.0273330609F, 0.0276489263F, 0.0279665712F, 0.0282859949F, - 0.0286071966F, 0.0289301753F, 0.0292549303F, 0.0295814607F, - 0.0299097656F, 0.0302398442F, 0.0305716957F, 0.0309053191F, - 0.0312407135F, 0.0315778782F, 0.0319168122F, 0.0322575145F, - 0.0325999844F, 0.0329442209F, 0.0332902231F, 0.0336379900F, - 0.0339875208F, 0.0343388146F, 0.0346918703F, 0.0350466871F, - 0.0354032640F, 0.0357616000F, 0.0361216943F, 0.0364835458F, - 0.0368471535F, 0.0372125166F, 0.0375796339F, 0.0379485046F, - 0.0383191276F, 0.0386915020F, 0.0390656267F, 0.0394415008F, - 0.0398191231F, 0.0401984927F, 0.0405796086F, 0.0409624698F, - 0.0413470751F, 0.0417334235F, 0.0421215141F, 0.0425113457F, - 0.0429029172F, 0.0432962277F, 0.0436912760F, 0.0440880610F, - 0.0444865817F, 0.0448868370F, 0.0452888257F, 0.0456925468F, - 0.0460979992F, 0.0465051816F, 0.0469140931F, 0.0473247325F, - 0.0477370986F, 0.0481511902F, 0.0485670064F, 0.0489845458F, - 0.0494038074F, 0.0498247899F, 0.0502474922F, 0.0506719131F, - 0.0510980514F, 0.0515259060F, 0.0519554756F, 0.0523867590F, - 0.0528197550F, 0.0532544624F, 0.0536908800F, 0.0541290066F, - 0.0545688408F, 0.0550103815F, 0.0554536274F, 0.0558985772F, - 0.0563452297F, 0.0567935837F, 0.0572436377F, 0.0576953907F, - 0.0581488412F, 0.0586039880F, 0.0590608297F, 0.0595193651F, - 0.0599795929F, 0.0604415117F, 0.0609051202F, 0.0613704170F, - 0.0618374009F, 0.0623060704F, 0.0627764243F, 0.0632484611F, - 0.0637221795F, 0.0641975781F, 0.0646746555F, 0.0651534104F, - 0.0656338413F, 0.0661159469F, 0.0665997257F, 0.0670851763F, - 0.0675722973F, 0.0680610873F, 0.0685515448F, 0.0690436684F, - 0.0695374567F, 0.0700329081F, 0.0705300213F, 0.0710287947F, - 0.0715292269F, 0.0720313163F, 0.0725350616F, 0.0730404612F, - 0.0735475136F, 0.0740562172F, 0.0745665707F, 0.0750785723F, - 0.0755922207F, 0.0761075143F, 0.0766244515F, 0.0771430307F, - 0.0776632505F, 0.0781851092F, 0.0787086052F, 0.0792337371F, - 0.0797605032F, 0.0802889018F, 0.0808189315F, 0.0813505905F, - 0.0818838773F, 0.0824187903F, 0.0829553277F, 0.0834934881F, - 0.0840332697F, 0.0845746708F, 0.0851176899F, 0.0856623252F, - 0.0862085751F, 0.0867564379F, 0.0873059119F, 0.0878569954F, - 0.0884096867F, 0.0889639840F, 0.0895198858F, 0.0900773902F, - 0.0906364955F, 0.0911972000F, 0.0917595019F, 0.0923233995F, - 0.0928888909F, 0.0934559745F, 0.0940246485F, 0.0945949110F, - 0.0951667604F, 0.0957401946F, 0.0963152121F, 0.0968918109F, - 0.0974699893F, 0.0980497454F, 0.0986310773F, 0.0992139832F, - 0.0997984614F, 0.1003845098F, 0.1009721267F, 0.1015613101F, - 0.1021520582F, 0.1027443692F, 0.1033382410F, 0.1039336718F, - 0.1045306597F, 0.1051292027F, 0.1057292990F, 0.1063309466F, - 0.1069341435F, 0.1075388878F, 0.1081451776F, 0.1087530108F, - 0.1093623856F, 0.1099732998F, 0.1105857516F, 0.1111997389F, - 0.1118152597F, 0.1124323121F, 0.1130508939F, 0.1136710032F, - 0.1142926379F, 0.1149157960F, 0.1155404755F, 0.1161666742F, - 0.1167943901F, 0.1174236211F, 0.1180543652F, 0.1186866202F, - 0.1193203841F, 0.1199556548F, 0.1205924300F, 0.1212307078F, - 0.1218704860F, 0.1225117624F, 0.1231545349F, 0.1237988013F, - 0.1244445596F, 0.1250918074F, 0.1257405427F, 0.1263907632F, - 0.1270424667F, 0.1276956512F, 0.1283503142F, 0.1290064537F, - 0.1296640674F, 0.1303231530F, 0.1309837084F, 0.1316457312F, - 0.1323092193F, 0.1329741703F, 0.1336405820F, 0.1343084520F, - 0.1349777782F, 0.1356485582F, 0.1363207897F, 0.1369944704F, - 0.1376695979F, 0.1383461700F, 0.1390241842F, 0.1397036384F, - 0.1403845300F, 0.1410668567F, 0.1417506162F, 0.1424358061F, - 0.1431224240F, 0.1438104674F, 0.1444999341F, 0.1451908216F, - 0.1458831274F, 0.1465768492F, 0.1472719844F, 0.1479685308F, - 0.1486664857F, 0.1493658468F, 0.1500666115F, 0.1507687775F, - 0.1514723422F, 0.1521773031F, 0.1528836577F, 0.1535914035F, - 0.1543005380F, 0.1550110587F, 0.1557229631F, 0.1564362485F, - 0.1571509124F, 0.1578669524F, 0.1585843657F, 0.1593031499F, - 0.1600233024F, 0.1607448205F, 0.1614677017F, 0.1621919433F, - 0.1629175428F, 0.1636444975F, 0.1643728047F, 0.1651024619F, - 0.1658334665F, 0.1665658156F, 0.1672995067F, 0.1680345371F, - 0.1687709041F, 0.1695086050F, 0.1702476372F, 0.1709879978F, - 0.1717296843F, 0.1724726938F, 0.1732170237F, 0.1739626711F, - 0.1747096335F, 0.1754579079F, 0.1762074916F, 0.1769583819F, - 0.1777105760F, 0.1784640710F, 0.1792188642F, 0.1799749529F, - 0.1807323340F, 0.1814910049F, 0.1822509628F, 0.1830122046F, - 0.1837747277F, 0.1845385292F, 0.1853036062F, 0.1860699558F, - 0.1868375751F, 0.1876064613F, 0.1883766114F, 0.1891480226F, - 0.1899206919F, 0.1906946164F, 0.1914697932F, 0.1922462194F, - 0.1930238919F, 0.1938028079F, 0.1945829643F, 0.1953643583F, - 0.1961469868F, 0.1969308468F, 0.1977159353F, 0.1985022494F, - 0.1992897859F, 0.2000785420F, 0.2008685145F, 0.2016597005F, - 0.2024520968F, 0.2032457005F, 0.2040405084F, 0.2048365175F, - 0.2056337247F, 0.2064321269F, 0.2072317211F, 0.2080325041F, - 0.2088344727F, 0.2096376240F, 0.2104419547F, 0.2112474618F, - 0.2120541420F, 0.2128619923F, 0.2136710094F, 0.2144811902F, - 0.2152925315F, 0.2161050301F, 0.2169186829F, 0.2177334866F, - 0.2185494381F, 0.2193665340F, 0.2201847712F, 0.2210041465F, - 0.2218246565F, 0.2226462981F, 0.2234690680F, 0.2242929629F, - 0.2251179796F, 0.2259441147F, 0.2267713650F, 0.2275997272F, - 0.2284291979F, 0.2292597739F, 0.2300914518F, 0.2309242283F, - 0.2317581001F, 0.2325930638F, 0.2334291160F, 0.2342662534F, - 0.2351044727F, 0.2359437703F, 0.2367841431F, 0.2376255875F, - 0.2384681001F, 0.2393116776F, 0.2401563165F, 0.2410020134F, - 0.2418487649F, 0.2426965675F, 0.2435454178F, 0.2443953122F, - 0.2452462474F, 0.2460982199F, 0.2469512262F, 0.2478052628F, - 0.2486603262F, 0.2495164129F, 0.2503735194F, 0.2512316421F, - 0.2520907776F, 0.2529509222F, 0.2538120726F, 0.2546742250F, - 0.2555373760F, 0.2564015219F, 0.2572666593F, 0.2581327845F, - 0.2589998939F, 0.2598679840F, 0.2607370510F, 0.2616070916F, - 0.2624781019F, 0.2633500783F, 0.2642230173F, 0.2650969152F, - 0.2659717684F, 0.2668475731F, 0.2677243257F, 0.2686020226F, - 0.2694806601F, 0.2703602344F, 0.2712407419F, 0.2721221789F, - 0.2730045417F, 0.2738878265F, 0.2747720297F, 0.2756571474F, - 0.2765431760F, 0.2774301117F, 0.2783179508F, 0.2792066895F, - 0.2800963240F, 0.2809868505F, 0.2818782654F, 0.2827705647F, - 0.2836637447F, 0.2845578016F, 0.2854527315F, 0.2863485307F, - 0.2872451953F, 0.2881427215F, 0.2890411055F, 0.2899403433F, - 0.2908404312F, 0.2917413654F, 0.2926431418F, 0.2935457567F, - 0.2944492061F, 0.2953534863F, 0.2962585932F, 0.2971645230F, - 0.2980712717F, 0.2989788356F, 0.2998872105F, 0.3007963927F, - 0.3017063781F, 0.3026171629F, 0.3035287430F, 0.3044411145F, - 0.3053542736F, 0.3062682161F, 0.3071829381F, 0.3080984356F, - 0.3090147047F, 0.3099317413F, 0.3108495414F, 0.3117681011F, - 0.3126874163F, 0.3136074830F, 0.3145282972F, 0.3154498548F, - 0.3163721517F, 0.3172951841F, 0.3182189477F, 0.3191434385F, - 0.3200686525F, 0.3209945856F, 0.3219212336F, 0.3228485927F, - 0.3237766585F, 0.3247054271F, 0.3256348943F, 0.3265650560F, - 0.3274959081F, 0.3284274465F, 0.3293596671F, 0.3302925657F, - 0.3312261382F, 0.3321603804F, 0.3330952882F, 0.3340308574F, - 0.3349670838F, 0.3359039634F, 0.3368414919F, 0.3377796651F, - 0.3387184789F, 0.3396579290F, 0.3405980113F, 0.3415387216F, - 0.3424800556F, 0.3434220091F, 0.3443645779F, 0.3453077578F, - 0.3462515446F, 0.3471959340F, 0.3481409217F, 0.3490865036F, - 0.3500326754F, 0.3509794328F, 0.3519267715F, 0.3528746873F, - 0.3538231759F, 0.3547722330F, 0.3557218544F, 0.3566720357F, - 0.3576227727F, 0.3585740610F, 0.3595258964F, 0.3604782745F, - 0.3614311910F, 0.3623846417F, 0.3633386221F, 0.3642931280F, - 0.3652481549F, 0.3662036987F, 0.3671597548F, 0.3681163191F, - 0.3690733870F, 0.3700309544F, 0.3709890167F, 0.3719475696F, - 0.3729066089F, 0.3738661299F, 0.3748261285F, 0.3757866002F, - 0.3767475406F, 0.3777089453F, 0.3786708100F, 0.3796331302F, - 0.3805959014F, 0.3815591194F, 0.3825227796F, 0.3834868777F, - 0.3844514093F, 0.3854163698F, 0.3863817549F, 0.3873475601F, - 0.3883137810F, 0.3892804131F, 0.3902474521F, 0.3912148933F, - 0.3921827325F, 0.3931509650F, 0.3941195865F, 0.3950885925F, - 0.3960579785F, 0.3970277400F, 0.3979978725F, 0.3989683716F, - 0.3999392328F, 0.4009104516F, 0.4018820234F, 0.4028539438F, - 0.4038262084F, 0.4047988125F, 0.4057717516F, 0.4067450214F, - 0.4077186172F, 0.4086925345F, 0.4096667688F, 0.4106413155F, - 0.4116161703F, 0.4125913284F, 0.4135667854F, 0.4145425368F, - 0.4155185780F, 0.4164949044F, 0.4174715116F, 0.4184483949F, - 0.4194255498F, 0.4204029718F, 0.4213806563F, 0.4223585987F, - 0.4233367946F, 0.4243152392F, 0.4252939281F, 0.4262728566F, - 0.4272520202F, 0.4282314144F, 0.4292110345F, 0.4301908760F, - 0.4311709343F, 0.4321512047F, 0.4331316828F, 0.4341123639F, - 0.4350932435F, 0.4360743168F, 0.4370555794F, 0.4380370267F, - 0.4390186540F, 0.4400004567F, 0.4409824303F, 0.4419645701F, - 0.4429468716F, 0.4439293300F, 0.4449119409F, 0.4458946996F, - 0.4468776014F, 0.4478606418F, 0.4488438162F, 0.4498271199F, - 0.4508105483F, 0.4517940967F, 0.4527777607F, 0.4537615355F, - 0.4547454165F, 0.4557293991F, 0.4567134786F, 0.4576976505F, - 0.4586819101F, 0.4596662527F, 0.4606506738F, 0.4616351687F, - 0.4626197328F, 0.4636043614F, 0.4645890499F, 0.4655737936F, - 0.4665585880F, 0.4675434284F, 0.4685283101F, 0.4695132286F, - 0.4704981791F, 0.4714831570F, 0.4724681577F, 0.4734531766F, - 0.4744382089F, 0.4754232501F, 0.4764082956F, 0.4773933406F, - 0.4783783806F, 0.4793634108F, 0.4803484267F, 0.4813334237F, - 0.4823183969F, 0.4833033419F, 0.4842882540F, 0.4852731285F, - 0.4862579608F, 0.4872427462F, 0.4882274802F, 0.4892121580F, - 0.4901967751F, 0.4911813267F, 0.4921658083F, 0.4931502151F, - 0.4941345427F, 0.4951187863F, 0.4961029412F, 0.4970870029F, - 0.4980709667F, 0.4990548280F, 0.5000385822F, 0.5010222245F, - 0.5020057505F, 0.5029891553F, 0.5039724345F, 0.5049555834F, - 0.5059385973F, 0.5069214716F, 0.5079042018F, 0.5088867831F, - 0.5098692110F, 0.5108514808F, 0.5118335879F, 0.5128155277F, - 0.5137972956F, 0.5147788869F, 0.5157602971F, 0.5167415215F, - 0.5177225555F, 0.5187033945F, 0.5196840339F, 0.5206644692F, - 0.5216446956F, 0.5226247086F, 0.5236045035F, 0.5245840759F, - 0.5255634211F, 0.5265425344F, 0.5275214114F, 0.5285000474F, - 0.5294784378F, 0.5304565781F, 0.5314344637F, 0.5324120899F, - 0.5333894522F, 0.5343665461F, 0.5353433670F, 0.5363199102F, - 0.5372961713F, 0.5382721457F, 0.5392478287F, 0.5402232159F, - 0.5411983027F, 0.5421730845F, 0.5431475569F, 0.5441217151F, - 0.5450955548F, 0.5460690714F, 0.5470422602F, 0.5480151169F, - 0.5489876368F, 0.5499598155F, 0.5509316484F, 0.5519031310F, - 0.5528742587F, 0.5538450271F, 0.5548154317F, 0.5557854680F, - 0.5567551314F, 0.5577244174F, 0.5586933216F, 0.5596618395F, - 0.5606299665F, 0.5615976983F, 0.5625650302F, 0.5635319580F, - 0.5644984770F, 0.5654645828F, 0.5664302709F, 0.5673955370F, - 0.5683603765F, 0.5693247850F, 0.5702887580F, 0.5712522912F, - 0.5722153800F, 0.5731780200F, 0.5741402069F, 0.5751019362F, - 0.5760632034F, 0.5770240042F, 0.5779843341F, 0.5789441889F, - 0.5799035639F, 0.5808624549F, 0.5818208575F, 0.5827787673F, - 0.5837361800F, 0.5846930910F, 0.5856494961F, 0.5866053910F, - 0.5875607712F, 0.5885156324F, 0.5894699703F, 0.5904237804F, - 0.5913770586F, 0.5923298004F, 0.5932820016F, 0.5942336578F, - 0.5951847646F, 0.5961353179F, 0.5970853132F, 0.5980347464F, - 0.5989836131F, 0.5999319090F, 0.6008796298F, 0.6018267713F, - 0.6027733292F, 0.6037192993F, 0.6046646773F, 0.6056094589F, - 0.6065536400F, 0.6074972162F, 0.6084401833F, 0.6093825372F, - 0.6103242736F, 0.6112653884F, 0.6122058772F, 0.6131457359F, - 0.6140849604F, 0.6150235464F, 0.6159614897F, 0.6168987862F, - 0.6178354318F, 0.6187714223F, 0.6197067535F, 0.6206414213F, - 0.6215754215F, 0.6225087501F, 0.6234414028F, 0.6243733757F, - 0.6253046646F, 0.6262352654F, 0.6271651739F, 0.6280943862F, - 0.6290228982F, 0.6299507057F, 0.6308778048F, 0.6318041913F, - 0.6327298612F, 0.6336548105F, 0.6345790352F, 0.6355025312F, - 0.6364252945F, 0.6373473211F, 0.6382686070F, 0.6391891483F, - 0.6401089409F, 0.6410279808F, 0.6419462642F, 0.6428637869F, - 0.6437805452F, 0.6446965350F, 0.6456117524F, 0.6465261935F, - 0.6474398544F, 0.6483527311F, 0.6492648197F, 0.6501761165F, - 0.6510866174F, 0.6519963186F, 0.6529052162F, 0.6538133064F, - 0.6547205854F, 0.6556270492F, 0.6565326941F, 0.6574375162F, - 0.6583415117F, 0.6592446769F, 0.6601470079F, 0.6610485009F, - 0.6619491521F, 0.6628489578F, 0.6637479143F, 0.6646460177F, - 0.6655432643F, 0.6664396505F, 0.6673351724F, 0.6682298264F, - 0.6691236087F, 0.6700165157F, 0.6709085436F, 0.6717996889F, - 0.6726899478F, 0.6735793167F, 0.6744677918F, 0.6753553697F, - 0.6762420466F, 0.6771278190F, 0.6780126832F, 0.6788966357F, - 0.6797796728F, 0.6806617909F, 0.6815429866F, 0.6824232562F, - 0.6833025961F, 0.6841810030F, 0.6850584731F, 0.6859350031F, - 0.6868105894F, 0.6876852284F, 0.6885589168F, 0.6894316510F, - 0.6903034275F, 0.6911742430F, 0.6920440939F, 0.6929129769F, - 0.6937808884F, 0.6946478251F, 0.6955137837F, 0.6963787606F, - 0.6972427525F, 0.6981057560F, 0.6989677678F, 0.6998287845F, - 0.7006888028F, 0.7015478194F, 0.7024058309F, 0.7032628340F, - 0.7041188254F, 0.7049738019F, 0.7058277601F, 0.7066806969F, - 0.7075326089F, 0.7083834929F, 0.7092333457F, 0.7100821640F, - 0.7109299447F, 0.7117766846F, 0.7126223804F, 0.7134670291F, - 0.7143106273F, 0.7151531721F, 0.7159946602F, 0.7168350885F, - 0.7176744539F, 0.7185127534F, 0.7193499837F, 0.7201861418F, - 0.7210212247F, 0.7218552293F, 0.7226881526F, 0.7235199914F, - 0.7243507428F, 0.7251804039F, 0.7260089715F, 0.7268364426F, - 0.7276628144F, 0.7284880839F, 0.7293122481F, 0.7301353040F, - 0.7309572487F, 0.7317780794F, 0.7325977930F, 0.7334163868F, - 0.7342338579F, 0.7350502033F, 0.7358654202F, 0.7366795059F, - 0.7374924573F, 0.7383042718F, 0.7391149465F, 0.7399244787F, - 0.7407328655F, 0.7415401041F, 0.7423461920F, 0.7431511261F, - 0.7439549040F, 0.7447575227F, 0.7455589797F, 0.7463592723F, - 0.7471583976F, 0.7479563532F, 0.7487531363F, 0.7495487443F, - 0.7503431745F, 0.7511364244F, 0.7519284913F, 0.7527193726F, - 0.7535090658F, 0.7542975683F, 0.7550848776F, 0.7558709910F, - 0.7566559062F, 0.7574396205F, 0.7582221314F, 0.7590034366F, - 0.7597835334F, 0.7605624194F, 0.7613400923F, 0.7621165495F, - 0.7628917886F, 0.7636658072F, 0.7644386030F, 0.7652101735F, - 0.7659805164F, 0.7667496292F, 0.7675175098F, 0.7682841556F, - 0.7690495645F, 0.7698137341F, 0.7705766622F, 0.7713383463F, - 0.7720987844F, 0.7728579741F, 0.7736159132F, 0.7743725994F, - 0.7751280306F, 0.7758822046F, 0.7766351192F, 0.7773867722F, - 0.7781371614F, 0.7788862848F, 0.7796341401F, 0.7803807253F, - 0.7811260383F, 0.7818700769F, 0.7826128392F, 0.7833543230F, - 0.7840945263F, 0.7848334471F, 0.7855710833F, 0.7863074330F, - 0.7870424941F, 0.7877762647F, 0.7885087428F, 0.7892399264F, - 0.7899698137F, 0.7906984026F, 0.7914256914F, 0.7921516780F, - 0.7928763607F, 0.7935997375F, 0.7943218065F, 0.7950425661F, - 0.7957620142F, 0.7964801492F, 0.7971969692F, 0.7979124724F, - 0.7986266570F, 0.7993395214F, 0.8000510638F, 0.8007612823F, - 0.8014701754F, 0.8021777413F, 0.8028839784F, 0.8035888849F, - 0.8042924592F, 0.8049946997F, 0.8056956048F, 0.8063951727F, - 0.8070934020F, 0.8077902910F, 0.8084858381F, 0.8091800419F, - 0.8098729007F, 0.8105644130F, 0.8112545774F, 0.8119433922F, - 0.8126308561F, 0.8133169676F, 0.8140017251F, 0.8146851272F, - 0.8153671726F, 0.8160478598F, 0.8167271874F, 0.8174051539F, - 0.8180817582F, 0.8187569986F, 0.8194308741F, 0.8201033831F, - 0.8207745244F, 0.8214442966F, 0.8221126986F, 0.8227797290F, - 0.8234453865F, 0.8241096700F, 0.8247725781F, 0.8254341097F, - 0.8260942636F, 0.8267530385F, 0.8274104334F, 0.8280664470F, - 0.8287210782F, 0.8293743259F, 0.8300261889F, 0.8306766662F, - 0.8313257566F, 0.8319734591F, 0.8326197727F, 0.8332646963F, - 0.8339082288F, 0.8345503692F, 0.8351911167F, 0.8358304700F, - 0.8364684284F, 0.8371049907F, 0.8377401562F, 0.8383739238F, - 0.8390062927F, 0.8396372618F, 0.8402668305F, 0.8408949977F, - 0.8415217626F, 0.8421471245F, 0.8427710823F, 0.8433936354F, - 0.8440147830F, 0.8446345242F, 0.8452528582F, 0.8458697844F, - 0.8464853020F, 0.8470994102F, 0.8477121084F, 0.8483233958F, - 0.8489332718F, 0.8495417356F, 0.8501487866F, 0.8507544243F, - 0.8513586479F, 0.8519614568F, 0.8525628505F, 0.8531628283F, - 0.8537613897F, 0.8543585341F, 0.8549542611F, 0.8555485699F, - 0.8561414603F, 0.8567329315F, 0.8573229832F, 0.8579116149F, - 0.8584988262F, 0.8590846165F, 0.8596689855F, 0.8602519327F, - 0.8608334577F, 0.8614135603F, 0.8619922399F, 0.8625694962F, - 0.8631453289F, 0.8637197377F, 0.8642927222F, 0.8648642821F, - 0.8654344172F, 0.8660031272F, 0.8665704118F, 0.8671362708F, - 0.8677007039F, 0.8682637109F, 0.8688252917F, 0.8693854460F, - 0.8699441737F, 0.8705014745F, 0.8710573485F, 0.8716117953F, - 0.8721648150F, 0.8727164073F, 0.8732665723F, 0.8738153098F, - 0.8743626197F, 0.8749085021F, 0.8754529569F, 0.8759959840F, - 0.8765375835F, 0.8770777553F, 0.8776164996F, 0.8781538162F, - 0.8786897054F, 0.8792241670F, 0.8797572013F, 0.8802888082F, - 0.8808189880F, 0.8813477407F, 0.8818750664F, 0.8824009653F, - 0.8829254375F, 0.8834484833F, 0.8839701028F, 0.8844902961F, - 0.8850090636F, 0.8855264054F, 0.8860423218F, 0.8865568131F, - 0.8870698794F, 0.8875815212F, 0.8880917386F, 0.8886005319F, - 0.8891079016F, 0.8896138479F, 0.8901183712F, 0.8906214719F, - 0.8911231503F, 0.8916234067F, 0.8921222417F, 0.8926196556F, - 0.8931156489F, 0.8936102219F, 0.8941033752F, 0.8945951092F, - 0.8950854244F, 0.8955743212F, 0.8960618003F, 0.8965478621F, - 0.8970325071F, 0.8975157359F, 0.8979975490F, 0.8984779471F, - 0.8989569307F, 0.8994345004F, 0.8999106568F, 0.9003854005F, - 0.9008587323F, 0.9013306526F, 0.9018011623F, 0.9022702619F, - 0.9027379521F, 0.9032042337F, 0.9036691074F, 0.9041325739F, - 0.9045946339F, 0.9050552882F, 0.9055145376F, 0.9059723828F, - 0.9064288246F, 0.9068838638F, 0.9073375013F, 0.9077897379F, - 0.9082405743F, 0.9086900115F, 0.9091380503F, 0.9095846917F, - 0.9100299364F, 0.9104737854F, 0.9109162397F, 0.9113573001F, - 0.9117969675F, 0.9122352430F, 0.9126721275F, 0.9131076219F, - 0.9135417273F, 0.9139744447F, 0.9144057750F, 0.9148357194F, - 0.9152642787F, 0.9156914542F, 0.9161172468F, 0.9165416576F, - 0.9169646877F, 0.9173863382F, 0.9178066102F, 0.9182255048F, - 0.9186430232F, 0.9190591665F, 0.9194739359F, 0.9198873324F, - 0.9202993574F, 0.9207100120F, 0.9211192973F, 0.9215272147F, - 0.9219337653F, 0.9223389504F, 0.9227427713F, 0.9231452290F, - 0.9235463251F, 0.9239460607F, 0.9243444371F, 0.9247414557F, - 0.9251371177F, 0.9255314245F, 0.9259243774F, 0.9263159778F, - 0.9267062270F, 0.9270951264F, 0.9274826774F, 0.9278688814F, - 0.9282537398F, 0.9286372540F, 0.9290194254F, 0.9294002555F, - 0.9297797458F, 0.9301578976F, 0.9305347125F, 0.9309101919F, - 0.9312843373F, 0.9316571503F, 0.9320286323F, 0.9323987849F, - 0.9327676097F, 0.9331351080F, 0.9335012816F, 0.9338661320F, - 0.9342296607F, 0.9345918694F, 0.9349527596F, 0.9353123330F, - 0.9356705911F, 0.9360275357F, 0.9363831683F, 0.9367374905F, - 0.9370905042F, 0.9374422108F, 0.9377926122F, 0.9381417099F, - 0.9384895057F, 0.9388360014F, 0.9391811985F, 0.9395250989F, - 0.9398677043F, 0.9402090165F, 0.9405490371F, 0.9408877680F, - 0.9412252110F, 0.9415613678F, 0.9418962402F, 0.9422298301F, - 0.9425621392F, 0.9428931695F, 0.9432229226F, 0.9435514005F, - 0.9438786050F, 0.9442045381F, 0.9445292014F, 0.9448525971F, - 0.9451747268F, 0.9454955926F, 0.9458151963F, 0.9461335399F, - 0.9464506253F, 0.9467664545F, 0.9470810293F, 0.9473943517F, - 0.9477064238F, 0.9480172474F, 0.9483268246F, 0.9486351573F, - 0.9489422475F, 0.9492480973F, 0.9495527087F, 0.9498560837F, - 0.9501582243F, 0.9504591325F, 0.9507588105F, 0.9510572603F, - 0.9513544839F, 0.9516504834F, 0.9519452609F, 0.9522388186F, - 0.9525311584F, 0.9528222826F, 0.9531121932F, 0.9534008923F, - 0.9536883821F, 0.9539746647F, 0.9542597424F, 0.9545436171F, - 0.9548262912F, 0.9551077667F, 0.9553880459F, 0.9556671309F, - 0.9559450239F, 0.9562217272F, 0.9564972429F, 0.9567715733F, - 0.9570447206F, 0.9573166871F, 0.9575874749F, 0.9578570863F, - 0.9581255236F, 0.9583927890F, 0.9586588849F, 0.9589238134F, - 0.9591875769F, 0.9594501777F, 0.9597116180F, 0.9599719003F, - 0.9602310267F, 0.9604889995F, 0.9607458213F, 0.9610014942F, - 0.9612560206F, 0.9615094028F, 0.9617616433F, 0.9620127443F, - 0.9622627083F, 0.9625115376F, 0.9627592345F, 0.9630058016F, - 0.9632512411F, 0.9634955555F, 0.9637387471F, 0.9639808185F, - 0.9642217720F, 0.9644616100F, 0.9647003349F, 0.9649379493F, - 0.9651744556F, 0.9654098561F, 0.9656441534F, 0.9658773499F, - 0.9661094480F, 0.9663404504F, 0.9665703593F, 0.9667991774F, - 0.9670269071F, 0.9672535509F, 0.9674791114F, 0.9677035909F, - 0.9679269921F, 0.9681493174F, 0.9683705694F, 0.9685907506F, - 0.9688098636F, 0.9690279108F, 0.9692448948F, 0.9694608182F, - 0.9696756836F, 0.9698894934F, 0.9701022503F, 0.9703139569F, - 0.9705246156F, 0.9707342291F, 0.9709428000F, 0.9711503309F, - 0.9713568243F, 0.9715622829F, 0.9717667093F, 0.9719701060F, - 0.9721724757F, 0.9723738210F, 0.9725741446F, 0.9727734490F, - 0.9729717369F, 0.9731690109F, 0.9733652737F, 0.9735605279F, - 0.9737547762F, 0.9739480212F, 0.9741402656F, 0.9743315120F, - 0.9745217631F, 0.9747110216F, 0.9748992901F, 0.9750865714F, - 0.9752728681F, 0.9754581829F, 0.9756425184F, 0.9758258775F, - 0.9760082627F, 0.9761896768F, 0.9763701224F, 0.9765496024F, - 0.9767281193F, 0.9769056760F, 0.9770822751F, 0.9772579193F, - 0.9774326114F, 0.9776063542F, 0.9777791502F, 0.9779510023F, - 0.9781219133F, 0.9782918858F, 0.9784609226F, 0.9786290264F, - 0.9787962000F, 0.9789624461F, 0.9791277676F, 0.9792921671F, - 0.9794556474F, 0.9796182113F, 0.9797798615F, 0.9799406009F, - 0.9801004321F, 0.9802593580F, 0.9804173813F, 0.9805745049F, - 0.9807307314F, 0.9808860637F, 0.9810405046F, 0.9811940568F, - 0.9813467232F, 0.9814985065F, 0.9816494095F, 0.9817994351F, - 0.9819485860F, 0.9820968650F, 0.9822442750F, 0.9823908186F, - 0.9825364988F, 0.9826813184F, 0.9828252801F, 0.9829683868F, - 0.9831106413F, 0.9832520463F, 0.9833926048F, 0.9835323195F, - 0.9836711932F, 0.9838092288F, 0.9839464291F, 0.9840827969F, - 0.9842183351F, 0.9843530464F, 0.9844869337F, 0.9846199998F, - 0.9847522475F, 0.9848836798F, 0.9850142993F, 0.9851441090F, - 0.9852731117F, 0.9854013101F, 0.9855287073F, 0.9856553058F, - 0.9857811087F, 0.9859061188F, 0.9860303388F, 0.9861537717F, - 0.9862764202F, 0.9863982872F, 0.9865193756F, 0.9866396882F, - 0.9867592277F, 0.9868779972F, 0.9869959993F, 0.9871132370F, - 0.9872297131F, 0.9873454304F, 0.9874603918F, 0.9875746001F, - 0.9876880581F, 0.9878007688F, 0.9879127348F, 0.9880239592F, - 0.9881344447F, 0.9882441941F, 0.9883532104F, 0.9884614962F, - 0.9885690546F, 0.9886758883F, 0.9887820001F, 0.9888873930F, - 0.9889920697F, 0.9890960331F, 0.9891992859F, 0.9893018312F, - 0.9894036716F, 0.9895048100F, 0.9896052493F, 0.9897049923F, - 0.9898040418F, 0.9899024006F, 0.9900000717F, 0.9900970577F, - 0.9901933616F, 0.9902889862F, 0.9903839343F, 0.9904782087F, - 0.9905718122F, 0.9906647477F, 0.9907570180F, 0.9908486259F, - 0.9909395742F, 0.9910298658F, 0.9911195034F, 0.9912084899F, - 0.9912968281F, 0.9913845208F, 0.9914715708F, 0.9915579810F, - 0.9916437540F, 0.9917288928F, 0.9918134001F, 0.9918972788F, - 0.9919805316F, 0.9920631613F, 0.9921451707F, 0.9922265626F, - 0.9923073399F, 0.9923875052F, 0.9924670615F, 0.9925460114F, - 0.9926243577F, 0.9927021033F, 0.9927792508F, 0.9928558032F, - 0.9929317631F, 0.9930071333F, 0.9930819167F, 0.9931561158F, - 0.9932297337F, 0.9933027728F, 0.9933752362F, 0.9934471264F, - 0.9935184462F, 0.9935891985F, 0.9936593859F, 0.9937290112F, - 0.9937980771F, 0.9938665864F, 0.9939345418F, 0.9940019460F, - 0.9940688018F, 0.9941351118F, 0.9942008789F, 0.9942661057F, - 0.9943307950F, 0.9943949494F, 0.9944585717F, 0.9945216645F, - 0.9945842307F, 0.9946462728F, 0.9947077936F, 0.9947687957F, - 0.9948292820F, 0.9948892550F, 0.9949487174F, 0.9950076719F, - 0.9950661212F, 0.9951240679F, 0.9951815148F, 0.9952384645F, - 0.9952949196F, 0.9953508828F, 0.9954063568F, 0.9954613442F, - 0.9955158476F, 0.9955698697F, 0.9956234132F, 0.9956764806F, - 0.9957290746F, 0.9957811978F, 0.9958328528F, 0.9958840423F, - 0.9959347688F, 0.9959850351F, 0.9960348435F, 0.9960841969F, - 0.9961330977F, 0.9961815486F, 0.9962295521F, 0.9962771108F, - 0.9963242274F, 0.9963709043F, 0.9964171441F, 0.9964629494F, - 0.9965083228F, 0.9965532668F, 0.9965977840F, 0.9966418768F, - 0.9966855479F, 0.9967287998F, 0.9967716350F, 0.9968140559F, - 0.9968560653F, 0.9968976655F, 0.9969388591F, 0.9969796485F, - 0.9970200363F, 0.9970600250F, 0.9970996170F, 0.9971388149F, - 0.9971776211F, 0.9972160380F, 0.9972540683F, 0.9972917142F, - 0.9973289783F, 0.9973658631F, 0.9974023709F, 0.9974385042F, - 0.9974742655F, 0.9975096571F, 0.9975446816F, 0.9975793413F, - 0.9976136386F, 0.9976475759F, 0.9976811557F, 0.9977143803F, - 0.9977472521F, 0.9977797736F, 0.9978119470F, 0.9978437748F, - 0.9978752593F, 0.9979064029F, 0.9979372079F, 0.9979676768F, - 0.9979978117F, 0.9980276151F, 0.9980570893F, 0.9980862367F, - 0.9981150595F, 0.9981435600F, 0.9981717406F, 0.9981996035F, - 0.9982271511F, 0.9982543856F, 0.9982813093F, 0.9983079246F, - 0.9983342336F, 0.9983602386F, 0.9983859418F, 0.9984113456F, - 0.9984364522F, 0.9984612638F, 0.9984857825F, 0.9985100108F, - 0.9985339507F, 0.9985576044F, 0.9985809743F, 0.9986040624F, - 0.9986268710F, 0.9986494022F, 0.9986716583F, 0.9986936413F, - 0.9987153535F, 0.9987367969F, 0.9987579738F, 0.9987788864F, - 0.9987995366F, 0.9988199267F, 0.9988400587F, 0.9988599348F, - 0.9988795572F, 0.9988989278F, 0.9989180487F, 0.9989369222F, - 0.9989555501F, 0.9989739347F, 0.9989920780F, 0.9990099820F, - 0.9990276487F, 0.9990450803F, 0.9990622787F, 0.9990792460F, - 0.9990959841F, 0.9991124952F, 0.9991287812F, 0.9991448440F, - 0.9991606858F, 0.9991763084F, 0.9991917139F, 0.9992069042F, - 0.9992218813F, 0.9992366471F, 0.9992512035F, 0.9992655525F, - 0.9992796961F, 0.9992936361F, 0.9993073744F, 0.9993209131F, - 0.9993342538F, 0.9993473987F, 0.9993603494F, 0.9993731080F, - 0.9993856762F, 0.9993980559F, 0.9994102490F, 0.9994222573F, - 0.9994340827F, 0.9994457269F, 0.9994571918F, 0.9994684793F, - 0.9994795910F, 0.9994905288F, 0.9995012945F, 0.9995118898F, - 0.9995223165F, 0.9995325765F, 0.9995426713F, 0.9995526029F, - 0.9995623728F, 0.9995719829F, 0.9995814349F, 0.9995907304F, - 0.9995998712F, 0.9996088590F, 0.9996176954F, 0.9996263821F, - 0.9996349208F, 0.9996433132F, 0.9996515609F, 0.9996596656F, - 0.9996676288F, 0.9996754522F, 0.9996831375F, 0.9996906862F, - 0.9996981000F, 0.9997053804F, 0.9997125290F, 0.9997195474F, - 0.9997264371F, 0.9997331998F, 0.9997398369F, 0.9997463500F, - 0.9997527406F, 0.9997590103F, 0.9997651606F, 0.9997711930F, - 0.9997771089F, 0.9997829098F, 0.9997885973F, 0.9997941728F, - 0.9997996378F, 0.9998049936F, 0.9998102419F, 0.9998153839F, - 0.9998204211F, 0.9998253550F, 0.9998301868F, 0.9998349182F, - 0.9998395503F, 0.9998440847F, 0.9998485226F, 0.9998528654F, - 0.9998571146F, 0.9998612713F, 0.9998653370F, 0.9998693130F, - 0.9998732007F, 0.9998770012F, 0.9998807159F, 0.9998843461F, - 0.9998878931F, 0.9998913581F, 0.9998947424F, 0.9998980473F, - 0.9999012740F, 0.9999044237F, 0.9999074976F, 0.9999104971F, - 0.9999134231F, 0.9999162771F, 0.9999190601F, 0.9999217733F, - 0.9999244179F, 0.9999269950F, 0.9999295058F, 0.9999319515F, - 0.9999343332F, 0.9999366519F, 0.9999389088F, 0.9999411050F, - 0.9999432416F, 0.9999453196F, 0.9999473402F, 0.9999493044F, - 0.9999512132F, 0.9999530677F, 0.9999548690F, 0.9999566180F, - 0.9999583157F, 0.9999599633F, 0.9999615616F, 0.9999631116F, - 0.9999646144F, 0.9999660709F, 0.9999674820F, 0.9999688487F, - 0.9999701719F, 0.9999714526F, 0.9999726917F, 0.9999738900F, - 0.9999750486F, 0.9999761682F, 0.9999772497F, 0.9999782941F, - 0.9999793021F, 0.9999802747F, 0.9999812126F, 0.9999821167F, - 0.9999829878F, 0.9999838268F, 0.9999846343F, 0.9999854113F, - 0.9999861584F, 0.9999868765F, 0.9999875664F, 0.9999882287F, - 0.9999888642F, 0.9999894736F, 0.9999900577F, 0.9999906172F, - 0.9999911528F, 0.9999916651F, 0.9999921548F, 0.9999926227F, - 0.9999930693F, 0.9999934954F, 0.9999939015F, 0.9999942883F, - 0.9999946564F, 0.9999950064F, 0.9999953390F, 0.9999956547F, - 0.9999959541F, 0.9999962377F, 0.9999965062F, 0.9999967601F, - 0.9999969998F, 0.9999972260F, 0.9999974392F, 0.9999976399F, - 0.9999978285F, 0.9999980056F, 0.9999981716F, 0.9999983271F, - 0.9999984724F, 0.9999986081F, 0.9999987345F, 0.9999988521F, - 0.9999989613F, 0.9999990625F, 0.9999991562F, 0.9999992426F, - 0.9999993223F, 0.9999993954F, 0.9999994625F, 0.9999995239F, - 0.9999995798F, 0.9999996307F, 0.9999996768F, 0.9999997184F, - 0.9999997559F, 0.9999997895F, 0.9999998195F, 0.9999998462F, - 0.9999998698F, 0.9999998906F, 0.9999999088F, 0.9999999246F, - 0.9999999383F, 0.9999999500F, 0.9999999600F, 0.9999999684F, - 0.9999999754F, 0.9999999811F, 0.9999999858F, 0.9999999896F, - 0.9999999925F, 0.9999999948F, 0.9999999965F, 0.9999999978F, - 0.9999999986F, 0.9999999992F, 0.9999999996F, 0.9999999998F, - 0.9999999999F, 1.0000000000F, 1.0000000000F, 1.0000000000F, -}; - -static const float vwin8192[4096] = { - 0.0000000578F, 0.0000005198F, 0.0000014438F, 0.0000028299F, - 0.0000046780F, 0.0000069882F, 0.0000097604F, 0.0000129945F, - 0.0000166908F, 0.0000208490F, 0.0000254692F, 0.0000305515F, - 0.0000360958F, 0.0000421021F, 0.0000485704F, 0.0000555006F, - 0.0000628929F, 0.0000707472F, 0.0000790635F, 0.0000878417F, - 0.0000970820F, 0.0001067842F, 0.0001169483F, 0.0001275744F, - 0.0001386625F, 0.0001502126F, 0.0001622245F, 0.0001746984F, - 0.0001876343F, 0.0002010320F, 0.0002148917F, 0.0002292132F, - 0.0002439967F, 0.0002592421F, 0.0002749493F, 0.0002911184F, - 0.0003077493F, 0.0003248421F, 0.0003423967F, 0.0003604132F, - 0.0003788915F, 0.0003978316F, 0.0004172335F, 0.0004370971F, - 0.0004574226F, 0.0004782098F, 0.0004994587F, 0.0005211694F, - 0.0005433418F, 0.0005659759F, 0.0005890717F, 0.0006126292F, - 0.0006366484F, 0.0006611292F, 0.0006860716F, 0.0007114757F, - 0.0007373414F, 0.0007636687F, 0.0007904576F, 0.0008177080F, - 0.0008454200F, 0.0008735935F, 0.0009022285F, 0.0009313250F, - 0.0009608830F, 0.0009909025F, 0.0010213834F, 0.0010523257F, - 0.0010837295F, 0.0011155946F, 0.0011479211F, 0.0011807090F, - 0.0012139582F, 0.0012476687F, 0.0012818405F, 0.0013164736F, - 0.0013515679F, 0.0013871235F, 0.0014231402F, 0.0014596182F, - 0.0014965573F, 0.0015339576F, 0.0015718190F, 0.0016101415F, - 0.0016489251F, 0.0016881698F, 0.0017278754F, 0.0017680421F, - 0.0018086698F, 0.0018497584F, 0.0018913080F, 0.0019333185F, - 0.0019757898F, 0.0020187221F, 0.0020621151F, 0.0021059690F, - 0.0021502837F, 0.0021950591F, 0.0022402953F, 0.0022859921F, - 0.0023321497F, 0.0023787679F, 0.0024258467F, 0.0024733861F, - 0.0025213861F, 0.0025698466F, 0.0026187676F, 0.0026681491F, - 0.0027179911F, 0.0027682935F, 0.0028190562F, 0.0028702794F, - 0.0029219628F, 0.0029741066F, 0.0030267107F, 0.0030797749F, - 0.0031332994F, 0.0031872841F, 0.0032417289F, 0.0032966338F, - 0.0033519988F, 0.0034078238F, 0.0034641089F, 0.0035208539F, - 0.0035780589F, 0.0036357237F, 0.0036938485F, 0.0037524331F, - 0.0038114775F, 0.0038709817F, 0.0039309456F, 0.0039913692F, - 0.0040522524F, 0.0041135953F, 0.0041753978F, 0.0042376599F, - 0.0043003814F, 0.0043635624F, 0.0044272029F, 0.0044913028F, - 0.0045558620F, 0.0046208806F, 0.0046863585F, 0.0047522955F, - 0.0048186919F, 0.0048855473F, 0.0049528619F, 0.0050206356F, - 0.0050888684F, 0.0051575601F, 0.0052267108F, 0.0052963204F, - 0.0053663890F, 0.0054369163F, 0.0055079025F, 0.0055793474F, - 0.0056512510F, 0.0057236133F, 0.0057964342F, 0.0058697137F, - 0.0059434517F, 0.0060176482F, 0.0060923032F, 0.0061674166F, - 0.0062429883F, 0.0063190183F, 0.0063955066F, 0.0064724532F, - 0.0065498579F, 0.0066277207F, 0.0067060416F, 0.0067848205F, - 0.0068640575F, 0.0069437523F, 0.0070239051F, 0.0071045157F, - 0.0071855840F, 0.0072671102F, 0.0073490940F, 0.0074315355F, - 0.0075144345F, 0.0075977911F, 0.0076816052F, 0.0077658768F, - 0.0078506057F, 0.0079357920F, 0.0080214355F, 0.0081075363F, - 0.0081940943F, 0.0082811094F, 0.0083685816F, 0.0084565108F, - 0.0085448970F, 0.0086337401F, 0.0087230401F, 0.0088127969F, - 0.0089030104F, 0.0089936807F, 0.0090848076F, 0.0091763911F, - 0.0092684311F, 0.0093609276F, 0.0094538805F, 0.0095472898F, - 0.0096411554F, 0.0097354772F, 0.0098302552F, 0.0099254894F, - 0.0100211796F, 0.0101173259F, 0.0102139281F, 0.0103109863F, - 0.0104085002F, 0.0105064700F, 0.0106048955F, 0.0107037766F, - 0.0108031133F, 0.0109029056F, 0.0110031534F, 0.0111038565F, - 0.0112050151F, 0.0113066289F, 0.0114086980F, 0.0115112222F, - 0.0116142015F, 0.0117176359F, 0.0118215252F, 0.0119258695F, - 0.0120306686F, 0.0121359225F, 0.0122416312F, 0.0123477944F, - 0.0124544123F, 0.0125614847F, 0.0126690116F, 0.0127769928F, - 0.0128854284F, 0.0129943182F, 0.0131036623F, 0.0132134604F, - 0.0133237126F, 0.0134344188F, 0.0135455790F, 0.0136571929F, - 0.0137692607F, 0.0138817821F, 0.0139947572F, 0.0141081859F, - 0.0142220681F, 0.0143364037F, 0.0144511927F, 0.0145664350F, - 0.0146821304F, 0.0147982791F, 0.0149148808F, 0.0150319355F, - 0.0151494431F, 0.0152674036F, 0.0153858168F, 0.0155046828F, - 0.0156240014F, 0.0157437726F, 0.0158639962F, 0.0159846723F, - 0.0161058007F, 0.0162273814F, 0.0163494142F, 0.0164718991F, - 0.0165948361F, 0.0167182250F, 0.0168420658F, 0.0169663584F, - 0.0170911027F, 0.0172162987F, 0.0173419462F, 0.0174680452F, - 0.0175945956F, 0.0177215974F, 0.0178490504F, 0.0179769545F, - 0.0181053098F, 0.0182341160F, 0.0183633732F, 0.0184930812F, - 0.0186232399F, 0.0187538494F, 0.0188849094F, 0.0190164200F, - 0.0191483809F, 0.0192807923F, 0.0194136539F, 0.0195469656F, - 0.0196807275F, 0.0198149394F, 0.0199496012F, 0.0200847128F, - 0.0202202742F, 0.0203562853F, 0.0204927460F, 0.0206296561F, - 0.0207670157F, 0.0209048245F, 0.0210430826F, 0.0211817899F, - 0.0213209462F, 0.0214605515F, 0.0216006057F, 0.0217411086F, - 0.0218820603F, 0.0220234605F, 0.0221653093F, 0.0223076066F, - 0.0224503521F, 0.0225935459F, 0.0227371879F, 0.0228812779F, - 0.0230258160F, 0.0231708018F, 0.0233162355F, 0.0234621169F, - 0.0236084459F, 0.0237552224F, 0.0239024462F, 0.0240501175F, - 0.0241982359F, 0.0243468015F, 0.0244958141F, 0.0246452736F, - 0.0247951800F, 0.0249455331F, 0.0250963329F, 0.0252475792F, - 0.0253992720F, 0.0255514111F, 0.0257039965F, 0.0258570281F, - 0.0260105057F, 0.0261644293F, 0.0263187987F, 0.0264736139F, - 0.0266288747F, 0.0267845811F, 0.0269407330F, 0.0270973302F, - 0.0272543727F, 0.0274118604F, 0.0275697930F, 0.0277281707F, - 0.0278869932F, 0.0280462604F, 0.0282059723F, 0.0283661287F, - 0.0285267295F, 0.0286877747F, 0.0288492641F, 0.0290111976F, - 0.0291735751F, 0.0293363965F, 0.0294996617F, 0.0296633706F, - 0.0298275231F, 0.0299921190F, 0.0301571583F, 0.0303226409F, - 0.0304885667F, 0.0306549354F, 0.0308217472F, 0.0309890017F, - 0.0311566989F, 0.0313248388F, 0.0314934211F, 0.0316624459F, - 0.0318319128F, 0.0320018220F, 0.0321721732F, 0.0323429663F, - 0.0325142013F, 0.0326858779F, 0.0328579962F, 0.0330305559F, - 0.0332035570F, 0.0333769994F, 0.0335508829F, 0.0337252074F, - 0.0338999728F, 0.0340751790F, 0.0342508259F, 0.0344269134F, - 0.0346034412F, 0.0347804094F, 0.0349578178F, 0.0351356663F, - 0.0353139548F, 0.0354926831F, 0.0356718511F, 0.0358514588F, - 0.0360315059F, 0.0362119924F, 0.0363929182F, 0.0365742831F, - 0.0367560870F, 0.0369383297F, 0.0371210113F, 0.0373041315F, - 0.0374876902F, 0.0376716873F, 0.0378561226F, 0.0380409961F, - 0.0382263077F, 0.0384120571F, 0.0385982443F, 0.0387848691F, - 0.0389719315F, 0.0391594313F, 0.0393473683F, 0.0395357425F, - 0.0397245537F, 0.0399138017F, 0.0401034866F, 0.0402936080F, - 0.0404841660F, 0.0406751603F, 0.0408665909F, 0.0410584576F, - 0.0412507603F, 0.0414434988F, 0.0416366731F, 0.0418302829F, - 0.0420243282F, 0.0422188088F, 0.0424137246F, 0.0426090755F, - 0.0428048613F, 0.0430010819F, 0.0431977371F, 0.0433948269F, - 0.0435923511F, 0.0437903095F, 0.0439887020F, 0.0441875285F, - 0.0443867889F, 0.0445864830F, 0.0447866106F, 0.0449871717F, - 0.0451881661F, 0.0453895936F, 0.0455914542F, 0.0457937477F, - 0.0459964738F, 0.0461996326F, 0.0464032239F, 0.0466072475F, - 0.0468117032F, 0.0470165910F, 0.0472219107F, 0.0474276622F, - 0.0476338452F, 0.0478404597F, 0.0480475056F, 0.0482549827F, - 0.0484628907F, 0.0486712297F, 0.0488799994F, 0.0490891998F, - 0.0492988306F, 0.0495088917F, 0.0497193830F, 0.0499303043F, - 0.0501416554F, 0.0503534363F, 0.0505656468F, 0.0507782867F, - 0.0509913559F, 0.0512048542F, 0.0514187815F, 0.0516331376F, - 0.0518479225F, 0.0520631358F, 0.0522787775F, 0.0524948475F, - 0.0527113455F, 0.0529282715F, 0.0531456252F, 0.0533634066F, - 0.0535816154F, 0.0538002515F, 0.0540193148F, 0.0542388051F, - 0.0544587222F, 0.0546790660F, 0.0548998364F, 0.0551210331F, - 0.0553426561F, 0.0555647051F, 0.0557871801F, 0.0560100807F, - 0.0562334070F, 0.0564571587F, 0.0566813357F, 0.0569059378F, - 0.0571309649F, 0.0573564168F, 0.0575822933F, 0.0578085942F, - 0.0580353195F, 0.0582624689F, 0.0584900423F, 0.0587180396F, - 0.0589464605F, 0.0591753049F, 0.0594045726F, 0.0596342635F, - 0.0598643774F, 0.0600949141F, 0.0603258735F, 0.0605572555F, - 0.0607890597F, 0.0610212862F, 0.0612539346F, 0.0614870049F, - 0.0617204968F, 0.0619544103F, 0.0621887451F, 0.0624235010F, - 0.0626586780F, 0.0628942758F, 0.0631302942F, 0.0633667331F, - 0.0636035923F, 0.0638408717F, 0.0640785710F, 0.0643166901F, - 0.0645552288F, 0.0647941870F, 0.0650335645F, 0.0652733610F, - 0.0655135765F, 0.0657542108F, 0.0659952636F, 0.0662367348F, - 0.0664786242F, 0.0667209316F, 0.0669636570F, 0.0672068000F, - 0.0674503605F, 0.0676943384F, 0.0679387334F, 0.0681835454F, - 0.0684287742F, 0.0686744196F, 0.0689204814F, 0.0691669595F, - 0.0694138536F, 0.0696611637F, 0.0699088894F, 0.0701570307F, - 0.0704055873F, 0.0706545590F, 0.0709039458F, 0.0711537473F, - 0.0714039634F, 0.0716545939F, 0.0719056387F, 0.0721570975F, - 0.0724089702F, 0.0726612565F, 0.0729139563F, 0.0731670694F, - 0.0734205956F, 0.0736745347F, 0.0739288866F, 0.0741836510F, - 0.0744388277F, 0.0746944166F, 0.0749504175F, 0.0752068301F, - 0.0754636543F, 0.0757208899F, 0.0759785367F, 0.0762365946F, - 0.0764950632F, 0.0767539424F, 0.0770132320F, 0.0772729319F, - 0.0775330418F, 0.0777935616F, 0.0780544909F, 0.0783158298F, - 0.0785775778F, 0.0788397349F, 0.0791023009F, 0.0793652755F, - 0.0796286585F, 0.0798924498F, 0.0801566492F, 0.0804212564F, - 0.0806862712F, 0.0809516935F, 0.0812175231F, 0.0814837597F, - 0.0817504031F, 0.0820174532F, 0.0822849097F, 0.0825527724F, - 0.0828210412F, 0.0830897158F, 0.0833587960F, 0.0836282816F, - 0.0838981724F, 0.0841684682F, 0.0844391688F, 0.0847102740F, - 0.0849817835F, 0.0852536973F, 0.0855260150F, 0.0857987364F, - 0.0860718614F, 0.0863453897F, 0.0866193211F, 0.0868936554F, - 0.0871683924F, 0.0874435319F, 0.0877190737F, 0.0879950175F, - 0.0882713632F, 0.0885481105F, 0.0888252592F, 0.0891028091F, - 0.0893807600F, 0.0896591117F, 0.0899378639F, 0.0902170165F, - 0.0904965692F, 0.0907765218F, 0.0910568740F, 0.0913376258F, - 0.0916187767F, 0.0919003268F, 0.0921822756F, 0.0924646230F, - 0.0927473687F, 0.0930305126F, 0.0933140545F, 0.0935979940F, - 0.0938823310F, 0.0941670653F, 0.0944521966F, 0.0947377247F, - 0.0950236494F, 0.0953099704F, 0.0955966876F, 0.0958838007F, - 0.0961713094F, 0.0964592136F, 0.0967475131F, 0.0970362075F, - 0.0973252967F, 0.0976147805F, 0.0979046585F, 0.0981949307F, - 0.0984855967F, 0.0987766563F, 0.0990681093F, 0.0993599555F, - 0.0996521945F, 0.0999448263F, 0.1002378506F, 0.1005312671F, - 0.1008250755F, 0.1011192757F, 0.1014138675F, 0.1017088505F, - 0.1020042246F, 0.1022999895F, 0.1025961450F, 0.1028926909F, - 0.1031896268F, 0.1034869526F, 0.1037846680F, 0.1040827729F, - 0.1043812668F, 0.1046801497F, 0.1049794213F, 0.1052790813F, - 0.1055791294F, 0.1058795656F, 0.1061803894F, 0.1064816006F, - 0.1067831991F, 0.1070851846F, 0.1073875568F, 0.1076903155F, - 0.1079934604F, 0.1082969913F, 0.1086009079F, 0.1089052101F, - 0.1092098975F, 0.1095149699F, 0.1098204270F, 0.1101262687F, - 0.1104324946F, 0.1107391045F, 0.1110460982F, 0.1113534754F, - 0.1116612359F, 0.1119693793F, 0.1122779055F, 0.1125868142F, - 0.1128961052F, 0.1132057781F, 0.1135158328F, 0.1138262690F, - 0.1141370863F, 0.1144482847F, 0.1147598638F, 0.1150718233F, - 0.1153841631F, 0.1156968828F, 0.1160099822F, 0.1163234610F, - 0.1166373190F, 0.1169515559F, 0.1172661714F, 0.1175811654F, - 0.1178965374F, 0.1182122874F, 0.1185284149F, 0.1188449198F, - 0.1191618018F, 0.1194790606F, 0.1197966960F, 0.1201147076F, - 0.1204330953F, 0.1207518587F, 0.1210709976F, 0.1213905118F, - 0.1217104009F, 0.1220306647F, 0.1223513029F, 0.1226723153F, - 0.1229937016F, 0.1233154615F, 0.1236375948F, 0.1239601011F, - 0.1242829803F, 0.1246062319F, 0.1249298559F, 0.1252538518F, - 0.1255782195F, 0.1259029586F, 0.1262280689F, 0.1265535501F, - 0.1268794019F, 0.1272056241F, 0.1275322163F, 0.1278591784F, - 0.1281865099F, 0.1285142108F, 0.1288422805F, 0.1291707190F, - 0.1294995259F, 0.1298287009F, 0.1301582437F, 0.1304881542F, - 0.1308184319F, 0.1311490766F, 0.1314800881F, 0.1318114660F, - 0.1321432100F, 0.1324753200F, 0.1328077955F, 0.1331406364F, - 0.1334738422F, 0.1338074129F, 0.1341413479F, 0.1344756472F, - 0.1348103103F, 0.1351453370F, 0.1354807270F, 0.1358164801F, - 0.1361525959F, 0.1364890741F, 0.1368259145F, 0.1371631167F, - 0.1375006805F, 0.1378386056F, 0.1381768917F, 0.1385155384F, - 0.1388545456F, 0.1391939129F, 0.1395336400F, 0.1398737266F, - 0.1402141724F, 0.1405549772F, 0.1408961406F, 0.1412376623F, - 0.1415795421F, 0.1419217797F, 0.1422643746F, 0.1426073268F, - 0.1429506358F, 0.1432943013F, 0.1436383231F, 0.1439827008F, - 0.1443274342F, 0.1446725229F, 0.1450179667F, 0.1453637652F, - 0.1457099181F, 0.1460564252F, 0.1464032861F, 0.1467505006F, - 0.1470980682F, 0.1474459888F, 0.1477942620F, 0.1481428875F, - 0.1484918651F, 0.1488411942F, 0.1491908748F, 0.1495409065F, - 0.1498912889F, 0.1502420218F, 0.1505931048F, 0.1509445376F, - 0.1512963200F, 0.1516484516F, 0.1520009321F, 0.1523537612F, - 0.1527069385F, 0.1530604638F, 0.1534143368F, 0.1537685571F, - 0.1541231244F, 0.1544780384F, 0.1548332987F, 0.1551889052F, - 0.1555448574F, 0.1559011550F, 0.1562577978F, 0.1566147853F, - 0.1569721173F, 0.1573297935F, 0.1576878135F, 0.1580461771F, - 0.1584048838F, 0.1587639334F, 0.1591233255F, 0.1594830599F, - 0.1598431361F, 0.1602035540F, 0.1605643131F, 0.1609254131F, - 0.1612868537F, 0.1616486346F, 0.1620107555F, 0.1623732160F, - 0.1627360158F, 0.1630991545F, 0.1634626319F, 0.1638264476F, - 0.1641906013F, 0.1645550926F, 0.1649199212F, 0.1652850869F, - 0.1656505892F, 0.1660164278F, 0.1663826024F, 0.1667491127F, - 0.1671159583F, 0.1674831388F, 0.1678506541F, 0.1682185036F, - 0.1685866872F, 0.1689552044F, 0.1693240549F, 0.1696932384F, - 0.1700627545F, 0.1704326029F, 0.1708027833F, 0.1711732952F, - 0.1715441385F, 0.1719153127F, 0.1722868175F, 0.1726586526F, - 0.1730308176F, 0.1734033121F, 0.1737761359F, 0.1741492886F, - 0.1745227698F, 0.1748965792F, 0.1752707164F, 0.1756451812F, - 0.1760199731F, 0.1763950918F, 0.1767705370F, 0.1771463083F, - 0.1775224054F, 0.1778988279F, 0.1782755754F, 0.1786526477F, - 0.1790300444F, 0.1794077651F, 0.1797858094F, 0.1801641771F, - 0.1805428677F, 0.1809218810F, 0.1813012165F, 0.1816808739F, - 0.1820608528F, 0.1824411530F, 0.1828217739F, 0.1832027154F, - 0.1835839770F, 0.1839655584F, 0.1843474592F, 0.1847296790F, - 0.1851122175F, 0.1854950744F, 0.1858782492F, 0.1862617417F, - 0.1866455514F, 0.1870296780F, 0.1874141211F, 0.1877988804F, - 0.1881839555F, 0.1885693461F, 0.1889550517F, 0.1893410721F, - 0.1897274068F, 0.1901140555F, 0.1905010178F, 0.1908882933F, - 0.1912758818F, 0.1916637828F, 0.1920519959F, 0.1924405208F, - 0.1928293571F, 0.1932185044F, 0.1936079625F, 0.1939977308F, - 0.1943878091F, 0.1947781969F, 0.1951688939F, 0.1955598998F, - 0.1959512141F, 0.1963428364F, 0.1967347665F, 0.1971270038F, - 0.1975195482F, 0.1979123990F, 0.1983055561F, 0.1986990190F, - 0.1990927873F, 0.1994868607F, 0.1998812388F, 0.2002759212F, - 0.2006709075F, 0.2010661974F, 0.2014617904F, 0.2018576862F, - 0.2022538844F, 0.2026503847F, 0.2030471865F, 0.2034442897F, - 0.2038416937F, 0.2042393982F, 0.2046374028F, 0.2050357071F, - 0.2054343107F, 0.2058332133F, 0.2062324145F, 0.2066319138F, - 0.2070317110F, 0.2074318055F, 0.2078321970F, 0.2082328852F, - 0.2086338696F, 0.2090351498F, 0.2094367255F, 0.2098385962F, - 0.2102407617F, 0.2106432213F, 0.2110459749F, 0.2114490220F, - 0.2118523621F, 0.2122559950F, 0.2126599202F, 0.2130641373F, - 0.2134686459F, 0.2138734456F, 0.2142785361F, 0.2146839168F, - 0.2150895875F, 0.2154955478F, 0.2159017972F, 0.2163083353F, - 0.2167151617F, 0.2171222761F, 0.2175296780F, 0.2179373670F, - 0.2183453428F, 0.2187536049F, 0.2191621529F, 0.2195709864F, - 0.2199801051F, 0.2203895085F, 0.2207991961F, 0.2212091677F, - 0.2216194228F, 0.2220299610F, 0.2224407818F, 0.2228518850F, - 0.2232632699F, 0.2236749364F, 0.2240868839F, 0.2244991121F, - 0.2249116204F, 0.2253244086F, 0.2257374763F, 0.2261508229F, - 0.2265644481F, 0.2269783514F, 0.2273925326F, 0.2278069911F, - 0.2282217265F, 0.2286367384F, 0.2290520265F, 0.2294675902F, - 0.2298834292F, 0.2302995431F, 0.2307159314F, 0.2311325937F, - 0.2315495297F, 0.2319667388F, 0.2323842207F, 0.2328019749F, - 0.2332200011F, 0.2336382988F, 0.2340568675F, 0.2344757070F, - 0.2348948166F, 0.2353141961F, 0.2357338450F, 0.2361537629F, - 0.2365739493F, 0.2369944038F, 0.2374151261F, 0.2378361156F, - 0.2382573720F, 0.2386788948F, 0.2391006836F, 0.2395227380F, - 0.2399450575F, 0.2403676417F, 0.2407904902F, 0.2412136026F, - 0.2416369783F, 0.2420606171F, 0.2424845185F, 0.2429086820F, - 0.2433331072F, 0.2437577936F, 0.2441827409F, 0.2446079486F, - 0.2450334163F, 0.2454591435F, 0.2458851298F, 0.2463113747F, - 0.2467378779F, 0.2471646389F, 0.2475916573F, 0.2480189325F, - 0.2484464643F, 0.2488742521F, 0.2493022955F, 0.2497305940F, - 0.2501591473F, 0.2505879549F, 0.2510170163F, 0.2514463311F, - 0.2518758989F, 0.2523057193F, 0.2527357916F, 0.2531661157F, - 0.2535966909F, 0.2540275169F, 0.2544585931F, 0.2548899193F, - 0.2553214948F, 0.2557533193F, 0.2561853924F, 0.2566177135F, - 0.2570502822F, 0.2574830981F, 0.2579161608F, 0.2583494697F, - 0.2587830245F, 0.2592168246F, 0.2596508697F, 0.2600851593F, - 0.2605196929F, 0.2609544701F, 0.2613894904F, 0.2618247534F, - 0.2622602586F, 0.2626960055F, 0.2631319938F, 0.2635682230F, - 0.2640046925F, 0.2644414021F, 0.2648783511F, 0.2653155391F, - 0.2657529657F, 0.2661906305F, 0.2666285329F, 0.2670666725F, - 0.2675050489F, 0.2679436616F, 0.2683825101F, 0.2688215940F, - 0.2692609127F, 0.2697004660F, 0.2701402532F, 0.2705802739F, - 0.2710205278F, 0.2714610142F, 0.2719017327F, 0.2723426830F, - 0.2727838644F, 0.2732252766F, 0.2736669191F, 0.2741087914F, - 0.2745508930F, 0.2749932235F, 0.2754357824F, 0.2758785693F, - 0.2763215837F, 0.2767648251F, 0.2772082930F, 0.2776519870F, - 0.2780959066F, 0.2785400513F, 0.2789844207F, 0.2794290143F, - 0.2798738316F, 0.2803188722F, 0.2807641355F, 0.2812096211F, - 0.2816553286F, 0.2821012574F, 0.2825474071F, 0.2829937773F, - 0.2834403673F, 0.2838871768F, 0.2843342053F, 0.2847814523F, - 0.2852289174F, 0.2856765999F, 0.2861244996F, 0.2865726159F, - 0.2870209482F, 0.2874694962F, 0.2879182594F, 0.2883672372F, - 0.2888164293F, 0.2892658350F, 0.2897154540F, 0.2901652858F, - 0.2906153298F, 0.2910655856F, 0.2915160527F, 0.2919667306F, - 0.2924176189F, 0.2928687171F, 0.2933200246F, 0.2937715409F, - 0.2942232657F, 0.2946751984F, 0.2951273386F, 0.2955796856F, - 0.2960322391F, 0.2964849986F, 0.2969379636F, 0.2973911335F, - 0.2978445080F, 0.2982980864F, 0.2987518684F, 0.2992058534F, - 0.2996600409F, 0.3001144305F, 0.3005690217F, 0.3010238139F, - 0.3014788067F, 0.3019339995F, 0.3023893920F, 0.3028449835F, - 0.3033007736F, 0.3037567618F, 0.3042129477F, 0.3046693306F, - 0.3051259102F, 0.3055826859F, 0.3060396572F, 0.3064968236F, - 0.3069541847F, 0.3074117399F, 0.3078694887F, 0.3083274307F, - 0.3087855653F, 0.3092438920F, 0.3097024104F, 0.3101611199F, - 0.3106200200F, 0.3110791103F, 0.3115383902F, 0.3119978592F, - 0.3124575169F, 0.3129173627F, 0.3133773961F, 0.3138376166F, - 0.3142980238F, 0.3147586170F, 0.3152193959F, 0.3156803598F, - 0.3161415084F, 0.3166028410F, 0.3170643573F, 0.3175260566F, - 0.3179879384F, 0.3184500023F, 0.3189122478F, 0.3193746743F, - 0.3198372814F, 0.3203000685F, 0.3207630351F, 0.3212261807F, - 0.3216895048F, 0.3221530069F, 0.3226166865F, 0.3230805430F, - 0.3235445760F, 0.3240087849F, 0.3244731693F, 0.3249377285F, - 0.3254024622F, 0.3258673698F, 0.3263324507F, 0.3267977045F, - 0.3272631306F, 0.3277287286F, 0.3281944978F, 0.3286604379F, - 0.3291265482F, 0.3295928284F, 0.3300592777F, 0.3305258958F, - 0.3309926821F, 0.3314596361F, 0.3319267573F, 0.3323940451F, - 0.3328614990F, 0.3333291186F, 0.3337969033F, 0.3342648525F, - 0.3347329658F, 0.3352012427F, 0.3356696825F, 0.3361382849F, - 0.3366070492F, 0.3370759749F, 0.3375450616F, 0.3380143087F, - 0.3384837156F, 0.3389532819F, 0.3394230071F, 0.3398928905F, - 0.3403629317F, 0.3408331302F, 0.3413034854F, 0.3417739967F, - 0.3422446638F, 0.3427154860F, 0.3431864628F, 0.3436575938F, - 0.3441288782F, 0.3446003158F, 0.3450719058F, 0.3455436478F, - 0.3460155412F, 0.3464875856F, 0.3469597804F, 0.3474321250F, - 0.3479046189F, 0.3483772617F, 0.3488500527F, 0.3493229914F, - 0.3497960774F, 0.3502693100F, 0.3507426887F, 0.3512162131F, - 0.3516898825F, 0.3521636965F, 0.3526376545F, 0.3531117559F, - 0.3535860003F, 0.3540603870F, 0.3545349157F, 0.3550095856F, - 0.3554843964F, 0.3559593474F, 0.3564344381F, 0.3569096680F, - 0.3573850366F, 0.3578605432F, 0.3583361875F, 0.3588119687F, - 0.3592878865F, 0.3597639402F, 0.3602401293F, 0.3607164533F, - 0.3611929117F, 0.3616695038F, 0.3621462292F, 0.3626230873F, - 0.3631000776F, 0.3635771995F, 0.3640544525F, 0.3645318360F, - 0.3650093496F, 0.3654869926F, 0.3659647645F, 0.3664426648F, - 0.3669206930F, 0.3673988484F, 0.3678771306F, 0.3683555390F, - 0.3688340731F, 0.3693127322F, 0.3697915160F, 0.3702704237F, - 0.3707494549F, 0.3712286091F, 0.3717078857F, 0.3721872840F, - 0.3726668037F, 0.3731464441F, 0.3736262047F, 0.3741060850F, - 0.3745860843F, 0.3750662023F, 0.3755464382F, 0.3760267915F, - 0.3765072618F, 0.3769878484F, 0.3774685509F, 0.3779493686F, - 0.3784303010F, 0.3789113475F, 0.3793925076F, 0.3798737809F, - 0.3803551666F, 0.3808366642F, 0.3813182733F, 0.3817999932F, - 0.3822818234F, 0.3827637633F, 0.3832458124F, 0.3837279702F, - 0.3842102360F, 0.3846926093F, 0.3851750897F, 0.3856576764F, - 0.3861403690F, 0.3866231670F, 0.3871060696F, 0.3875890765F, - 0.3880721870F, 0.3885554007F, 0.3890387168F, 0.3895221349F, - 0.3900056544F, 0.3904892748F, 0.3909729955F, 0.3914568160F, - 0.3919407356F, 0.3924247539F, 0.3929088702F, 0.3933930841F, - 0.3938773949F, 0.3943618021F, 0.3948463052F, 0.3953309035F, - 0.3958155966F, 0.3963003838F, 0.3967852646F, 0.3972702385F, - 0.3977553048F, 0.3982404631F, 0.3987257127F, 0.3992110531F, - 0.3996964838F, 0.4001820041F, 0.4006676136F, 0.4011533116F, - 0.4016390976F, 0.4021249710F, 0.4026109313F, 0.4030969779F, - 0.4035831102F, 0.4040693277F, 0.4045556299F, 0.4050420160F, - 0.4055284857F, 0.4060150383F, 0.4065016732F, 0.4069883899F, - 0.4074751879F, 0.4079620665F, 0.4084490252F, 0.4089360635F, - 0.4094231807F, 0.4099103763F, 0.4103976498F, 0.4108850005F, - 0.4113724280F, 0.4118599315F, 0.4123475107F, 0.4128351648F, - 0.4133228934F, 0.4138106959F, 0.4142985716F, 0.4147865201F, - 0.4152745408F, 0.4157626330F, 0.4162507963F, 0.4167390301F, - 0.4172273337F, 0.4177157067F, 0.4182041484F, 0.4186926583F, - 0.4191812359F, 0.4196698805F, 0.4201585915F, 0.4206473685F, - 0.4211362108F, 0.4216251179F, 0.4221140892F, 0.4226031241F, - 0.4230922221F, 0.4235813826F, 0.4240706050F, 0.4245598887F, - 0.4250492332F, 0.4255386379F, 0.4260281022F, 0.4265176256F, - 0.4270072075F, 0.4274968473F, 0.4279865445F, 0.4284762984F, - 0.4289661086F, 0.4294559743F, 0.4299458951F, 0.4304358704F, - 0.4309258996F, 0.4314159822F, 0.4319061175F, 0.4323963050F, - 0.4328865441F, 0.4333768342F, 0.4338671749F, 0.4343575654F, - 0.4348480052F, 0.4353384938F, 0.4358290306F, 0.4363196149F, - 0.4368102463F, 0.4373009241F, 0.4377916478F, 0.4382824168F, - 0.4387732305F, 0.4392640884F, 0.4397549899F, 0.4402459343F, - 0.4407369212F, 0.4412279499F, 0.4417190198F, 0.4422101305F, - 0.4427012813F, 0.4431924717F, 0.4436837010F, 0.4441749686F, - 0.4446662742F, 0.4451576169F, 0.4456489963F, 0.4461404118F, - 0.4466318628F, 0.4471233487F, 0.4476148690F, 0.4481064230F, - 0.4485980103F, 0.4490896302F, 0.4495812821F, 0.4500729654F, - 0.4505646797F, 0.4510564243F, 0.4515481986F, 0.4520400021F, - 0.4525318341F, 0.4530236942F, 0.4535155816F, 0.4540074959F, - 0.4544994365F, 0.4549914028F, 0.4554833941F, 0.4559754100F, - 0.4564674499F, 0.4569595131F, 0.4574515991F, 0.4579437074F, - 0.4584358372F, 0.4589279881F, 0.4594201595F, 0.4599123508F, - 0.4604045615F, 0.4608967908F, 0.4613890383F, 0.4618813034F, - 0.4623735855F, 0.4628658841F, 0.4633581984F, 0.4638505281F, - 0.4643428724F, 0.4648352308F, 0.4653276028F, 0.4658199877F, - 0.4663123849F, 0.4668047940F, 0.4672972143F, 0.4677896451F, - 0.4682820861F, 0.4687745365F, 0.4692669958F, 0.4697594634F, - 0.4702519387F, 0.4707444211F, 0.4712369102F, 0.4717294052F, - 0.4722219056F, 0.4727144109F, 0.4732069204F, 0.4736994336F, - 0.4741919498F, 0.4746844686F, 0.4751769893F, 0.4756695113F, - 0.4761620341F, 0.4766545571F, 0.4771470797F, 0.4776396013F, - 0.4781321213F, 0.4786246392F, 0.4791171544F, 0.4796096663F, - 0.4801021744F, 0.4805946779F, 0.4810871765F, 0.4815796694F, - 0.4820721561F, 0.4825646360F, 0.4830571086F, 0.4835495732F, - 0.4840420293F, 0.4845344763F, 0.4850269136F, 0.4855193407F, - 0.4860117569F, 0.4865041617F, 0.4869965545F, 0.4874889347F, - 0.4879813018F, 0.4884736551F, 0.4889659941F, 0.4894583182F, - 0.4899506268F, 0.4904429193F, 0.4909351952F, 0.4914274538F, - 0.4919196947F, 0.4924119172F, 0.4929041207F, 0.4933963046F, - 0.4938884685F, 0.4943806116F, 0.4948727335F, 0.4953648335F, - 0.4958569110F, 0.4963489656F, 0.4968409965F, 0.4973330032F, - 0.4978249852F, 0.4983169419F, 0.4988088726F, 0.4993007768F, - 0.4997926539F, 0.5002845034F, 0.5007763247F, 0.5012681171F, - 0.5017598801F, 0.5022516132F, 0.5027433157F, 0.5032349871F, - 0.5037266268F, 0.5042182341F, 0.5047098086F, 0.5052013497F, - 0.5056928567F, 0.5061843292F, 0.5066757664F, 0.5071671679F, - 0.5076585330F, 0.5081498613F, 0.5086411520F, 0.5091324047F, - 0.5096236187F, 0.5101147934F, 0.5106059284F, 0.5110970230F, - 0.5115880766F, 0.5120790887F, 0.5125700587F, 0.5130609860F, - 0.5135518700F, 0.5140427102F, 0.5145335059F, 0.5150242566F, - 0.5155149618F, 0.5160056208F, 0.5164962331F, 0.5169867980F, - 0.5174773151F, 0.5179677837F, 0.5184582033F, 0.5189485733F, - 0.5194388931F, 0.5199291621F, 0.5204193798F, 0.5209095455F, - 0.5213996588F, 0.5218897190F, 0.5223797256F, 0.5228696779F, - 0.5233595755F, 0.5238494177F, 0.5243392039F, 0.5248289337F, - 0.5253186063F, 0.5258082213F, 0.5262977781F, 0.5267872760F, - 0.5272767146F, 0.5277660932F, 0.5282554112F, 0.5287446682F, - 0.5292338635F, 0.5297229965F, 0.5302120667F, 0.5307010736F, - 0.5311900164F, 0.5316788947F, 0.5321677079F, 0.5326564554F, - 0.5331451366F, 0.5336337511F, 0.5341222981F, 0.5346107771F, - 0.5350991876F, 0.5355875290F, 0.5360758007F, 0.5365640021F, - 0.5370521327F, 0.5375401920F, 0.5380281792F, 0.5385160939F, - 0.5390039355F, 0.5394917034F, 0.5399793971F, 0.5404670159F, - 0.5409545594F, 0.5414420269F, 0.5419294179F, 0.5424167318F, - 0.5429039680F, 0.5433911261F, 0.5438782053F, 0.5443652051F, - 0.5448521250F, 0.5453389644F, 0.5458257228F, 0.5463123995F, - 0.5467989940F, 0.5472855057F, 0.5477719341F, 0.5482582786F, - 0.5487445387F, 0.5492307137F, 0.5497168031F, 0.5502028063F, - 0.5506887228F, 0.5511745520F, 0.5516602934F, 0.5521459463F, - 0.5526315103F, 0.5531169847F, 0.5536023690F, 0.5540876626F, - 0.5545728649F, 0.5550579755F, 0.5555429937F, 0.5560279189F, - 0.5565127507F, 0.5569974884F, 0.5574821315F, 0.5579666794F, - 0.5584511316F, 0.5589354875F, 0.5594197465F, 0.5599039080F, - 0.5603879716F, 0.5608719367F, 0.5613558026F, 0.5618395689F, - 0.5623232350F, 0.5628068002F, 0.5632902642F, 0.5637736262F, - 0.5642568858F, 0.5647400423F, 0.5652230953F, 0.5657060442F, - 0.5661888883F, 0.5666716272F, 0.5671542603F, 0.5676367870F, - 0.5681192069F, 0.5686015192F, 0.5690837235F, 0.5695658192F, - 0.5700478058F, 0.5705296827F, 0.5710114494F, 0.5714931052F, - 0.5719746497F, 0.5724560822F, 0.5729374023F, 0.5734186094F, - 0.5738997029F, 0.5743806823F, 0.5748615470F, 0.5753422965F, - 0.5758229301F, 0.5763034475F, 0.5767838480F, 0.5772641310F, - 0.5777442960F, 0.5782243426F, 0.5787042700F, 0.5791840778F, - 0.5796637654F, 0.5801433322F, 0.5806227778F, 0.5811021016F, - 0.5815813029F, 0.5820603814F, 0.5825393363F, 0.5830181673F, - 0.5834968737F, 0.5839754549F, 0.5844539105F, 0.5849322399F, - 0.5854104425F, 0.5858885179F, 0.5863664653F, 0.5868442844F, - 0.5873219746F, 0.5877995353F, 0.5882769660F, 0.5887542661F, - 0.5892314351F, 0.5897084724F, 0.5901853776F, 0.5906621500F, - 0.5911387892F, 0.5916152945F, 0.5920916655F, 0.5925679016F, - 0.5930440022F, 0.5935199669F, 0.5939957950F, 0.5944714861F, - 0.5949470396F, 0.5954224550F, 0.5958977317F, 0.5963728692F, - 0.5968478669F, 0.5973227244F, 0.5977974411F, 0.5982720163F, - 0.5987464497F, 0.5992207407F, 0.5996948887F, 0.6001688932F, - 0.6006427537F, 0.6011164696F, 0.6015900405F, 0.6020634657F, - 0.6025367447F, 0.6030098770F, 0.6034828621F, 0.6039556995F, - 0.6044283885F, 0.6049009288F, 0.6053733196F, 0.6058455606F, - 0.6063176512F, 0.6067895909F, 0.6072613790F, 0.6077330152F, - 0.6082044989F, 0.6086758295F, 0.6091470065F, 0.6096180294F, - 0.6100888977F, 0.6105596108F, 0.6110301682F, 0.6115005694F, - 0.6119708139F, 0.6124409011F, 0.6129108305F, 0.6133806017F, - 0.6138502139F, 0.6143196669F, 0.6147889599F, 0.6152580926F, - 0.6157270643F, 0.6161958746F, 0.6166645230F, 0.6171330088F, - 0.6176013317F, 0.6180694910F, 0.6185374863F, 0.6190053171F, - 0.6194729827F, 0.6199404828F, 0.6204078167F, 0.6208749841F, - 0.6213419842F, 0.6218088168F, 0.6222754811F, 0.6227419768F, - 0.6232083032F, 0.6236744600F, 0.6241404465F, 0.6246062622F, - 0.6250719067F, 0.6255373795F, 0.6260026799F, 0.6264678076F, - 0.6269327619F, 0.6273975425F, 0.6278621487F, 0.6283265800F, - 0.6287908361F, 0.6292549163F, 0.6297188201F, 0.6301825471F, - 0.6306460966F, 0.6311094683F, 0.6315726617F, 0.6320356761F, - 0.6324985111F, 0.6329611662F, 0.6334236410F, 0.6338859348F, - 0.6343480472F, 0.6348099777F, 0.6352717257F, 0.6357332909F, - 0.6361946726F, 0.6366558704F, 0.6371168837F, 0.6375777122F, - 0.6380383552F, 0.6384988123F, 0.6389590830F, 0.6394191668F, - 0.6398790631F, 0.6403387716F, 0.6407982916F, 0.6412576228F, - 0.6417167645F, 0.6421757163F, 0.6426344778F, 0.6430930483F, - 0.6435514275F, 0.6440096149F, 0.6444676098F, 0.6449254119F, - 0.6453830207F, 0.6458404356F, 0.6462976562F, 0.6467546820F, - 0.6472115125F, 0.6476681472F, 0.6481245856F, 0.6485808273F, - 0.6490368717F, 0.6494927183F, 0.6499483667F, 0.6504038164F, - 0.6508590670F, 0.6513141178F, 0.6517689684F, 0.6522236185F, - 0.6526780673F, 0.6531323146F, 0.6535863598F, 0.6540402024F, - 0.6544938419F, 0.6549472779F, 0.6554005099F, 0.6558535373F, - 0.6563063598F, 0.6567589769F, 0.6572113880F, 0.6576635927F, - 0.6581155906F, 0.6585673810F, 0.6590189637F, 0.6594703380F, - 0.6599215035F, 0.6603724598F, 0.6608232064F, 0.6612737427F, - 0.6617240684F, 0.6621741829F, 0.6626240859F, 0.6630737767F, - 0.6635232550F, 0.6639725202F, 0.6644215720F, 0.6648704098F, - 0.6653190332F, 0.6657674417F, 0.6662156348F, 0.6666636121F, - 0.6671113731F, 0.6675589174F, 0.6680062445F, 0.6684533538F, - 0.6689002450F, 0.6693469177F, 0.6697933712F, 0.6702396052F, - 0.6706856193F, 0.6711314129F, 0.6715769855F, 0.6720223369F, - 0.6724674664F, 0.6729123736F, 0.6733570581F, 0.6738015194F, - 0.6742457570F, 0.6746897706F, 0.6751335596F, 0.6755771236F, - 0.6760204621F, 0.6764635747F, 0.6769064609F, 0.6773491204F, - 0.6777915525F, 0.6782337570F, 0.6786757332F, 0.6791174809F, - 0.6795589995F, 0.6800002886F, 0.6804413477F, 0.6808821765F, - 0.6813227743F, 0.6817631409F, 0.6822032758F, 0.6826431785F, - 0.6830828485F, 0.6835222855F, 0.6839614890F, 0.6844004585F, - 0.6848391936F, 0.6852776939F, 0.6857159589F, 0.6861539883F, - 0.6865917815F, 0.6870293381F, 0.6874666576F, 0.6879037398F, - 0.6883405840F, 0.6887771899F, 0.6892135571F, 0.6896496850F, - 0.6900855733F, 0.6905212216F, 0.6909566294F, 0.6913917963F, - 0.6918267218F, 0.6922614055F, 0.6926958471F, 0.6931300459F, - 0.6935640018F, 0.6939977141F, 0.6944311825F, 0.6948644066F, - 0.6952973859F, 0.6957301200F, 0.6961626085F, 0.6965948510F, - 0.6970268470F, 0.6974585961F, 0.6978900980F, 0.6983213521F, - 0.6987523580F, 0.6991831154F, 0.6996136238F, 0.7000438828F, - 0.7004738921F, 0.7009036510F, 0.7013331594F, 0.7017624166F, - 0.7021914224F, 0.7026201763F, 0.7030486779F, 0.7034769268F, - 0.7039049226F, 0.7043326648F, 0.7047601531F, 0.7051873870F, - 0.7056143662F, 0.7060410902F, 0.7064675586F, 0.7068937711F, - 0.7073197271F, 0.7077454264F, 0.7081708684F, 0.7085960529F, - 0.7090209793F, 0.7094456474F, 0.7098700566F, 0.7102942066F, - 0.7107180970F, 0.7111417274F, 0.7115650974F, 0.7119882066F, - 0.7124110545F, 0.7128336409F, 0.7132559653F, 0.7136780272F, - 0.7140998264F, 0.7145213624F, 0.7149426348F, 0.7153636433F, - 0.7157843874F, 0.7162048668F, 0.7166250810F, 0.7170450296F, - 0.7174647124F, 0.7178841289F, 0.7183032786F, 0.7187221613F, - 0.7191407765F, 0.7195591239F, 0.7199772030F, 0.7203950135F, - 0.7208125550F, 0.7212298271F, 0.7216468294F, 0.7220635616F, - 0.7224800233F, 0.7228962140F, 0.7233121335F, 0.7237277813F, - 0.7241431571F, 0.7245582604F, 0.7249730910F, 0.7253876484F, - 0.7258019322F, 0.7262159422F, 0.7266296778F, 0.7270431388F, - 0.7274563247F, 0.7278692353F, 0.7282818700F, 0.7286942287F, - 0.7291063108F, 0.7295181160F, 0.7299296440F, 0.7303408944F, - 0.7307518669F, 0.7311625609F, 0.7315729763F, 0.7319831126F, - 0.7323929695F, 0.7328025466F, 0.7332118435F, 0.7336208600F, - 0.7340295955F, 0.7344380499F, 0.7348462226F, 0.7352541134F, - 0.7356617220F, 0.7360690478F, 0.7364760907F, 0.7368828502F, - 0.7372893259F, 0.7376955176F, 0.7381014249F, 0.7385070475F, - 0.7389123849F, 0.7393174368F, 0.7397222029F, 0.7401266829F, - 0.7405308763F, 0.7409347829F, 0.7413384023F, 0.7417417341F, - 0.7421447780F, 0.7425475338F, 0.7429500009F, 0.7433521791F, - 0.7437540681F, 0.7441556674F, 0.7445569769F, 0.7449579960F, - 0.7453587245F, 0.7457591621F, 0.7461593084F, 0.7465591631F, - 0.7469587259F, 0.7473579963F, 0.7477569741F, 0.7481556590F, - 0.7485540506F, 0.7489521486F, 0.7493499526F, 0.7497474623F, - 0.7501446775F, 0.7505415977F, 0.7509382227F, 0.7513345521F, - 0.7517305856F, 0.7521263229F, 0.7525217636F, 0.7529169074F, - 0.7533117541F, 0.7537063032F, 0.7541005545F, 0.7544945076F, - 0.7548881623F, 0.7552815182F, 0.7556745749F, 0.7560673323F, - 0.7564597899F, 0.7568519474F, 0.7572438046F, 0.7576353611F, - 0.7580266166F, 0.7584175708F, 0.7588082235F, 0.7591985741F, - 0.7595886226F, 0.7599783685F, 0.7603678116F, 0.7607569515F, - 0.7611457879F, 0.7615343206F, 0.7619225493F, 0.7623104735F, - 0.7626980931F, 0.7630854078F, 0.7634724171F, 0.7638591209F, - 0.7642455188F, 0.7646316106F, 0.7650173959F, 0.7654028744F, - 0.7657880459F, 0.7661729100F, 0.7665574664F, 0.7669417150F, - 0.7673256553F, 0.7677092871F, 0.7680926100F, 0.7684756239F, - 0.7688583284F, 0.7692407232F, 0.7696228080F, 0.7700045826F, - 0.7703860467F, 0.7707671999F, 0.7711480420F, 0.7715285728F, - 0.7719087918F, 0.7722886989F, 0.7726682938F, 0.7730475762F, - 0.7734265458F, 0.7738052023F, 0.7741835454F, 0.7745615750F, - 0.7749392906F, 0.7753166921F, 0.7756937791F, 0.7760705514F, - 0.7764470087F, 0.7768231508F, 0.7771989773F, 0.7775744880F, - 0.7779496827F, 0.7783245610F, 0.7786991227F, 0.7790733676F, - 0.7794472953F, 0.7798209056F, 0.7801941982F, 0.7805671729F, - 0.7809398294F, 0.7813121675F, 0.7816841869F, 0.7820558873F, - 0.7824272684F, 0.7827983301F, 0.7831690720F, 0.7835394940F, - 0.7839095957F, 0.7842793768F, 0.7846488373F, 0.7850179767F, - 0.7853867948F, 0.7857552914F, 0.7861234663F, 0.7864913191F, - 0.7868588497F, 0.7872260578F, 0.7875929431F, 0.7879595055F, - 0.7883257445F, 0.7886916601F, 0.7890572520F, 0.7894225198F, - 0.7897874635F, 0.7901520827F, 0.7905163772F, 0.7908803468F, - 0.7912439912F, 0.7916073102F, 0.7919703035F, 0.7923329710F, - 0.7926953124F, 0.7930573274F, 0.7934190158F, 0.7937803774F, - 0.7941414120F, 0.7945021193F, 0.7948624991F, 0.7952225511F, - 0.7955822752F, 0.7959416711F, 0.7963007387F, 0.7966594775F, - 0.7970178875F, 0.7973759685F, 0.7977337201F, 0.7980911422F, - 0.7984482346F, 0.7988049970F, 0.7991614292F, 0.7995175310F, - 0.7998733022F, 0.8002287426F, 0.8005838519F, 0.8009386299F, - 0.8012930765F, 0.8016471914F, 0.8020009744F, 0.8023544253F, - 0.8027075438F, 0.8030603298F, 0.8034127831F, 0.8037649035F, - 0.8041166906F, 0.8044681445F, 0.8048192647F, 0.8051700512F, - 0.8055205038F, 0.8058706222F, 0.8062204062F, 0.8065698556F, - 0.8069189702F, 0.8072677499F, 0.8076161944F, 0.8079643036F, - 0.8083120772F, 0.8086595151F, 0.8090066170F, 0.8093533827F, - 0.8096998122F, 0.8100459051F, 0.8103916613F, 0.8107370806F, - 0.8110821628F, 0.8114269077F, 0.8117713151F, 0.8121153849F, - 0.8124591169F, 0.8128025108F, 0.8131455666F, 0.8134882839F, - 0.8138306627F, 0.8141727027F, 0.8145144038F, 0.8148557658F, - 0.8151967886F, 0.8155374718F, 0.8158778154F, 0.8162178192F, - 0.8165574830F, 0.8168968067F, 0.8172357900F, 0.8175744328F, - 0.8179127349F, 0.8182506962F, 0.8185883164F, 0.8189255955F, - 0.8192625332F, 0.8195991295F, 0.8199353840F, 0.8202712967F, - 0.8206068673F, 0.8209420958F, 0.8212769820F, 0.8216115256F, - 0.8219457266F, 0.8222795848F, 0.8226131000F, 0.8229462721F, - 0.8232791009F, 0.8236115863F, 0.8239437280F, 0.8242755260F, - 0.8246069801F, 0.8249380901F, 0.8252688559F, 0.8255992774F, - 0.8259293544F, 0.8262590867F, 0.8265884741F, 0.8269175167F, - 0.8272462141F, 0.8275745663F, 0.8279025732F, 0.8282302344F, - 0.8285575501F, 0.8288845199F, 0.8292111437F, 0.8295374215F, - 0.8298633530F, 0.8301889382F, 0.8305141768F, 0.8308390688F, - 0.8311636141F, 0.8314878124F, 0.8318116637F, 0.8321351678F, - 0.8324583246F, 0.8327811340F, 0.8331035957F, 0.8334257098F, - 0.8337474761F, 0.8340688944F, 0.8343899647F, 0.8347106867F, - 0.8350310605F, 0.8353510857F, 0.8356707624F, 0.8359900904F, - 0.8363090696F, 0.8366276999F, 0.8369459811F, 0.8372639131F, - 0.8375814958F, 0.8378987292F, 0.8382156130F, 0.8385321472F, - 0.8388483316F, 0.8391641662F, 0.8394796508F, 0.8397947853F, - 0.8401095697F, 0.8404240037F, 0.8407380873F, 0.8410518204F, - 0.8413652029F, 0.8416782347F, 0.8419909156F, 0.8423032456F, - 0.8426152245F, 0.8429268523F, 0.8432381289F, 0.8435490541F, - 0.8438596279F, 0.8441698502F, 0.8444797208F, 0.8447892396F, - 0.8450984067F, 0.8454072218F, 0.8457156849F, 0.8460237959F, - 0.8463315547F, 0.8466389612F, 0.8469460154F, 0.8472527170F, - 0.8475590661F, 0.8478650625F, 0.8481707063F, 0.8484759971F, - 0.8487809351F, 0.8490855201F, 0.8493897521F, 0.8496936308F, - 0.8499971564F, 0.8503003286F, 0.8506031474F, 0.8509056128F, - 0.8512077246F, 0.8515094828F, 0.8518108872F, 0.8521119379F, - 0.8524126348F, 0.8527129777F, 0.8530129666F, 0.8533126015F, - 0.8536118822F, 0.8539108087F, 0.8542093809F, 0.8545075988F, - 0.8548054623F, 0.8551029712F, 0.8554001257F, 0.8556969255F, - 0.8559933707F, 0.8562894611F, 0.8565851968F, 0.8568805775F, - 0.8571756034F, 0.8574702743F, 0.8577645902F, 0.8580585509F, - 0.8583521566F, 0.8586454070F, 0.8589383021F, 0.8592308420F, - 0.8595230265F, 0.8598148556F, 0.8601063292F, 0.8603974473F, - 0.8606882098F, 0.8609786167F, 0.8612686680F, 0.8615583636F, - 0.8618477034F, 0.8621366874F, 0.8624253156F, 0.8627135878F, - 0.8630015042F, 0.8632890646F, 0.8635762690F, 0.8638631173F, - 0.8641496096F, 0.8644357457F, 0.8647215257F, 0.8650069495F, - 0.8652920171F, 0.8655767283F, 0.8658610833F, 0.8661450820F, - 0.8664287243F, 0.8667120102F, 0.8669949397F, 0.8672775127F, - 0.8675597293F, 0.8678415894F, 0.8681230929F, 0.8684042398F, - 0.8686850302F, 0.8689654640F, 0.8692455412F, 0.8695252617F, - 0.8698046255F, 0.8700836327F, 0.8703622831F, 0.8706405768F, - 0.8709185138F, 0.8711960940F, 0.8714733174F, 0.8717501840F, - 0.8720266939F, 0.8723028469F, 0.8725786430F, 0.8728540824F, - 0.8731291648F, 0.8734038905F, 0.8736782592F, 0.8739522711F, - 0.8742259261F, 0.8744992242F, 0.8747721653F, 0.8750447496F, - 0.8753169770F, 0.8755888475F, 0.8758603611F, 0.8761315177F, - 0.8764023175F, 0.8766727603F, 0.8769428462F, 0.8772125752F, - 0.8774819474F, 0.8777509626F, 0.8780196209F, 0.8782879224F, - 0.8785558669F, 0.8788234546F, 0.8790906854F, 0.8793575594F, - 0.8796240765F, 0.8798902368F, 0.8801560403F, 0.8804214870F, - 0.8806865768F, 0.8809513099F, 0.8812156863F, 0.8814797059F, - 0.8817433687F, 0.8820066749F, 0.8822696243F, 0.8825322171F, - 0.8827944532F, 0.8830563327F, 0.8833178556F, 0.8835790219F, - 0.8838398316F, 0.8841002848F, 0.8843603815F, 0.8846201217F, - 0.8848795054F, 0.8851385327F, 0.8853972036F, 0.8856555182F, - 0.8859134764F, 0.8861710783F, 0.8864283239F, 0.8866852133F, - 0.8869417464F, 0.8871979234F, 0.8874537443F, 0.8877092090F, - 0.8879643177F, 0.8882190704F, 0.8884734671F, 0.8887275078F, - 0.8889811927F, 0.8892345216F, 0.8894874948F, 0.8897401122F, - 0.8899923738F, 0.8902442798F, 0.8904958301F, 0.8907470248F, - 0.8909978640F, 0.8912483477F, 0.8914984759F, 0.8917482487F, - 0.8919976662F, 0.8922467284F, 0.8924954353F, 0.8927437871F, - 0.8929917837F, 0.8932394252F, 0.8934867118F, 0.8937336433F, - 0.8939802199F, 0.8942264417F, 0.8944723087F, 0.8947178210F, - 0.8949629785F, 0.8952077815F, 0.8954522299F, 0.8956963239F, - 0.8959400634F, 0.8961834486F, 0.8964264795F, 0.8966691561F, - 0.8969114786F, 0.8971534470F, 0.8973950614F, 0.8976363219F, - 0.8978772284F, 0.8981177812F, 0.8983579802F, 0.8985978256F, - 0.8988373174F, 0.8990764556F, 0.8993152405F, 0.8995536720F, - 0.8997917502F, 0.9000294751F, 0.9002668470F, 0.9005038658F, - 0.9007405317F, 0.9009768446F, 0.9012128048F, 0.9014484123F, - 0.9016836671F, 0.9019185693F, 0.9021531191F, 0.9023873165F, - 0.9026211616F, 0.9028546546F, 0.9030877954F, 0.9033205841F, - 0.9035530210F, 0.9037851059F, 0.9040168392F, 0.9042482207F, - 0.9044792507F, 0.9047099293F, 0.9049402564F, 0.9051702323F, - 0.9053998569F, 0.9056291305F, 0.9058580531F, 0.9060866248F, - 0.9063148457F, 0.9065427159F, 0.9067702355F, 0.9069974046F, - 0.9072242233F, 0.9074506917F, 0.9076768100F, 0.9079025782F, - 0.9081279964F, 0.9083530647F, 0.9085777833F, 0.9088021523F, - 0.9090261717F, 0.9092498417F, 0.9094731623F, 0.9096961338F, - 0.9099187561F, 0.9101410295F, 0.9103629540F, 0.9105845297F, - 0.9108057568F, 0.9110266354F, 0.9112471656F, 0.9114673475F, - 0.9116871812F, 0.9119066668F, 0.9121258046F, 0.9123445945F, - 0.9125630367F, 0.9127811314F, 0.9129988786F, 0.9132162785F, - 0.9134333312F, 0.9136500368F, 0.9138663954F, 0.9140824073F, - 0.9142980724F, 0.9145133910F, 0.9147283632F, 0.9149429890F, - 0.9151572687F, 0.9153712023F, 0.9155847900F, 0.9157980319F, - 0.9160109282F, 0.9162234790F, 0.9164356844F, 0.9166475445F, - 0.9168590595F, 0.9170702296F, 0.9172810548F, 0.9174915354F, - 0.9177016714F, 0.9179114629F, 0.9181209102F, 0.9183300134F, - 0.9185387726F, 0.9187471879F, 0.9189552595F, 0.9191629876F, - 0.9193703723F, 0.9195774136F, 0.9197841119F, 0.9199904672F, - 0.9201964797F, 0.9204021495F, 0.9206074767F, 0.9208124616F, - 0.9210171043F, 0.9212214049F, 0.9214253636F, 0.9216289805F, - 0.9218322558F, 0.9220351896F, 0.9222377821F, 0.9224400335F, - 0.9226419439F, 0.9228435134F, 0.9230447423F, 0.9232456307F, - 0.9234461787F, 0.9236463865F, 0.9238462543F, 0.9240457822F, - 0.9242449704F, 0.9244438190F, 0.9246423282F, 0.9248404983F, - 0.9250383293F, 0.9252358214F, 0.9254329747F, 0.9256297896F, - 0.9258262660F, 0.9260224042F, 0.9262182044F, 0.9264136667F, - 0.9266087913F, 0.9268035783F, 0.9269980280F, 0.9271921405F, - 0.9273859160F, 0.9275793546F, 0.9277724566F, 0.9279652221F, - 0.9281576513F, 0.9283497443F, 0.9285415014F, 0.9287329227F, - 0.9289240084F, 0.9291147586F, 0.9293051737F, 0.9294952536F, - 0.9296849987F, 0.9298744091F, 0.9300634850F, 0.9302522266F, - 0.9304406340F, 0.9306287074F, 0.9308164471F, 0.9310038532F, - 0.9311909259F, 0.9313776654F, 0.9315640719F, 0.9317501455F, - 0.9319358865F, 0.9321212951F, 0.9323063713F, 0.9324911155F, - 0.9326755279F, 0.9328596085F, 0.9330433577F, 0.9332267756F, - 0.9334098623F, 0.9335926182F, 0.9337750434F, 0.9339571380F, - 0.9341389023F, 0.9343203366F, 0.9345014409F, 0.9346822155F, - 0.9348626606F, 0.9350427763F, 0.9352225630F, 0.9354020207F, - 0.9355811498F, 0.9357599503F, 0.9359384226F, 0.9361165667F, - 0.9362943830F, 0.9364718716F, 0.9366490327F, 0.9368258666F, - 0.9370023733F, 0.9371785533F, 0.9373544066F, 0.9375299335F, - 0.9377051341F, 0.9378800087F, 0.9380545576F, 0.9382287809F, - 0.9384026787F, 0.9385762515F, 0.9387494993F, 0.9389224223F, - 0.9390950209F, 0.9392672951F, 0.9394392453F, 0.9396108716F, - 0.9397821743F, 0.9399531536F, 0.9401238096F, 0.9402941427F, - 0.9404641530F, 0.9406338407F, 0.9408032061F, 0.9409722495F, - 0.9411409709F, 0.9413093707F, 0.9414774491F, 0.9416452062F, - 0.9418126424F, 0.9419797579F, 0.9421465528F, 0.9423130274F, - 0.9424791819F, 0.9426450166F, 0.9428105317F, 0.9429757274F, - 0.9431406039F, 0.9433051616F, 0.9434694005F, 0.9436333209F, - 0.9437969232F, 0.9439602074F, 0.9441231739F, 0.9442858229F, - 0.9444481545F, 0.9446101691F, 0.9447718669F, 0.9449332481F, - 0.9450943129F, 0.9452550617F, 0.9454154945F, 0.9455756118F, - 0.9457354136F, 0.9458949003F, 0.9460540721F, 0.9462129292F, - 0.9463714719F, 0.9465297003F, 0.9466876149F, 0.9468452157F, - 0.9470025031F, 0.9471594772F, 0.9473161384F, 0.9474724869F, - 0.9476285229F, 0.9477842466F, 0.9479396584F, 0.9480947585F, - 0.9482495470F, 0.9484040243F, 0.9485581906F, 0.9487120462F, - 0.9488655913F, 0.9490188262F, 0.9491717511F, 0.9493243662F, - 0.9494766718F, 0.9496286683F, 0.9497803557F, 0.9499317345F, - 0.9500828047F, 0.9502335668F, 0.9503840209F, 0.9505341673F, - 0.9506840062F, 0.9508335380F, 0.9509827629F, 0.9511316810F, - 0.9512802928F, 0.9514285984F, 0.9515765982F, 0.9517242923F, - 0.9518716810F, 0.9520187646F, 0.9521655434F, 0.9523120176F, - 0.9524581875F, 0.9526040534F, 0.9527496154F, 0.9528948739F, - 0.9530398292F, 0.9531844814F, 0.9533288310F, 0.9534728780F, - 0.9536166229F, 0.9537600659F, 0.9539032071F, 0.9540460470F, - 0.9541885858F, 0.9543308237F, 0.9544727611F, 0.9546143981F, - 0.9547557351F, 0.9548967723F, 0.9550375100F, 0.9551779485F, - 0.9553180881F, 0.9554579290F, 0.9555974714F, 0.9557367158F, - 0.9558756623F, 0.9560143112F, 0.9561526628F, 0.9562907174F, - 0.9564284752F, 0.9565659366F, 0.9567031017F, 0.9568399710F, - 0.9569765446F, 0.9571128229F, 0.9572488061F, 0.9573844944F, - 0.9575198883F, 0.9576549879F, 0.9577897936F, 0.9579243056F, - 0.9580585242F, 0.9581924497F, 0.9583260824F, 0.9584594226F, - 0.9585924705F, 0.9587252264F, 0.9588576906F, 0.9589898634F, - 0.9591217452F, 0.9592533360F, 0.9593846364F, 0.9595156465F, - 0.9596463666F, 0.9597767971F, 0.9599069382F, 0.9600367901F, - 0.9601663533F, 0.9602956279F, 0.9604246143F, 0.9605533128F, - 0.9606817236F, 0.9608098471F, 0.9609376835F, 0.9610652332F, - 0.9611924963F, 0.9613194733F, 0.9614461644F, 0.9615725699F, - 0.9616986901F, 0.9618245253F, 0.9619500757F, 0.9620753418F, - 0.9622003238F, 0.9623250219F, 0.9624494365F, 0.9625735679F, - 0.9626974163F, 0.9628209821F, 0.9629442656F, 0.9630672671F, - 0.9631899868F, 0.9633124251F, 0.9634345822F, 0.9635564585F, - 0.9636780543F, 0.9637993699F, 0.9639204056F, 0.9640411616F, - 0.9641616383F, 0.9642818359F, 0.9644017549F, 0.9645213955F, - 0.9646407579F, 0.9647598426F, 0.9648786497F, 0.9649971797F, - 0.9651154328F, 0.9652334092F, 0.9653511095F, 0.9654685337F, - 0.9655856823F, 0.9657025556F, 0.9658191538F, 0.9659354773F, - 0.9660515263F, 0.9661673013F, 0.9662828024F, 0.9663980300F, - 0.9665129845F, 0.9666276660F, 0.9667420750F, 0.9668562118F, - 0.9669700766F, 0.9670836698F, 0.9671969917F, 0.9673100425F, - 0.9674228227F, 0.9675353325F, 0.9676475722F, 0.9677595422F, - 0.9678712428F, 0.9679826742F, 0.9680938368F, 0.9682047309F, - 0.9683153569F, 0.9684257150F, 0.9685358056F, 0.9686456289F, - 0.9687551853F, 0.9688644752F, 0.9689734987F, 0.9690822564F, - 0.9691907483F, 0.9692989750F, 0.9694069367F, 0.9695146337F, - 0.9696220663F, 0.9697292349F, 0.9698361398F, 0.9699427813F, - 0.9700491597F, 0.9701552754F, 0.9702611286F, 0.9703667197F, - 0.9704720490F, 0.9705771169F, 0.9706819236F, 0.9707864695F, - 0.9708907549F, 0.9709947802F, 0.9710985456F, 0.9712020514F, - 0.9713052981F, 0.9714082859F, 0.9715110151F, 0.9716134862F, - 0.9717156993F, 0.9718176549F, 0.9719193532F, 0.9720207946F, - 0.9721219794F, 0.9722229080F, 0.9723235806F, 0.9724239976F, - 0.9725241593F, 0.9726240661F, 0.9727237183F, 0.9728231161F, - 0.9729222601F, 0.9730211503F, 0.9731197873F, 0.9732181713F, - 0.9733163027F, 0.9734141817F, 0.9735118088F, 0.9736091842F, - 0.9737063083F, 0.9738031814F, 0.9738998039F, 0.9739961760F, - 0.9740922981F, 0.9741881706F, 0.9742837938F, 0.9743791680F, - 0.9744742935F, 0.9745691707F, 0.9746637999F, 0.9747581814F, - 0.9748523157F, 0.9749462029F, 0.9750398435F, 0.9751332378F, - 0.9752263861F, 0.9753192887F, 0.9754119461F, 0.9755043585F, - 0.9755965262F, 0.9756884496F, 0.9757801291F, 0.9758715650F, - 0.9759627575F, 0.9760537071F, 0.9761444141F, 0.9762348789F, - 0.9763251016F, 0.9764150828F, 0.9765048228F, 0.9765943218F, - 0.9766835802F, 0.9767725984F, 0.9768613767F, 0.9769499154F, - 0.9770382149F, 0.9771262755F, 0.9772140976F, 0.9773016815F, - 0.9773890275F, 0.9774761360F, 0.9775630073F, 0.9776496418F, - 0.9777360398F, 0.9778222016F, 0.9779081277F, 0.9779938182F, - 0.9780792736F, 0.9781644943F, 0.9782494805F, 0.9783342326F, - 0.9784187509F, 0.9785030359F, 0.9785870877F, 0.9786709069F, - 0.9787544936F, 0.9788378484F, 0.9789209714F, 0.9790038631F, - 0.9790865238F, 0.9791689538F, 0.9792511535F, 0.9793331232F, - 0.9794148633F, 0.9794963742F, 0.9795776561F, 0.9796587094F, - 0.9797395345F, 0.9798201316F, 0.9799005013F, 0.9799806437F, - 0.9800605593F, 0.9801402483F, 0.9802197112F, 0.9802989483F, - 0.9803779600F, 0.9804567465F, 0.9805353082F, 0.9806136455F, - 0.9806917587F, 0.9807696482F, 0.9808473143F, 0.9809247574F, - 0.9810019778F, 0.9810789759F, 0.9811557519F, 0.9812323064F, - 0.9813086395F, 0.9813847517F, 0.9814606433F, 0.9815363147F, - 0.9816117662F, 0.9816869981F, 0.9817620108F, 0.9818368047F, - 0.9819113801F, 0.9819857374F, 0.9820598769F, 0.9821337989F, - 0.9822075038F, 0.9822809920F, 0.9823542638F, 0.9824273195F, - 0.9825001596F, 0.9825727843F, 0.9826451940F, 0.9827173891F, - 0.9827893700F, 0.9828611368F, 0.9829326901F, 0.9830040302F, - 0.9830751574F, 0.9831460720F, 0.9832167745F, 0.9832872652F, - 0.9833575444F, 0.9834276124F, 0.9834974697F, 0.9835671166F, - 0.9836365535F, 0.9837057806F, 0.9837747983F, 0.9838436071F, - 0.9839122072F, 0.9839805990F, 0.9840487829F, 0.9841167591F, - 0.9841845282F, 0.9842520903F, 0.9843194459F, 0.9843865953F, - 0.9844535389F, 0.9845202771F, 0.9845868101F, 0.9846531383F, - 0.9847192622F, 0.9847851820F, 0.9848508980F, 0.9849164108F, - 0.9849817205F, 0.9850468276F, 0.9851117324F, 0.9851764352F, - 0.9852409365F, 0.9853052366F, 0.9853693358F, 0.9854332344F, - 0.9854969330F, 0.9855604317F, 0.9856237309F, 0.9856868310F, - 0.9857497325F, 0.9858124355F, 0.9858749404F, 0.9859372477F, - 0.9859993577F, 0.9860612707F, 0.9861229871F, 0.9861845072F, - 0.9862458315F, 0.9863069601F, 0.9863678936F, 0.9864286322F, - 0.9864891764F, 0.9865495264F, 0.9866096826F, 0.9866696454F, - 0.9867294152F, 0.9867889922F, 0.9868483769F, 0.9869075695F, - 0.9869665706F, 0.9870253803F, 0.9870839991F, 0.9871424273F, - 0.9872006653F, 0.9872587135F, 0.9873165721F, 0.9873742415F, - 0.9874317222F, 0.9874890144F, 0.9875461185F, 0.9876030348F, - 0.9876597638F, 0.9877163057F, 0.9877726610F, 0.9878288300F, - 0.9878848130F, 0.9879406104F, 0.9879962225F, 0.9880516497F, - 0.9881068924F, 0.9881619509F, 0.9882168256F, 0.9882715168F, - 0.9883260249F, 0.9883803502F, 0.9884344931F, 0.9884884539F, - 0.9885422331F, 0.9885958309F, 0.9886492477F, 0.9887024838F, - 0.9887555397F, 0.9888084157F, 0.9888611120F, 0.9889136292F, - 0.9889659675F, 0.9890181273F, 0.9890701089F, 0.9891219128F, - 0.9891735392F, 0.9892249885F, 0.9892762610F, 0.9893273572F, - 0.9893782774F, 0.9894290219F, 0.9894795911F, 0.9895299853F, - 0.9895802049F, 0.9896302502F, 0.9896801217F, 0.9897298196F, - 0.9897793443F, 0.9898286961F, 0.9898778755F, 0.9899268828F, - 0.9899757183F, 0.9900243823F, 0.9900728753F, 0.9901211976F, - 0.9901693495F, 0.9902173314F, 0.9902651436F, 0.9903127865F, - 0.9903602605F, 0.9904075659F, 0.9904547031F, 0.9905016723F, - 0.9905484740F, 0.9905951086F, 0.9906415763F, 0.9906878775F, - 0.9907340126F, 0.9907799819F, 0.9908257858F, 0.9908714247F, - 0.9909168988F, 0.9909622086F, 0.9910073543F, 0.9910523364F, - 0.9910971552F, 0.9911418110F, 0.9911863042F, 0.9912306351F, - 0.9912748042F, 0.9913188117F, 0.9913626580F, 0.9914063435F, - 0.9914498684F, 0.9914932333F, 0.9915364383F, 0.9915794839F, - 0.9916223703F, 0.9916650981F, 0.9917076674F, 0.9917500787F, - 0.9917923323F, 0.9918344286F, 0.9918763679F, 0.9919181505F, - 0.9919597769F, 0.9920012473F, 0.9920425621F, 0.9920837217F, - 0.9921247263F, 0.9921655765F, 0.9922062724F, 0.9922468145F, - 0.9922872030F, 0.9923274385F, 0.9923675211F, 0.9924074513F, - 0.9924472294F, 0.9924868557F, 0.9925263306F, 0.9925656544F, - 0.9926048275F, 0.9926438503F, 0.9926827230F, 0.9927214461F, - 0.9927600199F, 0.9927984446F, 0.9928367208F, 0.9928748486F, - 0.9929128285F, 0.9929506608F, 0.9929883459F, 0.9930258841F, - 0.9930632757F, 0.9931005211F, 0.9931376207F, 0.9931745747F, - 0.9932113836F, 0.9932480476F, 0.9932845671F, 0.9933209425F, - 0.9933571742F, 0.9933932623F, 0.9934292074F, 0.9934650097F, - 0.9935006696F, 0.9935361874F, 0.9935715635F, 0.9936067982F, - 0.9936418919F, 0.9936768448F, 0.9937116574F, 0.9937463300F, - 0.9937808629F, 0.9938152565F, 0.9938495111F, 0.9938836271F, - 0.9939176047F, 0.9939514444F, 0.9939851465F, 0.9940187112F, - 0.9940521391F, 0.9940854303F, 0.9941185853F, 0.9941516044F, - 0.9941844879F, 0.9942172361F, 0.9942498495F, 0.9942823283F, - 0.9943146729F, 0.9943468836F, 0.9943789608F, 0.9944109047F, - 0.9944427158F, 0.9944743944F, 0.9945059408F, 0.9945373553F, - 0.9945686384F, 0.9945997902F, 0.9946308112F, 0.9946617017F, - 0.9946924621F, 0.9947230926F, 0.9947535937F, 0.9947839656F, - 0.9948142086F, 0.9948443232F, 0.9948743097F, 0.9949041683F, - 0.9949338995F, 0.9949635035F, 0.9949929807F, 0.9950223315F, - 0.9950515561F, 0.9950806549F, 0.9951096282F, 0.9951384764F, - 0.9951671998F, 0.9951957987F, 0.9952242735F, 0.9952526245F, - 0.9952808520F, 0.9953089564F, 0.9953369380F, 0.9953647971F, - 0.9953925340F, 0.9954201491F, 0.9954476428F, 0.9954750153F, - 0.9955022670F, 0.9955293981F, 0.9955564092F, 0.9955833003F, - 0.9956100720F, 0.9956367245F, 0.9956632582F, 0.9956896733F, - 0.9957159703F, 0.9957421494F, 0.9957682110F, 0.9957941553F, - 0.9958199828F, 0.9958456937F, 0.9958712884F, 0.9958967672F, - 0.9959221305F, 0.9959473784F, 0.9959725115F, 0.9959975300F, - 0.9960224342F, 0.9960472244F, 0.9960719011F, 0.9960964644F, - 0.9961209148F, 0.9961452525F, 0.9961694779F, 0.9961935913F, - 0.9962175930F, 0.9962414834F, 0.9962652627F, 0.9962889313F, - 0.9963124895F, 0.9963359377F, 0.9963592761F, 0.9963825051F, - 0.9964056250F, 0.9964286361F, 0.9964515387F, 0.9964743332F, - 0.9964970198F, 0.9965195990F, 0.9965420709F, 0.9965644360F, - 0.9965866946F, 0.9966088469F, 0.9966308932F, 0.9966528340F, - 0.9966746695F, 0.9966964001F, 0.9967180260F, 0.9967395475F, - 0.9967609651F, 0.9967822789F, 0.9968034894F, 0.9968245968F, - 0.9968456014F, 0.9968665036F, 0.9968873037F, 0.9969080019F, - 0.9969285987F, 0.9969490942F, 0.9969694889F, 0.9969897830F, - 0.9970099769F, 0.9970300708F, 0.9970500651F, 0.9970699601F, - 0.9970897561F, 0.9971094533F, 0.9971290522F, 0.9971485531F, - 0.9971679561F, 0.9971872617F, 0.9972064702F, 0.9972255818F, - 0.9972445968F, 0.9972635157F, 0.9972823386F, 0.9973010659F, - 0.9973196980F, 0.9973382350F, 0.9973566773F, 0.9973750253F, - 0.9973932791F, 0.9974114392F, 0.9974295059F, 0.9974474793F, - 0.9974653599F, 0.9974831480F, 0.9975008438F, 0.9975184476F, - 0.9975359598F, 0.9975533806F, 0.9975707104F, 0.9975879495F, - 0.9976050981F, 0.9976221566F, 0.9976391252F, 0.9976560043F, - 0.9976727941F, 0.9976894950F, 0.9977061073F, 0.9977226312F, - 0.9977390671F, 0.9977554152F, 0.9977716759F, 0.9977878495F, - 0.9978039361F, 0.9978199363F, 0.9978358501F, 0.9978516780F, - 0.9978674202F, 0.9978830771F, 0.9978986488F, 0.9979141358F, - 0.9979295383F, 0.9979448566F, 0.9979600909F, 0.9979752417F, - 0.9979903091F, 0.9980052936F, 0.9980201952F, 0.9980350145F, - 0.9980497515F, 0.9980644067F, 0.9980789804F, 0.9980934727F, - 0.9981078841F, 0.9981222147F, 0.9981364649F, 0.9981506350F, - 0.9981647253F, 0.9981787360F, 0.9981926674F, 0.9982065199F, - 0.9982202936F, 0.9982339890F, 0.9982476062F, 0.9982611456F, - 0.9982746074F, 0.9982879920F, 0.9983012996F, 0.9983145304F, - 0.9983276849F, 0.9983407632F, 0.9983537657F, 0.9983666926F, - 0.9983795442F, 0.9983923208F, 0.9984050226F, 0.9984176501F, - 0.9984302033F, 0.9984426827F, 0.9984550884F, 0.9984674208F, - 0.9984796802F, 0.9984918667F, 0.9985039808F, 0.9985160227F, - 0.9985279926F, 0.9985398909F, 0.9985517177F, 0.9985634734F, - 0.9985751583F, 0.9985867727F, 0.9985983167F, 0.9986097907F, - 0.9986211949F, 0.9986325297F, 0.9986437953F, 0.9986549919F, - 0.9986661199F, 0.9986771795F, 0.9986881710F, 0.9986990946F, - 0.9987099507F, 0.9987207394F, 0.9987314611F, 0.9987421161F, - 0.9987527045F, 0.9987632267F, 0.9987736829F, 0.9987840734F, - 0.9987943985F, 0.9988046584F, 0.9988148534F, 0.9988249838F, - 0.9988350498F, 0.9988450516F, 0.9988549897F, 0.9988648641F, - 0.9988746753F, 0.9988844233F, 0.9988941086F, 0.9989037313F, - 0.9989132918F, 0.9989227902F, 0.9989322269F, 0.9989416021F, - 0.9989509160F, 0.9989601690F, 0.9989693613F, 0.9989784931F, - 0.9989875647F, 0.9989965763F, 0.9990055283F, 0.9990144208F, - 0.9990232541F, 0.9990320286F, 0.9990407443F, 0.9990494016F, - 0.9990580008F, 0.9990665421F, 0.9990750257F, 0.9990834519F, - 0.9990918209F, 0.9991001331F, 0.9991083886F, 0.9991165877F, - 0.9991247307F, 0.9991328177F, 0.9991408491F, 0.9991488251F, - 0.9991567460F, 0.9991646119F, 0.9991724232F, 0.9991801801F, - 0.9991878828F, 0.9991955316F, 0.9992031267F, 0.9992106684F, - 0.9992181569F, 0.9992255925F, 0.9992329753F, 0.9992403057F, - 0.9992475839F, 0.9992548101F, 0.9992619846F, 0.9992691076F, - 0.9992761793F, 0.9992832001F, 0.9992901701F, 0.9992970895F, - 0.9993039587F, 0.9993107777F, 0.9993175470F, 0.9993242667F, - 0.9993309371F, 0.9993375583F, 0.9993441307F, 0.9993506545F, - 0.9993571298F, 0.9993635570F, 0.9993699362F, 0.9993762678F, - 0.9993825519F, 0.9993887887F, 0.9993949785F, 0.9994011216F, - 0.9994072181F, 0.9994132683F, 0.9994192725F, 0.9994252307F, - 0.9994311434F, 0.9994370107F, 0.9994428327F, 0.9994486099F, - 0.9994543423F, 0.9994600303F, 0.9994656739F, 0.9994712736F, - 0.9994768294F, 0.9994823417F, 0.9994878105F, 0.9994932363F, - 0.9994986191F, 0.9995039592F, 0.9995092568F, 0.9995145122F, - 0.9995197256F, 0.9995248971F, 0.9995300270F, 0.9995351156F, - 0.9995401630F, 0.9995451695F, 0.9995501352F, 0.9995550604F, - 0.9995599454F, 0.9995647903F, 0.9995695953F, 0.9995743607F, - 0.9995790866F, 0.9995837734F, 0.9995884211F, 0.9995930300F, - 0.9995976004F, 0.9996021324F, 0.9996066263F, 0.9996110822F, - 0.9996155004F, 0.9996198810F, 0.9996242244F, 0.9996285306F, - 0.9996327999F, 0.9996370326F, 0.9996412287F, 0.9996453886F, - 0.9996495125F, 0.9996536004F, 0.9996576527F, 0.9996616696F, - 0.9996656512F, 0.9996695977F, 0.9996735094F, 0.9996773865F, - 0.9996812291F, 0.9996850374F, 0.9996888118F, 0.9996925523F, - 0.9996962591F, 0.9996999325F, 0.9997035727F, 0.9997071798F, - 0.9997107541F, 0.9997142957F, 0.9997178049F, 0.9997212818F, - 0.9997247266F, 0.9997281396F, 0.9997315209F, 0.9997348708F, - 0.9997381893F, 0.9997414767F, 0.9997447333F, 0.9997479591F, - 0.9997511544F, 0.9997543194F, 0.9997574542F, 0.9997605591F, - 0.9997636342F, 0.9997666797F, 0.9997696958F, 0.9997726828F, - 0.9997756407F, 0.9997785698F, 0.9997814703F, 0.9997843423F, - 0.9997871860F, 0.9997900016F, 0.9997927894F, 0.9997955494F, - 0.9997982818F, 0.9998009869F, 0.9998036648F, 0.9998063157F, - 0.9998089398F, 0.9998115373F, 0.9998141082F, 0.9998166529F, - 0.9998191715F, 0.9998216642F, 0.9998241311F, 0.9998265724F, - 0.9998289884F, 0.9998313790F, 0.9998337447F, 0.9998360854F, - 0.9998384015F, 0.9998406930F, 0.9998429602F, 0.9998452031F, - 0.9998474221F, 0.9998496171F, 0.9998517885F, 0.9998539364F, - 0.9998560610F, 0.9998581624F, 0.9998602407F, 0.9998622962F, - 0.9998643291F, 0.9998663394F, 0.9998683274F, 0.9998702932F, - 0.9998722370F, 0.9998741589F, 0.9998760591F, 0.9998779378F, - 0.9998797952F, 0.9998816313F, 0.9998834464F, 0.9998852406F, - 0.9998870141F, 0.9998887670F, 0.9998904995F, 0.9998922117F, - 0.9998939039F, 0.9998955761F, 0.9998972285F, 0.9998988613F, - 0.9999004746F, 0.9999020686F, 0.9999036434F, 0.9999051992F, - 0.9999067362F, 0.9999082544F, 0.9999097541F, 0.9999112354F, - 0.9999126984F, 0.9999141433F, 0.9999155703F, 0.9999169794F, - 0.9999183709F, 0.9999197449F, 0.9999211014F, 0.9999224408F, - 0.9999237631F, 0.9999250684F, 0.9999263570F, 0.9999276289F, - 0.9999288843F, 0.9999301233F, 0.9999313461F, 0.9999325529F, - 0.9999337437F, 0.9999349187F, 0.9999360780F, 0.9999372218F, - 0.9999383503F, 0.9999394635F, 0.9999405616F, 0.9999416447F, - 0.9999427129F, 0.9999437665F, 0.9999448055F, 0.9999458301F, - 0.9999468404F, 0.9999478365F, 0.9999488185F, 0.9999497867F, - 0.9999507411F, 0.9999516819F, 0.9999526091F, 0.9999535230F, - 0.9999544236F, 0.9999553111F, 0.9999561856F, 0.9999570472F, - 0.9999578960F, 0.9999587323F, 0.9999595560F, 0.9999603674F, - 0.9999611666F, 0.9999619536F, 0.9999627286F, 0.9999634917F, - 0.9999642431F, 0.9999649828F, 0.9999657110F, 0.9999664278F, - 0.9999671334F, 0.9999678278F, 0.9999685111F, 0.9999691835F, - 0.9999698451F, 0.9999704960F, 0.9999711364F, 0.9999717662F, - 0.9999723858F, 0.9999729950F, 0.9999735942F, 0.9999741834F, - 0.9999747626F, 0.9999753321F, 0.9999758919F, 0.9999764421F, - 0.9999769828F, 0.9999775143F, 0.9999780364F, 0.9999785495F, - 0.9999790535F, 0.9999795485F, 0.9999800348F, 0.9999805124F, - 0.9999809813F, 0.9999814417F, 0.9999818938F, 0.9999823375F, - 0.9999827731F, 0.9999832005F, 0.9999836200F, 0.9999840316F, - 0.9999844353F, 0.9999848314F, 0.9999852199F, 0.9999856008F, - 0.9999859744F, 0.9999863407F, 0.9999866997F, 0.9999870516F, - 0.9999873965F, 0.9999877345F, 0.9999880656F, 0.9999883900F, - 0.9999887078F, 0.9999890190F, 0.9999893237F, 0.9999896220F, - 0.9999899140F, 0.9999901999F, 0.9999904796F, 0.9999907533F, - 0.9999910211F, 0.9999912830F, 0.9999915391F, 0.9999917896F, - 0.9999920345F, 0.9999922738F, 0.9999925077F, 0.9999927363F, - 0.9999929596F, 0.9999931777F, 0.9999933907F, 0.9999935987F, - 0.9999938018F, 0.9999940000F, 0.9999941934F, 0.9999943820F, - 0.9999945661F, 0.9999947456F, 0.9999949206F, 0.9999950912F, - 0.9999952575F, 0.9999954195F, 0.9999955773F, 0.9999957311F, - 0.9999958807F, 0.9999960265F, 0.9999961683F, 0.9999963063F, - 0.9999964405F, 0.9999965710F, 0.9999966979F, 0.9999968213F, - 0.9999969412F, 0.9999970576F, 0.9999971707F, 0.9999972805F, - 0.9999973871F, 0.9999974905F, 0.9999975909F, 0.9999976881F, - 0.9999977824F, 0.9999978738F, 0.9999979624F, 0.9999980481F, - 0.9999981311F, 0.9999982115F, 0.9999982892F, 0.9999983644F, - 0.9999984370F, 0.9999985072F, 0.9999985750F, 0.9999986405F, - 0.9999987037F, 0.9999987647F, 0.9999988235F, 0.9999988802F, - 0.9999989348F, 0.9999989873F, 0.9999990379F, 0.9999990866F, - 0.9999991334F, 0.9999991784F, 0.9999992217F, 0.9999992632F, - 0.9999993030F, 0.9999993411F, 0.9999993777F, 0.9999994128F, - 0.9999994463F, 0.9999994784F, 0.9999995091F, 0.9999995384F, - 0.9999995663F, 0.9999995930F, 0.9999996184F, 0.9999996426F, - 0.9999996657F, 0.9999996876F, 0.9999997084F, 0.9999997282F, - 0.9999997469F, 0.9999997647F, 0.9999997815F, 0.9999997973F, - 0.9999998123F, 0.9999998265F, 0.9999998398F, 0.9999998524F, - 0.9999998642F, 0.9999998753F, 0.9999998857F, 0.9999998954F, - 0.9999999045F, 0.9999999130F, 0.9999999209F, 0.9999999282F, - 0.9999999351F, 0.9999999414F, 0.9999999472F, 0.9999999526F, - 0.9999999576F, 0.9999999622F, 0.9999999664F, 0.9999999702F, - 0.9999999737F, 0.9999999769F, 0.9999999798F, 0.9999999824F, - 0.9999999847F, 0.9999999868F, 0.9999999887F, 0.9999999904F, - 0.9999999919F, 0.9999999932F, 0.9999999943F, 0.9999999953F, - 0.9999999961F, 0.9999999969F, 0.9999999975F, 0.9999999980F, - 0.9999999985F, 0.9999999988F, 0.9999999991F, 0.9999999993F, - 0.9999999995F, 0.9999999997F, 0.9999999998F, 0.9999999999F, - 0.9999999999F, 1.0000000000F, 1.0000000000F, 1.0000000000F, - 1.0000000000F, 1.0000000000F, 1.0000000000F, 1.0000000000F, -}; - -static const float *const vwin[8] = { - vwin64, - vwin128, - vwin256, - vwin512, - vwin1024, - vwin2048, - vwin4096, - vwin8192, -}; - -const float *_vorbis_window_get(int n){ - return vwin[n]; -} - -void _vorbis_apply_window(float *d,int *winno,long *blocksizes, - int lW,int W,int nW){ - lW=(W?lW:0); - nW=(W?nW:0); - - { - const float *windowLW=vwin[winno[lW]]; - const float *windowNW=vwin[winno[nW]]; - - long n=blocksizes[W]; - long ln=blocksizes[lW]; - long rn=blocksizes[nW]; - - long leftbegin=n/4-ln/4; - long leftend=leftbegin+ln/2; - - long rightbegin=n/2+n/4-rn/4; - long rightend=rightbegin+rn/2; - - int i,p; - - for(i=0;i -#include -#include -/*#include "lsp.h"*/ -/*#include "os.h"*/ -/*#include "misc.h"*/ -/*#include "lookup.h"*/ -/*#include "scales.h"*/ - -/* three possible LSP to f curve functions; the exact computation - (float), a lookup based float implementation, and an integer - implementation. The float lookup is likely the optimal choice on - any machine with an FPU. The integer implementation is *not* fixed - point (due to the need for a large dynamic range and thus a - separately tracked exponent) and thus much more complex than the - relatively simple float implementations. It's mostly for future - work on a fully fixed point implementation for processors like the - ARM family. */ - -/* define either of these (preferably FLOAT_LOOKUP) to have faster - but less precise implementation. */ -#undef FLOAT_LOOKUP -#undef INT_LOOKUP - -#ifdef FLOAT_LOOKUP -/*#include "lookup.c"*/ /* catch this in the build system; we #include for - compilers (like gcc) that can't inline across - modules */ - -/* side effect: changes *lsp to cosines of lsp */ -void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m, - float amp,float ampoffset){ - int i; - float wdel=M_PI/ln; - vorbis_fpu_control fpu; - - vorbis_fpu_setround(&fpu); - for(i=0;i>1; - - while(c--){ - q*=ftmp[0]-w; - p*=ftmp[1]-w; - ftmp+=2; - } - - if(m&1){ - /* odd order filter; slightly assymetric */ - /* the last coefficient */ - q*=ftmp[0]-w; - q*=q; - p*=p*(1.f-w*w); - }else{ - /* even order filter; still symmetric */ - q*=q*(1.f+w); - p*=p*(1.f-w); - } - - q=frexp(p+q,&qexp); - q=vorbis_fromdBlook(amp* - vorbis_invsqlook(q)* - vorbis_invsq2explook(qexp+m)- - ampoffset); - - do{ - curve[i++]*=q; - }while(map[i]==k); - } - vorbis_fpu_restore(fpu); -} - -#else - -#ifdef INT_LOOKUP -/*#include "lookup.c"*/ /* catch this in the build system; we #include for - compilers (like gcc) that can't inline across - modules */ - -static const int MLOOP_1[64]={ - 0,10,11,11, 12,12,12,12, 13,13,13,13, 13,13,13,13, - 14,14,14,14, 14,14,14,14, 14,14,14,14, 14,14,14,14, - 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, - 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, -}; - -static const int MLOOP_2[64]={ - 0,4,5,5, 6,6,6,6, 7,7,7,7, 7,7,7,7, - 8,8,8,8, 8,8,8,8, 8,8,8,8, 8,8,8,8, - 9,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9, - 9,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9, -}; - -static const int MLOOP_3[8]={0,1,2,2,3,3,3,3}; - - -/* side effect: changes *lsp to cosines of lsp */ -void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m, - float amp,float ampoffset){ - - /* 0 <= m < 256 */ - - /* set up for using all int later */ - int i; - int ampoffseti=rint(ampoffset*4096.f); - int ampi=rint(amp*16.f); - long *ilsp=alloca(m*sizeof(*ilsp)); - for(i=0;i>25])) - if(!(shift=MLOOP_2[(pi|qi)>>19])) - shift=MLOOP_3[(pi|qi)>>16]; - qi=(qi>>shift)*labs(ilsp[j-1]-wi); - pi=(pi>>shift)*labs(ilsp[j]-wi); - qexp+=shift; - } - if(!(shift=MLOOP_1[(pi|qi)>>25])) - if(!(shift=MLOOP_2[(pi|qi)>>19])) - shift=MLOOP_3[(pi|qi)>>16]; - - /* pi,qi normalized collectively, both tracked using qexp */ - - if(m&1){ - /* odd order filter; slightly assymetric */ - /* the last coefficient */ - qi=(qi>>shift)*labs(ilsp[j-1]-wi); - pi=(pi>>shift)<<14; - qexp+=shift; - - if(!(shift=MLOOP_1[(pi|qi)>>25])) - if(!(shift=MLOOP_2[(pi|qi)>>19])) - shift=MLOOP_3[(pi|qi)>>16]; - - pi>>=shift; - qi>>=shift; - qexp+=shift-14*((m+1)>>1); - - pi=((pi*pi)>>16); - qi=((qi*qi)>>16); - qexp=qexp*2+m; - - pi*=(1<<14)-((wi*wi)>>14); - qi+=pi>>14; - - }else{ - /* even order filter; still symmetric */ - - /* p*=p(1-w), q*=q(1+w), let normalization drift because it isn't - worth tracking step by step */ - - pi>>=shift; - qi>>=shift; - qexp+=shift-7*m; - - pi=((pi*pi)>>16); - qi=((qi*qi)>>16); - qexp=qexp*2+m; - - pi*=(1<<14)-wi; - qi*=(1<<14)+wi; - qi=(qi+pi)>>14; - - } - - - /* we've let the normalization drift because it wasn't important; - however, for the lookup, things must be normalized again. We - need at most one right shift or a number of left shifts */ - - if(qi&0xffff0000){ /* checks for 1.xxxxxxxxxxxxxxxx */ - qi>>=1; qexp++; - }else - while(qi && !(qi&0x8000)){ /* checks for 0.0xxxxxxxxxxxxxxx or less*/ - qi<<=1; qexp--; - } - - amp=vorbis_fromdBlook_i(ampi* /* n.4 */ - vorbis_invsqlook_i(qi,qexp)- - /* m.8, m+n<=8 */ - ampoffseti); /* 8.12[0] */ - - curve[i]*=amp; - while(map[++i]==k)curve[i]*=amp; - } -} - -#else - -/* old, nonoptimized but simple version for any poor sap who needs to - figure out what the hell this code does, or wants the other - fraction of a dB precision */ - -/* side effect: changes *lsp to cosines of lsp */ -void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m, - float amp,float ampoffset){ - int i; - float wdel=M_PI/ln; - for(i=0;i= i; j--) { - g[j-2] -= g[j]; - g[j] += g[j]; - } - } -} - -static int comp(const void *a,const void *b){ - return (*(float *)a<*(float *)b)-(*(float *)a>*(float *)b); -} - -/* Newton-Raphson-Maehly actually functioned as a decent root finder, - but there are root sets for which it gets into limit cycles - (exacerbated by zero suppression) and fails. We can't afford to - fail, even if the failure is 1 in 100,000,000, so we now use - Laguerre and later polish with Newton-Raphson (which can then - afford to fail) */ - -#define EPSILON 10e-7 -static int Laguerre_With_Deflation(float *a,int ord,float *r){ - int i,m; - double *defl=(double *)alloca(sizeof(*defl)*(ord+1)); - for(i=0;i<=ord;i++)defl[i]=a[i]; - - for(m=ord;m>0;m--){ - double new_d=0.f,delta; - - /* iterate a root */ - while(1){ - double p=defl[m],pp=0.f,ppp=0.f,denom; - - /* eval the polynomial and its first two derivatives */ - for(i=m;i>0;i--){ - ppp = new_d*ppp + pp; - pp = new_d*pp + p; - p = new_d*p + defl[i-1]; - } - - /* Laguerre's method */ - denom=(m-1) * ((m-1)*pp*pp - m*p*ppp); - if(denom<0) - return(-1); /* complex root! The LPC generator handed us a bad filter */ - - if(pp>0){ - denom = pp + sqrt(denom); - if(denom-(EPSILON))denom=-(EPSILON); - } - - delta = m*p/denom; - new_d -= delta; - - if(delta<0.f)delta*=-1; - - if(fabs(delta/new_d)<10e-12)break; - } - - r[m-1]=new_d; - - /* forward deflation */ - - for(i=m;i>0;i--) - defl[i-1]+=new_d*defl[i]; - defl++; - - } - return(0); -} - - -/* for spit-and-polish only */ -static int Newton_Raphson(float *a,int ord,float *r){ - int i, k, count=0; - double error=1.f; - double *root=(double *)alloca(ord*sizeof(*root)); - - for(i=0; i1e-20){ - error=0; - - for(i=0; i= 0; k--) { - - pp= pp* rooti + p; - p = p * rooti + a[k]; - } - - delta = p/pp; - root[i] -= delta; - error+= delta*delta; - } - - if(count>40)return(-1); - - count++; - } - - /* Replaced the original bubble sort with a real sort. With your - help, we can eliminate the bubble sort in our lifetime. --Monty */ - - for(i=0; i>1; - int g1_order,g2_order; - float *g1=(float *)alloca(sizeof(*g1)*(order2+1)); - float *g2=(float *)alloca(sizeof(*g2)*(order2+1)); - float *g1r=(float *)alloca(sizeof(*g1r)*(order2+1)); - float *g2r=(float *)alloca(sizeof(*g2r)*(order2+1)); - int i; - - /* even and odd are slightly different base cases */ - g1_order=(m+1)>>1; - g2_order=(m) >>1; - - /* Compute the lengths of the x polynomials. */ - /* Compute the first half of K & R F1 & F2 polynomials. */ - /* Compute half of the symmetric and antisymmetric polynomials. */ - /* Remove the roots at +1 and -1. */ - - g1[g1_order] = 1.f; - for(i=1;i<=g1_order;i++) g1[g1_order-i] = lpc[i-1]+lpc[m-i]; - g2[g2_order] = 1.f; - for(i=1;i<=g2_order;i++) g2[g2_order-i] = lpc[i-1]-lpc[m-i]; - - if(g1_order>g2_order){ - for(i=2; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+2]; - }else{ - for(i=1; i<=g1_order;i++) g1[g1_order-i] -= g1[g1_order-i+1]; - for(i=1; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+1]; - } - - /* Convert into polynomials in cos(alpha) */ - cheby(g1,g1_order); - cheby(g2,g2_order); - - /* Find the roots of the 2 even polynomials.*/ - if(Laguerre_With_Deflation(g1,g1_order,g1r) || - Laguerre_With_Deflation(g2,g2_order,g2r)) - return(-1); - - Newton_Raphson(g1,g1_order,g1r); /* if it fails, it leaves g1r alone */ - Newton_Raphson(g2,g2_order,g2r); /* if it fails, it leaves g2r alone */ - - qsort(g1r,g1_order,sizeof(*g1r),comp); - qsort(g2r,g2_order,sizeof(*g2r),comp); - - for(i=0;i -#include -#include -/*#include "os.h"*/ -/*#include "smallft.h"*/ -/*#include "lpc.h"*/ -/*#include "scales.h"*/ -/*#include "misc.h"*/ - -/* Autocorrelation LPC coeff generation algorithm invented by - N. Levinson in 1947, modified by J. Durbin in 1959. */ - -/* Input : n elements of time doamin data - Output: m lpc coefficients, excitation energy */ - -float vorbis_lpc_from_data(float *data,float *lpci,int n,int m){ - double *aut=(double *)alloca(sizeof(*aut)*(m+1)); - double *lpc=(double *)alloca(sizeof(*lpc)*(m)); - double error; - double epsilon; - int i,j; - - /* autocorrelation, p+1 lag coefficients */ - j=m+1; - while(j--){ - double d=0; /* double needed for accumulator depth */ - for(i=j;i -#include -#include -/*#include */ -/*#include "vorbis/codec.h"*/ -/*#include "codec_internal.h"*/ -/*#include "registry.h"*/ -/*#include "scales.h"*/ -/*#include "os.h"*/ -/*#include "misc.h"*/ - -/* decides between modes, dispatches to the appropriate mapping. */ -int vorbis_analysis(vorbis_block *vb, ogg_packet *op){ - int ret,i; - vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal; - - vb->glue_bits=0; - vb->time_bits=0; - vb->floor_bits=0; - vb->res_bits=0; - - /* first things first. Make sure encode is ready */ - for(i=0;ipacketblob[i]); - - /* we only have one mapping type (0), and we let the mapping code - itself figure out what soft mode to use. This allows easier - bitrate management */ - - if((ret=_mapping_P[0]->forward(vb))) - return(ret); - - if(op){ - if(vorbis_bitrate_managed(vb)) - /* The app is using a bitmanaged mode... but not using the - bitrate management interface. */ - return(OV_EINVAL); - - op->packet=oggpack_get_buffer(&vb->opb); - op->bytes=oggpack_bytes(&vb->opb); - op->b_o_s=0; - op->e_o_s=vb->eofflag; - op->granulepos=vb->granulepos; - op->packetno=vb->sequence; /* for sake of completeness */ - } - return(0); -} - -#ifdef ANALYSIS -int analysis_noisy=1; - -/* there was no great place to put this.... */ -void _analysis_output_always(char *base,int i,float *v,int n,int bark,int dB,ogg_int64_t off){ - int j; - FILE *of; - char buffer[80]; - - sprintf(buffer,"%s_%d.m",base,i); - of=fopen(buffer,"w"); - - if(!of)perror("failed to open data dump file"); - - for(j=0;j -/*#include */ -/*#include "vorbis/codec.h"*/ -/*#include "codec_internal.h"*/ -/*#include "registry.h"*/ -/*#include "misc.h"*/ -/*#include "os.h"*/ - -int vorbis_synthesis(vorbis_block *vb,ogg_packet *op){ - vorbis_dsp_state *vd= vb ? vb->vd : 0; - private_state *b= (private_state *)(vd ? vd->backend_state : 0); - vorbis_info *vi= vd ? vd->vi : 0; - codec_setup_info *ci= (codec_setup_info *)(vi ? vi->codec_setup : 0); - oggpack_buffer *opb=vb ? &vb->opb : 0; - int type,mode,i; - - if (!vd || !b || !vi || !ci || !opb) { - return OV_EBADPACKET; - } - - /* first things first. Make sure decode is ready */ - _vorbis_block_ripcord(vb); - oggpack_readinit(opb,op->packet,op->bytes); - - /* Check the packet type */ - if(oggpack_read(opb,1)!=0){ - /* Oops. This is not an audio data packet */ - return(OV_ENOTAUDIO); - } - - /* read our mode and pre/post windowsize */ - mode=oggpack_read(opb,b->modebits); - if(mode==-1){ - return(OV_EBADPACKET); - } - - vb->mode=mode; - if(!ci->mode_param[mode]){ - return(OV_EBADPACKET); - } - - vb->W=ci->mode_param[mode]->blockflag; - if(vb->W){ - - /* this doesn;t get mapped through mode selection as it's used - only for window selection */ - vb->lW=oggpack_read(opb,1); - vb->nW=oggpack_read(opb,1); - if(vb->nW==-1){ - return(OV_EBADPACKET); - } - }else{ - vb->lW=0; - vb->nW=0; - } - - /* more setup */ - vb->granulepos=op->granulepos; - vb->sequence=op->packetno; - vb->eofflag=op->e_o_s; - - /* alloc pcm passback storage */ - vb->pcmend=ci->blocksizes[vb->W]; - vb->pcm=(float **)_vorbis_block_alloc(vb,sizeof(*vb->pcm)*vi->channels); - for(i=0;ichannels;i++) - vb->pcm[i]=(float *)_vorbis_block_alloc(vb,vb->pcmend*sizeof(*vb->pcm[i])); - - /* unpack_header enforces range checking */ - type=ci->map_type[ci->mode_param[mode]->mapping]; - - return(_mapping_P[type]->inverse(vb,ci->map_param[ci->mode_param[mode]-> - mapping])); -} - -/* used to track pcm position without actually performing decode. - Useful for sequential 'fast forward' */ -int vorbis_synthesis_trackonly(vorbis_block *vb,ogg_packet *op){ - vorbis_dsp_state *vd=vb->vd; - private_state *b=(private_state *)vd->backend_state; - vorbis_info *vi=vd->vi; - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - oggpack_buffer *opb=&vb->opb; - int mode; - - /* first things first. Make sure decode is ready */ - _vorbis_block_ripcord(vb); - oggpack_readinit(opb,op->packet,op->bytes); - - /* Check the packet type */ - if(oggpack_read(opb,1)!=0){ - /* Oops. This is not an audio data packet */ - return(OV_ENOTAUDIO); - } - - /* read our mode and pre/post windowsize */ - mode=oggpack_read(opb,b->modebits); - if(mode==-1)return(OV_EBADPACKET); - - vb->mode=mode; - if(!ci->mode_param[mode]){ - return(OV_EBADPACKET); - } - - vb->W=ci->mode_param[mode]->blockflag; - if(vb->W){ - vb->lW=oggpack_read(opb,1); - vb->nW=oggpack_read(opb,1); - if(vb->nW==-1) return(OV_EBADPACKET); - }else{ - vb->lW=0; - vb->nW=0; - } - - /* more setup */ - vb->granulepos=op->granulepos; - vb->sequence=op->packetno; - vb->eofflag=op->e_o_s; - - /* no pcm */ - vb->pcmend=0; - vb->pcm=NULL; - - return(0); -} - -long vorbis_packet_blocksize(vorbis_info *vi,ogg_packet *op){ - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - oggpack_buffer opb; - int mode; - - if(ci==NULL || ci->modes<=0){ - /* codec setup not properly intialized */ - return(OV_EFAULT); - } - - oggpack_readinit(&opb,op->packet,op->bytes); - - /* Check the packet type */ - if(oggpack_read(&opb,1)!=0){ - /* Oops. This is not an audio data packet */ - return(OV_ENOTAUDIO); - } - - /* read our mode and pre/post windowsize */ - mode=oggpack_read(&opb,ov_ilog(ci->modes-1)); - if(mode==-1 || !ci->mode_param[mode])return(OV_EBADPACKET); - return(ci->blocksizes[ci->mode_param[mode]->blockflag]); -} - -int vorbis_synthesis_halfrate(vorbis_info *vi,int flag){ - /* set / clear half-sample-rate mode */ - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - - /* right now, our MDCT can't handle < 64 sample windows. */ - if(ci->blocksizes[0]<=64 && flag)return -1; - ci->halfrate_flag=(flag?1:0); - return 0; -} - -int vorbis_synthesis_halfrate_p(vorbis_info *vi){ - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - return ci->halfrate_flag; -} -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: psychoacoustics not including preecho - - ********************************************************************/ - -#include -#include -#include -/*#include "vorbis/codec.h"*/ -/*#include "codec_internal.h"*/ - -/*#include "masking.h"*/ -/*#include "psy.h"*/ -/*#include "os.h"*/ -/*#include "lpc.h"*/ -/*#include "smallft.h"*/ -/*#include "scales.h"*/ -/*#include "misc.h"*/ - -#define NEGINF -9999.f -static const double stereo_threshholds[]={0.0, .5, 1.0, 1.5, 2.5, 4.5, 8.5, 16.5, 9e10}; -static const double stereo_threshholds_limited[]={0.0, .5, 1.0, 1.5, 2.0, 2.5, 4.5, 8.5, 9e10}; - -vorbis_look_psy_global *_vp_global_look(vorbis_info *vi){ - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - vorbis_info_psy_global *gi=&ci->psy_g_param; - vorbis_look_psy_global *look=(vorbis_look_psy_global *)_ogg_calloc(1,sizeof(*look)); - - look->channels=vi->channels; - - look->ampmax=-9999.; - look->gi=gi; - return(look); -} - -void _vp_global_free(vorbis_look_psy_global *look){ - if(look){ - memset(look,0,sizeof(*look)); - _ogg_free(look); - } -} - -void _vi_gpsy_free(vorbis_info_psy_global *i){ - if(i){ - memset(i,0,sizeof(*i)); - _ogg_free(i); - } -} - -void _vi_psy_free(vorbis_info_psy *i){ - if(i){ - memset(i,0,sizeof(*i)); - _ogg_free(i); - } -} - -static void min_curve(float *c, - float *c2){ - int i; - for(i=0;ic[i])c[i]=c2[i]; -} - -static void attenuate_curve(float *c,float att){ - int i; - for(i=0;iATH[j+k+ath_offset])min=ATH[j+k+ath_offset]; - }else{ - if(min>ATH[MAX_ATH-1])min=ATH[MAX_ATH-1]; - } - ath[j]=min; - } - - /* copy curves into working space, replicate the 50dB curve to 30 - and 40, replicate the 100dB curve to 110 */ - for(j=0;j<6;j++) - memcpy(workc[i][j+2],tonemasks[i][j],EHMER_MAX*sizeof(*tonemasks[i][j])); - memcpy(workc[i][0],tonemasks[i][0],EHMER_MAX*sizeof(*tonemasks[i][0])); - memcpy(workc[i][1],tonemasks[i][0],EHMER_MAX*sizeof(*tonemasks[i][0])); - - /* apply centered curve boost/decay */ - for(j=0;j0)adj=0.; - if(adj>0. && center_boost<0)adj=0.; - workc[i][j][k]+=adj; - } - } - - /* normalize curves so the driving amplitude is 0dB */ - /* make temp curves with the ATH overlayed */ - for(j=0;j an eighth of an octave and that the eighth - octave values may also be composited. */ - - /* which octave curves will we be compositing? */ - bin=floor(fromOC(i*.5)/binHz); - lo_curve= ceil(toOC(bin*binHz+1)*2); - hi_curve= floor(toOC((bin+1)*binHz)*2); - if(lo_curve>i)lo_curve=i; - if(lo_curve<0)lo_curve=0; - if(hi_curve>=P_BANDS)hi_curve=P_BANDS-1; - - for(m=0;mn)lo_bin=n; - if(lo_binn)hi_bin=n; - - for(;lworkc[k][m][j]) - brute_buffer[l]=workc[k][m][j]; - } - - for(;lworkc[k][m][EHMER_MAX-1]) - brute_buffer[l]=workc[k][m][EHMER_MAX-1]; - - } - - /* be equally paranoid about being valid up to next half ocatve */ - if(i+1n)lo_bin=n; - if(lo_binn)hi_bin=n; - - for(;lworkc[k][m][j]) - brute_buffer[l]=workc[k][m][j]; - } - - for(;lworkc[k][m][EHMER_MAX-1]) - brute_buffer[l]=workc[k][m][EHMER_MAX-1]; - - } - - - for(j=0;j=n){ - ret[i][m][j+2]=-999.; - }else{ - ret[i][m][j+2]=brute_buffer[bin]; - } - } - } - - /* add fenceposts */ - for(j=0;j-200.f)break; - ret[i][m][0]=j; - - for(j=EHMER_MAX-1;j>EHMER_OFFSET+1;j--) - if(ret[i][m][j+2]>-200.f) - break; - ret[i][m][1]=j; - - } - } - - return(ret); -} - -void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi, - vorbis_info_psy_global *gi,int n,long rate){ - long i,j,lo=-99,hi=1; - long maxoc; - memset(p,0,sizeof(*p)); - - p->eighth_octave_lines=gi->eighth_octave_lines; - p->shiftoc=rint(log(gi->eighth_octave_lines*8.f)/log(2.f))-1; - - p->firstoc=toOC(.25f*rate*.5/n)*(1<<(p->shiftoc+1))-gi->eighth_octave_lines; - maxoc=toOC((n+.25f)*rate*.5/n)*(1<<(p->shiftoc+1))+.5f; - p->total_octave_lines=maxoc-p->firstoc+1; - p->ath=(float *)_ogg_malloc(n*sizeof(*p->ath)); - - p->octave=(long int *)_ogg_malloc(n*sizeof(*p->octave)); - p->bark=(long int *)_ogg_malloc(n*sizeof(*p->bark)); - p->vi=vi; - p->n=n; - p->rate=rate; - - /* AoTuV HF weighting */ - p->m_val = 1.; - if(rate < 26000) p->m_val = 0; - else if(rate < 38000) p->m_val = .94; /* 32kHz */ - else if(rate > 46000) p->m_val = 1.275; /* 48kHz */ - - /* set up the lookups for a given blocksize and sample rate */ - - for(i=0,j=0;iath[j]=base+100.; - base+=delta; - } - } - } - - for(;jath[j]=p->ath[j-1]; - } - - for(i=0;inoisewindowlominnoisewindowlo);lo++); - - for(;hi<=n && (hinoisewindowhimin || - toBARK(rate/(2*n)*hi)<(bark+vi->noisewindowhi));hi++); - - p->bark[i]=((lo-1)<<16)+(hi-1); - - } - - for(i=0;ioctave[i]=toOC((i+.25f)*.5*rate/n)*(1<<(p->shiftoc+1))+.5f; - - p->tonecurves=setup_tone_curves(vi->toneatt,rate*.5/n,n, - vi->tone_centerboost,vi->tone_decay); - - /* set up rolling noise median */ - p->noiseoffset=(float **)_ogg_malloc(P_NOISECURVES*sizeof(*p->noiseoffset)); - for(i=0;inoiseoffset[i]=(float *)_ogg_malloc(n*sizeof(**p->noiseoffset)); - - for(i=0;i=P_BANDS-1)halfoc=P_BANDS-1; - inthalfoc=(int)halfoc; - del=halfoc-inthalfoc; - - for(j=0;jnoiseoffset[j][i]= - p->vi->noiseoff[j][inthalfoc]*(1.-del) + - p->vi->noiseoff[j][inthalfoc+1]*del; - - } -#if 0 - { - static int ls=0; - _analysis_output_always("noiseoff0",ls,p->noiseoffset[0],n,1,0,0); - _analysis_output_always("noiseoff1",ls,p->noiseoffset[1],n,1,0,0); - _analysis_output_always("noiseoff2",ls++,p->noiseoffset[2],n,1,0,0); - } -#endif -} - -void _vp_psy_clear(vorbis_look_psy *p){ - int i,j; - if(p){ - if(p->ath)_ogg_free(p->ath); - if(p->octave)_ogg_free(p->octave); - if(p->bark)_ogg_free(p->bark); - if(p->tonecurves){ - for(i=0;itonecurves[i][j]); - } - _ogg_free(p->tonecurves[i]); - } - _ogg_free(p->tonecurves); - } - if(p->noiseoffset){ - for(i=0;inoiseoffset[i]); - } - _ogg_free(p->noiseoffset); - } - memset(p,0,sizeof(*p)); - } -} - -/* octave/(8*eighth_octave_lines) x scale and dB y scale */ -static void seed_curve(float *seed, - const float **curves, - float amp, - int oc, int n, - int linesper,float dBoffset){ - int i,post1; - int seedptr; - const float *posts,*curve; - - int choice=(int)((amp+dBoffset-P_LEVEL_0)*.1f); - choice=max(choice,0); - choice=min(choice,P_LEVELS-1); - posts=curves[choice]; - curve=posts+2; - post1=(int)posts[1]; - seedptr=oc+(posts[0]-EHMER_OFFSET)*linesper-(linesper>>1); - - for(i=posts[0];i0){ - float lin=amp+curve[i]; - if(seed[seedptr]=n)break; - } -} - -static void seed_loop(vorbis_look_psy *p, - const float ***curves, - const float *f, - const float *flr, - float *seed, - float specmax){ - vorbis_info_psy *vi=p->vi; - long n=p->n,i; - float dBoffset=vi->max_curve_dB-specmax; - - /* prime the working vector with peak values */ - - for(i=0;ioctave[i]; - while(i+1octave[i+1]==oc){ - i++; - if(f[i]>max)max=f[i]; - } - - if(max+6.f>flr[i]){ - oc=oc>>p->shiftoc; - - if(oc>=P_BANDS)oc=P_BANDS-1; - if(oc<0)oc=0; - - seed_curve(seed, - curves[oc], - max, - p->octave[i]-p->firstoc, - p->total_octave_lines, - p->eighth_octave_lines, - dBoffset); - } - } -} - -static void seed_chase(float *seeds, int linesper, long n){ - long *posstack=(long int *)alloca(n*sizeof(*posstack)); - float *ampstack=(float *)alloca(n*sizeof(*ampstack)); - long stack=0; - long pos=0; - long i; - - for(i=0;i1 && ampstack[stack-1]<=ampstack[stack-2] && - iampstack[i]){ - endpos=posstack[i+1]; - }else{ - endpos=posstack[i]+linesper+1; /* +1 is important, else bin 0 is - discarded in short frames */ - } - if(endpos>n)endpos=n; - for(;pos -static void max_seeds(vorbis_look_psy *p, - float *seed, - float *flr){ - long n=p->total_octave_lines; - int linesper=p->eighth_octave_lines; - long linpos=0; - long pos; - - seed_chase(seed,linesper,n); /* for masking */ - - pos=p->octave[0]-p->firstoc-(linesper>>1); - - while(linpos+1n){ - float minV=seed[pos]; - long end=((p->octave[linpos]+p->octave[linpos+1])>>1)-p->firstoc; - if(minV>p->vi->tone_abs_limit)minV=p->vi->tone_abs_limit; - while(pos+1<=end){ - pos++; - if((seed[pos]>NEGINF && seed[pos]firstoc; - for(;linposn && p->octave[linpos]<=end;linpos++) - if(flr[linpos]total_octave_lines-1]; - for(;linposn;linpos++) - if(flr[linpos]> 16; - hi = b[i] & 0xffff; - if( lo>=0 || -lo>=n ) break; - if( hi>=n ) break; - - tN = N[hi] + N[-lo]; - tX = X[hi] - X[-lo]; - tXX = XX[hi] + XX[-lo]; - tY = Y[hi] + Y[-lo]; - tXY = XY[hi] - XY[-lo]; - - A = tY * tXX - tX * tXY; - B = tN * tXY - tX * tY; - D = tN * tXX - tX * tX; - R = (A + x * B) / D; - if (R < 0.f) R = 0.f; - - noise[i] = R - offset; - } - - for ( ; i < n; i++, x += 1.f) { - - lo = b[i] >> 16; - hi = b[i] & 0xffff; - if( lo<0 || lo>=n ) break; - if( hi>=n ) break; - - tN = N[hi] - N[lo]; - tX = X[hi] - X[lo]; - tXX = XX[hi] - XX[lo]; - tY = Y[hi] - Y[lo]; - tXY = XY[hi] - XY[lo]; - - A = tY * tXX - tX * tXY; - B = tN * tXY - tX * tY; - D = tN * tXX - tX * tX; - R = (A + x * B) / D; - if (R < 0.f) R = 0.f; - - noise[i] = R - offset; - } - - for ( ; i < n; i++, x += 1.f) { - - R = (A + x * B) / D; - if (R < 0.f) R = 0.f; - - noise[i] = R - offset; - } - - if (fixed <= 0) return; - - for (i = 0, x = 0.f; i < n; i++, x += 1.f) { - hi = i + fixed / 2; - lo = hi - fixed; - if ( hi>=n ) break; - if ( lo>=0 ) break; - - tN = N[hi] + N[-lo]; - tX = X[hi] - X[-lo]; - tXX = XX[hi] + XX[-lo]; - tY = Y[hi] + Y[-lo]; - tXY = XY[hi] - XY[-lo]; - - - A = tY * tXX - tX * tXY; - B = tN * tXY - tX * tY; - D = tN * tXX - tX * tX; - R = (A + x * B) / D; - - if (R - offset < noise[i]) noise[i] = R - offset; - } - for ( ; i < n; i++, x += 1.f) { - - hi = i + fixed / 2; - lo = hi - fixed; - if ( hi>=n ) break; - if ( lo<0 ) break; - - tN = N[hi] - N[lo]; - tX = X[hi] - X[lo]; - tXX = XX[hi] - XX[lo]; - tY = Y[hi] - Y[lo]; - tXY = XY[hi] - XY[lo]; - - A = tY * tXX - tX * tXY; - B = tN * tXY - tX * tY; - D = tN * tXX - tX * tX; - R = (A + x * B) / D; - - if (R - offset < noise[i]) noise[i] = R - offset; - } - for ( ; i < n; i++, x += 1.f) { - R = (A + x * B) / D; - if (R - offset < noise[i]) noise[i] = R - offset; - } -} - -void _vp_noisemask(vorbis_look_psy *p, - float *logmdct, - float *logmask){ - - int i,n=p->n; - float *work=(float *)alloca(n*sizeof(*work)); - - bark_noise_hybridmp(n,p->bark,logmdct,logmask, - 140.,-1); - - for(i=0;ibark,work,logmask,0., - p->vi->noisewindowfixed); - - for(i=0;i=NOISE_COMPAND_LEVELS)dB=NOISE_COMPAND_LEVELS-1; - if(dB<0)dB=0; - logmask[i]= work[i]+p->vi->noisecompand[dB]; - } - -} - -void _vp_tonemask(vorbis_look_psy *p, - float *logfft, - float *logmask, - float global_specmax, - float local_specmax){ - - int i,n=p->n; - - float *seed=(float *)alloca(sizeof(*seed)*p->total_octave_lines); - float att=local_specmax+p->vi->ath_adjatt; - for(i=0;itotal_octave_lines;i++)seed[i]=NEGINF; - - /* set the ATH (floating below localmax, not global max by a - specified att) */ - if(attvi->ath_maxatt)att=p->vi->ath_maxatt; - - for(i=0;iath[i]+att; - - /* tone masking */ - seed_loop(p,(const float ***)p->tonecurves,logfft,logmask,seed,global_specmax); - max_seeds(p,seed,logmask); - -} - -void _vp_offset_and_mix(vorbis_look_psy *p, - float *noise, - float *tone, - int offset_select, - float *logmask, - float *mdct, - float *logmdct){ - int i,n=p->n; - float de, coeffi, cx;/* AoTuV */ - float toneatt=p->vi->tone_masteratt[offset_select]; - - cx = p->m_val; - - for(i=0;inoiseoffset[offset_select][i]; - if(val>p->vi->noisemaxsupp)val=p->vi->noisemaxsupp; - logmask[i]=max(val,tone[i]+toneatt); - - - /* AoTuV */ - /** @ M1 ** - The following codes improve a noise problem. - A fundamental idea uses the value of masking and carries out - the relative compensation of the MDCT. - However, this code is not perfect and all noise problems cannot be solved. - by Aoyumi @ 2004/04/18 - */ - - if(offset_select == 1) { - coeffi = -17.2; /* coeffi is a -17.2dB threshold */ - val = val - logmdct[i]; /* val == mdct line value relative to floor in dB */ - - if(val > coeffi){ - /* mdct value is > -17.2 dB below floor */ - - de = 1.0-((val-coeffi)*0.005*cx); - /* pro-rated attenuation: - -0.00 dB boost if mdct value is -17.2dB (relative to floor) - -0.77 dB boost if mdct value is 0dB (relative to floor) - -1.64 dB boost if mdct value is +17.2dB (relative to floor) - etc... */ - - if(de < 0) de = 0.0001; - }else - /* mdct value is <= -17.2 dB below floor */ - - de = 1.0-((val-coeffi)*0.0003*cx); - /* pro-rated attenuation: - +0.00 dB atten if mdct value is -17.2dB (relative to floor) - +0.45 dB atten if mdct value is -34.4dB (relative to floor) - etc... */ - - mdct[i] *= de; - - } - } -} - -float _vp_ampmax_decay(float amp,vorbis_dsp_state *vd){ - vorbis_info *vi=vd->vi; - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - vorbis_info_psy_global *gi=&ci->psy_g_param; - - int n=ci->blocksizes[vd->W]/2; - float secs=(float)n/vi->rate; - - amp+=secs*gi->ampmax_att_per_sec; - if(amp<-9999)amp=-9999; - return(amp); -} - -static float _FLOOR1_fromdB_LOOKUP[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.128753e-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.F, -}; - -/* this is for per-channel noise normalization */ -static int apsort(const void *a, const void *b){ - float f1=**(float**)a; - float f2=**(float**)b; - return (f1f2); -} - -static void flag_lossless(int limit, float prepoint, float postpoint, float *mdct, - float *floor, int *flag, int i, int jn){ - int j; - for(j=0;j=limit-i ? postpoint : prepoint; - float r = fabs(mdct[j])/floor[j]; - if(rvi; - float **sort = (float **)alloca(n*sizeof(*sort)); - int j,count=0; - int start = (vi->normal_p ? vi->normal_start-i : n); - if(start>n)start=n; - - /* force classic behavior where only energy in the current band is considered */ - acc=0.f; - - /* still responsible for populating *out where noise norm not in - effect. There's no need to [re]populate *q in these areas */ - for(j=0;j pointlimit */ - if(ve<.25f && (!flags || j>=limit-i)){ - acc += ve; - sort[count++]=q+j; /* q is fabs(r) for unflagged element */ - }else{ - /* For now: no acc adjustment for nonzero quantization. populate *out and q as this value is final. */ - if(r[j]<0) - out[j] = -rint(sqrt(ve)); - else - out[j] = rint(sqrt(ve)); - q[j] = out[j]*out[j]*f[j]; - } - }/* else{ - again, no energy adjustment for error in nonzero quant-- for now - }*/ - } - - if(count){ - /* noise norm to do */ - qsort(sort,count,sizeof(*sort),apsort); - for(j=0;j=vi->normal_thresh){ - out[k]=unitnorm(r[k]); - acc-=1.f; - q[k]=f[k]; - }else{ - out[k]=0; - q[k]=0.f; - } - } - } - - return acc; -} - -/* Noise normalization, quantization and coupling are not wholly - seperable processes in depth>1 coupling. */ -void _vp_couple_quantize_normalize(int blobno, - vorbis_info_psy_global *g, - vorbis_look_psy *p, - vorbis_info_mapping0 *vi, - float **mdct, - int **iwork, - int *nonzero, - int sliding_lowpass, - int ch){ - - int i; - int n = p->n; - int partition=(p->vi->normal_p ? p->vi->normal_partition : 16); - int limit = g->coupling_pointlimit[p->vi->blockflag][blobno]; - float prepoint=stereo_threshholds[g->coupling_prepointamp[blobno]]; - float postpoint=stereo_threshholds[g->coupling_postpointamp[blobno]]; -#if 0 - float de=0.1*p->m_val; /* a blend of the AoTuV M2 and M3 code here and below */ -#endif - - /* mdct is our raw mdct output, floor not removed. */ - /* inout passes in the ifloor, passes back quantized result */ - - /* unquantized energy (negative indicates amplitude has negative sign) */ - float **raw = (float **)alloca(ch*sizeof(*raw)); - - /* dual pupose; quantized energy (if flag set), othersize fabs(raw) */ - float **quant = (float **)alloca(ch*sizeof(*quant)); - - /* floor energy */ - float **floor = (float **)alloca(ch*sizeof(*floor)); - - /* flags indicating raw/quantized status of elements in raw vector */ - int **flag = (int **)alloca(ch*sizeof(*flag)); - - /* non-zero flag working vector */ - int *nz = (int *)alloca(ch*sizeof(*nz)); - - /* energy surplus/defecit tracking */ - float *acc = (float *)alloca((ch+vi->coupling_steps)*sizeof(*acc)); - - /* The threshold of a stereo is changed with the size of n */ - if(n > 1000) - postpoint=stereo_threshholds_limited[g->coupling_postpointamp[blobno]]; - - raw[0] = (float *)alloca(ch*partition*sizeof(**raw)); - quant[0] = (float *)alloca(ch*partition*sizeof(**quant)); - floor[0] = (float *)alloca(ch*partition*sizeof(**floor)); - flag[0] = (int *)alloca(ch*partition*sizeof(**flag)); - - for(i=1;icoupling_steps;i++) - acc[i]=0.f; - - for(i=0;i n-i ? n-i : partition; - int step,track = 0; - - memcpy(nz,nonzero,sizeof(*nz)*ch); - - /* prefill */ - memset(flag[0],0,ch*partition*sizeof(**flag)); - for(k=0;kcoupling_steps;step++){ - int Mi = vi->coupling_mag[step]; - int Ai = vi->coupling_ang[step]; - int *iM = &iwork[Mi][i]; - int *iA = &iwork[Ai][i]; - float *reM = raw[Mi]; - float *reA = raw[Ai]; - float *qeM = quant[Mi]; - float *qeA = quant[Ai]; - float *floorM = floor[Mi]; - float *floorA = floor[Ai]; - int *fM = flag[Mi]; - int *fA = flag[Ai]; - - if(nz[Mi] || nz[Ai]){ - nz[Mi] = nz[Ai] = 1; - - for(j=0;jabs(B)){ - iA[j]=(A>0?A-B:B-A); - }else{ - iA[j]=(B>0?A-B:B-A); - iM[j]=B; - } - - /* collapse two equivalent tuples to one */ - if(iA[j]>=abs(iM[j])*2){ - iA[j]= -iA[j]; - iM[j]= -iM[j]; - } - - } - - }else{ - /* lossy (point) coupling */ - if(jcoupling_steps;i++){ - /* make sure coupling a zero and a nonzero channel results in two - nonzero channels. */ - if(nonzero[vi->coupling_mag[i]] || - nonzero[vi->coupling_ang[i]]){ - nonzero[vi->coupling_mag[i]]=1; - nonzero[vi->coupling_ang[i]]=1; - } - } -} -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: maintain the info structure, info <-> header packets - - ********************************************************************/ - -/* general handling of the header and the vorbis_info structure (and - substructures) */ - -#include -#include -/*#include */ -/*#include "vorbis/codec.h"*/ -/*#include "codec_internal.h"*/ -/*#include "codebook.h"*/ -/*#include "registry.h"*/ -/*#include "window.h"*/ -/*#include "psy.h"*/ -/*#include "misc.h"*/ -/*#include "os.h"*/ - -#define GENERAL_VENDOR_STRING "Xiph.Org libVorbis 1.3.7" -#define ENCODE_VENDOR_STRING "Xiph.Org libVorbis I 20200704 (Reducing Environment)" - -/* helpers */ -static void _v_writestring(oggpack_buffer *o,const char *s, int bytes){ - - while(bytes--){ - oggpack_write(o,*s++,8); - } -} - -static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){ - while(bytes--){ - *buf++=oggpack_read(o,8); - } -} - -static int _v_toupper(int c) { - return (c >= 'a' && c <= 'z') ? (c & ~('a' - 'A')) : c; -} - -void vorbis_comment_init(vorbis_comment *vc){ - memset(vc,0,sizeof(*vc)); -} - -void vorbis_comment_add(vorbis_comment *vc,const char *comment){ - vc->user_comments=(char **)_ogg_realloc(vc->user_comments, - (vc->comments+2)*sizeof(*vc->user_comments)); - vc->comment_lengths=(int *)_ogg_realloc(vc->comment_lengths, - (vc->comments+2)*sizeof(*vc->comment_lengths)); - vc->comment_lengths[vc->comments]=strlen(comment); - vc->user_comments[vc->comments]=(char *)_ogg_malloc(vc->comment_lengths[vc->comments]+1); - strcpy(vc->user_comments[vc->comments], comment); - vc->comments++; - vc->user_comments[vc->comments]=NULL; -} - -void vorbis_comment_add_tag(vorbis_comment *vc, const char *tag, const char *contents){ - /* Length for key and value +2 for = and \0 */ - char *comment=(char *)_ogg_malloc(strlen(tag)+strlen(contents)+2); - strcpy(comment, tag); - strcat(comment, "="); - strcat(comment, contents); - vorbis_comment_add(vc, comment); - _ogg_free(comment); -} - -/* This is more or less the same as strncasecmp - but that doesn't exist - * everywhere, and this is a fairly trivial function, so we include it */ -static int tagcompare(const char *s1, const char *s2, int n){ - int c=0; - while(c < n){ - if(_v_toupper(s1[c]) != _v_toupper(s2[c])) - return !0; - c++; - } - return 0; -} - -char *vorbis_comment_query(vorbis_comment *vc, const char *tag, int count){ - long i; - int found = 0; - int taglen = strlen(tag)+1; /* +1 for the = we append */ - char *fulltag = (char *)_ogg_malloc(taglen+1); - - strcpy(fulltag, tag); - strcat(fulltag, "="); - - for(i=0;icomments;i++){ - if(!tagcompare(vc->user_comments[i], fulltag, taglen)){ - if(count == found) { - /* We return a pointer to the data, not a copy */ - _ogg_free(fulltag); - return vc->user_comments[i] + taglen; - } else { - found++; - } - } - } - _ogg_free(fulltag); - return NULL; /* didn't find anything */ -} - -int vorbis_comment_query_count(vorbis_comment *vc, const char *tag){ - int i,count=0; - int taglen = strlen(tag)+1; /* +1 for the = we append */ - char *fulltag = (char *)_ogg_malloc(taglen+1); - strcpy(fulltag,tag); - strcat(fulltag, "="); - - for(i=0;icomments;i++){ - if(!tagcompare(vc->user_comments[i], fulltag, taglen)) - count++; - } - - _ogg_free(fulltag); - return count; -} - -void vorbis_comment_clear(vorbis_comment *vc){ - if(vc){ - long i; - if(vc->user_comments){ - for(i=0;icomments;i++) - if(vc->user_comments[i])_ogg_free(vc->user_comments[i]); - _ogg_free(vc->user_comments); - } - if(vc->comment_lengths)_ogg_free(vc->comment_lengths); - if(vc->vendor)_ogg_free(vc->vendor); - memset(vc,0,sizeof(*vc)); - } -} - -/* blocksize 0 is guaranteed to be short, 1 is guaranteed to be long. - They may be equal, but short will never ge greater than long */ -int vorbis_info_blocksize(vorbis_info *vi,int zo){ - codec_setup_info *ci = (codec_setup_info *)vi->codec_setup; - return ci ? ci->blocksizes[zo] : -1; -} - -/* used by synthesis, which has a full, alloced vi */ -void vorbis_info_init(vorbis_info *vi){ - memset(vi,0,sizeof(*vi)); - vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info)); -} - -void vorbis_info_clear(vorbis_info *vi){ - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - int i; - - if(ci){ - - for(i=0;imodes;i++) - if(ci->mode_param[i])_ogg_free(ci->mode_param[i]); - - for(i=0;imaps;i++) /* unpack does the range checking */ - if(ci->map_param[i]) /* this may be cleaning up an aborted - unpack, in which case the below type - cannot be trusted */ - _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]); - - for(i=0;ifloors;i++) /* unpack does the range checking */ - if(ci->floor_param[i]) /* this may be cleaning up an aborted - unpack, in which case the below type - cannot be trusted */ - _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]); - - for(i=0;iresidues;i++) /* unpack does the range checking */ - if(ci->residue_param[i]) /* this may be cleaning up an aborted - unpack, in which case the below type - cannot be trusted */ - _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]); - - for(i=0;ibooks;i++){ - if(ci->book_param[i]){ - /* knows if the book was not alloced */ - vorbis_staticbook_destroy(ci->book_param[i]); - } - if(ci->fullbooks) - vorbis_book_clear(ci->fullbooks+i); - } - if(ci->fullbooks) - _ogg_free(ci->fullbooks); - - for(i=0;ipsys;i++) - _vi_psy_free(ci->psy_param[i]); - - _ogg_free(ci); - } - - memset(vi,0,sizeof(*vi)); -} - -/* Header packing/unpacking ********************************************/ - -static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){ - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - int bs; - if(!ci)return(OV_EFAULT); - - vi->version=oggpack_read(opb,32); - if(vi->version!=0)return(OV_EVERSION); - - vi->channels=oggpack_read(opb,8); - vi->rate=oggpack_read(opb,32); - - vi->bitrate_upper=(ogg_int32_t)oggpack_read(opb,32); - vi->bitrate_nominal=(ogg_int32_t)oggpack_read(opb,32); - vi->bitrate_lower=(ogg_int32_t)oggpack_read(opb,32); - - bs = oggpack_read(opb,4); - if(bs<0)goto err_out; - ci->blocksizes[0]=1<blocksizes[1]=1<rate<1)goto err_out; - if(vi->channels<1)goto err_out; - if(ci->blocksizes[0]<64)goto err_out; - if(ci->blocksizes[1]blocksizes[0])goto err_out; - if(ci->blocksizes[1]>8192)goto err_out; - - if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */ - - return(0); - err_out: - vorbis_info_clear(vi); - return(OV_EBADHEADER); -} - -static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){ - int i; - int vendorlen=oggpack_read(opb,32); - if(vendorlen<0)goto err_out; - if(vendorlen>opb->storage-8)goto err_out; - vc->vendor=(char *)_ogg_calloc(vendorlen+1,1); - _v_readstring(opb,vc->vendor,vendorlen); - i=oggpack_read(opb,32); - if(i<0)goto err_out; - if(i>((opb->storage-oggpack_bytes(opb))>>2))goto err_out; - vc->comments=i; - vc->user_comments=(char **)_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments)); - vc->comment_lengths=(int *)_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths)); - - for(i=0;icomments;i++){ - int len=oggpack_read(opb,32); - if(len<0)goto err_out; - if(len>opb->storage-oggpack_bytes(opb))goto err_out; - vc->comment_lengths[i]=len; - vc->user_comments[i]=(char *)_ogg_calloc(len+1,1); - _v_readstring(opb,vc->user_comments[i],len); - } - if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */ - - return(0); - err_out: - vorbis_comment_clear(vc); - return(OV_EBADHEADER); -} - -/* all of the real encoding details are here. The modes, books, - everything */ -static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){ - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - int i; - - /* codebooks */ - ci->books=oggpack_read(opb,8)+1; - if(ci->books<=0)goto err_out; - for(i=0;ibooks;i++){ - ci->book_param[i]=vorbis_staticbook_unpack(opb); - if(!ci->book_param[i])goto err_out; - } - - /* time backend settings; hooks are unused */ - { - int times=oggpack_read(opb,6)+1; - if(times<=0)goto err_out; - for(i=0;i=VI_TIMEB)goto err_out; - } - } - - /* floor backend settings */ - ci->floors=oggpack_read(opb,6)+1; - if(ci->floors<=0)goto err_out; - for(i=0;ifloors;i++){ - ci->floor_type[i]=oggpack_read(opb,16); - if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out; - ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb); - if(!ci->floor_param[i])goto err_out; - } - - /* residue backend settings */ - ci->residues=oggpack_read(opb,6)+1; - if(ci->residues<=0)goto err_out; - for(i=0;iresidues;i++){ - ci->residue_type[i]=oggpack_read(opb,16); - if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out; - ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb); - if(!ci->residue_param[i])goto err_out; - } - - /* map backend settings */ - ci->maps=oggpack_read(opb,6)+1; - if(ci->maps<=0)goto err_out; - for(i=0;imaps;i++){ - ci->map_type[i]=oggpack_read(opb,16); - if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out; - ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb); - if(!ci->map_param[i])goto err_out; - } - - /* mode settings */ - ci->modes=oggpack_read(opb,6)+1; - if(ci->modes<=0)goto err_out; - for(i=0;imodes;i++){ - ci->mode_param[i]=(vorbis_info_mode *)_ogg_calloc(1,sizeof(*ci->mode_param[i])); - ci->mode_param[i]->blockflag=oggpack_read(opb,1); - ci->mode_param[i]->windowtype=oggpack_read(opb,16); - ci->mode_param[i]->transformtype=oggpack_read(opb,16); - ci->mode_param[i]->mapping=oggpack_read(opb,8); - - if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out; - if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out; - if(ci->mode_param[i]->mapping>=ci->maps)goto err_out; - if(ci->mode_param[i]->mapping<0)goto err_out; - } - - if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */ - - return(0); - err_out: - vorbis_info_clear(vi); - return(OV_EBADHEADER); -} - -/* Is this packet a vorbis ID header? */ -int vorbis_synthesis_idheader(ogg_packet *op){ - oggpack_buffer opb; - char buffer[6]; - - if(op){ - oggpack_readinit(&opb,op->packet,op->bytes); - - if(!op->b_o_s) - return(0); /* Not the initial packet */ - - if(oggpack_read(&opb,8) != 1) - return 0; /* not an ID header */ - - memset(buffer,0,6); - _v_readstring(&opb,buffer,6); - if(memcmp(buffer,"vorbis",6)) - return 0; /* not vorbis */ - - return 1; - } - - return 0; -} - -/* The Vorbis header is in three packets; the initial small packet in - the first page that identifies basic parameters, a second packet - with bitstream comments and a third packet that holds the - codebook. */ - -int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){ - oggpack_buffer opb; - - if(op){ - oggpack_readinit(&opb,op->packet,op->bytes); - - /* Which of the three types of header is this? */ - /* Also verify header-ness, vorbis */ - { - char buffer[6]; - int packtype=oggpack_read(&opb,8); - memset(buffer,0,6); - _v_readstring(&opb,buffer,6); - if(memcmp(buffer,"vorbis",6)){ - /* not a vorbis header */ - return(OV_ENOTVORBIS); - } - switch(packtype){ - case 0x01: /* least significant *bit* is read first */ - if(!op->b_o_s){ - /* Not the initial packet */ - return(OV_EBADHEADER); - } - if(vi->rate!=0){ - /* previously initialized info header */ - return(OV_EBADHEADER); - } - - return(_vorbis_unpack_info(vi,&opb)); - - case 0x03: /* least significant *bit* is read first */ - if(vi->rate==0){ - /* um... we didn't get the initial header */ - return(OV_EBADHEADER); - } - if(vc->vendor!=NULL){ - /* previously initialized comment header */ - return(OV_EBADHEADER); - } - - return(_vorbis_unpack_comment(vc,&opb)); - - case 0x05: /* least significant *bit* is read first */ - if(vi->rate==0 || vc->vendor==NULL){ - /* um... we didn;t get the initial header or comments yet */ - return(OV_EBADHEADER); - } - if(vi->codec_setup==NULL){ - /* improperly initialized vorbis_info */ - return(OV_EFAULT); - } - if(((codec_setup_info *)vi->codec_setup)->books>0){ - /* previously initialized setup header */ - return(OV_EBADHEADER); - } - - return(_vorbis_unpack_books(vi,&opb)); - - default: - /* Not a valid vorbis header type */ - return(OV_EBADHEADER); - break; - } - } - } - return(OV_EBADHEADER); -} - -/* pack side **********************************************************/ - -static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){ - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - if(!ci|| - ci->blocksizes[0]<64|| - ci->blocksizes[1]blocksizes[0]){ - return(OV_EFAULT); - } - - /* preamble */ - oggpack_write(opb,0x01,8); - _v_writestring(opb,"vorbis", 6); - - /* basic information about the stream */ - oggpack_write(opb,0x00,32); - oggpack_write(opb,vi->channels,8); - oggpack_write(opb,vi->rate,32); - - oggpack_write(opb,vi->bitrate_upper,32); - oggpack_write(opb,vi->bitrate_nominal,32); - oggpack_write(opb,vi->bitrate_lower,32); - - oggpack_write(opb,ov_ilog(ci->blocksizes[0]-1),4); - oggpack_write(opb,ov_ilog(ci->blocksizes[1]-1),4); - oggpack_write(opb,1,1); - - return(0); -} - -static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){ - int bytes = strlen(ENCODE_VENDOR_STRING); - - /* preamble */ - oggpack_write(opb,0x03,8); - _v_writestring(opb,"vorbis", 6); - - /* vendor */ - oggpack_write(opb,bytes,32); - _v_writestring(opb,ENCODE_VENDOR_STRING, bytes); - - /* comments */ - - oggpack_write(opb,vc->comments,32); - if(vc->comments){ - int i; - for(i=0;icomments;i++){ - if(vc->user_comments[i]){ - oggpack_write(opb,vc->comment_lengths[i],32); - _v_writestring(opb,vc->user_comments[i], vc->comment_lengths[i]); - }else{ - oggpack_write(opb,0,32); - } - } - } - oggpack_write(opb,1,1); - - return(0); -} - -static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){ - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - int i; - if(!ci)return(OV_EFAULT); - - oggpack_write(opb,0x05,8); - _v_writestring(opb,"vorbis", 6); - - /* books */ - oggpack_write(opb,ci->books-1,8); - for(i=0;ibooks;i++) - if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out; - - /* times; hook placeholders */ - oggpack_write(opb,0,6); - oggpack_write(opb,0,16); - - /* floors */ - oggpack_write(opb,ci->floors-1,6); - for(i=0;ifloors;i++){ - oggpack_write(opb,ci->floor_type[i],16); - if(_floor_P[ci->floor_type[i]]->pack) - _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb); - else - goto err_out; - } - - /* residues */ - oggpack_write(opb,ci->residues-1,6); - for(i=0;iresidues;i++){ - oggpack_write(opb,ci->residue_type[i],16); - _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb); - } - - /* maps */ - oggpack_write(opb,ci->maps-1,6); - for(i=0;imaps;i++){ - oggpack_write(opb,ci->map_type[i],16); - _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb); - } - - /* modes */ - oggpack_write(opb,ci->modes-1,6); - for(i=0;imodes;i++){ - oggpack_write(opb,ci->mode_param[i]->blockflag,1); - oggpack_write(opb,ci->mode_param[i]->windowtype,16); - oggpack_write(opb,ci->mode_param[i]->transformtype,16); - oggpack_write(opb,ci->mode_param[i]->mapping,8); - } - oggpack_write(opb,1,1); - - return(0); -err_out: - return(-1); -} - -int vorbis_commentheader_out(vorbis_comment *vc, - ogg_packet *op){ - - oggpack_buffer opb; - - oggpack_writeinit(&opb); - if(_vorbis_pack_comment(&opb,vc)){ - oggpack_writeclear(&opb); - return OV_EIMPL; - } - - op->packet = (unsigned char *)_ogg_malloc(oggpack_bytes(&opb)); - memcpy(op->packet, opb.buffer, oggpack_bytes(&opb)); - - op->bytes=oggpack_bytes(&opb); - op->b_o_s=0; - op->e_o_s=0; - op->granulepos=0; - op->packetno=1; - - oggpack_writeclear(&opb); - return 0; -} - -int vorbis_analysis_headerout(vorbis_dsp_state *v, - vorbis_comment *vc, - ogg_packet *op, - ogg_packet *op_comm, - ogg_packet *op_code){ - int ret=OV_EIMPL; - vorbis_info *vi=v->vi; - oggpack_buffer opb; - private_state *b=(private_state *)v->backend_state; - - if(!b||vi->channels<=0||vi->channels>256){ - b = NULL; - ret=OV_EFAULT; - goto err_out; - } - - /* first header packet **********************************************/ - - oggpack_writeinit(&opb); - if(_vorbis_pack_info(&opb,vi))goto err_out; - - /* build the packet */ - if(b->header)_ogg_free(b->header); - b->header=(unsigned char *)_ogg_malloc(oggpack_bytes(&opb)); - memcpy(b->header,opb.buffer,oggpack_bytes(&opb)); - op->packet=b->header; - op->bytes=oggpack_bytes(&opb); - op->b_o_s=1; - op->e_o_s=0; - op->granulepos=0; - op->packetno=0; - - /* second header packet (comments) **********************************/ - - oggpack_reset(&opb); - if(_vorbis_pack_comment(&opb,vc))goto err_out; - - if(b->header1)_ogg_free(b->header1); - b->header1=(unsigned char *)_ogg_malloc(oggpack_bytes(&opb)); - memcpy(b->header1,opb.buffer,oggpack_bytes(&opb)); - op_comm->packet=b->header1; - op_comm->bytes=oggpack_bytes(&opb); - op_comm->b_o_s=0; - op_comm->e_o_s=0; - op_comm->granulepos=0; - op_comm->packetno=1; - - /* third header packet (modes/codebooks) ****************************/ - - oggpack_reset(&opb); - if(_vorbis_pack_books(&opb,vi))goto err_out; - - if(b->header2)_ogg_free(b->header2); - b->header2=(unsigned char *)_ogg_malloc(oggpack_bytes(&opb)); - memcpy(b->header2,opb.buffer,oggpack_bytes(&opb)); - op_code->packet=b->header2; - op_code->bytes=oggpack_bytes(&opb); - op_code->b_o_s=0; - op_code->e_o_s=0; - op_code->granulepos=0; - op_code->packetno=2; - - oggpack_writeclear(&opb); - return(0); - err_out: - memset(op,0,sizeof(*op)); - memset(op_comm,0,sizeof(*op_comm)); - memset(op_code,0,sizeof(*op_code)); - - if(b){ - if(vi->channels>0)oggpack_writeclear(&opb); - if(b->header)_ogg_free(b->header); - if(b->header1)_ogg_free(b->header1); - if(b->header2)_ogg_free(b->header2); - b->header=NULL; - b->header1=NULL; - b->header2=NULL; - } - return(ret); -} - -double vorbis_granule_time(vorbis_dsp_state *v,ogg_int64_t granulepos){ - if(granulepos == -1) return -1; - - /* We're not guaranteed a 64 bit unsigned type everywhere, so we - have to put the unsigned granpo in a signed type. */ - if(granulepos>=0){ - return((double)granulepos/v->vi->rate); - }else{ - ogg_int64_t granuleoff=0xffffffff; - granuleoff<<=31; - granuleoff|=0x7ffffffff; - return(((double)granulepos+2+granuleoff+granuleoff)/v->vi->rate); - } -} - -const char *vorbis_version_string(void){ - return GENERAL_VENDOR_STRING; -} -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: floor backend 1 implementation - - ********************************************************************/ - -#include -#include -#include -/*#include */ -/*#include "vorbis/codec.h"*/ -/*#include "codec_internal.h"*/ -/*#include "registry.h"*/ -/*#include "codebook.h"*/ -/*#include "misc.h"*/ -/*#include "scales.h"*/ - -#include - -#define floor1_rangedB 140 /* floor 1 fixed at -140dB to 0dB range */ - -typedef struct lsfit_acc{ - int x0; - int x1; - - int xa; - int ya; - int x2a; - int y2a; - int xya; - int an; - - int xb; - int yb; - int x2b; - int y2b; - int xyb; - int bn; -} lsfit_acc; - -/***********************************************/ - -static void floor1_free_info(vorbis_info_floor *i){ - vorbis_info_floor1 *info=(vorbis_info_floor1 *)i; - if(info){ - memset(info,0,sizeof(*info)); - _ogg_free(info); - } -} - -static void floor1_free_look(vorbis_look_floor *i){ - vorbis_look_floor1 *look=(vorbis_look_floor1 *)i; - if(look){ - /*fprintf(stderr,"floor 1 bit usage %f:%f (%f total)\n", - (float)look->phrasebits/look->frames, - (float)look->postbits/look->frames, - (float)(look->postbits+look->phrasebits)/look->frames);*/ - - memset(look,0,sizeof(*look)); - _ogg_free(look); - } -} - -static void floor1_pack (vorbis_info_floor *i,oggpack_buffer *opb){ - vorbis_info_floor1 *info=(vorbis_info_floor1 *)i; - int j,k; - int count=0; - int rangebits; - int maxposit=info->postlist[1]; - int maxclass=-1; - - /* save out partitions */ - oggpack_write(opb,info->partitions,5); /* only 0 to 31 legal */ - for(j=0;jpartitions;j++){ - oggpack_write(opb,info->partitionclass[j],4); /* only 0 to 15 legal */ - if(maxclasspartitionclass[j])maxclass=info->partitionclass[j]; - } - - /* save out partition classes */ - for(j=0;jclass_dim[j]-1,3); /* 1 to 8 */ - oggpack_write(opb,info->class_subs[j],2); /* 0 to 3 */ - if(info->class_subs[j])oggpack_write(opb,info->class_book[j],8); - for(k=0;k<(1<class_subs[j]);k++) - oggpack_write(opb,info->class_subbook[j][k]+1,8); - } - - /* save out the post list */ - oggpack_write(opb,info->mult-1,2); /* only 1,2,3,4 legal now */ - /* maxposit cannot legally be less than 1; this is encode-side, we - can assume our setup is OK */ - oggpack_write(opb,ov_ilog(maxposit-1),4); - rangebits=ov_ilog(maxposit-1); - - for(j=0,k=0;jpartitions;j++){ - count+=info->class_dim[info->partitionclass[j]]; - for(;kpostlist[k+2],rangebits); - } -} - -static int icomp(const void *a,const void *b){ - return(**(int **)a-**(int **)b); -} - -static vorbis_info_floor *floor1_unpack (vorbis_info *vi,oggpack_buffer *opb){ - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - int j,k,count=0,maxclass=-1,rangebits; - - vorbis_info_floor1 *info=(vorbis_info_floor1 *)_ogg_calloc(1,sizeof(*info)); - /* read partitions */ - info->partitions=oggpack_read(opb,5); /* only 0 to 31 legal */ - for(j=0;jpartitions;j++){ - info->partitionclass[j]=oggpack_read(opb,4); /* only 0 to 15 legal */ - if(info->partitionclass[j]<0)goto err_out; - if(maxclasspartitionclass[j])maxclass=info->partitionclass[j]; - } - - /* read partition classes */ - for(j=0;jclass_dim[j]=oggpack_read(opb,3)+1; /* 1 to 8 */ - info->class_subs[j]=oggpack_read(opb,2); /* 0,1,2,3 bits */ - if(info->class_subs[j]<0) - goto err_out; - if(info->class_subs[j])info->class_book[j]=oggpack_read(opb,8); - if(info->class_book[j]<0 || info->class_book[j]>=ci->books) - goto err_out; - for(k=0;k<(1<class_subs[j]);k++){ - info->class_subbook[j][k]=oggpack_read(opb,8)-1; - if(info->class_subbook[j][k]<-1 || info->class_subbook[j][k]>=ci->books) - goto err_out; - } - } - - /* read the post list */ - info->mult=oggpack_read(opb,2)+1; /* only 1,2,3,4 legal now */ - rangebits=oggpack_read(opb,4); - if(rangebits<0)goto err_out; - - for(j=0,k=0;jpartitions;j++){ - count+=info->class_dim[info->partitionclass[j]]; - if(count>VIF_POSIT) goto err_out; - for(;kpostlist[k+2]=oggpack_read(opb,rangebits); - if(t<0 || t>=(1<postlist[0]=0; - info->postlist[1]=1<postlist+j; - qsort(sortpointer,count+2,sizeof(*sortpointer),icomp); - - for(j=1;jvi=info; - look->n=info->postlist[1]; - - /* we drop each position value in-between already decoded values, - and use linear interpolation to predict each new value past the - edges. The positions are read in the order of the position - list... we precompute the bounding positions in the lookup. Of - course, the neighbors can change (if a position is declined), but - this is an initial mapping */ - - for(i=0;ipartitions;i++)n+=info->class_dim[info->partitionclass[i]]; - n+=2; - look->posts=n; - - /* also store a sorted position index */ - for(i=0;ipostlist+i; - qsort(sortpointer,n,sizeof(*sortpointer),icomp); - - /* points from sort order back to range number */ - for(i=0;iforward_index[i]=sortpointer[i]-info->postlist; - /* points from range order to sorted position */ - for(i=0;ireverse_index[look->forward_index[i]]=i; - /* we actually need the post values too */ - for(i=0;isorted_index[i]=info->postlist[look->forward_index[i]]; - - /* quantize values to multiplier spec */ - switch(info->mult){ - case 1: /* 1024 -> 256 */ - look->quant_q=256; - break; - case 2: /* 1024 -> 128 */ - look->quant_q=128; - break; - case 3: /* 1024 -> 86 */ - look->quant_q=86; - break; - case 4: /* 1024 -> 64 */ - look->quant_q=64; - break; - } - - /* discover our neighbors for decode where we don't use fit flags - (that would push the neighbors outward) */ - for(i=0;in; - int currentx=info->postlist[i+2]; - for(j=0;jpostlist[j]; - if(x>lx && xcurrentx){ - hi=j; - hx=x; - } - } - look->loneighbor[i]=lo; - look->hineighbor[i]=hi; - } - - return(look); -} - -static int render_point(int x0,int x1,int y0,int y1,int x){ - y0&=0x7fff; /* mask off flag */ - y1&=0x7fff; - - { - int dy=y1-y0; - int adx=x1-x0; - int ady=abs(dy); - int err=ady*(x-x0); - - int off=err/adx; - if(dy<0)return(y0-off); - return(y0+off); - } -} - -static int vorbis_dBquant(const float *x){ - int i= *x*7.3142857f+1023.5f; - if(i>1023)return(1023); - if(i<0)return(0); - return i; -} - -static const float FLOOR1_fromdB_LOOKUP[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.128753e-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.F, -}; - -static void render_line(int n, int x0,int x1,int y0,int y1,float *d){ - int dy=y1-y0; - int adx=x1-x0; - int ady=abs(dy); - int base=dy/adx; - int sy=(dy<0?base-1:base+1); - int x=x0; - int y=y0; - int err=0; - - ady-=abs(base*adx); - - if(n>x1)n=x1; - - if(x=adx){ - err-=adx; - y+=sy; - }else{ - y+=base; - } - d[x]*=FLOOR1_fromdB_LOOKUP[y]; - } -} - -static void render_line0(int n, int x0,int x1,int y0,int y1,int *d){ - int dy=y1-y0; - int adx=x1-x0; - int ady=abs(dy); - int base=dy/adx; - int sy=(dy<0?base-1:base+1); - int x=x0; - int y=y0; - int err=0; - - ady-=abs(base*adx); - - if(n>x1)n=x1; - - if(x=adx){ - err-=adx; - y+=sy; - }else{ - y+=base; - } - d[x]=y; - } -} - -/* the floor has already been filtered to only include relevant sections */ -static int accumulate_fit(const float *flr,const float *mdct, - int x0, int x1,lsfit_acc *a, - int n,vorbis_info_floor1 *info){ - long i; - - int xa=0,ya=0,x2a=0,y2a=0,xya=0,na=0, xb=0,yb=0,x2b=0,y2b=0,xyb=0,nb=0; - - memset(a,0,sizeof(*a)); - a->x0=x0; - a->x1=x1; - if(x1>=n)x1=n-1; - - for(i=x0;i<=x1;i++){ - int quantized=vorbis_dBquant(flr+i); - if(quantized){ - if(mdct[i]+info->twofitatten>=flr[i]){ - xa += i; - ya += quantized; - x2a += i*i; - y2a += quantized*quantized; - xya += i*quantized; - na++; - }else{ - xb += i; - yb += quantized; - x2b += i*i; - y2b += quantized*quantized; - xyb += i*quantized; - nb++; - } - } - } - - a->xa=xa; - a->ya=ya; - a->x2a=x2a; - a->y2a=y2a; - a->xya=xya; - a->an=na; - - a->xb=xb; - a->yb=yb; - a->x2b=x2b; - a->y2b=y2b; - a->xyb=xyb; - a->bn=nb; - - return(na); -} - -static int fit_line(lsfit_acc *a,int fits,int *y0,int *y1, - vorbis_info_floor1 *info){ - double xb=0,yb=0,x2b=0,y2b=0,xyb=0,bn=0; - int i; - int x0=a[0].x0; - int x1=a[fits-1].x1; - - for(i=0;itwofitweight/(a[i].an+1)+1.; - - xb+=a[i].xb + a[i].xa * weight; - yb+=a[i].yb + a[i].ya * weight; - x2b+=a[i].x2b + a[i].x2a * weight; - y2b+=a[i].y2b + a[i].y2a * weight; - xyb+=a[i].xyb + a[i].xya * weight; - bn+=a[i].bn + a[i].an * weight; - } - - if(*y0>=0){ - xb+= x0; - yb+= *y0; - x2b+= x0 * x0; - y2b+= *y0 * *y0; - xyb+= *y0 * x0; - bn++; - } - - if(*y1>=0){ - xb+= x1; - yb+= *y1; - x2b+= x1 * x1; - y2b+= *y1 * *y1; - xyb+= *y1 * x1; - bn++; - } - - { - double denom=(bn*x2b-xb*xb); - - if(denom>0.){ - double a=(yb*x2b-xyb*xb)/denom; - double b=(bn*xyb-xb*yb)/denom; - *y0=rint(a+b*x0); - *y1=rint(a+b*x1); - - /* limit to our range! */ - if(*y0>1023)*y0=1023; - if(*y1>1023)*y1=1023; - if(*y0<0)*y0=0; - if(*y1<0)*y1=0; - - return 0; - }else{ - *y0=0; - *y1=0; - return 1; - } - } -} - -static int inspect_error(int x0,int x1,int y0,int y1,const float *mask, - const float *mdct, - vorbis_info_floor1 *info){ - int dy=y1-y0; - int adx=x1-x0; - int ady=abs(dy); - int base=dy/adx; - int sy=(dy<0?base-1:base+1); - int x=x0; - int y=y0; - int err=0; - int val=vorbis_dBquant(mask+x); - int mse=0; - int n=0; - - ady-=abs(base*adx); - - mse=(y-val); - mse*=mse; - n++; - if(mdct[x]+info->twofitatten>=mask[x]){ - if(y+info->maxovermaxunder>val)return(1); - } - - while(++x=adx){ - err-=adx; - y+=sy; - }else{ - y+=base; - } - - val=vorbis_dBquant(mask+x); - mse+=((y-val)*(y-val)); - n++; - if(mdct[x]+info->twofitatten>=mask[x]){ - if(val){ - if(y+info->maxovermaxunder>val)return(1); - } - } - } - - if(info->maxover*info->maxover/n>info->maxerr)return(0); - if(info->maxunder*info->maxunder/n>info->maxerr)return(0); - if(mse/n>info->maxerr)return(1); - return(0); -} - -static int post_Y(int *A,int *B,int pos){ - if(A[pos]<0) - return B[pos]; - if(B[pos]<0) - return A[pos]; - - return (A[pos]+B[pos])>>1; -} - -int *floor1_fit(vorbis_block *vb,vorbis_look_floor1 *look, - const float *logmdct, /* in */ - const float *logmask){ - long i,j; - vorbis_info_floor1 *info=look->vi; - long n=look->n; - long posts=look->posts; - long nonzero=0; - lsfit_acc fits[VIF_POSIT+1]; - int fit_valueA[VIF_POSIT+2]; /* index by range list position */ - int fit_valueB[VIF_POSIT+2]; /* index by range list position */ - - int loneighbor[VIF_POSIT+2]; /* sorted index of range list position (+2) */ - int hineighbor[VIF_POSIT+2]; - int *output=NULL; - int memo[VIF_POSIT+2]; - - for(i=0;isorted_index[i], - look->sorted_index[i+1],fits+i, - n,info); - } - - if(nonzero){ - /* start by fitting the implicit base case.... */ - int y0=-200; - int y1=-200; - fit_line(fits,posts-1,&y0,&y1,info); - - fit_valueA[0]=y0; - fit_valueB[0]=y0; - fit_valueB[1]=y1; - fit_valueA[1]=y1; - - /* Non degenerate case */ - /* start progressive splitting. This is a greedy, non-optimal - algorithm, but simple and close enough to the best - answer. */ - for(i=2;ireverse_index[i]; - int ln=loneighbor[sortpos]; - int hn=hineighbor[sortpos]; - - /* eliminate repeat searches of a particular range with a memo */ - if(memo[ln]!=hn){ - /* haven't performed this error search yet */ - int lsortpos=look->reverse_index[ln]; - int hsortpos=look->reverse_index[hn]; - memo[ln]=hn; - - { - /* A note: we want to bound/minimize *local*, not global, error */ - int lx=info->postlist[ln]; - int hx=info->postlist[hn]; - int ly=post_Y(fit_valueA,fit_valueB,ln); - int hy=post_Y(fit_valueA,fit_valueB,hn); - - if(ly==-1 || hy==-1){ - exit(1); - } - - if(inspect_error(lx,hx,ly,hy,logmask,logmdct,info)){ - /* outside error bounds/begin search area. Split it. */ - int ly0=-200; - int ly1=-200; - int hy0=-200; - int hy1=-200; - int ret0=fit_line(fits+lsortpos,sortpos-lsortpos,&ly0,&ly1,info); - int ret1=fit_line(fits+sortpos,hsortpos-sortpos,&hy0,&hy1,info); - - if(ret0){ - ly0=ly; - ly1=hy0; - } - if(ret1){ - hy0=ly1; - hy1=hy; - } - - if(ret0 && ret1){ - fit_valueA[i]=-200; - fit_valueB[i]=-200; - }else{ - /* store new edge values */ - fit_valueB[ln]=ly0; - if(ln==0)fit_valueA[ln]=ly0; - fit_valueA[i]=ly1; - fit_valueB[i]=hy0; - fit_valueA[hn]=hy1; - if(hn==1)fit_valueB[hn]=hy1; - - if(ly1>=0 || hy0>=0){ - /* store new neighbor values */ - for(j=sortpos-1;j>=0;j--) - if(hineighbor[j]==hn) - hineighbor[j]=i; - else - break; - for(j=sortpos+1;jloneighbor[i-2]; - int hn=look->hineighbor[i-2]; - int x0=info->postlist[ln]; - int x1=info->postlist[hn]; - int y0=output[ln]; - int y1=output[hn]; - - int predicted=render_point(x0,x1,y0,y1,info->postlist[i]); - int vx=post_Y(fit_valueA,fit_valueB,i); - - if(vx>=0 && predicted!=vx){ - output[i]=vx; - }else{ - output[i]= predicted|0x8000; - } - } - } - - return(output); - -} - -int *floor1_interpolate_fit(vorbis_block *vb,vorbis_look_floor1 *look, - int *A,int *B, - int del){ - - long i; - long posts=look->posts; - int *output=NULL; - - if(A && B){ - output=(int *)_vorbis_block_alloc(vb,sizeof(*output)*posts); - - /* overly simpleminded--- look again post 1.2 */ - for(i=0;i>16; - if(A[i]&0x8000 && B[i]&0x8000)output[i]|=0x8000; - } - } - - return(output); -} - - -int floor1_encode(oggpack_buffer *opb,vorbis_block *vb, - vorbis_look_floor1 *look, - int *post,int *ilogmask){ - - long i,j; - vorbis_info_floor1 *info=look->vi; - long posts=look->posts; - codec_setup_info *ci=(codec_setup_info *)vb->vd->vi->codec_setup; - int out[VIF_POSIT+2]; - static_codebook **sbooks=ci->book_param; - codebook *books=ci->fullbooks; - - /* quantize values to multiplier spec */ - if(post){ - for(i=0;imult){ - case 1: /* 1024 -> 256 */ - val>>=2; - break; - case 2: /* 1024 -> 128 */ - val>>=3; - break; - case 3: /* 1024 -> 86 */ - val/=12; - break; - case 4: /* 1024 -> 64 */ - val>>=4; - break; - } - post[i]=val | (post[i]&0x8000); - } - - out[0]=post[0]; - out[1]=post[1]; - - /* find prediction values for each post and subtract them */ - for(i=2;iloneighbor[i-2]; - int hn=look->hineighbor[i-2]; - int x0=info->postlist[ln]; - int x1=info->postlist[hn]; - int y0=post[ln]; - int y1=post[hn]; - - int predicted=render_point(x0,x1,y0,y1,info->postlist[i]); - - if((post[i]&0x8000) || (predicted==post[i])){ - post[i]=predicted|0x8000; /* in case there was roundoff jitter - in interpolation */ - out[i]=0; - }else{ - int headroom=(look->quant_q-predictedquant_q-predicted:predicted); - - int val=post[i]-predicted; - - /* at this point the 'deviation' value is in the range +/- max - range, but the real, unique range can always be mapped to - only [0-maxrange). So we want to wrap the deviation into - this limited range, but do it in the way that least screws - an essentially gaussian probability distribution. */ - - if(val<0) - if(val<-headroom) - val=headroom-val-1; - else - val=-1-(val<<1); - else - if(val>=headroom) - val= val+headroom; - else - val<<=1; - - out[i]=val; - post[ln]&=0x7fff; - post[hn]&=0x7fff; - } - } - - /* we have everything we need. pack it out */ - /* mark nontrivial floor */ - oggpack_write(opb,1,1); - - /* beginning/end post */ - look->frames++; - look->postbits+=ov_ilog(look->quant_q-1)*2; - oggpack_write(opb,out[0],ov_ilog(look->quant_q-1)); - oggpack_write(opb,out[1],ov_ilog(look->quant_q-1)); - - - /* partition by partition */ - for(i=0,j=2;ipartitions;i++){ - int class_i=info->partitionclass[i]; - int cdim=info->class_dim[class_i]; - int csubbits=info->class_subs[class_i]; - int csub=1<class_subbook[class_i][k]; - if(booknum<0){ - maxval[k]=1; - }else{ - maxval[k]=sbooks[info->class_subbook[class_i][k]]->entries; - } - } - for(k=0;kphrasebits+= - vorbis_book_encode(books+info->class_book[class_i],cval,opb); - -#ifdef TRAIN_FLOOR1 - { - FILE *of; - char buffer[80]; - sprintf(buffer,"line_%dx%ld_class%d.vqd", - vb->pcmend/2,posts-2,class_i); - of=fopen(buffer,"a"); - fprintf(of,"%d\n",cval); - fclose(of); - } -#endif - } - - /* write post values */ - for(k=0;kclass_subbook[class_i][bookas[k]]; - if(book>=0){ - /* hack to allow training with 'bad' books */ - if(out[j+k]<(books+book)->entries) - look->postbits+=vorbis_book_encode(books+book, - out[j+k],opb); - /*else - fprintf(stderr,"+!");*/ - -#ifdef TRAIN_FLOOR1 - { - FILE *of; - char buffer[80]; - sprintf(buffer,"line_%dx%ld_%dsub%d.vqd", - vb->pcmend/2,posts-2,class_i,bookas[k]); - of=fopen(buffer,"a"); - fprintf(of,"%d\n",out[j+k]); - fclose(of); - } -#endif - } - } - j+=cdim; - } - - { - /* generate quantized floor equivalent to what we'd unpack in decode */ - /* render the lines */ - int hx=0; - int lx=0; - int ly=post[0]*info->mult; - int n=ci->blocksizes[vb->W]/2; - - for(j=1;jposts;j++){ - int current=look->forward_index[j]; - int hy=post[current]&0x7fff; - if(hy==post[current]){ - - hy*=info->mult; - hx=info->postlist[current]; - - render_line0(n,lx,hx,ly,hy,ilogmask); - - lx=hx; - ly=hy; - } - } - for(j=hx;jpcmend/2;j++)ilogmask[j]=ly; /* be certain */ - return(1); - } - }else{ - oggpack_write(opb,0,1); - memset(ilogmask,0,vb->pcmend/2*sizeof(*ilogmask)); - return(0); - } -} - -static void *floor1_inverse1(vorbis_block *vb,vorbis_look_floor *in){ - vorbis_look_floor1 *look=(vorbis_look_floor1 *)in; - vorbis_info_floor1 *info=look->vi; - codec_setup_info *ci=(codec_setup_info *)vb->vd->vi->codec_setup; - - int i,j,k; - codebook *books=ci->fullbooks; - - /* unpack wrapped/predicted values from stream */ - if(oggpack_read(&vb->opb,1)==1){ - int *fit_value=(int *)_vorbis_block_alloc(vb,(look->posts)*sizeof(*fit_value)); - - fit_value[0]=oggpack_read(&vb->opb,ov_ilog(look->quant_q-1)); - fit_value[1]=oggpack_read(&vb->opb,ov_ilog(look->quant_q-1)); - - /* partition by partition */ - for(i=0,j=2;ipartitions;i++){ - int class_i=info->partitionclass[i]; - int cdim=info->class_dim[class_i]; - int csubbits=info->class_subs[class_i]; - int csub=1<class_book[class_i],&vb->opb); - - if(cval==-1)goto eop; - } - - for(k=0;kclass_subbook[class_i][cval&(csub-1)]; - cval>>=csubbits; - if(book>=0){ - if((fit_value[j+k]=vorbis_book_decode(books+book,&vb->opb))==-1) - goto eop; - }else{ - fit_value[j+k]=0; - } - } - j+=cdim; - } - - /* unwrap positive values and reconsitute via linear interpolation */ - for(i=2;iposts;i++){ - int predicted=render_point(info->postlist[look->loneighbor[i-2]], - info->postlist[look->hineighbor[i-2]], - fit_value[look->loneighbor[i-2]], - fit_value[look->hineighbor[i-2]], - info->postlist[i]); - int hiroom=look->quant_q-predicted; - int loroom=predicted; - int room=(hiroom=room){ - if(hiroom>loroom){ - val = val-loroom; - }else{ - val = -1-(val-hiroom); - } - }else{ - if(val&1){ - val= -((val+1)>>1); - }else{ - val>>=1; - } - } - - fit_value[i]=(val+predicted)&0x7fff; - fit_value[look->loneighbor[i-2]]&=0x7fff; - fit_value[look->hineighbor[i-2]]&=0x7fff; - - }else{ - fit_value[i]=predicted|0x8000; - } - - } - - return(fit_value); - } - eop: - return(NULL); -} - -static int floor1_inverse2(vorbis_block *vb,vorbis_look_floor *in,void *memo, - float *out){ - vorbis_look_floor1 *look=(vorbis_look_floor1 *)in; - vorbis_info_floor1 *info=look->vi; - - codec_setup_info *ci=(codec_setup_info *)vb->vd->vi->codec_setup; - int n=ci->blocksizes[vb->W]/2; - int j; - - if(memo){ - /* render the lines */ - int *fit_value=(int *)memo; - int hx=0; - int lx=0; - int ly=fit_value[0]*info->mult; - /* guard lookup against out-of-range values */ - ly=(ly<0?0:ly>255?255:ly); - - for(j=1;jposts;j++){ - int current=look->forward_index[j]; - int hy=fit_value[current]&0x7fff; - if(hy==fit_value[current]){ - - hx=info->postlist[current]; - hy*=info->mult; - /* guard lookup against out-of-range values */ - hy=(hy<0?0:hy>255?255:hy); - - render_line(n,lx,hx,ly,hy,out); - - lx=hx; - ly=hy; - } - } - for(j=hx;j -#include -#include -/*#include */ -/*#include "vorbis/codec.h"*/ -/*#include "codec_internal.h"*/ -/*#include "registry.h"*/ -/*#include "lpc.h"*/ -/*#include "lsp.h"*/ -/*#include "codebook.h"*/ -/*#include "scales.h"*/ -/*#include "misc.h"*/ -/*#include "os.h"*/ - -/*#include "misc.h"*/ -#include - -typedef struct { - int ln; - int m; - int **linearmap; - int n[2]; - - vorbis_info_floor0 *vi; - - long bits; - long frames; -} vorbis_look_floor0; - - -/***********************************************/ - -static void floor0_free_info(vorbis_info_floor *i){ - vorbis_info_floor0 *info=(vorbis_info_floor0 *)i; - if(info){ - memset(info,0,sizeof(*info)); - _ogg_free(info); - } -} - -static void floor0_free_look(vorbis_look_floor *i){ - vorbis_look_floor0 *look=(vorbis_look_floor0 *)i; - if(look){ - - if(look->linearmap){ - - if(look->linearmap[0])_ogg_free(look->linearmap[0]); - if(look->linearmap[1])_ogg_free(look->linearmap[1]); - - _ogg_free(look->linearmap); - } - memset(look,0,sizeof(*look)); - _ogg_free(look); - } -} - -static vorbis_info_floor *floor0_unpack (vorbis_info *vi,oggpack_buffer *opb){ - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - int j; - - vorbis_info_floor0 *info=(vorbis_info_floor0 *)_ogg_malloc(sizeof(*info)); - info->order=oggpack_read(opb,8); - info->rate=oggpack_read(opb,16); - info->barkmap=oggpack_read(opb,16); - info->ampbits=oggpack_read(opb,6); - info->ampdB=oggpack_read(opb,8); - info->numbooks=oggpack_read(opb,4)+1; - - if(info->order<1)goto err_out; - if(info->rate<1)goto err_out; - if(info->barkmap<1)goto err_out; - if(info->numbooks<1)goto err_out; - - for(j=0;jnumbooks;j++){ - info->books[j]=oggpack_read(opb,8); - if(info->books[j]<0 || info->books[j]>=ci->books)goto err_out; - if(ci->book_param[info->books[j]]->maptype==0)goto err_out; - if(ci->book_param[info->books[j]]->dim<1)goto err_out; - } - return(info); - - err_out: - floor0_free_info(info); - return(NULL); -} - -/* initialize Bark scale and normalization lookups. We could do this - with static tables, but Vorbis allows a number of possible - combinations, so it's best to do it computationally. - - The below is authoritative in terms of defining scale mapping. - Note that the scale depends on the sampling rate as well as the - linear block and mapping sizes */ - -static void floor0_map_lazy_init(vorbis_block *vb, - vorbis_info_floor *infoX, - vorbis_look_floor0 *look){ - if(!look->linearmap[vb->W]){ - vorbis_dsp_state *vd=vb->vd; - vorbis_info *vi=vd->vi; - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - vorbis_info_floor0 *info=(vorbis_info_floor0 *)infoX; - int W=vb->W; - int n=ci->blocksizes[W]/2,j; - - /* we choose a scaling constant so that: - floor(bark(rate/2-1)*C)=mapped-1 - floor(bark(rate/2)*C)=mapped */ - float scale=look->ln/toBARK(info->rate/2.f); - - /* the mapping from a linear scale to a smaller bark scale is - straightforward. We do *not* make sure that the linear mapping - does not skip bark-scale bins; the decoder simply skips them and - the encoder may do what it wishes in filling them. They're - necessary in some mapping combinations to keep the scale spacing - accurate */ - look->linearmap[W]=(int *)_ogg_malloc((n+1)*sizeof(**look->linearmap)); - for(j=0;jrate/2.f)/n*j) - *scale); /* bark numbers represent band edges */ - if(val>=look->ln)val=look->ln-1; /* guard against the approximation */ - look->linearmap[W][j]=val; - } - look->linearmap[W][j]=-1; - look->n[W]=n; - } -} - -static vorbis_look_floor *floor0_look(vorbis_dsp_state *vd, - vorbis_info_floor *i){ - vorbis_info_floor0 *info=(vorbis_info_floor0 *)i; - vorbis_look_floor0 *look=(vorbis_look_floor0 *)_ogg_calloc(1,sizeof(*look)); - - (void)vd; - - look->m=info->order; - look->ln=info->barkmap; - look->vi=info; - - look->linearmap=(int **)_ogg_calloc(2,sizeof(*look->linearmap)); - - return look; -} - -static void *floor0_inverse1(vorbis_block *vb,vorbis_look_floor *i){ - vorbis_look_floor0 *look=(vorbis_look_floor0 *)i; - vorbis_info_floor0 *info=look->vi; - int j,k; - - int ampraw=oggpack_read(&vb->opb,info->ampbits); - if(ampraw>0){ /* also handles the -1 out of data case */ - long maxval=(1<ampbits)-1; - float amp=(float)ampraw/maxval*info->ampdB; - int booknum=oggpack_read(&vb->opb,ov_ilog(info->numbooks)); - - if(booknum!=-1 && booknumnumbooks){ /* be paranoid */ - codec_setup_info *ci=(codec_setup_info *)vb->vd->vi->codec_setup; - codebook *b=ci->fullbooks+info->books[booknum]; - float last=0.f; - - /* the additional b->dim is a guard against any possible stack - smash; b->dim is provably more than we can overflow the - vector */ - float *lsp=(float *)_vorbis_block_alloc(vb,sizeof(*lsp)*(look->m+b->dim+1)); - - if(vorbis_book_decodev_set(b,lsp,&vb->opb,look->m)==-1)goto eop; - for(j=0;jm;){ - for(k=0;jm && kdim;k++,j++)lsp[j]+=last; - last=lsp[j-1]; - } - - lsp[look->m]=amp; - return(lsp); - } - } - eop: - return(NULL); -} - -static int floor0_inverse2(vorbis_block *vb,vorbis_look_floor *i, - void *memo,float *out){ - vorbis_look_floor0 *look=(vorbis_look_floor0 *)i; - vorbis_info_floor0 *info=look->vi; - - floor0_map_lazy_init(vb,info,look); - - if(memo){ - float *lsp=(float *)memo; - float amp=lsp[look->m]; - - /* take the coefficients back to a spectral envelope curve */ - vorbis_lsp_to_curve(out, - look->linearmap[vb->W], - look->n[vb->W], - look->ln, - lsp,look->m,amp,(float)info->ampdB); - return(1); - } - memset(out,0,sizeof(*out)*look->n[vb->W]); - return(0); -} - -/* export hooks */ -const vorbis_func_floor floor0_exportbundle={ - NULL,&floor0_unpack,&floor0_look,&floor0_free_info, - &floor0_free_look,&floor0_inverse1,&floor0_inverse2 -}; -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: residue backend 0, 1 and 2 implementation - - ********************************************************************/ - -/* Slow, slow, slow, simpleminded and did I mention it was slow? The - encode/decode loops are coded for clarity and performance is not - yet even a nagging little idea lurking in the shadows. Oh and BTW, - it's slow. */ - -#include -#include -#include -/*#include */ -/*#include "vorbis/codec.h"*/ -/*#include "codec_internal.h"*/ -/*#include "registry.h"*/ -/*#include "codebook.h"*/ -/*#include "misc.h"*/ -/*#include "os.h"*/ - -#if defined(TRAIN_RES) || defined (TRAIN_RESAUX) -#include -#endif - -typedef struct { - vorbis_info_residue0 *info; - - int parts; - int stages; - codebook *fullbooks; - codebook *phrasebook; - codebook ***partbooks; - - int partvals; - int **decodemap; - - long postbits; - long phrasebits; - long frames; - -#if defined(TRAIN_RES) || defined(TRAIN_RESAUX) - int train_seq; - long *training_data[8][64]; - float training_max[8][64]; - float training_min[8][64]; - float tmin; - float tmax; - int submap; -#endif - -} vorbis_look_residue0; - -void res0_free_info(vorbis_info_residue *i){ - vorbis_info_residue0 *info=(vorbis_info_residue0 *)i; - if(info){ - memset(reinterpret_cast(info),0,sizeof(*info)); - _ogg_free(info); - } -} - -void res0_free_look(vorbis_look_residue *i){ - int j; - if(i){ - - vorbis_look_residue0 *look=(vorbis_look_residue0 *)i; - -#ifdef TRAIN_RES - { - int j,k,l; - for(j=0;jparts;j++){ - /*fprintf(stderr,"partition %d: ",j);*/ - for(k=0;k<8;k++) - if(look->training_data[k][j]){ - char buffer[80]; - FILE *of; - codebook *statebook=look->partbooks[j][k]; - - /* long and short into the same bucket by current convention */ - sprintf(buffer,"res_sub%d_part%d_pass%d.vqd",look->submap,j,k); - of=fopen(buffer,"a"); - - for(l=0;lentries;l++) - fprintf(of,"%d:%ld\n",l,look->training_data[k][j][l]); - - fclose(of); - - /*fprintf(stderr,"%d(%.2f|%.2f) ",k, - look->training_min[k][j],look->training_max[k][j]);*/ - - _ogg_free(look->training_data[k][j]); - look->training_data[k][j]=NULL; - } - /*fprintf(stderr,"\n");*/ - } - } - fprintf(stderr,"min/max residue: %g::%g\n",look->tmin,look->tmax); - - /*fprintf(stderr,"residue bit usage %f:%f (%f total)\n", - (float)look->phrasebits/look->frames, - (float)look->postbits/look->frames, - (float)(look->postbits+look->phrasebits)/look->frames);*/ -#endif - - - /*vorbis_info_residue0 *info=look->info; - - fprintf(stderr, - "%ld frames encoded in %ld phrasebits and %ld residue bits " - "(%g/frame) \n",look->frames,look->phrasebits, - look->resbitsflat, - (look->phrasebits+look->resbitsflat)/(float)look->frames); - - for(j=0;jparts;j++){ - long acc=0; - fprintf(stderr,"\t[%d] == ",j); - for(k=0;kstages;k++) - if((info->secondstages[j]>>k)&1){ - fprintf(stderr,"%ld,",look->resbits[j][k]); - acc+=look->resbits[j][k]; - } - - fprintf(stderr,":: (%ld vals) %1.2fbits/sample\n",look->resvals[j], - acc?(float)acc/(look->resvals[j]*info->grouping):0); - } - fprintf(stderr,"\n");*/ - - for(j=0;jparts;j++) - if(look->partbooks[j])_ogg_free(look->partbooks[j]); - _ogg_free(look->partbooks); - for(j=0;jpartvals;j++) - _ogg_free(look->decodemap[j]); - _ogg_free(look->decodemap); - - memset(look,0,sizeof(*look)); - _ogg_free(look); - } -} - -static int icount(unsigned int v){ - int ret=0; - while(v){ - ret+=v&1; - v>>=1; - } - return(ret); -} - - -void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){ - vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr; - int j,acc=0; - oggpack_write(opb,info->begin,24); - oggpack_write(opb,info->end,24); - - oggpack_write(opb,info->grouping-1,24); /* residue vectors to group and - code with a partitioned book */ - oggpack_write(opb,info->partitions-1,6); /* possible partition choices */ - oggpack_write(opb,info->groupbook,8); /* group huffman book */ - - /* secondstages is a bitmask; as encoding progresses pass by pass, a - bitmask of one indicates this partition class has bits to write - this pass */ - for(j=0;jpartitions;j++){ - if(ov_ilog(info->secondstages[j])>3){ - /* yes, this is a minor hack due to not thinking ahead */ - oggpack_write(opb,info->secondstages[j],3); - oggpack_write(opb,1,1); - oggpack_write(opb,info->secondstages[j]>>3,5); - }else - oggpack_write(opb,info->secondstages[j],4); /* trailing zero */ - acc+=icount(info->secondstages[j]); - } - for(j=0;jbooklist[j],8); - -} - -/* vorbis_info is for range checking */ -vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){ - int j,acc=0; - vorbis_info_residue0 *info=(vorbis_info_residue0 *)_ogg_calloc(1,sizeof(*info)); - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - - info->begin=oggpack_read(opb,24); - info->end=oggpack_read(opb,24); - info->grouping=oggpack_read(opb,24)+1; - info->partitions=oggpack_read(opb,6)+1; - info->groupbook=oggpack_read(opb,8); - - /* check for premature EOP */ - if(info->groupbook<0)goto errout; - - for(j=0;jpartitions;j++){ - int cascade=oggpack_read(opb,3); - int cflag=oggpack_read(opb,1); - if(cflag<0) goto errout; - if(cflag){ - int c=oggpack_read(opb,5); - if(c<0) goto errout; - cascade|=(c<<3); - } - info->secondstages[j]=cascade; - - acc+=icount(cascade); - } - for(j=0;jbooklist[j]=book; - } - - if(info->groupbook>=ci->books)goto errout; - for(j=0;jbooklist[j]>=ci->books)goto errout; - if(ci->book_param[info->booklist[j]]->maptype==0)goto errout; - } - - /* verify the phrasebook is not specifying an impossible or - inconsistent partitioning scheme. */ - /* modify the phrasebook ranging check from r16327; an early beta - encoder had a bug where it used an oversized phrasebook by - accident. These files should continue to be playable, but don't - allow an exploit */ - { - int entries = ci->book_param[info->groupbook]->entries; - int dim = ci->book_param[info->groupbook]->dim; - int partvals = 1; - if (dim<1) goto errout; - while(dim>0){ - partvals *= info->partitions; - if(partvals > entries) goto errout; - dim--; - } - info->partvals = partvals; - } - - return(info); - errout: - res0_free_info(info); - return(NULL); -} - -vorbis_look_residue *res0_look(vorbis_dsp_state *vd, - vorbis_info_residue *vr){ - vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr; - vorbis_look_residue0 *look=(vorbis_look_residue0 *)_ogg_calloc(1,sizeof(*look)); - codec_setup_info *ci=(codec_setup_info *)vd->vi->codec_setup; - - int j,k,acc=0; - int dim; - int maxstage=0; - look->info=info; - - look->parts=info->partitions; - look->fullbooks=ci->fullbooks; - look->phrasebook=ci->fullbooks+info->groupbook; - dim=look->phrasebook->dim; - - look->partbooks=(codebook ***)_ogg_calloc(look->parts,sizeof(*look->partbooks)); - - for(j=0;jparts;j++){ - int stages=ov_ilog(info->secondstages[j]); - if(stages){ - if(stages>maxstage)maxstage=stages; - look->partbooks[j]=(codebook **)_ogg_calloc(stages,sizeof(*look->partbooks[j])); - for(k=0;ksecondstages[j]&(1<partbooks[j][k]=ci->fullbooks+info->booklist[acc++]; -#ifdef TRAIN_RES - look->training_data[k][j]=_ogg_calloc(look->partbooks[j][k]->entries, - sizeof(***look->training_data)); -#endif - } - } - } - - look->partvals=1; - for(j=0;jpartvals*=look->parts; - - look->stages=maxstage; - look->decodemap=(int **)_ogg_malloc(look->partvals*sizeof(*look->decodemap)); - for(j=0;jpartvals;j++){ - long val=j; - long mult=look->partvals/look->parts; - look->decodemap[j]=(int *)_ogg_malloc(dim*sizeof(*look->decodemap[j])); - for(k=0;kparts; - look->decodemap[j][k]=deco; - } - } -#if defined(TRAIN_RES) || defined (TRAIN_RESAUX) - { - static int train_seq=0; - look->train_seq=train_seq++; - } -#endif - return(look); -} - -/* break an abstraction and copy some code for performance purposes */ -static int local_book_besterror(codebook *book,int *a){ - int dim=book->dim; - int i,j,o; - int minval=book->minval; - int del=book->delta; - int qv=book->quantvals; - int ze=(qv>>1); - int index=0; - /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */ - int p[8]={0,0,0,0,0,0,0,0}; - - if(del!=1){ - for(i=0,o=dim;i>1))/del; - int m = (v=qv?qv-1:m)); - p[o]=v*del+minval; - } - }else{ - for(i=0,o=dim;i=qv?qv-1:m)); - p[o]=v*del+minval; - } - } - - if(book->c->lengthlist[index]<=0){ - const static_codebook *c=book->c; - int best=-1; - /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */ - int e[8]={0,0,0,0,0,0,0,0}; - int maxval = book->minval + book->delta*(book->quantvals-1); - for(i=0;ientries;i++){ - if(c->lengthlist[i]>0){ - int this_i=0; - for(j=0;j=maxval) - e[j++]=0; - if(e[j]>=0) - e[j]+=book->delta; - e[j]= -e[j]; - } - } - - if(index>-1){ - for(i=0;idim; - int step=n/dim; - - for(i=0;i=0) - acc[entry]++; -#endif - - bits+=vorbis_book_encode(book,entry,opb); - - } - - return(bits); -} - -static long **_01class(vorbis_block *vb,vorbis_look_residue *vl, - int **in,int ch){ - long i,j,k; - vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl; - vorbis_info_residue0 *info=look->info; - - /* move all this setup out later */ - int samples_per_partition=info->grouping; - int possible_partitions=info->partitions; - int n=info->end-info->begin; - - int partvals=n/samples_per_partition; - long **partword=(long int **)_vorbis_block_alloc(vb,ch*sizeof(*partword)); - float scale=100./samples_per_partition; - - /* we find the partition type for each partition of each - channel. We'll go back and do the interleaved encoding in a - bit. For now, clarity */ - - for(i=0;ibegin; - for(j=0;jmax)max=abs(in[j][offset+k]); - ent+=abs(in[j][offset+k]); - } - ent*=scale; - - for(k=0;kclassmetric1[k] && - (info->classmetric2[k]<0 || entclassmetric2[k])) - break; - - partword[j][i]=k; - } - } - -#ifdef TRAIN_RESAUX - { - FILE *of; - char buffer[80]; - - for(i=0;itrain_seq); - of=fopen(buffer,"a"); - for(j=0;jframes++; - - return(partword); -} - -/* designed for stereo or other modes where the partition size is an - integer multiple of the number of channels encoded in the current - submap */ -static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,int **in, - int ch){ - long i,j,k,l; - vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl; - vorbis_info_residue0 *info=look->info; - - /* move all this setup out later */ - int samples_per_partition=info->grouping; - int possible_partitions=info->partitions; - int n=info->end-info->begin; - - int partvals=n/samples_per_partition; - long **partword=(long int **)_vorbis_block_alloc(vb,sizeof(*partword)); - -#if defined(TRAIN_RES) || defined (TRAIN_RESAUX) - FILE *of; - char buffer[80]; -#endif - - partword[0]=(long int *)_vorbis_block_alloc(vb,partvals*sizeof(*partword[0])); - memset(partword[0],0,partvals*sizeof(*partword[0])); - - for(i=0,l=info->begin/ch;imagmax)magmax=abs(in[0][l]); - for(k=1;kangmax)angmax=abs(in[k][l]); - l++; - } - - for(j=0;jclassmetric1[j] && - angmax<=info->classmetric2[j]) - break; - - partword[0][i]=j; - - } - -#ifdef TRAIN_RESAUX - sprintf(buffer,"resaux_%d.vqd",look->train_seq); - of=fopen(buffer,"a"); - for(i=0;iframes++; - - return(partword); -} - -static int _01forward(oggpack_buffer *opb, - vorbis_look_residue *vl, - int **in,int ch, - long **partword, -#ifdef TRAIN_RES - int (*encode)(oggpack_buffer *,int *,int, - codebook *,long *), - int submap -#else - int (*encode)(oggpack_buffer *,int *,int, - codebook *) -#endif -){ - long i,j,k,s; - vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl; - vorbis_info_residue0 *info=look->info; - -#ifdef TRAIN_RES - look->submap=submap; -#endif - - /* move all this setup out later */ - int samples_per_partition=info->grouping; - int possible_partitions=info->partitions; - int partitions_per_word=look->phrasebook->dim; - int n=info->end-info->begin; - - int partvals=n/samples_per_partition; - long resbits[128]; - long resvals[128]; - -#ifdef TRAIN_RES - for(i=0;ibegin;jend;j++){ - if(in[i][j]>look->tmax)look->tmax=in[i][j]; - if(in[i][j]tmin)look->tmin=in[i][j]; - } -#endif - - memset(resbits,0,sizeof(resbits)); - memset(resvals,0,sizeof(resvals)); - - /* we code the partition words for each channel, then the residual - words for a partition per channel until we've written all the - residual words for that partition word. Then write the next - partition channel words... */ - - for(s=0;sstages;s++){ - - for(i=0;iphrasebook->entries) - look->phrasebits+=vorbis_book_encode(look->phrasebook,val,opb); -#if 0 /*def TRAIN_RES*/ - else - fprintf(stderr,"!"); -#endif - - } - } - - /* now we encode interleaved residual values for the partitions */ - for(k=0;kbegin; - - for(j=0;jsecondstages[partword[j][i]]&(1<partbooks[partword[j][i]][s]; - if(statebook){ - int ret; -#ifdef TRAIN_RES - long *accumulator=NULL; - accumulator=look->training_data[s][partword[j][i]]; - { - int l; - int *samples=in[j]+offset; - for(l=0;ltraining_min[s][partword[j][i]]) - look->training_min[s][partword[j][i]]=samples[l]; - if(samples[l]>look->training_max[s][partword[j][i]]) - look->training_max[s][partword[j][i]]=samples[l]; - } - } - ret=encode(opb,in[j]+offset,samples_per_partition, - statebook,accumulator); -#else - ret=encode(opb,in[j]+offset,samples_per_partition, - statebook); -#endif - - look->postbits+=ret; - resbits[partword[j][i]]+=ret; - } - } - } - } - } - } - - return(0); -} - -/* a truncated packet here just means 'stop working'; it's not an error */ -static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl, - float **in,int ch, - long (*decodepart)(codebook *, float *, - oggpack_buffer *,int)){ - - long i,j,k,l,s; - vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl; - vorbis_info_residue0 *info=look->info; - - /* move all this setup out later */ - int samples_per_partition=info->grouping; - int partitions_per_word=look->phrasebook->dim; - int max=vb->pcmend>>1; - int end=(info->endend:max); - int n=end-info->begin; - - if(n>0){ - int partvals=n/samples_per_partition; - int partwords=(partvals+partitions_per_word-1)/partitions_per_word; - int ***partword=(int ***)alloca(ch*sizeof(*partword)); - - for(j=0;jstages;s++){ - - /* each loop decodes on partition codeword containing - partitions_per_word partitions */ - for(i=0,l=0;iphrasebook,&vb->opb); - - if(temp==-1 || temp>=info->partvals)goto eopbreak; - partword[j][l]=look->decodemap[temp]; - if(partword[j][l]==NULL)goto errout; - } - } - - /* now we decode residual values for the partitions */ - for(k=0;kbegin+i*samples_per_partition; - if(info->secondstages[partword[j][l][k]]&(1<partbooks[partword[j][l][k]][s]; - if(stagebook){ - if(decodepart(stagebook,in[j]+offset,&vb->opb, - samples_per_partition)==-1)goto eopbreak; - } - } - } - } - } - } - errout: - eopbreak: - return(0); -} - -int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl, - float **in,int *nonzero,int ch){ - int i,used=0; - for(i=0;ipcmend/2,used=0; - - /* don't duplicate the code; use a working vector hack for now and - reshape ourselves into a single channel res1 */ - /* ugly; reallocs for each coupling pass :-( */ - int *work=(int *)_vorbis_block_alloc(vb,ch*n*sizeof(*work)); - for(i=0;iinfo; - - /* move all this setup out later */ - int samples_per_partition=info->grouping; - int partitions_per_word=look->phrasebook->dim; - int max=(vb->pcmend*ch)>>1; - int end=(info->endend:max); - int n=end-info->begin; - - if(n>0){ - int partvals=n/samples_per_partition; - int partwords=(partvals+partitions_per_word-1)/partitions_per_word; - int **partword=(int **)_vorbis_block_alloc(vb,partwords*sizeof(*partword)); - - for(i=0;istages;s++){ - for(i=0,l=0;iphrasebook,&vb->opb); - if(temp==-1 || temp>=info->partvals)goto eopbreak; - partword[l]=look->decodemap[temp]; - if(partword[l]==NULL)goto errout; - } - - /* now we decode residual values for the partitions */ - for(k=0;ksecondstages[partword[l][k]]&(1<partbooks[partword[l][k]][s]; - - if(stagebook){ - if(vorbis_book_decodevv_add(stagebook,in, - i*samples_per_partition+info->begin,ch, - &vb->opb,samples_per_partition)==-1) - goto eopbreak; - } - } - } - } - } - errout: - eopbreak: - return(0); -} - - -const vorbis_func_residue residue0_exportbundle={ - NULL, - &res0_unpack, - &res0_look, - &res0_free_info, - &res0_free_look, - NULL, - NULL, - &res0_inverse -}; - -const vorbis_func_residue residue1_exportbundle={ - &res0_pack, - &res0_unpack, - &res0_look, - &res0_free_info, - &res0_free_look, - &res1_class, - &res1_forward, - &res1_inverse -}; - -const vorbis_func_residue residue2_exportbundle={ - &res0_pack, - &res0_unpack, - &res0_look, - &res0_free_info, - &res0_free_look, - &res2_class, - &res2_forward, - &res2_inverse -}; -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: channel mapping 0 implementation - - ********************************************************************/ - -#include -#include -#include -#include -/*#include */ -/*#include "vorbis/codec.h"*/ -/*#include "codec_internal.h"*/ -/*#include "codebook.h"*/ -/*#include "window.h"*/ -/*#include "registry.h"*/ -/*#include "psy.h"*/ -/*#include "misc.h"*/ - -/* simplistic, wasteful way of doing this (unique lookup for each - mode/submapping); there should be a central repository for - identical lookups. That will require minor work, so I'm putting it - off as low priority. - - Why a lookup for each backend in a given mode? Because the - blocksize is set by the mode, and low backend lookups may require - parameters from other areas of the mode/mapping */ - -static void mapping0_free_info(vorbis_info_mapping *i){ - vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)i; - if(info){ - memset(info,0,sizeof(*info)); - _ogg_free(info); - } -} - -static void mapping0_pack(vorbis_info *vi,vorbis_info_mapping *vm, - oggpack_buffer *opb){ - int i; - vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm; - - /* another 'we meant to do it this way' hack... up to beta 4, we - packed 4 binary zeros here to signify one submapping in use. We - now redefine that to mean four bitflags that indicate use of - deeper features; bit0:submappings, bit1:coupling, - bit2,3:reserved. This is backward compatable with all actual uses - of the beta code. */ - - if(info->submaps>1){ - oggpack_write(opb,1,1); - oggpack_write(opb,info->submaps-1,4); - }else - oggpack_write(opb,0,1); - - if(info->coupling_steps>0){ - oggpack_write(opb,1,1); - oggpack_write(opb,info->coupling_steps-1,8); - - for(i=0;icoupling_steps;i++){ - oggpack_write(opb,info->coupling_mag[i],ov_ilog(vi->channels-1)); - oggpack_write(opb,info->coupling_ang[i],ov_ilog(vi->channels-1)); - } - }else - oggpack_write(opb,0,1); - - oggpack_write(opb,0,2); /* 2,3:reserved */ - - /* we don't write the channel submappings if we only have one... */ - if(info->submaps>1){ - for(i=0;ichannels;i++) - oggpack_write(opb,info->chmuxlist[i],4); - } - for(i=0;isubmaps;i++){ - oggpack_write(opb,0,8); /* time submap unused */ - oggpack_write(opb,info->floorsubmap[i],8); - oggpack_write(opb,info->residuesubmap[i],8); - } -} - -/* also responsible for range checking */ -static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){ - int i,b; - vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)_ogg_calloc(1,sizeof(*info)); - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - if(vi->channels<=0)goto err_out; - - b=oggpack_read(opb,1); - if(b<0)goto err_out; - if(b){ - info->submaps=oggpack_read(opb,4)+1; - if(info->submaps<=0)goto err_out; - }else - info->submaps=1; - - b=oggpack_read(opb,1); - if(b<0)goto err_out; - if(b){ - info->coupling_steps=oggpack_read(opb,8)+1; - if(info->coupling_steps<=0)goto err_out; - for(i=0;icoupling_steps;i++){ - /* vi->channels > 0 is enforced in the caller */ - int testM=info->coupling_mag[i]= - oggpack_read(opb,ov_ilog(vi->channels-1)); - int testA=info->coupling_ang[i]= - oggpack_read(opb,ov_ilog(vi->channels-1)); - - if(testM<0 || - testA<0 || - testM==testA || - testM>=vi->channels || - testA>=vi->channels) goto err_out; - } - - } - - if(oggpack_read(opb,2)!=0)goto err_out; /* 2,3:reserved */ - - if(info->submaps>1){ - for(i=0;ichannels;i++){ - info->chmuxlist[i]=oggpack_read(opb,4); - if(info->chmuxlist[i]>=info->submaps || info->chmuxlist[i]<0)goto err_out; - } - } - for(i=0;isubmaps;i++){ - oggpack_read(opb,8); /* time submap unused */ - info->floorsubmap[i]=oggpack_read(opb,8); - if(info->floorsubmap[i]>=ci->floors || info->floorsubmap[i]<0)goto err_out; - info->residuesubmap[i]=oggpack_read(opb,8); - if(info->residuesubmap[i]>=ci->residues || info->residuesubmap[i]<0)goto err_out; - } - - return info; - - err_out: - mapping0_free_info(info); - return(NULL); -} - -/*#include "os.h"*/ -/*#include "lpc.h"*/ -/*#include "lsp.h"*/ -/*#include "envelope.h"*/ -/*#include "mdct.h"*/ -/*#include "psy.h"*/ -/*#include "scales.h"*/ - -#if 0 -static long seq=0; -static ogg_int64_t total=0; -static float FLOOR1_fromdB_LOOKUP[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.128753e-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.F, -}; - -#endif - - -static int mapping0_forward(vorbis_block *vb){ - vorbis_dsp_state *vd=vb->vd; - vorbis_info *vi=vd->vi; - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - private_state *b=(private_state *)vb->vd->backend_state; - vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal; - int n=vb->pcmend; - int i,j,k; - - int *nonzero = (int *)alloca(sizeof(*nonzero)*vi->channels); - float **gmdct = (float **)_vorbis_block_alloc(vb,vi->channels*sizeof(*gmdct)); - int **iwork = (int **)_vorbis_block_alloc(vb,vi->channels*sizeof(*iwork)); - int ***floor_posts = (int ***)_vorbis_block_alloc(vb,vi->channels*sizeof(*floor_posts)); - - float global_ampmax=vbi->ampmax; - float *local_ampmax=(float *)alloca(sizeof(*local_ampmax)*vi->channels); - int blocktype=vbi->blocktype; - - int modenumber=vb->W; - vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)ci->map_param[modenumber]; - vorbis_look_psy *psy_look=b->psy+blocktype+(vb->W?2:0); - - vb->mode=modenumber; - - for(i=0;ichannels;i++){ - float scale=4.f/n; - float scale_dB; - - float *pcm =vb->pcm[i]; - float *logfft =pcm; - - iwork[i]=(int *)_vorbis_block_alloc(vb,n/2*sizeof(**iwork)); - gmdct[i]=(float *)_vorbis_block_alloc(vb,n/2*sizeof(**gmdct)); - - scale_dB=todB(&scale) + .345; /* + .345 is a hack; the original - todB estimation used on IEEE 754 - compliant machines had a bug that - returned dB values about a third - of a decibel too high. The bug - was harmless because tunings - implicitly took that into - account. However, fixing the bug - in the estimator requires - changing all the tunings as well. - For now, it's easier to sync - things back up here, and - recalibrate the tunings in the - next major model upgrade. */ - -#if 0 - if(vi->channels==2){ - if(i==0) - _analysis_output("pcmL",seq,pcm,n,0,0,total-n/2); - else - _analysis_output("pcmR",seq,pcm,n,0,0,total-n/2); - }else{ - _analysis_output("pcm",seq,pcm,n,0,0,total-n/2); - } -#endif - - /* window the PCM data */ - _vorbis_apply_window(pcm,b->window,ci->blocksizes,vb->lW,vb->W,vb->nW); - -#if 0 - if(vi->channels==2){ - if(i==0) - _analysis_output("windowedL",seq,pcm,n,0,0,total-n/2); - else - _analysis_output("windowedR",seq,pcm,n,0,0,total-n/2); - }else{ - _analysis_output("windowed",seq,pcm,n,0,0,total-n/2); - } -#endif - - /* transform the PCM data */ - /* only MDCT right now.... */ - mdct_forward((mdct_lookup *)b->transform[vb->W][0],pcm,gmdct[i]); - - /* FFT yields more accurate tonal estimation (not phase sensitive) */ - drft_forward(&b->fft_look[vb->W],pcm); - logfft[0]=scale_dB+todB(pcm) + .345; /* + .345 is a hack; the - original todB estimation used on - IEEE 754 compliant machines had a - bug that returned dB values about - a third of a decibel too high. - The bug was harmless because - tunings implicitly took that into - account. However, fixing the bug - in the estimator requires - changing all the tunings as well. - For now, it's easier to sync - things back up here, and - recalibrate the tunings in the - next major model upgrade. */ - local_ampmax[i]=logfft[0]; - for(j=1;j>1]=scale_dB+.5f*todB(&temp) + .345; /* + - .345 is a hack; the original todB - estimation used on IEEE 754 - compliant machines had a bug that - returned dB values about a third - of a decibel too high. The bug - was harmless because tunings - implicitly took that into - account. However, fixing the bug - in the estimator requires - changing all the tunings as well. - For now, it's easier to sync - things back up here, and - recalibrate the tunings in the - next major model upgrade. */ - if(temp>local_ampmax[i])local_ampmax[i]=temp; - } - - if(local_ampmax[i]>0.f)local_ampmax[i]=0.f; - if(local_ampmax[i]>global_ampmax)global_ampmax=local_ampmax[i]; - -#if 0 - if(vi->channels==2){ - if(i==0){ - _analysis_output("fftL",seq,logfft,n/2,1,0,0); - }else{ - _analysis_output("fftR",seq,logfft,n/2,1,0,0); - } - }else{ - _analysis_output("fft",seq,logfft,n/2,1,0,0); - } -#endif - - } - - { - float *noise = (float *)_vorbis_block_alloc(vb,n/2*sizeof(*noise)); - float *tone = (float *)_vorbis_block_alloc(vb,n/2*sizeof(*tone)); - - for(i=0;ichannels;i++){ - /* the encoder setup assumes that all the modes used by any - specific bitrate tweaking use the same floor */ - - int submap=info->chmuxlist[i]; - - /* the following makes things clearer to *me* anyway */ - float *mdct =gmdct[i]; - float *logfft =vb->pcm[i]; - - float *logmdct =logfft+n/2; - float *logmask =logfft; - - vb->mode=modenumber; - - floor_posts[i]=(int **)_vorbis_block_alloc(vb,PACKETBLOBS*sizeof(**floor_posts)); - memset(floor_posts[i],0,sizeof(**floor_posts)*PACKETBLOBS); - - for(j=0;jchannels==2){ - if(i==0) - _analysis_output("mdctL",seq,logmdct,n/2,1,0,0); - else - _analysis_output("mdctR",seq,logmdct,n/2,1,0,0); - }else{ - _analysis_output("mdct",seq,logmdct,n/2,1,0,0); - } -#endif - - /* first step; noise masking. Not only does 'noise masking' - give us curves from which we can decide how much resolution - to give noise parts of the spectrum, it also implicitly hands - us a tonality estimate (the larger the value in the - 'noise_depth' vector, the more tonal that area is) */ - - _vp_noisemask(psy_look, - logmdct, - noise); /* noise does not have by-frequency offset - bias applied yet */ -#if 0 - if(vi->channels==2){ - if(i==0) - _analysis_output("noiseL",seq,noise,n/2,1,0,0); - else - _analysis_output("noiseR",seq,noise,n/2,1,0,0); - }else{ - _analysis_output("noise",seq,noise,n/2,1,0,0); - } -#endif - - /* second step: 'all the other crap'; all the stuff that isn't - computed/fit for bitrate management goes in the second psy - vector. This includes tone masking, peak limiting and ATH */ - - _vp_tonemask(psy_look, - logfft, - tone, - global_ampmax, - local_ampmax[i]); - -#if 0 - if(vi->channels==2){ - if(i==0) - _analysis_output("toneL",seq,tone,n/2,1,0,0); - else - _analysis_output("toneR",seq,tone,n/2,1,0,0); - }else{ - _analysis_output("tone",seq,tone,n/2,1,0,0); - } -#endif - - /* third step; we offset the noise vectors, overlay tone - masking. We then do a floor1-specific line fit. If we're - performing bitrate management, the line fit is performed - multiple times for up/down tweakage on demand. */ - -#if 0 - { - float aotuv[psy_look->n]; -#endif - - _vp_offset_and_mix(psy_look, - noise, - tone, - 1, - logmask, - mdct, - logmdct); - -#if 0 - if(vi->channels==2){ - if(i==0) - _analysis_output("aotuvM1_L",seq,aotuv,psy_look->n,1,1,0); - else - _analysis_output("aotuvM1_R",seq,aotuv,psy_look->n,1,1,0); - }else{ - _analysis_output("aotuvM1",seq,aotuv,psy_look->n,1,1,0); - } - } -#endif - - -#if 0 - if(vi->channels==2){ - if(i==0) - _analysis_output("mask1L",seq,logmask,n/2,1,0,0); - else - _analysis_output("mask1R",seq,logmask,n/2,1,0,0); - }else{ - _analysis_output("mask1",seq,logmask,n/2,1,0,0); - } -#endif - - /* this algorithm is hardwired to floor 1 for now; abort out if - we're *not* floor1. This won't happen unless someone has - broken the encode setup lib. Guard it anyway. */ - if(ci->floor_type[info->floorsubmap[submap]]!=1)return(-1); - - floor_posts[i][PACKETBLOBS/2]= - floor1_fit(vb,(vorbis_look_floor1 *)b->flr[info->floorsubmap[submap]], - logmdct, - logmask); - - /* are we managing bitrate? If so, perform two more fits for - later rate tweaking (fits represent hi/lo) */ - if(vorbis_bitrate_managed(vb) && floor_posts[i][PACKETBLOBS/2]){ - /* higher rate by way of lower noise curve */ - - _vp_offset_and_mix(psy_look, - noise, - tone, - 2, - logmask, - mdct, - logmdct); - -#if 0 - if(vi->channels==2){ - if(i==0) - _analysis_output("mask2L",seq,logmask,n/2,1,0,0); - else - _analysis_output("mask2R",seq,logmask,n/2,1,0,0); - }else{ - _analysis_output("mask2",seq,logmask,n/2,1,0,0); - } -#endif - - floor_posts[i][PACKETBLOBS-1]= - floor1_fit(vb,(vorbis_look_floor1 *)b->flr[info->floorsubmap[submap]], - logmdct, - logmask); - - /* lower rate by way of higher noise curve */ - _vp_offset_and_mix(psy_look, - noise, - tone, - 0, - logmask, - mdct, - logmdct); - -#if 0 - if(vi->channels==2){ - if(i==0) - _analysis_output("mask0L",seq,logmask,n/2,1,0,0); - else - _analysis_output("mask0R",seq,logmask,n/2,1,0,0); - }else{ - _analysis_output("mask0",seq,logmask,n/2,1,0,0); - } -#endif - - floor_posts[i][0]= - floor1_fit(vb,(vorbis_look_floor1 *)b->flr[info->floorsubmap[submap]], - logmdct, - logmask); - - /* we also interpolate a range of intermediate curves for - intermediate rates */ - for(k=1;kflr[info->floorsubmap[submap]], - floor_posts[i][0], - floor_posts[i][PACKETBLOBS/2], - k*65536/(PACKETBLOBS/2)); - for(k=PACKETBLOBS/2+1;kflr[info->floorsubmap[submap]], - floor_posts[i][PACKETBLOBS/2], - floor_posts[i][PACKETBLOBS-1], - (k-PACKETBLOBS/2)*65536/(PACKETBLOBS/2)); - } - } - } - vbi->ampmax=global_ampmax; - - /* - the next phases are performed once for vbr-only and PACKETBLOB - times for bitrate managed modes. - - 1) encode actual mode being used - 2) encode the floor for each channel, compute coded mask curve/res - 3) normalize and couple. - 4) encode residue - 5) save packet bytes to the packetblob vector - - */ - - /* iterate over the many masking curve fits we've created */ - - { - int **couple_bundle=(int **)alloca(sizeof(*couple_bundle)*vi->channels); - int *zerobundle=(int *)alloca(sizeof(*zerobundle)*vi->channels); - - for(k=(vorbis_bitrate_managed(vb)?0:PACKETBLOBS/2); - k<=(vorbis_bitrate_managed(vb)?PACKETBLOBS-1:PACKETBLOBS/2); - k++){ - oggpack_buffer *opb=vbi->packetblob[k]; - - /* start out our new packet blob with packet type and mode */ - /* Encode the packet type */ - oggpack_write(opb,0,1); - /* Encode the modenumber */ - /* Encode frame mode, pre,post windowsize, then dispatch */ - oggpack_write(opb,modenumber,b->modebits); - if(vb->W){ - oggpack_write(opb,vb->lW,1); - oggpack_write(opb,vb->nW,1); - } - - /* encode floor, compute masking curve, sep out residue */ - for(i=0;ichannels;i++){ - int submap=info->chmuxlist[i]; - int *ilogmask=iwork[i]; - - nonzero[i]=floor1_encode(opb,vb,(vorbis_look_floor1 *)b->flr[info->floorsubmap[submap]], - floor_posts[i][k], - ilogmask); -#if 0 - { - char buf[80]; - sprintf(buf,"maskI%c%d",i?'R':'L',k); - float work[n/2]; - for(j=0;jpsy_g_param, - psy_look, - info, - gmdct, - iwork, - nonzero, - ci->psy_g_param.sliding_lowpass[vb->W][k], - vi->channels); - -#if 0 - for(i=0;ichannels;i++){ - char buf[80]; - sprintf(buf,"res%c%d",i?'R':'L',k); - float work[n/2]; - for(j=0;jsubmaps;i++){ - int ch_in_bundle=0; - long **classifications; - int resnum=info->residuesubmap[i]; - - for(j=0;jchannels;j++){ - if(info->chmuxlist[j]==i){ - zerobundle[ch_in_bundle]=0; - if(nonzero[j])zerobundle[ch_in_bundle]=1; - couple_bundle[ch_in_bundle++]=iwork[j]; - } - } - - classifications=_residue_P[ci->residue_type[resnum]]-> - class_l(vb,b->residue[resnum],couple_bundle,zerobundle,ch_in_bundle); - - ch_in_bundle=0; - for(j=0;jchannels;j++) - if(info->chmuxlist[j]==i) - couple_bundle[ch_in_bundle++]=iwork[j]; - - _residue_P[ci->residue_type[resnum]]-> - forward(opb,vb,b->residue[resnum], - couple_bundle,zerobundle,ch_in_bundle,classifications,i); - } - - /* ok, done encoding. Next protopacket. */ - } - - } - -#if 0 - seq++; - total+=ci->blocksizes[vb->W]/4+ci->blocksizes[vb->nW]/4; -#endif - return(0); -} - -static int mapping0_inverse(vorbis_block *vb,vorbis_info_mapping *l){ - vorbis_dsp_state *vd=vb->vd; - vorbis_info *vi=vd->vi; - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - private_state *b=(private_state *)vd->backend_state; - vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)l; - - int i,j; - long n=vb->pcmend=ci->blocksizes[vb->W]; - - float **pcmbundle=(float **)alloca(sizeof(*pcmbundle)*vi->channels); - int *zerobundle=(int *)alloca(sizeof(*zerobundle)*vi->channels); - - int *nonzero =(int *)alloca(sizeof(*nonzero)*vi->channels); - void **floormemo=(void **)alloca(sizeof(*floormemo)*vi->channels); - - /* recover the spectral envelope; store it in the PCM vector for now */ - for(i=0;ichannels;i++){ - int submap=info->chmuxlist[i]; - floormemo[i]=_floor_P[ci->floor_type[info->floorsubmap[submap]]]-> - inverse1(vb,b->flr[info->floorsubmap[submap]]); - if(floormemo[i]) - nonzero[i]=1; - else - nonzero[i]=0; - memset(vb->pcm[i],0,sizeof(*vb->pcm[i])*n/2); - } - - /* channel coupling can 'dirty' the nonzero listing */ - for(i=0;icoupling_steps;i++){ - if(nonzero[info->coupling_mag[i]] || - nonzero[info->coupling_ang[i]]){ - nonzero[info->coupling_mag[i]]=1; - nonzero[info->coupling_ang[i]]=1; - } - } - - /* recover the residue into our working vectors */ - for(i=0;isubmaps;i++){ - int ch_in_bundle=0; - for(j=0;jchannels;j++){ - if(info->chmuxlist[j]==i){ - if(nonzero[j]) - zerobundle[ch_in_bundle]=1; - else - zerobundle[ch_in_bundle]=0; - pcmbundle[ch_in_bundle++]=vb->pcm[j]; - } - } - - _residue_P[ci->residue_type[info->residuesubmap[i]]]-> - inverse(vb,b->residue[info->residuesubmap[i]], - pcmbundle,zerobundle,ch_in_bundle); - } - - /* channel coupling */ - for(i=info->coupling_steps-1;i>=0;i--){ - float *pcmM=vb->pcm[info->coupling_mag[i]]; - float *pcmA=vb->pcm[info->coupling_ang[i]]; - - for(j=0;j0) - if(ang>0){ - pcmM[j]=mag; - pcmA[j]=mag-ang; - }else{ - pcmA[j]=mag; - pcmM[j]=mag+ang; - } - else - if(ang>0){ - pcmM[j]=mag; - pcmA[j]=mag+ang; - }else{ - pcmA[j]=mag; - pcmM[j]=mag-ang; - } - } - } - - /* compute and apply spectral envelope */ - for(i=0;ichannels;i++){ - float *pcm=vb->pcm[i]; - int submap=info->chmuxlist[i]; - _floor_P[ci->floor_type[info->floorsubmap[submap]]]-> - inverse2(vb,b->flr[info->floorsubmap[submap]], - floormemo[i],pcm); - } - - /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */ - /* only MDCT right now.... */ - for(i=0;ichannels;i++){ - float *pcm=vb->pcm[i]; - mdct_backward((mdct_lookup *)b->transform[vb->W][0],pcm,pcm); - } - - /* all done! */ - return(0); -} - -/* export hooks */ -const vorbis_func_mapping mapping0_exportbundle={ - &mapping0_pack, - &mapping0_unpack, - &mapping0_free_info, - &mapping0_forward, - &mapping0_inverse -}; -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: registry for time, floor, res backends and channel mappings - - ********************************************************************/ - -/*#include "vorbis/codec.h"*/ -/*#include "codec_internal.h"*/ -/*#include "registry.h"*/ -/*#include "misc.h"*/ -/* seems like major overkill now; the backend numbers will grow into - the infrastructure soon enough */ - -extern const vorbis_func_floor floor0_exportbundle; -extern const vorbis_func_floor floor1_exportbundle; -extern const vorbis_func_residue residue0_exportbundle; -extern const vorbis_func_residue residue1_exportbundle; -extern const vorbis_func_residue residue2_exportbundle; -extern const vorbis_func_mapping mapping0_exportbundle; - -const vorbis_func_floor *const _floor_P[]={ - &floor0_exportbundle, - &floor1_exportbundle, -}; - -const vorbis_func_residue *const _residue_P[]={ - &residue0_exportbundle, - &residue1_exportbundle, - &residue2_exportbundle, -}; - -const vorbis_func_mapping *const _mapping_P[]={ - &mapping0_exportbundle, -}; -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: basic codebook pack/unpack/code/decode operations - - ********************************************************************/ - -#include -#include -#include -/*#include */ -/*#include "vorbis/codec.h"*/ -/*#include "codebook.h"*/ -/*#include "scales.h"*/ -/*#include "misc.h"*/ -/*#include "os.h"*/ - -/* packs the given codebook into the bitstream **************************/ - -int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *opb){ - long i,j; - int ordered=0; - - /* first the basic parameters */ - oggpack_write(opb,0x564342,24); - oggpack_write(opb,c->dim,16); - oggpack_write(opb,c->entries,24); - - /* pack the codewords. There are two packings; length ordered and - length random. Decide between the two now. */ - - for(i=1;ientries;i++) - if(c->lengthlist[i-1]==0 || c->lengthlist[i]lengthlist[i-1])break; - if(i==c->entries)ordered=1; - - if(ordered){ - /* length ordered. We only need to say how many codewords of - each length. The actual codewords are generated - deterministically */ - - long count=0; - oggpack_write(opb,1,1); /* ordered */ - oggpack_write(opb,c->lengthlist[0]-1,5); /* 1 to 32 */ - - for(i=1;ientries;i++){ - char this_c=c->lengthlist[i]; - char last=c->lengthlist[i-1]; - if(this_c>last){ - for(j=last;jentries-count)); - count=i; - } - } - } - oggpack_write(opb,i-count,ov_ilog(c->entries-count)); - - }else{ - /* length random. Again, we don't code the codeword itself, just - the length. This time, though, we have to encode each length */ - oggpack_write(opb,0,1); /* unordered */ - - /* algortihmic mapping has use for 'unused entries', which we tag - here. The algorithmic mapping happens as usual, but the unused - entry has no codeword. */ - for(i=0;ientries;i++) - if(c->lengthlist[i]==0)break; - - if(i==c->entries){ - oggpack_write(opb,0,1); /* no unused entries */ - for(i=0;ientries;i++) - oggpack_write(opb,c->lengthlist[i]-1,5); - }else{ - oggpack_write(opb,1,1); /* we have unused entries; thus we tag */ - for(i=0;ientries;i++){ - if(c->lengthlist[i]==0){ - oggpack_write(opb,0,1); - }else{ - oggpack_write(opb,1,1); - oggpack_write(opb,c->lengthlist[i]-1,5); - } - } - } - } - - /* is the entry number the desired return value, or do we have a - mapping? If we have a mapping, what type? */ - oggpack_write(opb,c->maptype,4); - switch(c->maptype){ - case 0: - /* no mapping */ - break; - case 1:case 2: - /* implicitly populated value mapping */ - /* explicitly populated value mapping */ - - if(!c->quantlist){ - /* no quantlist? error */ - return(-1); - } - - /* values that define the dequantization */ - oggpack_write(opb,c->q_min,32); - oggpack_write(opb,c->q_delta,32); - oggpack_write(opb,c->q_quant-1,4); - oggpack_write(opb,c->q_sequencep,1); - - { - int quantvals; - switch(c->maptype){ - case 1: - /* a single column of (c->entries/c->dim) quantized values for - building a full value list algorithmically (square lattice) */ - quantvals=_book_maptype1_quantvals(c); - break; - case 2: - /* every value (c->entries*c->dim total) specified explicitly */ - quantvals=c->entries*c->dim; - break; - default: /* NOT_REACHABLE */ - quantvals=-1; - } - - /* quantized values */ - for(i=0;iquantlist[i]),c->q_quant); - - } - break; - default: - /* error case; we don't have any other map types now */ - return(-1); - } - - return(0); -} - -/* unpacks a codebook from the packet buffer into the codebook struct, - readies the codebook auxiliary structures for decode *************/ -static_codebook *vorbis_staticbook_unpack(oggpack_buffer *opb){ - long i,j; - static_codebook *s=(static_codebook *)_ogg_calloc(1,sizeof(*s)); - s->allocedp=1; - - /* make sure alignment is correct */ - if(oggpack_read(opb,24)!=0x564342)goto _eofout; - - /* first the basic parameters */ - s->dim=oggpack_read(opb,16); - s->entries=oggpack_read(opb,24); - if(s->entries==-1)goto _eofout; - - if(ov_ilog(s->dim)+ov_ilog(s->entries)>24)goto _eofout; - - /* codeword ordering.... length ordered or unordered? */ - switch((int)oggpack_read(opb,1)){ - case 0:{ - long unused; - /* allocated but unused entries? */ - unused=oggpack_read(opb,1); - if((s->entries*(unused?1:5)+7)>>3>opb->storage-oggpack_bytes(opb)) - goto _eofout; - /* unordered */ - s->lengthlist=(char *)_ogg_malloc(sizeof(*s->lengthlist)*s->entries); - - /* allocated but unused entries? */ - if(unused){ - /* yes, unused entries */ - - for(i=0;ientries;i++){ - if(oggpack_read(opb,1)){ - long num=oggpack_read(opb,5); - if(num==-1)goto _eofout; - s->lengthlist[i]=num+1; - }else - s->lengthlist[i]=0; - } - }else{ - /* all entries used; no tagging */ - for(i=0;ientries;i++){ - long num=oggpack_read(opb,5); - if(num==-1)goto _eofout; - s->lengthlist[i]=num+1; - } - } - - break; - } - case 1: - /* ordered */ - { - long length=oggpack_read(opb,5)+1; - if(length==0)goto _eofout; - s->lengthlist=(char *)_ogg_malloc(sizeof(*s->lengthlist)*s->entries); - - for(i=0;ientries;){ - long num=oggpack_read(opb,ov_ilog(s->entries-i)); - if(num==-1)goto _eofout; - if(length>32 || num>s->entries-i || - (num>0 && (num-1)>>(length-1)>1)){ - goto _errout; - } - if(length>32)goto _errout; - for(j=0;jlengthlist[i]=length; - length++; - } - } - break; - default: - /* EOF */ - goto _eofout; - } - - /* Do we have a mapping to unpack? */ - switch((s->maptype=oggpack_read(opb,4))){ - case 0: - /* no mapping */ - break; - case 1: case 2: - /* implicitly populated value mapping */ - /* explicitly populated value mapping */ - - s->q_min=oggpack_read(opb,32); - s->q_delta=oggpack_read(opb,32); - s->q_quant=oggpack_read(opb,4)+1; - s->q_sequencep=oggpack_read(opb,1); - if(s->q_sequencep==-1)goto _eofout; - - { - int quantvals=0; - switch(s->maptype){ - case 1: - quantvals=(s->dim==0?0:_book_maptype1_quantvals(s)); - break; - case 2: - quantvals=s->entries*s->dim; - break; - } - - /* quantized values */ - if(((quantvals*s->q_quant+7)>>3)>opb->storage-oggpack_bytes(opb)) - goto _eofout; - s->quantlist=(long int *)_ogg_malloc(sizeof(*s->quantlist)*quantvals); - for(i=0;iquantlist[i]=oggpack_read(opb,s->q_quant); - - if(quantvals&&s->quantlist[quantvals-1]==-1)goto _eofout; - } - break; - default: - goto _errout; - } - - /* all set */ - return(s); - - _errout: - _eofout: - vorbis_staticbook_destroy(s); - return(NULL); -} - -/* returns the number of bits ************************************************/ -int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b){ - if(a<0 || a>=book->c->entries)return(0); - oggpack_write(b,book->codelist[a],book->c->lengthlist[a]); - return(book->c->lengthlist[a]); -} - -/* the 'eliminate the decode tree' optimization actually requires the - codewords to be MSb first, not LSb. This is an annoying inelegancy - (and one of the first places where carefully thought out design - turned out to be wrong; Vorbis II and future Ogg codecs should go - to an MSb bitpacker), but not actually the huge hit it appears to - be. The first-stage decode table catches most words so that - _bitreverse is not in the main execution path. */ - -static ogg_uint32_t _bitreverse(ogg_uint32_t x){ - x= ((x>>16)&0x0000ffff) | ((x<<16)&0xffff0000); - x= ((x>> 8)&0x00ff00ff) | ((x<< 8)&0xff00ff00); - x= ((x>> 4)&0x0f0f0f0f) | ((x<< 4)&0xf0f0f0f0); - x= ((x>> 2)&0x33333333) | ((x<< 2)&0xcccccccc); - return((x>> 1)&0x55555555) | ((x<< 1)&0xaaaaaaaa); -} - -STIN long decode_packed_entry_number(codebook *book, oggpack_buffer *b){ - int read=book->dec_maxlength; - long lo,hi; - long lok = oggpack_look(b,book->dec_firsttablen); - - if (lok >= 0) { - long entry = book->dec_firsttable[lok]; - if(entry&0x80000000UL){ - lo=(entry>>15)&0x7fff; - hi=book->used_entries-(entry&0x7fff); - }else{ - oggpack_adv(b, book->dec_codelengths[entry-1]); - return(entry-1); - } - }else{ - lo=0; - hi=book->used_entries; - } - - /* Single entry codebooks use a firsttablen of 1 and a - dec_maxlength of 1. If a single-entry codebook gets here (due to - failure to read one bit above), the next look attempt will also - fail and we'll correctly kick out instead of trying to walk the - underformed tree */ - - lok = oggpack_look(b, read); - - while(lok<0 && read>1) - lok = oggpack_look(b, --read); - if(lok<0)return -1; - - /* bisect search for the codeword in the ordered list */ - { - ogg_uint32_t testword=_bitreverse((ogg_uint32_t)lok); - - while(hi-lo>1){ - long p=(hi-lo)>>1; - long test=book->codelist[lo+p]>testword; - lo+=p&(test-1); - hi-=p&(-test); - } - - if(book->dec_codelengths[lo]<=read){ - oggpack_adv(b, book->dec_codelengths[lo]); - return(lo); - } - } - - oggpack_adv(b, read); - - return(-1); -} - -/* Decode side is specced and easier, because we don't need to find - matches using different criteria; we simply read and map. There are - two things we need to do 'depending': - - We may need to support interleave. We don't really, but it's - convenient to do it here rather than rebuild the vector later. - - Cascades may be additive or multiplicitive; this is not inherent in - the codebook, but set in the code using the codebook. Like - interleaving, it's easiest to do it here. - addmul==0 -> declarative (set the value) - addmul==1 -> additive - addmul==2 -> multiplicitive */ - -/* returns the [original, not compacted] entry number or -1 on eof *********/ -long vorbis_book_decode(codebook *book, oggpack_buffer *b){ - if(book->used_entries>0){ - long packed_entry=decode_packed_entry_number(book,b); - if(packed_entry>=0) - return(book->dec_index[packed_entry]); - } - - /* if there's no dec_index, the codebook unpacking isn't collapsed */ - return(-1); -} - -/* returns 0 on OK or -1 on eof *************************************/ -/* decode vector / dim granularity gaurding is done in the upper layer */ -long vorbis_book_decodevs_add(codebook *book,float *a,oggpack_buffer *b,int n){ - if(book->used_entries>0){ - int step=n/book->dim; - long *entry = (long int *)alloca(sizeof(*entry)*step); - float **t = (float **)alloca(sizeof(*t)*step); - int i,j,o; - - for (i = 0; i < step; i++) { - entry[i]=decode_packed_entry_number(book,b); - if(entry[i]==-1)return(-1); - t[i] = book->valuelist+entry[i]*book->dim; - } - for(i=0,o=0;idim;i++,o+=step) - for (j=0;o+jused_entries>0){ - int i,j,entry; - float *t; - - for(i=0;ivaluelist+entry*book->dim; - for(j=0;idim;) - a[i++]+=t[j++]; - } - } - return(0); -} - -/* unlike the others, we guard against n not being an integer number - of internally rather than in the upper layer (called only by - floor0) */ -long vorbis_book_decodev_set(codebook *book,float *a,oggpack_buffer *b,int n){ - if(book->used_entries>0){ - int i,j,entry; - float *t; - - for(i=0;ivaluelist+entry*book->dim; - for (j=0;idim;){ - a[i++]=t[j++]; - } - } - }else{ - int i; - - for(i=0;iused_entries>0){ - int m=(offset+n)/ch; - for(i=offset/ch;ivaluelist+entry*book->dim; - for (j=0;idim;j++){ - a[chptr++][i]+=t[j]; - if(chptr==ch){ - chptr=0; - i++; - } - } - } - } - } - return(0); -} -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: basic shared codebook operations - - ********************************************************************/ - -#include -#include -#include -#include -/*#include */ -/*#include "os.h"*/ -/*#include "misc.h"*/ -/*#include "vorbis/codec.h"*/ -/*#include "codebook.h"*/ -/*#include "scales.h"*/ - -/**** pack/unpack helpers ******************************************/ - -int ov_ilog(ogg_uint32_t v){ - int ret; - for(ret=0;v;ret++)v>>=1; - return ret; -} - -/* 32 bit float (not IEEE; nonnormalized mantissa + - biased exponent) : neeeeeee eeemmmmm mmmmmmmm mmmmmmmm - Why not IEEE? It's just not that important here. */ - -#define VQ_FEXP 10 -#define VQ_FMAN 21 -#define VQ_FEXP_BIAS 768 /* bias toward values smaller than 1. */ - -/* doesn't currently guard under/overflow */ -long _float32_pack(float val){ - int sign=0; - long exp; - long mant; - if(val<0){ - sign=0x80000000; - val= -val; - } - exp= floor(log(val)/log(2.f)+.001); /* +epsilon */ - mant=rint(ldexp(val,(VQ_FMAN-1)-exp)); - exp=(exp+VQ_FEXP_BIAS)<>VQ_FMAN; - if(sign)mant= -mant; - exp=exp-(VQ_FMAN-1)-VQ_FEXP_BIAS; - /* clamp excessive exponent values */ - if (exp>63){ - exp=63; - } - if (exp<-63){ - exp=-63; - } - return(ldexp(mant,exp)); -} - -/* given a list of word lengths, generate a list of codewords. Works - for length ordered or unordered, always assigns the lowest valued - codewords first. Extended to handle unused entries (length 0) */ -ogg_uint32_t *_make_words(char *l,long n,long sparsecount){ - long i,j,count=0; - ogg_uint32_t marker[33]; - ogg_uint32_t *r=(ogg_uint32_t *)_ogg_malloc((sparsecount?sparsecount:n)*sizeof(*r)); - memset(marker,0,sizeof(marker)); - - for(i=0;i0){ - ogg_uint32_t entry=marker[length]; - - /* when we claim a node for an entry, we also claim the nodes - below it (pruning off the imagined tree that may have dangled - from it) as well as blocking the use of any nodes directly - above for leaves */ - - /* update ourself */ - if(length<32 && (entry>>length)){ - /* error condition; the lengths must specify an overpopulated tree */ - _ogg_free(r); - return(NULL); - } - r[count++]=entry; - - /* Look to see if the next shorter marker points to the node - above. if so, update it and repeat. */ - { - for(j=length;j>0;j--){ - - if(marker[j]&1){ - /* have to jump branches */ - if(j==1) - marker[1]++; - else - marker[j]=marker[j-1]<<1; - break; /* invariant says next upper marker would already - have been moved if it was on the same path */ - } - marker[j]++; - } - } - - /* prune the tree; the implicit invariant says all the longer - markers were dangling from our just-taken node. Dangle them - from our *new* node. */ - for(j=length+1;j<33;j++) - if((marker[j]>>1) == entry){ - entry=marker[j]; - marker[j]=marker[j-1]<<1; - }else - break; - }else - if(sparsecount==0)count++; - } - - /* any underpopulated tree must be rejected. */ - /* Single-entry codebooks are a retconned extension to the spec. - They have a single codeword '0' of length 1 that results in an - underpopulated tree. Shield that case from the underformed tree check. */ - if(!(count==1 && marker[2]==2)){ - for(i=1;i<33;i++) - if(marker[i] & (0xffffffffUL>>(32-i))){ - _ogg_free(r); - return(NULL); - } - } - - /* bitreverse the words because our bitwise packer/unpacker is LSb - endian */ - for(i=0,count=0;i>j)&1; - } - - if(sparsecount){ - if(l[i]) - r[count++]=temp; - }else - r[count++]=temp; - } - - return(r); -} - -/* there might be a straightforward one-line way to do the below - that's portable and totally safe against roundoff, but I haven't - thought of it. Therefore, we opt on the side of caution */ -long _book_maptype1_quantvals(const static_codebook *b){ - long vals; - if(b->entries<1){ - return(0); - } - vals=floor(pow((float)b->entries,1.f/b->dim)); - - /* the above *should* be reliable, but we'll not assume that FP is - ever reliable when bitstream sync is at stake; verify via integer - means that vals really is the greatest value of dim for which - vals^b->bim <= b->entries */ - /* treat the above as an initial guess */ - if(vals<1){ - vals=1; - } - while(1){ - long acc=1; - long acc1=1; - int i; - for(i=0;idim;i++){ - if(b->entries/vals=b->dim && acc<=b->entries && acc1>b->entries){ - return(vals); - }else{ - if(idim || acc>b->entries){ - vals--; - }else{ - vals++; - } - } - } -} - -/* unpack the quantized list of values for encode/decode ***********/ -/* we need to deal with two map types: in map type 1, the values are - generated algorithmically (each column of the vector counts through - the values in the quant vector). in map type 2, all the values came - in in an explicit list. Both value lists must be unpacked */ -float *_book_unquantize(const static_codebook *b,int n,int *sparsemap){ - long j,k,count=0; - if(b->maptype==1 || b->maptype==2){ - int quantvals; - float mindel=_float32_unpack(b->q_min); - float delta=_float32_unpack(b->q_delta); - float *r=(float *)_ogg_calloc(n*b->dim,sizeof(*r)); - - /* maptype 1 and 2 both use a quantized value vector, but - different sizes */ - switch(b->maptype){ - case 1: - /* most of the time, entries%dimensions == 0, but we need to be - well defined. We define that the possible vales at each - scalar is values == entries/dim. If entries%dim != 0, we'll - have 'too few' values (values*dimentries;j++){ - if((sparsemap && b->lengthlist[j]) || !sparsemap){ - float last=0.f; - int indexdiv=1; - for(k=0;kdim;k++){ - int index= (j/indexdiv)%quantvals; - float val=b->quantlist[index]; - val=fabs(val)*delta+mindel+last; - if(b->q_sequencep)last=val; - if(sparsemap) - r[sparsemap[count]*b->dim+k]=val; - else - r[count*b->dim+k]=val; - indexdiv*=quantvals; - } - count++; - } - - } - break; - case 2: - for(j=0;jentries;j++){ - if((sparsemap && b->lengthlist[j]) || !sparsemap){ - float last=0.f; - - for(k=0;kdim;k++){ - float val=b->quantlist[j*b->dim+k]; - val=fabs(val)*delta+mindel+last; - if(b->q_sequencep)last=val; - if(sparsemap) - r[sparsemap[count]*b->dim+k]=val; - else - r[count*b->dim+k]=val; - } - count++; - } - } - break; - } - - return(r); - } - return(NULL); -} - -void vorbis_staticbook_destroy(static_codebook *b){ - if(b->allocedp){ - if(b->quantlist)_ogg_free(b->quantlist); - if(b->lengthlist)_ogg_free(b->lengthlist); - memset(b,0,sizeof(*b)); - _ogg_free(b); - } /* otherwise, it is in static memory */ -} - -void vorbis_book_clear(codebook *b){ - /* static book is not cleared; we're likely called on the lookup and - the static codebook belongs to the info struct */ - if(b->valuelist)_ogg_free(b->valuelist); - if(b->codelist)_ogg_free(b->codelist); - - if(b->dec_index)_ogg_free(b->dec_index); - if(b->dec_codelengths)_ogg_free(b->dec_codelengths); - if(b->dec_firsttable)_ogg_free(b->dec_firsttable); - - memset(b,0,sizeof(*b)); -} - -int vorbis_book_init_encode(codebook *c,const static_codebook *s){ - - memset(c,0,sizeof(*c)); - c->c=s; - c->entries=s->entries; - c->used_entries=s->entries; - c->dim=s->dim; - c->codelist=_make_words(s->lengthlist,s->entries,0); - /* c->valuelist=_book_unquantize(s,s->entries,NULL); */ - c->quantvals=_book_maptype1_quantvals(s); - c->minval=(int)rint(_float32_unpack(s->q_min)); - c->delta=(int)rint(_float32_unpack(s->q_delta)); - - return(0); -} - -static ogg_uint32_t bitreverse(ogg_uint32_t x){ - x= ((x>>16)&0x0000ffffUL) | ((x<<16)&0xffff0000UL); - x= ((x>> 8)&0x00ff00ffUL) | ((x<< 8)&0xff00ff00UL); - x= ((x>> 4)&0x0f0f0f0fUL) | ((x<< 4)&0xf0f0f0f0UL); - x= ((x>> 2)&0x33333333UL) | ((x<< 2)&0xccccccccUL); - return((x>> 1)&0x55555555UL) | ((x<< 1)&0xaaaaaaaaUL); -} - -static int sort32a(const void *a,const void *b){ - return ( **(ogg_uint32_t **)a>**(ogg_uint32_t **)b)- - ( **(ogg_uint32_t **)a<**(ogg_uint32_t **)b); -} - -/* decode codebook arrangement is more heavily optimized than encode */ -int vorbis_book_init_decode(codebook *c,const static_codebook *s){ - int i,j,n=0,tabn; - int *sortindex; - - memset(c,0,sizeof(*c)); - - /* count actually used entries and find max length */ - for(i=0;ientries;i++) - if(s->lengthlist[i]>0) - n++; - - c->entries=s->entries; - c->used_entries=n; - c->dim=s->dim; - - if(n>0){ - /* two different remappings go on here. - - First, we collapse the likely sparse codebook down only to - actually represented values/words. This collapsing needs to be - indexed as map-valueless books are used to encode original entry - positions as integers. - - Second, we reorder all vectors, including the entry index above, - by sorted bitreversed codeword to allow treeless decode. */ - - /* perform sort */ - ogg_uint32_t *codes=_make_words(s->lengthlist,s->entries,c->used_entries); - ogg_uint32_t **codep=(ogg_uint32_t **)alloca(sizeof(*codep)*n); - - if(codes==NULL)goto err_out; - - for(i=0;icodelist=(ogg_uint32_t *)_ogg_malloc(n*sizeof(*c->codelist)); - /* the index is a reverse index */ - for(i=0;icodelist[sortindex[i]]=codes[i]; - _ogg_free(codes); - - c->valuelist=_book_unquantize(s,n,sortindex); - c->dec_index=(int *)_ogg_malloc(n*sizeof(*c->dec_index)); - - for(n=0,i=0;ientries;i++) - if(s->lengthlist[i]>0) - c->dec_index[sortindex[n++]]=i; - - c->dec_codelengths=(char *)_ogg_malloc(n*sizeof(*c->dec_codelengths)); - c->dec_maxlength=0; - for(n=0,i=0;ientries;i++) - if(s->lengthlist[i]>0){ - c->dec_codelengths[sortindex[n++]]=s->lengthlist[i]; - if(s->lengthlist[i]>c->dec_maxlength) - c->dec_maxlength=s->lengthlist[i]; - } - - if(n==1 && c->dec_maxlength==1){ - /* special case the 'single entry codebook' with a single bit - fastpath table (that always returns entry 0 )in order to use - unmodified decode paths. */ - c->dec_firsttablen=1; - c->dec_firsttable=(ogg_uint32_t *)_ogg_calloc(2,sizeof(*c->dec_firsttable)); - c->dec_firsttable[0]=c->dec_firsttable[1]=1; - - }else{ - c->dec_firsttablen=ov_ilog(c->used_entries)-4; /* this is magic */ - if(c->dec_firsttablen<5)c->dec_firsttablen=5; - if(c->dec_firsttablen>8)c->dec_firsttablen=8; - - tabn=1<dec_firsttablen; - c->dec_firsttable=(ogg_uint32_t *)_ogg_calloc(tabn,sizeof(*c->dec_firsttable)); - - for(i=0;idec_codelengths[i]<=c->dec_firsttablen){ - ogg_uint32_t orig=bitreverse(c->codelist[i]); - for(j=0;j<(1<<(c->dec_firsttablen-c->dec_codelengths[i]));j++) - c->dec_firsttable[orig|(j<dec_codelengths[i])]=i+1; - } - } - - /* now fill in 'unused' entries in the firsttable with hi/lo search - hints for the non-direct-hits */ - { - ogg_uint32_t mask=0xfffffffeUL<<(31-c->dec_firsttablen); - long lo=0,hi=0; - - for(i=0;idec_firsttablen); - if(c->dec_firsttable[bitreverse(word)]==0){ - while((lo+1)codelist[lo+1]<=word)lo++; - while( hi=(c->codelist[hi]&mask))hi++; - - /* we only actually have 15 bits per hint to play with here. - In order to overflow gracefully (nothing breaks, efficiency - just drops), encode as the difference from the extremes. */ - { - unsigned long loval=lo; - unsigned long hival=n-hi; - - if(loval>0x7fff)loval=0x7fff; - if(hival>0x7fff)hival=0x7fff; - c->dec_firsttable[bitreverse(word)]= - 0x80000000UL | (loval<<15) | hival; - } - } - } - } - } - } - - return(0); - err_out: - vorbis_book_clear(c); - return(-1); -} - -long vorbis_book_codeword(codebook *book,int entry){ - if(book->c) /* only use with encode; decode optimizations are - allowed to break this */ - return book->codelist[entry]; - return -1; -} - -long vorbis_book_codelen(codebook *book,int entry){ - if(book->c) /* only use with encode; decode optimizations are - allowed to break this */ - return book->c->lengthlist[entry]; - return -1; -} - -#ifdef _V_SELFTEST - -/* Unit tests of the dequantizer; this stuff will be OK - cross-platform, I simply want to be sure that special mapping cases - actually work properly; a bug could go unnoticed for a while */ - -#include - -/* cases: - - no mapping - full, explicit mapping - algorithmic mapping - - nonsequential - sequential -*/ - -static long full_quantlist1[]={0,1,2,3, 4,5,6,7, 8,3,6,1}; -static long partial_quantlist1[]={0,7,2}; - -/* no mapping */ -static_codebook test1={ - 4,16, - NULL, - 0, - 0,0,0,0, - NULL, - 0 -}; -static float *test1_result=NULL; - -/* linear, full mapping, nonsequential */ -static_codebook test2={ - 4,3, - NULL, - 2, - -533200896,1611661312,4,0, - full_quantlist1, - 0 -}; -static float test2_result[]={-3,-2,-1,0, 1,2,3,4, 5,0,3,-2}; - -/* linear, full mapping, sequential */ -static_codebook test3={ - 4,3, - NULL, - 2, - -533200896,1611661312,4,1, - full_quantlist1, - 0 -}; -static float test3_result[]={-3,-5,-6,-6, 1,3,6,10, 5,5,8,6}; - -/* linear, algorithmic mapping, nonsequential */ -static_codebook test4={ - 3,27, - NULL, - 1, - -533200896,1611661312,4,0, - partial_quantlist1, - 0 -}; -static float test4_result[]={-3,-3,-3, 4,-3,-3, -1,-3,-3, - -3, 4,-3, 4, 4,-3, -1, 4,-3, - -3,-1,-3, 4,-1,-3, -1,-1,-3, - -3,-3, 4, 4,-3, 4, -1,-3, 4, - -3, 4, 4, 4, 4, 4, -1, 4, 4, - -3,-1, 4, 4,-1, 4, -1,-1, 4, - -3,-3,-1, 4,-3,-1, -1,-3,-1, - -3, 4,-1, 4, 4,-1, -1, 4,-1, - -3,-1,-1, 4,-1,-1, -1,-1,-1}; - -/* linear, algorithmic mapping, sequential */ -static_codebook test5={ - 3,27, - NULL, - 1, - -533200896,1611661312,4,1, - partial_quantlist1, - 0 -}; -static float test5_result[]={-3,-6,-9, 4, 1,-2, -1,-4,-7, - -3, 1,-2, 4, 8, 5, -1, 3, 0, - -3,-4,-7, 4, 3, 0, -1,-2,-5, - -3,-6,-2, 4, 1, 5, -1,-4, 0, - -3, 1, 5, 4, 8,12, -1, 3, 7, - -3,-4, 0, 4, 3, 7, -1,-2, 2, - -3,-6,-7, 4, 1, 0, -1,-4,-5, - -3, 1, 0, 4, 8, 7, -1, 3, 2, - -3,-4,-5, 4, 3, 2, -1,-2,-3}; - -void run_test(static_codebook *b,float *comp){ - float *out=_book_unquantize(b,b->entries,NULL); - int i; - - if(comp){ - if(!out){ - fprintf(stderr,"_book_unquantize incorrectly returned NULL\n"); - exit(1); - } - - for(i=0;ientries*b->dim;i++) - if(fabs(out[i]-comp[i])>.0001){ - fprintf(stderr,"disagreement in unquantized and reference data:\n" - "position %d, %g != %g\n",i,out[i],comp[i]); - exit(1); - } - - }else{ - if(out){ - fprintf(stderr,"_book_unquantize returned a value array: \n" - " correct result should have been NULL\n"); - exit(1); - } - } - free(out); -} - -int main(){ - /* run the nine dequant tests, and compare to the hand-rolled results */ - fprintf(stderr,"Dequant test 1... "); - run_test(&test1,test1_result); - fprintf(stderr,"OK\nDequant test 2... "); - run_test(&test2,test2_result); - fprintf(stderr,"OK\nDequant test 3... "); - run_test(&test3,test3_result); - fprintf(stderr,"OK\nDequant test 4... "); - run_test(&test4,test4_result); - fprintf(stderr,"OK\nDequant test 5... "); - run_test(&test5,test5_result); - fprintf(stderr,"OK\n\n"); - - return(0); -} - -#endif -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: lookup based functions - - ********************************************************************/ - -#include -/*#include "lookup.h"*/ -/*#include "lookup_data.h"*/ -/*#include "os.h"*/ -/*#include "misc.h"*/ - -#ifdef FLOAT_LOOKUP - -/* interpolated lookup based cos function, domain 0 to PI only */ -float vorbis_coslook(float a){ - double d=a*(.31830989*(float)COS_LOOKUP_SZ); - int i=vorbis_ftoi(d-.5); - - return COS_LOOKUP[i]+ (d-i)*(COS_LOOKUP[i+1]-COS_LOOKUP[i]); -} - -/* interpolated 1./sqrt(p) where .5 <= p < 1. */ -float vorbis_invsqlook(float a){ - double d=a*(2.f*(float)INVSQ_LOOKUP_SZ)-(float)INVSQ_LOOKUP_SZ; - int i=vorbis_ftoi(d-.5f); - return INVSQ_LOOKUP[i]+ (d-i)*(INVSQ_LOOKUP[i+1]-INVSQ_LOOKUP[i]); -} - -/* interpolated 1./sqrt(p) where .5 <= p < 1. */ -float vorbis_invsq2explook(int a){ - return INVSQ2EXP_LOOKUP[a-INVSQ2EXP_LOOKUP_MIN]; -} - -#include -/* interpolated lookup based fromdB function, domain -140dB to 0dB only */ -float vorbis_fromdBlook(float a){ - int i=vorbis_ftoi(a*((float)(-(1<=(FROMdB_LOOKUP_SZ<>FROMdB_SHIFT]*FROMdB2_LOOKUP[i&FROMdB2_MASK]); -} - -#endif - -#ifdef INT_LOOKUP -/* interpolated 1./sqrt(p) where .5 <= a < 1. (.100000... to .111111...) in - 16.16 format - - returns in m.8 format */ -long vorbis_invsqlook_i(long a,long e){ - long i=(a&0x7fff)>>(INVSQ_LOOKUP_I_SHIFT-1); - long d=(a&INVSQ_LOOKUP_I_MASK)<<(16-INVSQ_LOOKUP_I_SHIFT); /* 0.16 */ - long val=INVSQ_LOOKUP_I[i]- /* 1.16 */ - (((INVSQ_LOOKUP_I[i]-INVSQ_LOOKUP_I[i+1])* /* 0.16 */ - d)>>16); /* result 1.16 */ - - e+=32; - if(e&1)val=(val*5792)>>13; /* multiply val by 1/sqrt(2) */ - e=(e>>1)-8; - - return(val>>e); -} - -/* interpolated lookup based fromdB function, domain -140dB to 0dB only */ -/* a is in n.12 format */ -float vorbis_fromdBlook_i(long a){ - int i=(-a)>>(12-FROMdB2_SHIFT); - return (i<0)?1.f: - ((i>=(FROMdB_LOOKUP_SZ<>FROMdB_SHIFT]*FROMdB2_LOOKUP[i&FROMdB2_MASK]); -} - -/* interpolated lookup based cos function, domain 0 to PI only */ -/* a is in 0.16 format, where 0==0, 2^^16-1==PI, return 0.14 */ -long vorbis_coslook_i(long a){ - int i=a>>COS_LOOKUP_I_SHIFT; - int d=a&COS_LOOKUP_I_MASK; - return COS_LOOKUP_I[i]- ((d*(COS_LOOKUP_I[i]-COS_LOOKUP_I[i+1]))>> - COS_LOOKUP_I_SHIFT); -} - -#endif -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: bitrate tracking and management - - ********************************************************************/ - -#include -#include -#include -/*#include */ -/*#include "vorbis/codec.h"*/ -/*#include "codec_internal.h"*/ -/*#include "os.h"*/ -/*#include "misc.h"*/ -/*#include "bitrate.h"*/ - -/* compute bitrate tracking setup */ -void vorbis_bitrate_init(vorbis_info *vi,bitrate_manager_state *bm){ - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - bitrate_manager_info *bi=&ci->bi; - - memset(bm,0,sizeof(*bm)); - - if(bi && (bi->reservoir_bits>0)){ - long ratesamples=vi->rate; - int halfsamples=ci->blocksizes[0]>>1; - - bm->short_per_long=ci->blocksizes[1]/ci->blocksizes[0]; - bm->managed=1; - - bm->avg_bitsper= rint(1.*bi->avg_rate*halfsamples/ratesamples); - bm->min_bitsper= rint(1.*bi->min_rate*halfsamples/ratesamples); - bm->max_bitsper= rint(1.*bi->max_rate*halfsamples/ratesamples); - - bm->avgfloat=PACKETBLOBS/2; - - /* not a necessary fix, but one that leads to a more balanced - typical initialization */ - { - long desired_fill=bi->reservoir_bits*bi->reservoir_bias; - bm->minmax_reservoir=desired_fill; - bm->avg_reservoir=desired_fill; - } - - } -} - -void vorbis_bitrate_clear(bitrate_manager_state *bm){ - memset(bm,0,sizeof(*bm)); - return; -} - -int vorbis_bitrate_managed(vorbis_block *vb){ - vorbis_dsp_state *vd=vb->vd; - private_state *b=(private_state *)vd->backend_state; - bitrate_manager_state *bm=&b->bms; - - if(bm && bm->managed)return(1); - return(0); -} - -/* finish taking in the block we just processed */ -int vorbis_bitrate_addblock(vorbis_block *vb){ - vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal; - vorbis_dsp_state *vd=vb->vd; - private_state *b=(private_state *)vd->backend_state; - bitrate_manager_state *bm=&b->bms; - vorbis_info *vi=vd->vi; - codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; - bitrate_manager_info *bi=&ci->bi; - - int choice=rint(bm->avgfloat); - long this_bits=oggpack_bytes(vbi->packetblob[choice])*8; - long min_target_bits=(vb->W?bm->min_bitsper*bm->short_per_long:bm->min_bitsper); - long max_target_bits=(vb->W?bm->max_bitsper*bm->short_per_long:bm->max_bitsper); - int samples=ci->blocksizes[vb->W]>>1; - long desired_fill=bi->reservoir_bits*bi->reservoir_bias; - if(!bm->managed){ - /* not a bitrate managed stream, but for API simplicity, we'll - buffer the packet to keep the code path clean */ - - if(bm->vb)return(-1); /* one has been submitted without - being claimed */ - bm->vb=vb; - return(0); - } - - bm->vb=vb; - - /* look ahead for avg floater */ - if(bm->avg_bitsper>0){ - double slew=0.; - long avg_target_bits=(vb->W?bm->avg_bitsper*bm->short_per_long:bm->avg_bitsper); - double slewlimit= 15./bi->slew_damp; - - /* choosing a new floater: - if we're over target, we slew down - if we're under target, we slew up - - choose slew as follows: look through packetblobs of this frame - and set slew as the first in the appropriate direction that - gives us the slew we want. This may mean no slew if delta is - already favorable. - - Then limit slew to slew max */ - - if(bm->avg_reservoir+(this_bits-avg_target_bits)>desired_fill){ - while(choice>0 && this_bits>avg_target_bits && - bm->avg_reservoir+(this_bits-avg_target_bits)>desired_fill){ - choice--; - this_bits=oggpack_bytes(vbi->packetblob[choice])*8; - } - }else if(bm->avg_reservoir+(this_bits-avg_target_bits)avg_reservoir+(this_bits-avg_target_bits)packetblob[choice])*8; - } - } - - slew=rint(choice-bm->avgfloat)/samples*vi->rate; - if(slew<-slewlimit)slew=-slewlimit; - if(slew>slewlimit)slew=slewlimit; - choice=rint(bm->avgfloat+= slew/vi->rate*samples); - this_bits=oggpack_bytes(vbi->packetblob[choice])*8; - } - - - - /* enforce min(if used) on the current floater (if used) */ - if(bm->min_bitsper>0){ - /* do we need to force the bitrate up? */ - if(this_bitsminmax_reservoir-(min_target_bits-this_bits)<0){ - choice++; - if(choice>=PACKETBLOBS)break; - this_bits=oggpack_bytes(vbi->packetblob[choice])*8; - } - } - } - - /* enforce max (if used) on the current floater (if used) */ - if(bm->max_bitsper>0){ - /* do we need to force the bitrate down? */ - if(this_bits>max_target_bits){ - while(bm->minmax_reservoir+(this_bits-max_target_bits)>bi->reservoir_bits){ - choice--; - if(choice<0)break; - this_bits=oggpack_bytes(vbi->packetblob[choice])*8; - } - } - } - - /* Choice of packetblobs now made based on floater, and min/max - requirements. Now boundary check extreme choices */ - - if(choice<0){ - /* choosing a smaller packetblob is insufficient to trim bitrate. - frame will need to be truncated */ - long maxsize=(max_target_bits+(bi->reservoir_bits-bm->minmax_reservoir))/8; - bm->choice=choice=0; - - if(oggpack_bytes(vbi->packetblob[choice])>maxsize){ - - oggpack_writetrunc(vbi->packetblob[choice],maxsize*8); - this_bits=oggpack_bytes(vbi->packetblob[choice])*8; - } - }else{ - long minsize=(min_target_bits-bm->minmax_reservoir+7)/8; - if(choice>=PACKETBLOBS) - choice=PACKETBLOBS-1; - - bm->choice=choice; - - /* prop up bitrate according to demand. pad this frame out with zeroes */ - minsize-=oggpack_bytes(vbi->packetblob[choice]); - while(minsize-->0)oggpack_write(vbi->packetblob[choice],0,8); - this_bits=oggpack_bytes(vbi->packetblob[choice])*8; - - } - - /* now we have the final packet and the final packet size. Update statistics */ - /* min and max reservoir */ - if(bm->min_bitsper>0 || bm->max_bitsper>0){ - - if(max_target_bits>0 && this_bits>max_target_bits){ - bm->minmax_reservoir+=(this_bits-max_target_bits); - }else if(min_target_bits>0 && this_bitsminmax_reservoir+=(this_bits-min_target_bits); - }else{ - /* inbetween; we want to take reservoir toward but not past desired_fill */ - if(bm->minmax_reservoir>desired_fill){ - if(max_target_bits>0){ /* logical bulletproofing against initialization state */ - bm->minmax_reservoir+=(this_bits-max_target_bits); - if(bm->minmax_reservoirminmax_reservoir=desired_fill; - }else{ - bm->minmax_reservoir=desired_fill; - } - }else{ - if(min_target_bits>0){ /* logical bulletproofing against initialization state */ - bm->minmax_reservoir+=(this_bits-min_target_bits); - if(bm->minmax_reservoir>desired_fill)bm->minmax_reservoir=desired_fill; - }else{ - bm->minmax_reservoir=desired_fill; - } - } - } - } - - /* avg reservoir */ - if(bm->avg_bitsper>0){ - long avg_target_bits=(vb->W?bm->avg_bitsper*bm->short_per_long:bm->avg_bitsper); - bm->avg_reservoir+=this_bits-avg_target_bits; - } - - return(0); -} - -int vorbis_bitrate_flushpacket(vorbis_dsp_state *vd,ogg_packet *op){ - private_state *b=(private_state *)vd->backend_state; - bitrate_manager_state *bm=&b->bms; - vorbis_block *vb=bm->vb; - int choice=PACKETBLOBS/2; - if(!vb)return 0; - - if(op){ - vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal; - - if(vorbis_bitrate_managed(vb)) - choice=bm->choice; - - op->packet=oggpack_get_buffer(vbi->packetblob[choice]); - op->bytes=oggpack_bytes(vbi->packetblob[choice]); - op->b_o_s=0; - op->e_o_s=vb->eofflag; - op->granulepos=vb->granulepos; - op->packetno=vb->sequence; /* for sake of completeness */ - } - - bm->vb=0; - return(1); -} -/******************************************************************** - * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015 * - * by the Xiph.Org Foundation https://xiph.org/ * - * * - ******************************************************************** - - function: stdio-based convenience library for opening/seeking/decoding - - ********************************************************************/ - -#include -#include -#include -#include -#include - -/*#include "vorbis/codec.h"*/ - -/* we don't need or want the static callback symbols here */ -#define OV_EXCLUDE_STATIC_CALLBACKS -/*#include "vorbis/vorbisfile.h"*/ - -/*#include "os.h"*/ -/*#include "misc.h"*/ - -/* A 'chained bitstream' is a Vorbis bitstream that contains more than - one logical bitstream arranged end to end (the only form of Ogg - multiplexing allowed in a Vorbis bitstream; grouping [parallel - multiplexing] is not allowed in Vorbis) */ - -/* A Vorbis file can be played beginning to end (streamed) without - worrying ahead of time about chaining (see decoder_example.c). If - we have the whole file, however, and want random access - (seeking/scrubbing) or desire to know the total length/time of a - file, we need to account for the possibility of chaining. */ - -/* We can handle things a number of ways; we can determine the entire - bitstream structure right off the bat, or find pieces on demand. - This example determines and caches structure for the entire - bitstream, but builds a virtual decoder on the fly when moving - between links in the chain. */ - -/* There are also different ways to implement seeking. Enough - information exists in an Ogg bitstream to seek to - sample-granularity positions in the output. Or, one can seek by - picking some portion of the stream roughly in the desired area if - we only want coarse navigation through the stream. */ - -/************************************************************************* - * Many, many internal helpers. The intention is not to be confusing; - * rampant duplication and monolithic function implementation would be - * harder to understand anyway. The high level functions are last. Begin - * grokking near the end of the file */ - -/* read a little more data from the file/pipe into the ogg_sync framer -*/ -#define CHUNKSIZE 65536 /* greater-than-page-size granularity seeking */ -#define READSIZE 2048 /* a smaller read size is needed for low-rate streaming. */ - -static long _get_data(OggVorbis_File *vf){ - errno=0; - if(!(vf->callbacks.read_func))return(-1); - if(vf->datasource){ - char *buffer=ogg_sync_buffer(&vf->oy,READSIZE); - long bytes=(vf->callbacks.read_func)(buffer,1,READSIZE,vf->datasource); - if(bytes>0)ogg_sync_wrote(&vf->oy,bytes); - if(bytes==0 && errno)return(-1); - return(bytes); - }else - return(0); -} - -/* save a tiny smidge of verbosity to make the code more readable */ -static int _seek_helper(OggVorbis_File *vf,ogg_int64_t offset){ - if(vf->datasource){ - /* only seek if the file position isn't already there */ - if(vf->offset != offset){ - if(!(vf->callbacks.seek_func)|| - (vf->callbacks.seek_func)(vf->datasource, offset, SEEK_SET) == -1) - return OV_EREAD; - vf->offset=offset; - ogg_sync_reset(&vf->oy); - } - }else{ - /* shouldn't happen unless someone writes a broken callback */ - return OV_EFAULT; - } - return 0; -} - -/* The read/seek functions track absolute position within the stream */ - -/* from the head of the stream, get the next page. boundary specifies - if the function is allowed to fetch more data from the stream (and - how much) or only use internally buffered data. - - boundary: -1) unbounded search - 0) read no additional data; use cached only - n) search for a new page beginning for n bytes - - return: <0) did not find a page (OV_FALSE, OV_EOF, OV_EREAD) - n) found a page at absolute offset n */ - -static ogg_int64_t _get_next_page(OggVorbis_File *vf,ogg_page *og, - ogg_int64_t boundary){ - if(boundary>0)boundary+=vf->offset; - while(1){ - long more; - - if(boundary>0 && vf->offset>=boundary)return(OV_FALSE); - more=ogg_sync_pageseek(&vf->oy,og); - - if(more<0){ - /* skipped n bytes */ - vf->offset-=more; - }else{ - if(more==0){ - /* send more paramedics */ - if(!boundary)return(OV_FALSE); - { - long ret=_get_data(vf); - if(ret==0)return(OV_EOF); - if(ret<0)return(OV_EREAD); - } - }else{ - /* got a page. Return the offset at the page beginning, - advance the internal offset past the page end */ - ogg_int64_t ret=vf->offset; - vf->offset+=more; - return(ret); - - } - } - } -} - -/* find the latest page beginning before the passed in position. Much - dirtier than the above as Ogg doesn't have any backward search - linkage. no 'readp' as it will certainly have to read. */ -/* returns offset or OV_EREAD, OV_FAULT */ -static ogg_int64_t _get_prev_page(OggVorbis_File *vf,ogg_int64_t begin,ogg_page *og){ - ogg_int64_t end = begin; - ogg_int64_t ret; - ogg_int64_t offset=-1; - - while(offset==-1){ - begin-=CHUNKSIZE; - if(begin<0) - begin=0; - - ret=_seek_helper(vf,begin); - if(ret)return(ret); - - while(vf->offsetoffset); - if(ret==OV_EREAD)return(OV_EREAD); - if(ret<0){ - break; - }else{ - offset=ret; - } - } - } - - /* In a fully compliant, non-multiplexed stream, we'll still be - holding the last page. In multiplexed (or noncompliant streams), - we will probably have to re-read the last page we saw */ - if(og->header_len==0){ - ret=_seek_helper(vf,offset); - if(ret)return(ret); - - ret=_get_next_page(vf,og,CHUNKSIZE); - if(ret<0) - /* this shouldn't be possible */ - return(OV_EFAULT); - } - - return(offset); -} - -static void _add_serialno(ogg_page *og,long **serialno_list, int *n){ - long s = ogg_page_serialno(og); - (*n)++; - - if(*serialno_list){ - *serialno_list = (long int *)_ogg_realloc(*serialno_list, sizeof(**serialno_list)*(*n)); - }else{ - *serialno_list = (long int *)_ogg_malloc(sizeof(**serialno_list)); - } - - (*serialno_list)[(*n)-1] = s; -} - -/* returns nonzero if found */ -static int _lookup_serialno(long s, long *serialno_list, int n){ - if(serialno_list){ - while(n--){ - if(*serialno_list == s) return 1; - serialno_list++; - } - } - return 0; -} - -static int _lookup_page_serialno(ogg_page *og, long *serialno_list, int n){ - long s = ogg_page_serialno(og); - return _lookup_serialno(s,serialno_list,n); -} - -/* performs the same search as _get_prev_page, but prefers pages of - the specified serial number. If a page of the specified serialno is - spotted during the seek-back-and-read-forward, it will return the - info of last page of the matching serial number instead of the very - last page. If no page of the specified serialno is seen, it will - return the info of last page and alter *serialno. */ -static ogg_int64_t _get_prev_page_serial(OggVorbis_File *vf, ogg_int64_t begin, - long *serial_list, int serial_n, - int *serialno, ogg_int64_t *granpos){ - ogg_page og; - ogg_int64_t end=begin; - ogg_int64_t ret; - - ogg_int64_t prefoffset=-1; - ogg_int64_t offset=-1; - ogg_int64_t ret_serialno=-1; - ogg_int64_t ret_gran=-1; - - while(offset==-1){ - begin-=CHUNKSIZE; - if(begin<0) - begin=0; - - ret=_seek_helper(vf,begin); - if(ret)return(ret); - - while(vf->offsetoffset); - if(ret==OV_EREAD)return(OV_EREAD); - if(ret<0){ - break; - }else{ - ret_serialno=ogg_page_serialno(&og); - ret_gran=ogg_page_granulepos(&og); - offset=ret; - - if(ret_serialno == *serialno){ - prefoffset=ret; - *granpos=ret_gran; - } - - if(!_lookup_serialno(ret_serialno,serial_list,serial_n)){ - /* we fell off the end of the link, which means we seeked - back too far and shouldn't have been looking in that link - to begin with. If we found the preferred serial number, - forget that we saw it. */ - prefoffset=-1; - } - } - } - /*We started from the beginning of the stream and found nothing. - This should be impossible unless the contents of the stream changed out - from under us after we read from it.*/ - if(!begin&&vf->offset<0)return OV_EBADLINK; - } - - /* we're not interested in the page... just the serialno and granpos. */ - if(prefoffset>=0)return(prefoffset); - - *serialno = ret_serialno; - *granpos = ret_gran; - return(offset); - -} - -/* uses the local ogg_stream storage in vf; this is important for - non-streaming input sources */ -static int _fetch_headers(OggVorbis_File *vf,vorbis_info *vi,vorbis_comment *vc, - long **serialno_list, int *serialno_n, - ogg_page *og_ptr){ - ogg_page og; - ogg_packet op; - int i,ret; - int allbos=0; - - if(!og_ptr){ - ogg_int64_t llret=_get_next_page(vf,&og,CHUNKSIZE); - if(llret==OV_EREAD)return(OV_EREAD); - if(llret<0)return(OV_ENOTVORBIS); - og_ptr=&og; - } - - vorbis_info_init(vi); - vorbis_comment_init(vc); - vf->ready_state=OPENED; - - /* extract the serialnos of all BOS pages + the first set of vorbis - headers we see in the link */ - - while(ogg_page_bos(og_ptr)){ - if(serialno_list){ - if(_lookup_page_serialno(og_ptr,*serialno_list,*serialno_n)){ - /* a dupe serialnumber in an initial header packet set == invalid stream */ - if(*serialno_list)_ogg_free(*serialno_list); - *serialno_list=0; - *serialno_n=0; - ret=OV_EBADHEADER; - goto bail_header; - } - - _add_serialno(og_ptr,serialno_list,serialno_n); - } - - if(vf->ready_stateos,ogg_page_serialno(og_ptr)); - ogg_stream_pagein(&vf->os,og_ptr); - - if(ogg_stream_packetout(&vf->os,&op) > 0 && - vorbis_synthesis_idheader(&op)){ - /* vorbis header; continue setup */ - vf->ready_state=STREAMSET; - if((ret=vorbis_synthesis_headerin(vi,vc,&op))){ - ret=OV_EBADHEADER; - goto bail_header; - } - } - } - - /* get next page */ - { - ogg_int64_t llret=_get_next_page(vf,og_ptr,CHUNKSIZE); - if(llret==OV_EREAD){ - ret=OV_EREAD; - goto bail_header; - } - if(llret<0){ - ret=OV_ENOTVORBIS; - goto bail_header; - } - - /* if this page also belongs to our vorbis stream, submit it and break */ - if(vf->ready_state==STREAMSET && - vf->os.serialno == ogg_page_serialno(og_ptr)){ - ogg_stream_pagein(&vf->os,og_ptr); - break; - } - } - } - - if(vf->ready_state!=STREAMSET){ - ret = OV_ENOTVORBIS; - goto bail_header; - } - - while(1){ - - i=0; - while(i<2){ /* get a page loop */ - - while(i<2){ /* get a packet loop */ - - int result=ogg_stream_packetout(&vf->os,&op); - if(result==0)break; - if(result==-1){ - ret=OV_EBADHEADER; - goto bail_header; - } - - if((ret=vorbis_synthesis_headerin(vi,vc,&op))) - goto bail_header; - - i++; - } - - while(i<2){ - if(_get_next_page(vf,og_ptr,CHUNKSIZE)<0){ - ret=OV_EBADHEADER; - goto bail_header; - } - - /* if this page belongs to the correct stream, go parse it */ - if(vf->os.serialno == ogg_page_serialno(og_ptr)){ - ogg_stream_pagein(&vf->os,og_ptr); - break; - } - - /* if we never see the final vorbis headers before the link - ends, abort */ - if(ogg_page_bos(og_ptr)){ - if(allbos){ - ret = OV_EBADHEADER; - goto bail_header; - }else - allbos=1; - } - - /* otherwise, keep looking */ - } - } - - return 0; - } - - bail_header: - vorbis_info_clear(vi); - vorbis_comment_clear(vc); - vf->ready_state=OPENED; - - return ret; -} - -/* Starting from current cursor position, get initial PCM offset of - next page. Consumes the page in the process without decoding - audio, however this is only called during stream parsing upon - seekable open. */ -static ogg_int64_t _initial_pcmoffset(OggVorbis_File *vf, vorbis_info *vi){ - ogg_page og; - ogg_int64_t accumulated=0; - long lastblock=-1; - int result; - int serialno = vf->os.serialno; - - while(1){ - ogg_packet op; - if(_get_next_page(vf,&og,-1)<0) - break; /* should not be possible unless the file is truncated/mangled */ - - if(ogg_page_bos(&og)) break; - if(ogg_page_serialno(&og)!=serialno) continue; - - /* count blocksizes of all frames in the page */ - ogg_stream_pagein(&vf->os,&og); - while((result=ogg_stream_packetout(&vf->os,&op))){ - if(result>0){ /* ignore holes */ - long thisblock=vorbis_packet_blocksize(vi,&op); - if(thisblock>=0){ - if(lastblock!=-1) - accumulated+=(lastblock+thisblock)>>2; - lastblock=thisblock; - } - } - } - - if(ogg_page_granulepos(&og)!=-1){ - /* pcm offset of last packet on the first audio page */ - accumulated= ogg_page_granulepos(&og)-accumulated; - break; - } - } - - /* less than zero? Either a corrupt file or a stream with samples - trimmed off the beginning, a normal occurrence; in both cases set - the offset to zero */ - if(accumulated<0)accumulated=0; - - return accumulated; -} - -/* finds each bitstream link one at a time using a bisection search - (has to begin by knowing the offset of the lb's initial page). - Recurses for each link so it can alloc the link storage after - finding them all, then unroll and fill the cache at the same time */ -static int _bisect_forward_serialno(OggVorbis_File *vf, - ogg_int64_t begin, - ogg_int64_t searched, - ogg_int64_t end, - ogg_int64_t endgran, - int endserial, - long *currentno_list, - int currentnos, - long m){ - ogg_int64_t pcmoffset; - ogg_int64_t dataoffset=searched; - ogg_int64_t endsearched=end; - ogg_int64_t next=end; - ogg_int64_t searchgran=-1; - ogg_page og; - ogg_int64_t ret,last; - int serialno = vf->os.serialno; - - /* invariants: - we have the headers and serialnos for the link beginning at 'begin' - we have the offset and granpos of the last page in the file (potentially - not a page we care about) - */ - - /* Is the last page in our list of current serialnumbers? */ - if(_lookup_serialno(endserial,currentno_list,currentnos)){ - - /* last page is in the starting serialno list, so we've bisected - down to (or just started with) a single link. Now we need to - find the last vorbis page belonging to the first vorbis stream - for this link. */ - searched = end; - while(endserial != serialno){ - endserial = serialno; - searched=_get_prev_page_serial(vf,searched,currentno_list,currentnos,&endserial,&endgran); - } - - vf->links=m+1; - if(vf->offsets)_ogg_free(vf->offsets); - if(vf->serialnos)_ogg_free(vf->serialnos); - if(vf->dataoffsets)_ogg_free(vf->dataoffsets); - - vf->offsets=(ogg_int64_t *)_ogg_malloc((vf->links+1)*sizeof(*vf->offsets)); - vf->vi=(vorbis_info *)_ogg_realloc(vf->vi,vf->links*sizeof(*vf->vi)); - vf->vc=(vorbis_comment *)_ogg_realloc(vf->vc,vf->links*sizeof(*vf->vc)); - vf->serialnos=(long int *)_ogg_malloc(vf->links*sizeof(*vf->serialnos)); - vf->dataoffsets=(ogg_int64_t *)_ogg_malloc(vf->links*sizeof(*vf->dataoffsets)); - vf->pcmlengths=(ogg_int64_t *)_ogg_malloc(vf->links*2*sizeof(*vf->pcmlengths)); - - vf->offsets[m+1]=end; - vf->offsets[m]=begin; - vf->pcmlengths[m*2+1]=(endgran<0?0:endgran); - - }else{ - - /* last page is not in the starting stream's serial number list, - so we have multiple links. Find where the stream that begins - our bisection ends. */ - - long *next_serialno_list=NULL; - int next_serialnos=0; - vorbis_info vi; - vorbis_comment vc; - int testserial = serialno+1; - - /* the below guards against garbage seperating the last and - first pages of two links. */ - while(searched=0)next=last; - }else{ - searched=vf->offset; - } - } - - /* Bisection point found */ - /* for the time being, fetch end PCM offset the simple way */ - searched = next; - while(testserial != serialno){ - testserial = serialno; - searched = _get_prev_page_serial(vf,searched,currentno_list,currentnos,&testserial,&searchgran); - } - - ret=_seek_helper(vf,next); - if(ret)return(ret); - - ret=_fetch_headers(vf,&vi,&vc,&next_serialno_list,&next_serialnos,NULL); - if(ret)return(ret); - serialno = vf->os.serialno; - dataoffset = vf->offset; - - /* this will consume a page, however the next bisection always - starts with a raw seek */ - pcmoffset = _initial_pcmoffset(vf,&vi); - - ret=_bisect_forward_serialno(vf,next,vf->offset,end,endgran,endserial, - next_serialno_list,next_serialnos,m+1); - if(ret)return(ret); - - if(next_serialno_list)_ogg_free(next_serialno_list); - - vf->offsets[m+1]=next; - vf->serialnos[m+1]=serialno; - vf->dataoffsets[m+1]=dataoffset; - - vf->vi[m+1]=vi; - vf->vc[m+1]=vc; - - vf->pcmlengths[m*2+1]=searchgran; - vf->pcmlengths[m*2+2]=pcmoffset; - vf->pcmlengths[m*2+3]-=pcmoffset; - if(vf->pcmlengths[m*2+3]<0)vf->pcmlengths[m*2+3]=0; - } - return(0); -} - -static int _make_decode_ready(OggVorbis_File *vf){ - if(vf->ready_state>STREAMSET)return 0; - if(vf->ready_stateseekable){ - if(vorbis_synthesis_init(&vf->vd,vf->vi+vf->current_link)) - return OV_EBADLINK; - }else{ - if(vorbis_synthesis_init(&vf->vd,vf->vi)) - return OV_EBADLINK; - } - vorbis_block_init(&vf->vd,&vf->vb); - vf->ready_state=INITSET; - vf->bittrack=0.f; - vf->samptrack=0.f; - return 0; -} - -static int _open_seekable2(OggVorbis_File *vf){ - ogg_int64_t dataoffset=vf->dataoffsets[0],end,endgran=-1; - int endserial=vf->os.serialno; - int serialno=vf->os.serialno; - - /* we're partially open and have a first link header state in - storage in vf */ - - /* fetch initial PCM offset */ - ogg_int64_t pcmoffset = _initial_pcmoffset(vf,vf->vi); - - /* we can seek, so set out learning all about this file */ - if(vf->callbacks.seek_func && vf->callbacks.tell_func){ - (vf->callbacks.seek_func)(vf->datasource,0,SEEK_END); - vf->offset=vf->end=(vf->callbacks.tell_func)(vf->datasource); - }else{ - vf->offset=vf->end=-1; - } - - /* If seek_func is implemented, tell_func must also be implemented */ - if(vf->end==-1) return(OV_EINVAL); - - /* Get the offset of the last page of the physical bitstream, or, if - we're lucky the last vorbis page of this link as most OggVorbis - files will contain a single logical bitstream */ - end=_get_prev_page_serial(vf,vf->end,vf->serialnos+2,vf->serialnos[1],&endserial,&endgran); - if(end<0)return(end); - - /* now determine bitstream structure recursively */ - if(_bisect_forward_serialno(vf,0,dataoffset,end,endgran,endserial, - vf->serialnos+2,vf->serialnos[1],0)<0)return(OV_EREAD); - - vf->offsets[0]=0; - vf->serialnos[0]=serialno; - vf->dataoffsets[0]=dataoffset; - vf->pcmlengths[0]=pcmoffset; - vf->pcmlengths[1]-=pcmoffset; - if(vf->pcmlengths[1]<0)vf->pcmlengths[1]=0; - - return(ov_raw_seek(vf,dataoffset)); -} - -/* clear out the current logical bitstream decoder */ -static void _decode_clear(OggVorbis_File *vf){ - vorbis_dsp_clear(&vf->vd); - vorbis_block_clear(&vf->vb); - vf->ready_state=OPENED; -} - -/* fetch and process a packet. Handles the case where we're at a - bitstream boundary and dumps the decoding machine. If the decoding - machine is unloaded, it loads it. It also keeps pcm_offset up to - date (seek and read both use this. seek uses a special hack with - readp). - - return: <0) error, OV_HOLE (lost packet) or OV_EOF - 0) need more data (only if readp==0) - 1) got a packet -*/ - -static int _fetch_and_process_packet(OggVorbis_File *vf, - ogg_packet *op_in, - int readp, - int spanp){ - ogg_page og; - - /* handle one packet. Try to fetch it from current stream state */ - /* extract packets from page */ - while(1){ - - if(vf->ready_state==STREAMSET){ - int ret=_make_decode_ready(vf); - if(ret<0)return ret; - } - - /* process a packet if we can. */ - - if(vf->ready_state==INITSET){ - int hs=vorbis_synthesis_halfrate_p(vf->vi); - - while(1) { - ogg_packet op; - ogg_packet *op_ptr=(op_in?op_in:&op); - int result=ogg_stream_packetout(&vf->os,op_ptr); - ogg_int64_t granulepos; - - op_in=NULL; - if(result==-1)return(OV_HOLE); /* hole in the data. */ - if(result>0){ - /* got a packet. process it */ - granulepos=op_ptr->granulepos; - if(!vorbis_synthesis(&vf->vb,op_ptr)){ /* lazy check for lazy - header handling. The - header packets aren't - audio, so if/when we - submit them, - vorbis_synthesis will - reject them */ - - /* suck in the synthesis data and track bitrate */ - { - int oldsamples=vorbis_synthesis_pcmout(&vf->vd,NULL); - /* for proper use of libvorbis within libvorbisfile, - oldsamples will always be zero. */ - if(oldsamples)return(OV_EFAULT); - - vorbis_synthesis_blockin(&vf->vd,&vf->vb); - vf->samptrack+=(vorbis_synthesis_pcmout(&vf->vd,NULL)<bittrack+=op_ptr->bytes*8; - } - - /* update the pcm offset. */ - if(granulepos!=-1 && !op_ptr->e_o_s){ - int link=(vf->seekable?vf->current_link:0); - int i,samples; - - /* this packet has a pcm_offset on it (the last packet - completed on a page carries the offset) After processing - (above), we know the pcm position of the *last* sample - ready to be returned. Find the offset of the *first* - - As an aside, this trick is inaccurate if we begin - reading anew right at the last page; the end-of-stream - granulepos declares the last frame in the stream, and the - last packet of the last page may be a partial frame. - So, we need a previous granulepos from an in-sequence page - to have a reference point. Thus the !op_ptr->e_o_s clause - above */ - - if(vf->seekable && link>0) - granulepos-=vf->pcmlengths[link*2]; - if(granulepos<0)granulepos=0; /* actually, this - shouldn't be possible - here unless the stream - is very broken */ - - samples=(vorbis_synthesis_pcmout(&vf->vd,NULL)<pcmlengths[i*2+1]; - vf->pcm_offset=granulepos; - } - return(1); - } - } - else - break; - } - } - - if(vf->ready_state>=OPENED){ - ogg_int64_t ret; - - while(1){ - /* the loop is not strictly necessary, but there's no sense in - doing the extra checks of the larger loop for the common - case in a multiplexed bistream where the page is simply - part of a different logical bitstream; keep reading until - we get one with the correct serialno */ - - if(!readp)return(0); - if((ret=_get_next_page(vf,&og,-1))<0){ - return(OV_EOF); /* eof. leave unitialized */ - } - - /* bitrate tracking; add the header's bytes here, the body bytes - are done by packet above */ - vf->bittrack+=og.header_len*8; - - if(vf->ready_state==INITSET){ - if(vf->current_serialno!=ogg_page_serialno(&og)){ - - /* two possibilities: - 1) our decoding just traversed a bitstream boundary - 2) another stream is multiplexed into this logical section */ - - if(ogg_page_bos(&og)){ - /* boundary case */ - if(!spanp) - return(OV_EOF); - - _decode_clear(vf); - - if(!vf->seekable){ - vorbis_info_clear(vf->vi); - vorbis_comment_clear(vf->vc); - } - break; - - }else - continue; /* possibility #2 */ - } - } - - break; - } - } - - /* Do we need to load a new machine before submitting the page? */ - /* This is different in the seekable and non-seekable cases. - - In the seekable case, we already have all the header - information loaded and cached; we just initialize the machine - with it and continue on our merry way. - - In the non-seekable (streaming) case, we'll only be at a - boundary if we just left the previous logical bitstream and - we're now nominally at the header of the next bitstream - */ - - if(vf->ready_state!=INITSET){ - int link; - - if(vf->ready_stateseekable){ - long serialno = ogg_page_serialno(&og); - - /* match the serialno to bitstream section. We use this rather than - offset positions to avoid problems near logical bitstream - boundaries */ - - for(link=0;linklinks;link++) - if(vf->serialnos[link]==serialno)break; - - if(link==vf->links) continue; /* not the desired Vorbis - bitstream section; keep - trying */ - - vf->current_serialno=serialno; - vf->current_link=link; - - ogg_stream_reset_serialno(&vf->os,vf->current_serialno); - vf->ready_state=STREAMSET; - - }else{ - /* we're streaming */ - /* fetch the three header packets, build the info struct */ - - int ret=_fetch_headers(vf,vf->vi,vf->vc,NULL,NULL,&og); - if(ret)return(ret); - vf->current_serialno=vf->os.serialno; - vf->current_link++; - link=0; - } - } - } - - /* the buffered page is the data we want, and we're ready for it; - add it to the stream state */ - ogg_stream_pagein(&vf->os,&og); - - } -} - -/* if, eg, 64 bit stdio is configured by default, this will build with - fseek64 */ -static int _fseek64_wrap(FILE *f,ogg_int64_t off,int whence){ - if(f==NULL)return(-1); - return fseek(f,off,whence); -} - -static int _ov_open1(void *f,OggVorbis_File *vf,const char *initial, - long ibytes, ov_callbacks callbacks){ - int offsettest=((f && callbacks.seek_func)?callbacks.seek_func(f,0,SEEK_CUR):-1); - long *serialno_list=NULL; - int serialno_list_size=0; - int ret; - - memset(vf,0,sizeof(*vf)); - vf->datasource=f; - vf->callbacks = callbacks; - - /* init the framing state */ - ogg_sync_init(&vf->oy); - - /* perhaps some data was previously read into a buffer for testing - against other stream types. Allow initialization from this - previously read data (especially as we may be reading from a - non-seekable stream) */ - if(initial){ - char *buffer=ogg_sync_buffer(&vf->oy,ibytes); - memcpy(buffer,initial,ibytes); - ogg_sync_wrote(&vf->oy,ibytes); - } - - /* can we seek? Stevens suggests the seek test was portable */ - if(offsettest!=-1)vf->seekable=1; - - /* No seeking yet; Set up a 'single' (current) logical bitstream - entry for partial open */ - vf->links=1; - vf->vi=(vorbis_info *)_ogg_calloc(vf->links,sizeof(*vf->vi)); - vf->vc=(vorbis_comment *)_ogg_calloc(vf->links,sizeof(*vf->vc)); - ogg_stream_init(&vf->os,-1); /* fill in the serialno later */ - - /* Fetch all BOS pages, store the vorbis header and all seen serial - numbers, load subsequent vorbis setup headers */ - if((ret=_fetch_headers(vf,vf->vi,vf->vc,&serialno_list,&serialno_list_size,NULL))<0){ - vf->datasource=NULL; - ov_clear(vf); - }else{ - /* serial number list for first link needs to be held somewhere - for second stage of seekable stream open; this saves having to - seek/reread first link's serialnumber data then. */ - vf->serialnos=(long int *)_ogg_calloc(serialno_list_size+2,sizeof(*vf->serialnos)); - vf->serialnos[0]=vf->current_serialno=vf->os.serialno; - vf->serialnos[1]=serialno_list_size; - memcpy(vf->serialnos+2,serialno_list,serialno_list_size*sizeof(*vf->serialnos)); - - vf->offsets=(ogg_int64_t *)_ogg_calloc(1,sizeof(*vf->offsets)); - vf->dataoffsets=(ogg_int64_t *)_ogg_calloc(1,sizeof(*vf->dataoffsets)); - vf->offsets[0]=0; - vf->dataoffsets[0]=vf->offset; - - vf->ready_state=PARTOPEN; - } - if(serialno_list)_ogg_free(serialno_list); - return(ret); -} - -static int _ov_open2(OggVorbis_File *vf){ - if(vf->ready_state != PARTOPEN) return OV_EINVAL; - vf->ready_state=OPENED; - if(vf->seekable){ - int ret=_open_seekable2(vf); - if(ret){ - vf->datasource=NULL; - ov_clear(vf); - } - return(ret); - }else - vf->ready_state=STREAMSET; - - return 0; -} - - -/* clear out the OggVorbis_File struct */ -int ov_clear(OggVorbis_File *vf){ - if(vf){ - vorbis_block_clear(&vf->vb); - vorbis_dsp_clear(&vf->vd); - ogg_stream_clear(&vf->os); - - if(vf->vi && vf->links){ - int i; - for(i=0;ilinks;i++){ - vorbis_info_clear(vf->vi+i); - vorbis_comment_clear(vf->vc+i); - } - _ogg_free(vf->vi); - _ogg_free(vf->vc); - } - if(vf->dataoffsets)_ogg_free(vf->dataoffsets); - if(vf->pcmlengths)_ogg_free(vf->pcmlengths); - if(vf->serialnos)_ogg_free(vf->serialnos); - if(vf->offsets)_ogg_free(vf->offsets); - ogg_sync_clear(&vf->oy); - if(vf->datasource && vf->callbacks.close_func) - (vf->callbacks.close_func)(vf->datasource); - memset(vf,0,sizeof(*vf)); - } -#ifdef DEBUG_LEAKS - _VDBG_dump(); -#endif - return(0); -} - -/* inspects the OggVorbis file and finds/documents all the logical - bitstreams contained in it. Tries to be tolerant of logical - bitstream sections that are truncated/woogie. - - return: -1) error - 0) OK -*/ - -int ov_open_callbacks(void *f,OggVorbis_File *vf, - const char *initial,long ibytes,ov_callbacks callbacks){ - int ret=_ov_open1(f,vf,initial,ibytes,callbacks); - if(ret)return ret; - return _ov_open2(vf); -} - -int ov_open(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes){ - ov_callbacks callbacks = { - (size_t (*)(void *, size_t, size_t, void *)) fread, - (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap, - (int (*)(void *)) fclose, - (long (*)(void *)) ftell - }; - - return ov_open_callbacks((void *)f, vf, initial, ibytes, callbacks); -} - -int ov_fopen(const char *path,OggVorbis_File *vf){ - int ret; - FILE *f = fopen(path,"rb"); - if(!f) return -1; - - ret = ov_open(f,vf,NULL,0); - if(ret) fclose(f); - return ret; -} - - -/* cheap hack for game usage where downsampling is desirable; there's - no need for SRC as we can just do it cheaply in libvorbis. */ - -int ov_halfrate(OggVorbis_File *vf,int flag){ - int i; - if(vf->vi==NULL)return OV_EINVAL; - if(vf->ready_state>STREAMSET){ - /* clear out stream state; dumping the decode machine is needed to - reinit the MDCT lookups. */ - vorbis_dsp_clear(&vf->vd); - vorbis_block_clear(&vf->vb); - vf->ready_state=STREAMSET; - if(vf->pcm_offset>=0){ - ogg_int64_t pos=vf->pcm_offset; - vf->pcm_offset=-1; /* make sure the pos is dumped if unseekable */ - ov_pcm_seek(vf,pos); - } - } - - for(i=0;ilinks;i++){ - if(vorbis_synthesis_halfrate(vf->vi+i,flag)){ - if(flag) ov_halfrate(vf,0); - return OV_EINVAL; - } - } - return 0; -} - -int ov_halfrate_p(OggVorbis_File *vf){ - if(vf->vi==NULL)return OV_EINVAL; - return vorbis_synthesis_halfrate_p(vf->vi); -} - -/* Only partially open the vorbis file; test for Vorbisness, and load - the headers for the first chain. Do not seek (although test for - seekability). Use ov_test_open to finish opening the file, else - ov_clear to close/free it. Same return codes as open. - - Note that vorbisfile does _not_ take ownership of the file if the - call fails; the calling applicaiton is responsible for closing the file - if this call returns an error. */ - -int ov_test_callbacks(void *f,OggVorbis_File *vf, - const char *initial,long ibytes,ov_callbacks callbacks) -{ - return _ov_open1(f,vf,initial,ibytes,callbacks); -} - -int ov_test(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes){ - ov_callbacks callbacks = { - (size_t (*)(void *, size_t, size_t, void *)) fread, - (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap, - (int (*)(void *)) fclose, - (long (*)(void *)) ftell - }; - - return ov_test_callbacks((void *)f, vf, initial, ibytes, callbacks); -} - -int ov_test_open(OggVorbis_File *vf){ - if(vf->ready_state!=PARTOPEN)return(OV_EINVAL); - return _ov_open2(vf); -} - -/* How many logical bitstreams in this physical bitstream? */ -long ov_streams(OggVorbis_File *vf){ - return vf->links; -} - -/* Is the FILE * associated with vf seekable? */ -long ov_seekable(OggVorbis_File *vf){ - return vf->seekable; -} - -/* returns the bitrate for a given logical bitstream or the entire - physical bitstream. If the file is open for random access, it will - find the *actual* average bitrate. If the file is streaming, it - returns the nominal bitrate (if set) else the average of the - upper/lower bounds (if set) else -1 (unset). - - If you want the actual bitrate field settings, get them from the - vorbis_info structs */ - -long ov_bitrate(OggVorbis_File *vf,int i){ - if(vf->ready_state=vf->links)return(OV_EINVAL); - if(!vf->seekable && i!=0)return(ov_bitrate(vf,0)); - if(i<0){ - ogg_int64_t bits=0; - int i; - float br; - for(i=0;ilinks;i++) - bits+=(vf->offsets[i+1]-vf->dataoffsets[i])*8; - /* This once read: return(rint(bits/ov_time_total(vf,-1))); - * gcc 3.x on x86 miscompiled this at optimisation level 2 and above, - * so this is slightly transformed to make it work. - */ - br = bits/ov_time_total(vf,-1); - return(rint(br)); - }else{ - if(vf->seekable){ - /* return the actual bitrate */ - return(rint((vf->offsets[i+1]-vf->dataoffsets[i])*8/ov_time_total(vf,i))); - }else{ - /* return nominal if set */ - if(vf->vi[i].bitrate_nominal>0){ - return vf->vi[i].bitrate_nominal; - }else{ - if(vf->vi[i].bitrate_upper>0){ - if(vf->vi[i].bitrate_lower>0){ - return (vf->vi[i].bitrate_upper+vf->vi[i].bitrate_lower)/2; - }else{ - return vf->vi[i].bitrate_upper; - } - } - return(OV_FALSE); - } - } - } -} - -/* returns the actual bitrate since last call. returns -1 if no - additional data to offer since last call (or at beginning of stream), - EINVAL if stream is only partially open -*/ -long ov_bitrate_instant(OggVorbis_File *vf){ - int link=(vf->seekable?vf->current_link:0); - long ret; - if(vf->ready_statesamptrack==0)return(OV_FALSE); - ret=vf->bittrack/vf->samptrack*vf->vi[link].rate+.5; - vf->bittrack=0.f; - vf->samptrack=0.f; - return(ret); -} - -/* Guess */ -long ov_serialnumber(OggVorbis_File *vf,int i){ - if(i>=vf->links)return(ov_serialnumber(vf,vf->links-1)); - if(!vf->seekable && i>=0)return(ov_serialnumber(vf,-1)); - if(i<0){ - return(vf->current_serialno); - }else{ - return(vf->serialnos[i]); - } -} - -/* returns: total raw (compressed) length of content if i==-1 - raw (compressed) length of that logical bitstream for i==0 to n - OV_EINVAL if the stream is not seekable (we can't know the length) - or if stream is only partially open -*/ -ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i){ - if(vf->ready_stateseekable || i>=vf->links)return(OV_EINVAL); - if(i<0){ - ogg_int64_t acc=0; - int i; - for(i=0;ilinks;i++) - acc+=ov_raw_total(vf,i); - return(acc); - }else{ - return(vf->offsets[i+1]-vf->offsets[i]); - } -} - -/* returns: total PCM length (samples) of content if i==-1 PCM length - (samples) of that logical bitstream for i==0 to n - OV_EINVAL if the stream is not seekable (we can't know the - length) or only partially open -*/ -ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i){ - if(vf->ready_stateseekable || i>=vf->links)return(OV_EINVAL); - if(i<0){ - ogg_int64_t acc=0; - int i; - for(i=0;ilinks;i++) - acc+=ov_pcm_total(vf,i); - return(acc); - }else{ - return(vf->pcmlengths[i*2+1]); - } -} - -/* returns: total seconds of content if i==-1 - seconds in that logical bitstream for i==0 to n - OV_EINVAL if the stream is not seekable (we can't know the - length) or only partially open -*/ -double ov_time_total(OggVorbis_File *vf,int i){ - if(vf->ready_stateseekable || i>=vf->links)return(OV_EINVAL); - if(i<0){ - double acc=0; - int i; - for(i=0;ilinks;i++) - acc+=ov_time_total(vf,i); - return(acc); - }else{ - return((double)(vf->pcmlengths[i*2+1])/vf->vi[i].rate); - } -} - -/* seek to an offset relative to the *compressed* data. This also - scans packets to update the PCM cursor. It will cross a logical - bitstream boundary, but only if it can't get any packets out of the - tail of the bitstream we seek to (so no surprises). - - returns zero on success, nonzero on failure */ - -int ov_raw_seek(OggVorbis_File *vf,ogg_int64_t pos){ - ogg_stream_state work_os; - - if(vf->ready_stateseekable) - return(OV_ENOSEEK); /* don't dump machine if we can't seek */ - - if(pos<0 || pos>vf->end)return(OV_EINVAL); - - /* is the seek position outside our current link [if any]? */ - if(vf->ready_state>=STREAMSET){ - if(posoffsets[vf->current_link] || pos>=vf->offsets[vf->current_link+1]) - _decode_clear(vf); /* clear out stream state */ - } - - /* don't yet clear out decoding machine (if it's initialized), in - the case we're in the same link. Restart the decode lapping, and - let _fetch_and_process_packet deal with a potential bitstream - boundary */ - vf->pcm_offset=-1; - ogg_stream_reset_serialno(&vf->os, - vf->current_serialno); /* must set serialno */ - vorbis_synthesis_restart(&vf->vd); - - if(_seek_helper(vf,pos)) { - /* dump the machine so we're in a known state */ - vf->pcm_offset=-1; - _decode_clear(vf); - return OV_EBADLINK; - } - - /* we need to make sure the pcm_offset is set, but we don't want to - advance the raw cursor past good packets just to get to the first - with a granulepos. That's not equivalent behavior to beginning - decoding as immediately after the seek position as possible. - - So, a hack. We use two stream states; a local scratch state and - the shared vf->os stream state. We use the local state to - scan, and the shared state as a buffer for later decode. - - Unfortuantely, on the last page we still advance to last packet - because the granulepos on the last page is not necessarily on a - packet boundary, and we need to make sure the granpos is - correct. - */ - - { - ogg_page og; - ogg_packet op; - int lastblock=0; - int accblock=0; - int thisblock=0; - int lastflag=0; - int firstflag=0; - ogg_int64_t pagepos=-1; - - ogg_stream_init(&work_os,vf->current_serialno); /* get the memory ready */ - ogg_stream_reset(&work_os); /* eliminate the spurious OV_HOLE - return from not necessarily - starting from the beginning */ - - while(1){ - if(vf->ready_state>=STREAMSET){ - /* snarf/scan a packet if we can */ - int result=ogg_stream_packetout(&work_os,&op); - - if(result>0){ - - if(vf->vi[vf->current_link].codec_setup){ - thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op); - if(thisblock<0){ - ogg_stream_packetout(&vf->os,NULL); - thisblock=0; - }else{ - - /* We can't get a guaranteed correct pcm position out of the - last page in a stream because it might have a 'short' - granpos, which can only be detected in the presence of a - preceding page. However, if the last page is also the first - page, the granpos rules of a first page take precedence. Not - only that, but for first==last, the EOS page must be treated - as if its a normal first page for the stream to open/play. */ - if(lastflag && !firstflag) - ogg_stream_packetout(&vf->os,NULL); - else - if(lastblock)accblock+=(lastblock+thisblock)>>2; - } - - if(op.granulepos!=-1){ - int i,link=vf->current_link; - ogg_int64_t granulepos=op.granulepos-vf->pcmlengths[link*2]; - if(granulepos<0)granulepos=0; - - for(i=0;ipcmlengths[i*2+1]; - vf->pcm_offset=granulepos-accblock; - if(vf->pcm_offset<0)vf->pcm_offset=0; - break; - } - lastblock=thisblock; - continue; - }else - ogg_stream_packetout(&vf->os,NULL); - } - } - - if(!lastblock){ - pagepos=_get_next_page(vf,&og,-1); - if(pagepos<0){ - vf->pcm_offset=ov_pcm_total(vf,-1); - break; - } - }else{ - /* huh? Bogus stream with packets but no granulepos */ - vf->pcm_offset=-1; - break; - } - - /* has our decoding just traversed a bitstream boundary? */ - if(vf->ready_state>=STREAMSET){ - if(vf->current_serialno!=ogg_page_serialno(&og)){ - - /* two possibilities: - 1) our decoding just traversed a bitstream boundary - 2) another stream is multiplexed into this logical section? */ - - if(ogg_page_bos(&og)){ - /* we traversed */ - _decode_clear(vf); /* clear out stream state */ - ogg_stream_clear(&work_os); - } /* else, do nothing; next loop will scoop another page */ - } - } - - if(vf->ready_statelinks;link++) - if(vf->serialnos[link]==serialno)break; - - if(link==vf->links) continue; /* not the desired Vorbis - bitstream section; keep - trying */ - vf->current_link=link; - vf->current_serialno=serialno; - ogg_stream_reset_serialno(&vf->os,serialno); - ogg_stream_reset_serialno(&work_os,serialno); - vf->ready_state=STREAMSET; - firstflag=(pagepos<=vf->dataoffsets[link]); - } - - ogg_stream_pagein(&vf->os,&og); - ogg_stream_pagein(&work_os,&og); - lastflag=ogg_page_eos(&og); - - } - } - - ogg_stream_clear(&work_os); - vf->bittrack=0.f; - vf->samptrack=0.f; - return(0); -} - -/* Page granularity seek (faster than sample granularity because we - don't do the last bit of decode to find a specific sample). - - Seek to the last [granule marked] page preceding the specified pos - location, such that decoding past the returned point will quickly - arrive at the requested position. */ -int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){ - int link=-1; - ogg_int64_t result=0; - ogg_int64_t total=ov_pcm_total(vf,-1); - - if(vf->ready_stateseekable)return(OV_ENOSEEK); - - if(pos<0 || pos>total)return(OV_EINVAL); - - /* which bitstream section does this pcm offset occur in? */ - for(link=vf->links-1;link>=0;link--){ - total-=vf->pcmlengths[link*2+1]; - if(pos>=total)break; - } - - /* Search within the logical bitstream for the page with the highest - pcm_pos preceding pos. If we're looking for a position on the - first page, bisection will halt without finding our position as - it's before the first explicit granulepos fencepost. That case is - handled separately below. - - There is a danger here; missing pages or incorrect frame number - information in the bitstream could make our task impossible. - Account for that (it would be an error condition) */ - - /* new search algorithm originally by HB (Nicholas Vinen) */ - - { - ogg_int64_t end=vf->offsets[link+1]; - ogg_int64_t begin=vf->dataoffsets[link]; - ogg_int64_t begintime = vf->pcmlengths[link*2]; - ogg_int64_t endtime = vf->pcmlengths[link*2+1]+begintime; - ogg_int64_t target=pos-total+begintime; - ogg_int64_t best=-1; - int got_page=0; - - ogg_page og; - - /* if we have only one page, there will be no bisection. Grab the page here */ - if(begin==end){ - result=_seek_helper(vf,begin); - if(result) goto seek_error; - - result=_get_next_page(vf,&og,1); - if(result<0) goto seek_error; - - got_page=1; - } - - /* bisection loop */ - while(beginoffset); - if(result==OV_EREAD) goto seek_error; - if(result<0){ - /* there is no next page! */ - if(bisect<=begin+1) - /* No bisection left to perform. We've either found the - best candidate already or failed. Exit loop. */ - end=begin; - else{ - /* We tried to load a fraction of the last page; back up a - bit and try to get the whole last page */ - if(bisect==0) goto seek_error; - bisect-=CHUNKSIZE; - - /* don't repeat/loop on a read we've already performed */ - if(bisect<=begin)bisect=begin+1; - - /* seek and cntinue bisection */ - result=_seek_helper(vf,bisect); - if(result) goto seek_error; - } - }else{ - ogg_int64_t granulepos; - got_page=1; - - /* got a page. analyze it */ - /* only consider pages from primary vorbis stream */ - if(ogg_page_serialno(&og)!=vf->serialnos[link]) - continue; - - /* only consider pages with the granulepos set */ - granulepos=ogg_page_granulepos(&og); - if(granulepos==-1)continue; - - if(granuleposoffset; /* raw offset of next page */ - begintime=granulepos; - - /* if we're before our target but within a short distance, - don't bisect; read forward */ - if(target-begintime>44100)break; - - bisect=begin; /* *not* begin + 1 as above */ - }else{ - - /* This is one of our pages, but the granpos is - post-target; it is not a bisection return - candidate. (The only way we'd use it is if it's the - first page in the stream; we handle that case later - outside the bisection) */ - if(bisect<=begin+1){ - /* No bisection left to perform. We've either found the - best candidate already or failed. Exit loop. */ - end=begin; - }else{ - if(end==vf->offset){ - /* bisection read to the end; use the known page - boundary (result) to update bisection, back up a - little bit, and try again */ - end=result; - bisect-=CHUNKSIZE; - if(bisect<=begin)bisect=begin+1; - result=_seek_helper(vf,bisect); - if(result) goto seek_error; - }else{ - /* Normal bisection */ - end=bisect; - endtime=granulepos; - break; - } - } - } - } - } - } - - /* Out of bisection: did it 'fail?' */ - if(best == -1){ - - /* Check the 'looking for data in first page' special case; - bisection would 'fail' because our search target was before the - first PCM granule position fencepost. */ - - if(got_page && - begin == vf->dataoffsets[link] && - ogg_page_serialno(&og)==vf->serialnos[link]){ - - /* Yes, this is the beginning-of-stream case. We already have - our page, right at the beginning of PCM data. Set state - and return. */ - - vf->pcm_offset=total; - - if(link!=vf->current_link){ - /* Different link; dump entire decode machine */ - _decode_clear(vf); - - vf->current_link=link; - vf->current_serialno=vf->serialnos[link]; - vf->ready_state=STREAMSET; - - }else{ - vorbis_synthesis_restart(&vf->vd); - } - - ogg_stream_reset_serialno(&vf->os,vf->current_serialno); - ogg_stream_pagein(&vf->os,&og); - - }else - goto seek_error; - - }else{ - - /* Bisection found our page. seek to it, update pcm offset. Easier case than - raw_seek, don't keep packets preceding granulepos. */ - - ogg_page og; - ogg_packet op; - - /* seek */ - result=_seek_helper(vf,best); - vf->pcm_offset=-1; - if(result) goto seek_error; - result=_get_next_page(vf,&og,-1); - if(result<0) goto seek_error; - - if(link!=vf->current_link){ - /* Different link; dump entire decode machine */ - _decode_clear(vf); - - vf->current_link=link; - vf->current_serialno=vf->serialnos[link]; - vf->ready_state=STREAMSET; - - }else{ - vorbis_synthesis_restart(&vf->vd); - } - - ogg_stream_reset_serialno(&vf->os,vf->current_serialno); - ogg_stream_pagein(&vf->os,&og); - - /* pull out all but last packet; the one with granulepos */ - while(1){ - result=ogg_stream_packetpeek(&vf->os,&op); - if(result==0){ - /* No packet returned; we exited the bisection with 'best' - pointing to a page with a granule position, so the packet - finishing this page ('best') originated on a preceding - page. Keep fetching previous pages until we get one with - a granulepos or without the 'continued' flag set. Then - just use raw_seek for simplicity. */ - /* Do not rewind past the beginning of link data; if we do, - it's either a bug or a broken stream */ - result=best; - while(result>vf->dataoffsets[link]){ - result=_get_prev_page(vf,result,&og); - if(result<0) goto seek_error; - if(ogg_page_serialno(&og)==vf->current_serialno && - (ogg_page_granulepos(&og)>-1 || - !ogg_page_continued(&og))){ - return ov_raw_seek(vf,result); - } - } - } - if(result<0){ - result = OV_EBADPACKET; - goto seek_error; - } - if(op.granulepos!=-1){ - vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2]; - if(vf->pcm_offset<0)vf->pcm_offset=0; - vf->pcm_offset+=total; - break; - }else - result=ogg_stream_packetout(&vf->os,NULL); - } - } - } - - /* verify result */ - if(vf->pcm_offset>pos || pos>ov_pcm_total(vf,-1)){ - result=OV_EFAULT; - goto seek_error; - } - vf->bittrack=0.f; - vf->samptrack=0.f; - return(0); - - seek_error: - /* dump machine so we're in a known state */ - vf->pcm_offset=-1; - _decode_clear(vf); - return (int)result; -} - -/* seek to a sample offset relative to the decompressed pcm stream - returns zero on success, nonzero on failure */ - -int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos){ - int thisblock,lastblock=0; - int ret=ov_pcm_seek_page(vf,pos); - if(ret<0)return(ret); - if((ret=_make_decode_ready(vf)))return ret; - - /* discard leading packets we don't need for the lapping of the - position we want; don't decode them */ - - while(1){ - ogg_packet op; - ogg_page og; - - int ret=ogg_stream_packetpeek(&vf->os,&op); - if(ret>0){ - thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op); - if(thisblock<0){ - ogg_stream_packetout(&vf->os,NULL); - continue; /* non audio packet */ - } - if(lastblock)vf->pcm_offset+=(lastblock+thisblock)>>2; - - if(vf->pcm_offset+((thisblock+ - vorbis_info_blocksize(vf->vi,1))>>2)>=pos)break; - - /* remove the packet from packet queue and track its granulepos */ - ogg_stream_packetout(&vf->os,NULL); - vorbis_synthesis_trackonly(&vf->vb,&op); /* set up a vb with - only tracking, no - pcm_decode */ - vorbis_synthesis_blockin(&vf->vd,&vf->vb); - - /* end of logical stream case is hard, especially with exact - length positioning. */ - - if(op.granulepos>-1){ - int i; - /* always believe the stream markers */ - vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2]; - if(vf->pcm_offset<0)vf->pcm_offset=0; - for(i=0;icurrent_link;i++) - vf->pcm_offset+=vf->pcmlengths[i*2+1]; - } - - lastblock=thisblock; - - }else{ - if(ret<0 && ret!=OV_HOLE)break; - - /* suck in a new page */ - if(_get_next_page(vf,&og,-1)<0)break; - if(ogg_page_bos(&og))_decode_clear(vf); - - if(vf->ready_statelinks;link++) - if(vf->serialnos[link]==serialno)break; - if(link==vf->links) continue; - vf->current_link=link; - - vf->ready_state=STREAMSET; - vf->current_serialno=ogg_page_serialno(&og); - ogg_stream_reset_serialno(&vf->os,serialno); - ret=_make_decode_ready(vf); - if(ret)return ret; - lastblock=0; - } - - ogg_stream_pagein(&vf->os,&og); - } - } - - vf->bittrack=0.f; - vf->samptrack=0.f; - /* discard samples until we reach the desired position. Crossing a - logical bitstream boundary with abandon is OK. */ - { - /* note that halfrate could be set differently in each link, but - vorbisfile encoforces all links are set or unset */ - int hs=vorbis_synthesis_halfrate_p(vf->vi); - while(vf->pcm_offset<((pos>>hs)<pcm_offset)>>hs; - long samples=vorbis_synthesis_pcmout(&vf->vd,NULL); - - if(samples>target)samples=target; - vorbis_synthesis_read(&vf->vd,samples); - vf->pcm_offset+=samples<pcm_offset=ov_pcm_total(vf,-1); /* eof */ - } - } - return 0; -} - -/* seek to a playback time relative to the decompressed pcm stream - returns zero on success, nonzero on failure */ -int ov_time_seek(OggVorbis_File *vf,double seconds){ - /* translate time to PCM position and call ov_pcm_seek */ - - int link=-1; - ogg_int64_t pcm_total=0; - double time_total=0.; - - if(vf->ready_stateseekable)return(OV_ENOSEEK); - if(seconds<0)return(OV_EINVAL); - - /* which bitstream section does this time offset occur in? */ - for(link=0;linklinks;link++){ - double addsec = ov_time_total(vf,link); - if(secondspcmlengths[link*2+1]; - } - - if(link==vf->links)return(OV_EINVAL); - - /* enough information to convert time offset to pcm offset */ - { - ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate; - return(ov_pcm_seek(vf,target)); - } -} - -/* page-granularity version of ov_time_seek - returns zero on success, nonzero on failure */ -int ov_time_seek_page(OggVorbis_File *vf,double seconds){ - /* translate time to PCM position and call ov_pcm_seek */ - - int link=-1; - ogg_int64_t pcm_total=0; - double time_total=0.; - - if(vf->ready_stateseekable)return(OV_ENOSEEK); - if(seconds<0)return(OV_EINVAL); - - /* which bitstream section does this time offset occur in? */ - for(link=0;linklinks;link++){ - double addsec = ov_time_total(vf,link); - if(secondspcmlengths[link*2+1]; - } - - if(link==vf->links)return(OV_EINVAL); - - /* enough information to convert time offset to pcm offset */ - { - ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate; - return(ov_pcm_seek_page(vf,target)); - } -} - -/* tell the current stream offset cursor. Note that seek followed by - tell will likely not give the set offset due to caching */ -ogg_int64_t ov_raw_tell(OggVorbis_File *vf){ - if(vf->ready_stateoffset); -} - -/* return PCM offset (sample) of next PCM sample to be read */ -ogg_int64_t ov_pcm_tell(OggVorbis_File *vf){ - if(vf->ready_statepcm_offset); -} - -/* return time offset (seconds) of next PCM sample to be read */ -double ov_time_tell(OggVorbis_File *vf){ - int link=0; - ogg_int64_t pcm_total=0; - double time_total=0.f; - - if(vf->ready_stateseekable){ - pcm_total=ov_pcm_total(vf,-1); - time_total=ov_time_total(vf,-1); - - /* which bitstream section does this time offset occur in? */ - for(link=vf->links-1;link>=0;link--){ - pcm_total-=vf->pcmlengths[link*2+1]; - time_total-=ov_time_total(vf,link); - if(vf->pcm_offset>=pcm_total)break; - } - } - - return((double)time_total+(double)(vf->pcm_offset-pcm_total)/vf->vi[link].rate); -} - -/* link: -1) return the vorbis_info struct for the bitstream section - currently being decoded - 0-n) to request information for a specific bitstream section - - In the case of a non-seekable bitstream, any call returns the - current bitstream. NULL in the case that the machine is not - initialized */ - -vorbis_info *ov_info(OggVorbis_File *vf,int link){ - if(vf->seekable){ - if(link<0) - if(vf->ready_state>=STREAMSET) - return vf->vi+vf->current_link; - else - return vf->vi; - else - if(link>=vf->links) - return NULL; - else - return vf->vi+link; - }else{ - return vf->vi; - } -} - -/* grr, strong typing, grr, no templates/inheritence, grr */ -vorbis_comment *ov_comment(OggVorbis_File *vf,int link){ - if(vf->seekable){ - if(link<0) - if(vf->ready_state>=STREAMSET) - return vf->vc+vf->current_link; - else - return vf->vc; - else - if(link>=vf->links) - return NULL; - else - return vf->vc+link; - }else{ - return vf->vc; - } -} - -static int host_is_big_endian() { - ogg_int32_t pattern = 0xfeedface; /* deadbeef */ - unsigned char *bytewise = (unsigned char *)&pattern; - if (bytewise[0] == 0xfe) return 1; - return 0; -} - -/* up to this point, everything could more or less hide the multiple - logical bitstream nature of chaining from the toplevel application - if the toplevel application didn't particularly care. However, at - the point that we actually read audio back, the multiple-section - nature must surface: Multiple bitstream sections do not necessarily - have to have the same number of channels or sampling rate. - - ov_read returns the sequential logical bitstream number currently - being decoded along with the PCM data in order that the toplevel - application can take action on channel/sample rate changes. This - number will be incremented even for streamed (non-seekable) streams - (for seekable streams, it represents the actual logical bitstream - index within the physical bitstream. Note that the accessor - functions above are aware of this dichotomy). - - ov_read_filter is exactly the same as ov_read except that it processes - the decoded audio data through a filter before packing it into the - requested format. This gives greater accuracy than applying a filter - after the audio has been converted into integral PCM. - - input values: buffer) a buffer to hold packed PCM data for return - length) the byte length requested to be placed into buffer - bigendianp) should the data be packed LSB first (0) or - MSB first (1) - word) word size for output. currently 1 (byte) or - 2 (16 bit short) - - return values: <0) error/hole in data (OV_HOLE), partial open (OV_EINVAL) - 0) EOF - n) number of bytes of PCM actually returned. The - below works on a packet-by-packet basis, so the - return length is not related to the 'length' passed - in, just guaranteed to fit. - - *section) set to the logical bitstream number */ - -long ov_read_filter(OggVorbis_File *vf,char *buffer,int length, - int bigendianp,int word,int sgned,int *bitstream, - void (*filter)(float **pcm,long channels,long samples,void *filter_param),void *filter_param){ - int i,j; - int host_endian = host_is_big_endian(); - int hs; - - float **pcm; - long samples; - - if(vf->ready_stateready_state==INITSET){ - samples=vorbis_synthesis_pcmout(&vf->vd,&pcm); - if(samples)break; - } - - /* suck in another packet */ - { - int ret=_fetch_and_process_packet(vf,NULL,1,1); - if(ret==OV_EOF) - return(0); - if(ret<=0) - return(ret); - } - - } - - if(samples>0){ - - /* yay! proceed to pack data into the byte buffer */ - - long channels=ov_info(vf,-1)->channels; - long bytespersample=word * channels; - vorbis_fpu_control fpu; - - if(channels<1||channels>255)return(OV_EINVAL); - if(samples>length/bytespersample)samples=length/bytespersample; - - if(samples <= 0) - return OV_EINVAL; - - /* Here. */ - if(filter) - filter(pcm,channels,samples,filter_param); - - /* a tight loop to pack each size */ - { - int val; - if(word==1){ - int off=(sgned?0:128); - vorbis_fpu_setround(&fpu); - for(j=0;j127)val=127; - else if(val<-128)val=-128; - *buffer++=val+off; - } - vorbis_fpu_restore(fpu); - }else{ - int off=(sgned?0:32768); - - if(host_endian==bigendianp){ - if(sgned){ - - vorbis_fpu_setround(&fpu); - for(i=0;i32767)val=32767; - else if(val<-32768)val=-32768; - *dest=val; - dest+=channels; - } - } - vorbis_fpu_restore(fpu); - - }else{ - - vorbis_fpu_setround(&fpu); - for(i=0;i32767)val=32767; - else if(val<-32768)val=-32768; - *dest=val+off; - dest+=channels; - } - } - vorbis_fpu_restore(fpu); - - } - }else if(bigendianp){ - - vorbis_fpu_setround(&fpu); - for(j=0;j32767)val=32767; - else if(val<-32768)val=-32768; - val+=off; - *buffer++=(val>>8); - *buffer++=(val&0xff); - } - vorbis_fpu_restore(fpu); - - }else{ - int val; - vorbis_fpu_setround(&fpu); - for(j=0;j32767)val=32767; - else if(val<-32768)val=-32768; - val+=off; - *buffer++=(val&0xff); - *buffer++=(val>>8); - } - vorbis_fpu_restore(fpu); - - } - } - } - - vorbis_synthesis_read(&vf->vd,samples); - hs=vorbis_synthesis_halfrate_p(vf->vi); - vf->pcm_offset+=(samples<current_link; - return(samples*bytespersample); - }else{ - return(samples); - } -} - -long ov_read(OggVorbis_File *vf,char *buffer,int length, - int bigendianp,int word,int sgned,int *bitstream){ - return ov_read_filter(vf, buffer, length, bigendianp, word, sgned, bitstream, NULL, NULL); -} - -/* input values: pcm_channels) a float vector per channel of output - length) the sample length being read by the app - - return values: <0) error/hole in data (OV_HOLE), partial open (OV_EINVAL) - 0) EOF - n) number of samples of PCM actually returned. The - below works on a packet-by-packet basis, so the - return length is not related to the 'length' passed - in, just guaranteed to fit. - - *section) set to the logical bitstream number */ - - - -long ov_read_float(OggVorbis_File *vf,float ***pcm_channels,int length, - int *bitstream){ - - if(vf->ready_stateready_state==INITSET){ - float **pcm; - long samples=vorbis_synthesis_pcmout(&vf->vd,&pcm); - if(samples){ - int hs=vorbis_synthesis_halfrate_p(vf->vi); - if(pcm_channels)*pcm_channels=pcm; - if(samples>length)samples=length; - vorbis_synthesis_read(&vf->vd,samples); - vf->pcm_offset+=samples<current_link; - return samples; - - } - } - - /* suck in another packet */ - { - int ret=_fetch_and_process_packet(vf,NULL,1,1); - if(ret==OV_EOF)return(0); - if(ret<=0)return(ret); - } - - } -} - -extern const float *vorbis_window(vorbis_dsp_state *v,int W); - -static void _ov_splice(float **pcm,float **lappcm, - int n1, int n2, - int ch1, int ch2, - const float *w1, const float *w2){ - int i,j; - const float *w=w1; - int n=n1; - - if(n1>n2){ - n=n2; - w=w2; - } - - /* splice */ - for(j=0;jready_state==INITSET)break; - /* suck in another packet */ - { - int ret=_fetch_and_process_packet(vf,NULL,1,0); - if(ret<0 && ret!=OV_HOLE)return(ret); - } - } - return 0; -} - -/* make sure vf is INITSET and that we have a primed buffer; if - we're crosslapping at a stream section boundary, this also makes - sure we're sanity checking against the right stream information */ -static int _ov_initprime(OggVorbis_File *vf){ - vorbis_dsp_state *vd=&vf->vd; - while(1){ - if(vf->ready_state==INITSET) - if(vorbis_synthesis_pcmout(vd,NULL))break; - - /* suck in another packet */ - { - int ret=_fetch_and_process_packet(vf,NULL,1,0); - if(ret<0 && ret!=OV_HOLE)return(ret); - } - } - return 0; -} - -/* grab enough data for lapping from vf; this may be in the form of - unreturned, already-decoded pcm, remaining PCM we will need to - decode, or synthetic postextrapolation from last packets. */ -static void _ov_getlap(OggVorbis_File *vf,vorbis_info *vi,vorbis_dsp_state *vd, - float **lappcm,int lapsize){ - int lapcount=0,i; - float **pcm; - - /* try first to decode the lapping data */ - while(lapcountlapsize-lapcount)samples=lapsize-lapcount; - for(i=0;ichannels;i++) - memcpy(lappcm[i]+lapcount,pcm[i],sizeof(**pcm)*samples); - lapcount+=samples; - vorbis_synthesis_read(vd,samples); - }else{ - /* suck in another packet */ - int ret=_fetch_and_process_packet(vf,NULL,1,0); /* do *not* span */ - if(ret==OV_EOF)break; - } - } - if(lapcountvd,&pcm); - if(samples==0){ - for(i=0;ichannels;i++) - memset(lappcm[i]+lapcount,0,sizeof(**pcm)*lapsize-lapcount); - lapcount=lapsize; - }else{ - if(samples>lapsize-lapcount)samples=lapsize-lapcount; - for(i=0;ichannels;i++) - memcpy(lappcm[i]+lapcount,pcm[i],sizeof(**pcm)*samples); - lapcount+=samples; - } - } -} - -/* this sets up crosslapping of a sample by using trailing data from - sample 1 and lapping it into the windowing buffer of sample 2 */ -int ov_crosslap(OggVorbis_File *vf1, OggVorbis_File *vf2){ - vorbis_info *vi1,*vi2; - float **lappcm; - float **pcm; - const float *w1,*w2; - int n1,n2,i,ret,hs1,hs2; - - if(vf1==vf2)return(0); /* degenerate case */ - if(vf1->ready_stateready_statechannels); - n1=vorbis_info_blocksize(vi1,0)>>(1+hs1); - n2=vorbis_info_blocksize(vi2,0)>>(1+hs2); - w1=vorbis_window(&vf1->vd,0); - w2=vorbis_window(&vf2->vd,0); - - for(i=0;ichannels;i++) - lappcm[i]=(float *)alloca(sizeof(**lappcm)*n1); - - _ov_getlap(vf1,vi1,&vf1->vd,lappcm,n1); - - /* have a lapping buffer from vf1; now to splice it into the lapping - buffer of vf2 */ - /* consolidate and expose the buffer. */ - vorbis_synthesis_lapout(&vf2->vd,&pcm); - -#if 0 - _analysis_output_always("pcmL",0,pcm[0],n1*2,0,0,0); - _analysis_output_always("pcmR",0,pcm[1],n1*2,0,0,0); -#endif - - /* splice */ - _ov_splice(pcm,lappcm,n1,n2,vi1->channels,vi2->channels,w1,w2); - - /* done */ - return(0); -} - -static int _ov_64_seek_lap(OggVorbis_File *vf,ogg_int64_t pos, - int (*localseek)(OggVorbis_File *,ogg_int64_t)){ - vorbis_info *vi; - float **lappcm; - float **pcm; - const float *w1,*w2; - int n1,n2,ch1,ch2,hs; - int i,ret; - - if(vf->ready_statechannels; - n1=vorbis_info_blocksize(vi,0)>>(1+hs); - w1=vorbis_window(&vf->vd,0); /* window arrays from libvorbis are - persistent; even if the decode state - from this link gets dumped, this - window array continues to exist */ - - lappcm=(float **)alloca(sizeof(*lappcm)*ch1); - for(i=0;ivd,lappcm,n1); - - /* have lapping data; seek and prime the buffer */ - ret=localseek(vf,pos); - if(ret)return ret; - ret=_ov_initprime(vf); - if(ret)return(ret); - - /* Guard against cross-link changes; they're perfectly legal */ - vi=ov_info(vf,-1); - ch2=vi->channels; - n2=vorbis_info_blocksize(vi,0)>>(1+hs); - w2=vorbis_window(&vf->vd,0); - - /* consolidate and expose the buffer. */ - vorbis_synthesis_lapout(&vf->vd,&pcm); - - /* splice */ - _ov_splice(pcm,lappcm,n1,n2,ch1,ch2,w1,w2); - - /* done */ - return(0); -} - -int ov_raw_seek_lap(OggVorbis_File *vf,ogg_int64_t pos){ - return _ov_64_seek_lap(vf,pos,ov_raw_seek); -} - -int ov_pcm_seek_lap(OggVorbis_File *vf,ogg_int64_t pos){ - return _ov_64_seek_lap(vf,pos,ov_pcm_seek); -} - -int ov_pcm_seek_page_lap(OggVorbis_File *vf,ogg_int64_t pos){ - return _ov_64_seek_lap(vf,pos,ov_pcm_seek_page); -} - -static int _ov_d_seek_lap(OggVorbis_File *vf,double pos, - int (*localseek)(OggVorbis_File *,double)){ - vorbis_info *vi; - float **lappcm; - float **pcm; - const float *w1,*w2; - int n1,n2,ch1,ch2,hs; - int i,ret; - - if(vf->ready_statechannels; - n1=vorbis_info_blocksize(vi,0)>>(1+hs); - w1=vorbis_window(&vf->vd,0); /* window arrays from libvorbis are - persistent; even if the decode state - from this link gets dumped, this - window array continues to exist */ - - lappcm=(float **)alloca(sizeof(*lappcm)*ch1); - for(i=0;ivd,lappcm,n1); - - /* have lapping data; seek and prime the buffer */ - ret=localseek(vf,pos); - if(ret)return ret; - ret=_ov_initprime(vf); - if(ret)return(ret); - - /* Guard against cross-link changes; they're perfectly legal */ - vi=ov_info(vf,-1); - ch2=vi->channels; - n2=vorbis_info_blocksize(vi,0)>>(1+hs); - w2=vorbis_window(&vf->vd,0); - - /* consolidate and expose the buffer. */ - vorbis_synthesis_lapout(&vf->vd,&pcm); - - /* splice */ - _ov_splice(pcm,lappcm,n1,n2,ch1,ch2,w1,w2); - - /* done */ - return(0); -} - -int ov_time_seek_lap(OggVorbis_File *vf,double pos){ - return _ov_d_seek_lap(vf,pos,ov_time_seek); -} - -int ov_time_seek_page_lap(OggVorbis_File *vf,double pos){ - return _ov_d_seek_lap(vf,pos,ov_time_seek_page); -} -#ifdef __cplusplus -} -#endif - -#ifdef _MSC_VER -#pragma warning(pop) -#endif - -#endif /* VORBIS_IMPL */ -/* -Copyright (c) 2002-2020 Xiph.org Foundation -Copyright (c) 2020 Eduardo Bart (https://github.com/edubart) - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -- Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -- Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -- Neither the name of the Xiph.org Foundation nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION -OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ diff --git a/libraries/stb/stb_vorbis.h b/libraries/stb/stb_vorbis.h new file mode 100644 index 000000000..3e5c2504c --- /dev/null +++ b/libraries/stb/stb_vorbis.h @@ -0,0 +1,5584 @@ +// Ogg Vorbis audio decoder - v1.22 - public domain +// http://nothings.org/stb_vorbis/ +// +// Original version written by Sean Barrett in 2007. +// +// Originally sponsored by RAD Game Tools. Seeking implementation +// sponsored by Phillip Bennefall, Marc Andersen, Aaron Baker, +// Elias Software, Aras Pranckevicius, and Sean Barrett. +// +// LICENSE +// +// See end of file for license information. +// +// Limitations: +// +// - floor 0 not supported (used in old ogg vorbis files pre-2004) +// - lossless sample-truncation at beginning ignored +// - cannot concatenate multiple vorbis streams +// - sample positions are 32-bit, limiting seekable 192Khz +// files to around 6 hours (Ogg supports 64-bit) +// +// Feature contributors: +// Dougall Johnson (sample-exact seeking) +// +// Bugfix/warning contributors: +// Terje Mathisen Niklas Frykholm Andy Hill +// Casey Muratori John Bolton Gargaj +// Laurent Gomila Marc LeBlanc Ronny Chevalier +// Bernhard Wodo Evan Balster github:alxprd +// Tom Beaumont Ingo Leitgeb Nicolas Guillemot +// Phillip Bennefall Rohit Thiago Goulart +// github:manxorist Saga Musix github:infatum +// Timur Gagiev Maxwell Koo Peter Waller +// github:audinowho Dougall Johnson David Reid +// github:Clownacy Pedro J. Estebanez Remi Verschelde +// AnthoFoxo github:morlat Gabriel Ravier +// +// Partial history: +// 1.22 - 2021-07-11 - various small fixes +// 1.21 - 2021-07-02 - fix bug for files with no comments +// 1.20 - 2020-07-11 - several small fixes +// 1.19 - 2020-02-05 - warnings +// 1.18 - 2020-02-02 - fix seek bugs; parse header comments; misc warnings etc. +// 1.17 - 2019-07-08 - fix CVE-2019-13217..CVE-2019-13223 (by ForAllSecure) +// 1.16 - 2019-03-04 - fix warnings +// 1.15 - 2019-02-07 - explicit failure if Ogg Skeleton data is found +// 1.14 - 2018-02-11 - delete bogus dealloca usage +// 1.13 - 2018-01-29 - fix truncation of last frame (hopefully) +// 1.12 - 2017-11-21 - limit residue begin/end to blocksize/2 to avoid large temp allocs in bad/corrupt files +// 1.11 - 2017-07-23 - fix MinGW compilation +// 1.10 - 2017-03-03 - more robust seeking; fix negative ilog(); clear error in open_memory +// 1.09 - 2016-04-04 - back out 'truncation of last frame' fix from previous version +// 1.08 - 2016-04-02 - warnings; setup memory leaks; truncation of last frame +// 1.07 - 2015-01-16 - fixes for crashes on invalid files; warning fixes; const +// 1.06 - 2015-08-31 - full, correct support for seeking API (Dougall Johnson) +// some crash fixes when out of memory or with corrupt files +// fix some inappropriately signed shifts +// 1.05 - 2015-04-19 - don't define __forceinline if it's redundant +// 1.04 - 2014-08-27 - fix missing const-correct case in API +// 1.03 - 2014-08-07 - warning fixes +// 1.02 - 2014-07-09 - declare qsort comparison as explicitly _cdecl in Windows +// 1.01 - 2014-06-18 - fix stb_vorbis_get_samples_float (interleaved was correct) +// 1.0 - 2014-05-26 - fix memory leaks; fix warnings; fix bugs in >2-channel; +// (API change) report sample rate for decode-full-file funcs +// +// See end of file for full version history. + + +////////////////////////////////////////////////////////////////////////////// +// +// HEADER BEGINS HERE +// + +#ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H +#define STB_VORBIS_INCLUDE_STB_VORBIS_H + +#if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO) +#define STB_VORBIS_NO_STDIO 1 +#endif + +#ifndef STB_VORBIS_NO_STDIO +#include +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/////////// THREAD SAFETY + +// Individual stb_vorbis* handles are not thread-safe; you cannot decode from +// them from multiple threads at the same time. However, you can have multiple +// stb_vorbis* handles and decode from them independently in multiple thrads. + + +/////////// MEMORY ALLOCATION + +// normally stb_vorbis uses malloc() to allocate memory at startup, +// and alloca() to allocate temporary memory during a frame on the +// stack. (Memory consumption will depend on the amount of setup +// data in the file and how you set the compile flags for speed +// vs. size. In my test files the maximal-size usage is ~150KB.) +// +// You can modify the wrapper functions in the source (setup_malloc, +// setup_temp_malloc, temp_malloc) to change this behavior, or you +// can use a simpler allocation model: you pass in a buffer from +// which stb_vorbis will allocate _all_ its memory (including the +// temp memory). "open" may fail with a VORBIS_outofmem if you +// do not pass in enough data; there is no way to determine how +// much you do need except to succeed (at which point you can +// query get_info to find the exact amount required. yes I know +// this is lame). +// +// If you pass in a non-NULL buffer of the type below, allocation +// will occur from it as described above. Otherwise just pass NULL +// to use malloc()/alloca() + +typedef struct +{ + char *alloc_buffer; + int alloc_buffer_length_in_bytes; +} stb_vorbis_alloc; + + +/////////// FUNCTIONS USEABLE WITH ALL INPUT MODES + +typedef struct stb_vorbis stb_vorbis; + +typedef struct +{ + unsigned int sample_rate; + int channels; + + unsigned int setup_memory_required; + unsigned int setup_temp_memory_required; + unsigned int temp_memory_required; + + int max_frame_size; +} stb_vorbis_info; + +typedef struct +{ + char *vendor; + + int comment_list_length; + char **comment_list; +} stb_vorbis_comment; + +// get general information about the file +extern stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f); + +// get ogg comments +extern stb_vorbis_comment stb_vorbis_get_comment(stb_vorbis *f); + +// get the last error detected (clears it, too) +extern int stb_vorbis_get_error(stb_vorbis *f); + +// close an ogg vorbis file and free all memory in use +extern void stb_vorbis_close(stb_vorbis *f); + +// this function returns the offset (in samples) from the beginning of the +// file that will be returned by the next decode, if it is known, or -1 +// otherwise. after a flush_pushdata() call, this may take a while before +// it becomes valid again. +// NOT WORKING YET after a seek with PULLDATA API +extern int stb_vorbis_get_sample_offset(stb_vorbis *f); + +// returns the current seek point within the file, or offset from the beginning +// of the memory buffer. In pushdata mode it returns 0. +extern unsigned int stb_vorbis_get_file_offset(stb_vorbis *f); + +/////////// PUSHDATA API + +#ifndef STB_VORBIS_NO_PUSHDATA_API + +// this API allows you to get blocks of data from any source and hand +// them to stb_vorbis. you have to buffer them; stb_vorbis will tell +// you how much it used, and you have to give it the rest next time; +// and stb_vorbis may not have enough data to work with and you will +// need to give it the same data again PLUS more. Note that the Vorbis +// specification does not bound the size of an individual frame. + +extern stb_vorbis *stb_vorbis_open_pushdata( + const unsigned char * datablock, int datablock_length_in_bytes, + int *datablock_memory_consumed_in_bytes, + int *error, + const stb_vorbis_alloc *alloc_buffer); +// create a vorbis decoder by passing in the initial data block containing +// the ogg&vorbis headers (you don't need to do parse them, just provide +// the first N bytes of the file--you're told if it's not enough, see below) +// on success, returns an stb_vorbis *, does not set error, returns the amount of +// data parsed/consumed on this call in *datablock_memory_consumed_in_bytes; +// on failure, returns NULL on error and sets *error, does not change *datablock_memory_consumed +// if returns NULL and *error is VORBIS_need_more_data, then the input block was +// incomplete and you need to pass in a larger block from the start of the file + +extern int stb_vorbis_decode_frame_pushdata( + stb_vorbis *f, + const unsigned char *datablock, int datablock_length_in_bytes, + int *channels, // place to write number of float * buffers + float ***output, // place to write float ** array of float * buffers + int *samples // place to write number of output samples + ); +// decode a frame of audio sample data if possible from the passed-in data block +// +// return value: number of bytes we used from datablock +// +// possible cases: +// 0 bytes used, 0 samples output (need more data) +// N bytes used, 0 samples output (resynching the stream, keep going) +// N bytes used, M samples output (one frame of data) +// note that after opening a file, you will ALWAYS get one N-bytes,0-sample +// frame, because Vorbis always "discards" the first frame. +// +// Note that on resynch, stb_vorbis will rarely consume all of the buffer, +// instead only datablock_length_in_bytes-3 or less. This is because it wants +// to avoid missing parts of a page header if they cross a datablock boundary, +// without writing state-machiney code to record a partial detection. +// +// The number of channels returned are stored in *channels (which can be +// NULL--it is always the same as the number of channels reported by +// get_info). *output will contain an array of float* buffers, one per +// channel. In other words, (*output)[0][0] contains the first sample from +// the first channel, and (*output)[1][0] contains the first sample from +// the second channel. +// +// *output points into stb_vorbis's internal output buffer storage; these +// buffers are owned by stb_vorbis and application code should not free +// them or modify their contents. They are transient and will be overwritten +// once you ask for more data to get decoded, so be sure to grab any data +// you need before then. + +extern void stb_vorbis_flush_pushdata(stb_vorbis *f); +// inform stb_vorbis that your next datablock will not be contiguous with +// previous ones (e.g. you've seeked in the data); future attempts to decode +// frames will cause stb_vorbis to resynchronize (as noted above), and +// once it sees a valid Ogg page (typically 4-8KB, as large as 64KB), it +// will begin decoding the _next_ frame. +// +// if you want to seek using pushdata, you need to seek in your file, then +// call stb_vorbis_flush_pushdata(), then start calling decoding, then once +// decoding is returning you data, call stb_vorbis_get_sample_offset, and +// if you don't like the result, seek your file again and repeat. +#endif + + +////////// PULLING INPUT API + +#ifndef STB_VORBIS_NO_PULLDATA_API +// This API assumes stb_vorbis is allowed to pull data from a source-- +// either a block of memory containing the _entire_ vorbis stream, or a +// FILE * that you or it create, or possibly some other reading mechanism +// if you go modify the source to replace the FILE * case with some kind +// of callback to your code. (But if you don't support seeking, you may +// just want to go ahead and use pushdata.) + +#if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION) +extern int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output); +#endif +#if !defined(STB_VORBIS_NO_INTEGER_CONVERSION) +extern int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output); +#endif +// decode an entire file and output the data interleaved into a malloc()ed +// buffer stored in *output. The return value is the number of samples +// decoded, or -1 if the file could not be opened or was not an ogg vorbis file. +// When you're done with it, just free() the pointer returned in *output. + +extern stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, + int *error, const stb_vorbis_alloc *alloc_buffer); +// create an ogg vorbis decoder from an ogg vorbis stream in memory (note +// this must be the entire stream!). on failure, returns NULL and sets *error + +#ifndef STB_VORBIS_NO_STDIO +extern stb_vorbis * stb_vorbis_open_filename(const char *filename, + int *error, const stb_vorbis_alloc *alloc_buffer); +// create an ogg vorbis decoder from a filename via fopen(). on failure, +// returns NULL and sets *error (possibly to VORBIS_file_open_failure). + +extern stb_vorbis * stb_vorbis_open_file(FILE *f, int close_handle_on_close, + int *error, const stb_vorbis_alloc *alloc_buffer); +// create an ogg vorbis decoder from an open FILE *, looking for a stream at +// the _current_ seek point (ftell). on failure, returns NULL and sets *error. +// note that stb_vorbis must "own" this stream; if you seek it in between +// calls to stb_vorbis, it will become confused. Moreover, if you attempt to +// perform stb_vorbis_seek_*() operations on this file, it will assume it +// owns the _entire_ rest of the file after the start point. Use the next +// function, stb_vorbis_open_file_section(), to limit it. + +extern stb_vorbis * stb_vorbis_open_file_section(FILE *f, int close_handle_on_close, + int *error, const stb_vorbis_alloc *alloc_buffer, unsigned int len); +// create an ogg vorbis decoder from an open FILE *, looking for a stream at +// the _current_ seek point (ftell); the stream will be of length 'len' bytes. +// on failure, returns NULL and sets *error. note that stb_vorbis must "own" +// this stream; if you seek it in between calls to stb_vorbis, it will become +// confused. +#endif + +extern int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number); +extern int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number); +// these functions seek in the Vorbis file to (approximately) 'sample_number'. +// after calling seek_frame(), the next call to get_frame_*() will include +// the specified sample. after calling stb_vorbis_seek(), the next call to +// stb_vorbis_get_samples_* will start with the specified sample. If you +// do not need to seek to EXACTLY the target sample when using get_samples_*, +// you can also use seek_frame(). + +extern int stb_vorbis_seek_start(stb_vorbis *f); +// this function is equivalent to stb_vorbis_seek(f,0) + +extern unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f); +extern float stb_vorbis_stream_length_in_seconds(stb_vorbis *f); +// these functions return the total length of the vorbis stream + +extern int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output); +// decode the next frame and return the number of samples. the number of +// channels returned are stored in *channels (which can be NULL--it is always +// the same as the number of channels reported by get_info). *output will +// contain an array of float* buffers, one per channel. These outputs will +// be overwritten on the next call to stb_vorbis_get_frame_*. +// +// You generally should not intermix calls to stb_vorbis_get_frame_*() +// and stb_vorbis_get_samples_*(), since the latter calls the former. + +#ifndef STB_VORBIS_NO_INTEGER_CONVERSION +extern int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts); +extern int stb_vorbis_get_frame_short (stb_vorbis *f, int num_c, short **buffer, int num_samples); +#endif +// decode the next frame and return the number of *samples* per channel. +// Note that for interleaved data, you pass in the number of shorts (the +// size of your array), but the return value is the number of samples per +// channel, not the total number of samples. +// +// The data is coerced to the number of channels you request according to the +// channel coercion rules (see below). You must pass in the size of your +// buffer(s) so that stb_vorbis will not overwrite the end of the buffer. +// The maximum buffer size needed can be gotten from get_info(); however, +// the Vorbis I specification implies an absolute maximum of 4096 samples +// per channel. + +// Channel coercion rules: +// Let M be the number of channels requested, and N the number of channels present, +// and Cn be the nth channel; let stereo L be the sum of all L and center channels, +// and stereo R be the sum of all R and center channels (channel assignment from the +// vorbis spec). +// M N output +// 1 k sum(Ck) for all k +// 2 * stereo L, stereo R +// k l k > l, the first l channels, then 0s +// k l k <= l, the first k channels +// Note that this is not _good_ surround etc. mixing at all! It's just so +// you get something useful. + +extern int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats); +extern int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples); +// gets num_samples samples, not necessarily on a frame boundary--this requires +// buffering so you have to supply the buffers. DOES NOT APPLY THE COERCION RULES. +// Returns the number of samples stored per channel; it may be less than requested +// at the end of the file. If there are no more samples in the file, returns 0. + +#ifndef STB_VORBIS_NO_INTEGER_CONVERSION +extern int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts); +extern int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int num_samples); +#endif +// gets num_samples samples, not necessarily on a frame boundary--this requires +// buffering so you have to supply the buffers. Applies the coercion rules above +// to produce 'channels' channels. Returns the number of samples stored per channel; +// it may be less than requested at the end of the file. If there are no more +// samples in the file, returns 0. + +#endif + +//////// ERROR CODES + +enum STBVorbisError +{ + VORBIS__no_error, + + VORBIS_need_more_data=1, // not a real error + + VORBIS_invalid_api_mixing, // can't mix API modes + VORBIS_outofmem, // not enough memory + VORBIS_feature_not_supported, // uses floor 0 + VORBIS_too_many_channels, // STB_VORBIS_MAX_CHANNELS is too small + VORBIS_file_open_failure, // fopen() failed + VORBIS_seek_without_length, // can't seek in unknown-length file + + VORBIS_unexpected_eof=10, // file is truncated? + VORBIS_seek_invalid, // seek past EOF + + // decoding errors (corrupt/invalid stream) -- you probably + // don't care about the exact details of these + + // vorbis errors: + VORBIS_invalid_setup=20, + VORBIS_invalid_stream, + + // ogg errors: + VORBIS_missing_capture_pattern=30, + VORBIS_invalid_stream_structure_version, + VORBIS_continued_packet_flag_invalid, + VORBIS_incorrect_stream_serial_number, + VORBIS_invalid_first_page, + VORBIS_bad_packet_type, + VORBIS_cant_find_last_page, + VORBIS_seek_failed, + VORBIS_ogg_skeleton_not_supported +}; + + +#ifdef __cplusplus +} +#endif + +#endif // STB_VORBIS_INCLUDE_STB_VORBIS_H +// +// HEADER ENDS HERE +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef STB_VORBIS_HEADER_ONLY + +// global configuration settings (e.g. set these in the project/makefile), +// or just set them in this file at the top (although ideally the first few +// should be visible when the header file is compiled too, although it's not +// crucial) + +// STB_VORBIS_NO_PUSHDATA_API +// does not compile the code for the various stb_vorbis_*_pushdata() +// functions +// #define STB_VORBIS_NO_PUSHDATA_API + +// STB_VORBIS_NO_PULLDATA_API +// does not compile the code for the non-pushdata APIs +// #define STB_VORBIS_NO_PULLDATA_API + +// STB_VORBIS_NO_STDIO +// does not compile the code for the APIs that use FILE *s internally +// or externally (implied by STB_VORBIS_NO_PULLDATA_API) +// #define STB_VORBIS_NO_STDIO + +// STB_VORBIS_NO_INTEGER_CONVERSION +// does not compile the code for converting audio sample data from +// float to integer (implied by STB_VORBIS_NO_PULLDATA_API) +// #define STB_VORBIS_NO_INTEGER_CONVERSION + +// STB_VORBIS_NO_FAST_SCALED_FLOAT +// does not use a fast float-to-int trick to accelerate float-to-int on +// most platforms which requires endianness be defined correctly. +//#define STB_VORBIS_NO_FAST_SCALED_FLOAT + + +// STB_VORBIS_MAX_CHANNELS [number] +// globally define this to the maximum number of channels you need. +// The spec does not put a restriction on channels except that +// the count is stored in a byte, so 255 is the hard limit. +// Reducing this saves about 16 bytes per value, so using 16 saves +// (255-16)*16 or around 4KB. Plus anything other memory usage +// I forgot to account for. Can probably go as low as 8 (7.1 audio), +// 6 (5.1 audio), or 2 (stereo only). +#ifndef STB_VORBIS_MAX_CHANNELS +#define STB_VORBIS_MAX_CHANNELS 16 // enough for anyone? +#endif + +// STB_VORBIS_PUSHDATA_CRC_COUNT [number] +// after a flush_pushdata(), stb_vorbis begins scanning for the +// next valid page, without backtracking. when it finds something +// that looks like a page, it streams through it and verifies its +// CRC32. Should that validation fail, it keeps scanning. But it's +// possible that _while_ streaming through to check the CRC32 of +// one candidate page, it sees another candidate page. This #define +// determines how many "overlapping" candidate pages it can search +// at once. Note that "real" pages are typically ~4KB to ~8KB, whereas +// garbage pages could be as big as 64KB, but probably average ~16KB. +// So don't hose ourselves by scanning an apparent 64KB page and +// missing a ton of real ones in the interim; so minimum of 2 +#ifndef STB_VORBIS_PUSHDATA_CRC_COUNT +#define STB_VORBIS_PUSHDATA_CRC_COUNT 4 +#endif + +// STB_VORBIS_FAST_HUFFMAN_LENGTH [number] +// sets the log size of the huffman-acceleration table. Maximum +// supported value is 24. with larger numbers, more decodings are O(1), +// but the table size is larger so worse cache missing, so you'll have +// to probe (and try multiple ogg vorbis files) to find the sweet spot. +#ifndef STB_VORBIS_FAST_HUFFMAN_LENGTH +#define STB_VORBIS_FAST_HUFFMAN_LENGTH 10 +#endif + +// STB_VORBIS_FAST_BINARY_LENGTH [number] +// sets the log size of the binary-search acceleration table. this +// is used in similar fashion to the fast-huffman size to set initial +// parameters for the binary search + +// STB_VORBIS_FAST_HUFFMAN_INT +// The fast huffman tables are much more efficient if they can be +// stored as 16-bit results instead of 32-bit results. This restricts +// the codebooks to having only 65535 possible outcomes, though. +// (At least, accelerated by the huffman table.) +#ifndef STB_VORBIS_FAST_HUFFMAN_INT +#define STB_VORBIS_FAST_HUFFMAN_SHORT +#endif + +// STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH +// If the 'fast huffman' search doesn't succeed, then stb_vorbis falls +// back on binary searching for the correct one. This requires storing +// extra tables with the huffman codes in sorted order. Defining this +// symbol trades off space for speed by forcing a linear search in the +// non-fast case, except for "sparse" codebooks. +// #define STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH + +// STB_VORBIS_DIVIDES_IN_RESIDUE +// stb_vorbis precomputes the result of the scalar residue decoding +// that would otherwise require a divide per chunk. you can trade off +// space for time by defining this symbol. +// #define STB_VORBIS_DIVIDES_IN_RESIDUE + +// STB_VORBIS_DIVIDES_IN_CODEBOOK +// vorbis VQ codebooks can be encoded two ways: with every case explicitly +// stored, or with all elements being chosen from a small range of values, +// and all values possible in all elements. By default, stb_vorbis expands +// this latter kind out to look like the former kind for ease of decoding, +// because otherwise an integer divide-per-vector-element is required to +// unpack the index. If you define STB_VORBIS_DIVIDES_IN_CODEBOOK, you can +// trade off storage for speed. +//#define STB_VORBIS_DIVIDES_IN_CODEBOOK + +#ifdef STB_VORBIS_CODEBOOK_SHORTS +#error "STB_VORBIS_CODEBOOK_SHORTS is no longer supported as it produced incorrect results for some input formats" +#endif + +// STB_VORBIS_DIVIDE_TABLE +// this replaces small integer divides in the floor decode loop with +// table lookups. made less than 1% difference, so disabled by default. + +// STB_VORBIS_NO_INLINE_DECODE +// disables the inlining of the scalar codebook fast-huffman decode. +// might save a little codespace; useful for debugging +// #define STB_VORBIS_NO_INLINE_DECODE + +// STB_VORBIS_NO_DEFER_FLOOR +// Normally we only decode the floor without synthesizing the actual +// full curve. We can instead synthesize the curve immediately. This +// requires more memory and is very likely slower, so I don't think +// you'd ever want to do it except for debugging. +// #define STB_VORBIS_NO_DEFER_FLOOR + + + + +////////////////////////////////////////////////////////////////////////////// + +#ifdef STB_VORBIS_NO_PULLDATA_API + #define STB_VORBIS_NO_INTEGER_CONVERSION + #define STB_VORBIS_NO_STDIO +#endif + +#if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO) + #define STB_VORBIS_NO_STDIO 1 +#endif + +#ifndef STB_VORBIS_NO_INTEGER_CONVERSION +#ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT + + // only need endianness for fast-float-to-int, which we don't + // use for pushdata + + #ifndef STB_VORBIS_BIG_ENDIAN + #define STB_VORBIS_ENDIAN 0 + #else + #define STB_VORBIS_ENDIAN 1 + #endif + +#endif +#endif + + +#ifndef STB_VORBIS_NO_STDIO +#include +#endif + +#ifndef STB_VORBIS_NO_CRT + #include + #include + #include + #include + + // find definition of alloca if it's not in stdlib.h: + #if defined(_MSC_VER) || defined(__MINGW32__) + #include + #endif + #if defined(__linux__) || defined(__linux) || defined(__sun__) || defined(__EMSCRIPTEN__) || defined(__NEWLIB__) + #include + #endif +#else // STB_VORBIS_NO_CRT + #define NULL 0 + #define malloc(s) 0 + #define free(s) ((void) 0) + #define realloc(s) 0 +#endif // STB_VORBIS_NO_CRT + +#include + +#ifdef __MINGW32__ + // eff you mingw: + // "fixed": + // http://sourceforge.net/p/mingw-w64/mailman/message/32882927/ + // "no that broke the build, reverted, who cares about C": + // http://sourceforge.net/p/mingw-w64/mailman/message/32890381/ + #ifdef __forceinline + #undef __forceinline + #endif + #define __forceinline + #ifndef alloca + #define alloca __builtin_alloca + #endif +#elif !defined(_MSC_VER) + #if __GNUC__ + #define __forceinline inline + #else + #define __forceinline + #endif +#endif + +#if STB_VORBIS_MAX_CHANNELS > 256 +#error "Value of STB_VORBIS_MAX_CHANNELS outside of allowed range" +#endif + +#if STB_VORBIS_FAST_HUFFMAN_LENGTH > 24 +#error "Value of STB_VORBIS_FAST_HUFFMAN_LENGTH outside of allowed range" +#endif + + +#if 0 +#include +#define CHECK(f) _CrtIsValidHeapPointer(f->channel_buffers[1]) +#else +#define CHECK(f) ((void) 0) +#endif + +#define MAX_BLOCKSIZE_LOG 13 // from specification +#define MAX_BLOCKSIZE (1 << MAX_BLOCKSIZE_LOG) + + +typedef unsigned char uint8; +typedef signed char int8; +typedef unsigned short uint16; +typedef signed short int16; +typedef unsigned int uint32; +typedef signed int int32; + +#ifndef TRUE +#define TRUE 1 +#define FALSE 0 +#endif + +typedef float codetype; + +#ifdef _MSC_VER +#define STBV_NOTUSED(v) (void)(v) +#else +#define STBV_NOTUSED(v) (void)sizeof(v) +#endif + +// @NOTE +// +// Some arrays below are tagged "//varies", which means it's actually +// a variable-sized piece of data, but rather than malloc I assume it's +// small enough it's better to just allocate it all together with the +// main thing +// +// Most of the variables are specified with the smallest size I could pack +// them into. It might give better performance to make them all full-sized +// integers. It should be safe to freely rearrange the structures or change +// the sizes larger--nothing relies on silently truncating etc., nor the +// order of variables. + +#define FAST_HUFFMAN_TABLE_SIZE (1 << STB_VORBIS_FAST_HUFFMAN_LENGTH) +#define FAST_HUFFMAN_TABLE_MASK (FAST_HUFFMAN_TABLE_SIZE - 1) + +typedef struct +{ + int dimensions, entries; + uint8 *codeword_lengths; + float minimum_value; + float delta_value; + uint8 value_bits; + uint8 lookup_type; + uint8 sequence_p; + uint8 sparse; + uint32 lookup_values; + codetype *multiplicands; + uint32 *codewords; + #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT + int16 fast_huffman[FAST_HUFFMAN_TABLE_SIZE]; + #else + int32 fast_huffman[FAST_HUFFMAN_TABLE_SIZE]; + #endif + uint32 *sorted_codewords; + int *sorted_values; + int sorted_entries; +} Codebook; + +typedef struct +{ + uint8 order; + uint16 rate; + uint16 bark_map_size; + uint8 amplitude_bits; + uint8 amplitude_offset; + uint8 number_of_books; + uint8 book_list[16]; // varies +} Floor0; + +typedef struct +{ + uint8 partitions; + uint8 partition_class_list[32]; // varies + uint8 class_dimensions[16]; // varies + uint8 class_subclasses[16]; // varies + uint8 class_masterbooks[16]; // varies + int16 subclass_books[16][8]; // varies + uint16 Xlist[31*8+2]; // varies + uint8 sorted_order[31*8+2]; + uint8 neighbors[31*8+2][2]; + uint8 floor1_multiplier; + uint8 rangebits; + int values; +} Floor1; + +typedef union +{ + Floor0 floor0; + Floor1 floor1; +} Floor; + +typedef struct +{ + uint32 begin, end; + uint32 part_size; + uint8 classifications; + uint8 classbook; + uint8 **classdata; + int16 (*residue_books)[8]; +} Residue; + +typedef struct +{ + uint8 magnitude; + uint8 angle; + uint8 mux; +} MappingChannel; + +typedef struct +{ + uint16 coupling_steps; + MappingChannel *chan; + uint8 submaps; + uint8 submap_floor[15]; // varies + uint8 submap_residue[15]; // varies +} Mapping; + +typedef struct +{ + uint8 blockflag; + uint8 mapping; + uint16 windowtype; + uint16 transformtype; +} Mode; + +typedef struct +{ + uint32 goal_crc; // expected crc if match + int bytes_left; // bytes left in packet + uint32 crc_so_far; // running crc + int bytes_done; // bytes processed in _current_ chunk + uint32 sample_loc; // granule pos encoded in page +} CRCscan; + +typedef struct +{ + uint32 page_start, page_end; + uint32 last_decoded_sample; +} ProbedPage; + +struct stb_vorbis +{ + // user-accessible info + unsigned int sample_rate; + int channels; + + unsigned int setup_memory_required; + unsigned int temp_memory_required; + unsigned int setup_temp_memory_required; + + char *vendor; + int comment_list_length; + char **comment_list; + + // input config +#ifndef STB_VORBIS_NO_STDIO + FILE *f; + uint32 f_start; + int close_on_free; +#endif + + uint8 *stream; + uint8 *stream_start; + uint8 *stream_end; + + uint32 stream_len; + + uint8 push_mode; + + // the page to seek to when seeking to start, may be zero + uint32 first_audio_page_offset; + + // p_first is the page on which the first audio packet ends + // (but not necessarily the page on which it starts) + ProbedPage p_first, p_last; + + // memory management + stb_vorbis_alloc alloc; + int setup_offset; + int temp_offset; + + // run-time results + int eof; + enum STBVorbisError error; + + // user-useful data + + // header info + int blocksize[2]; + int blocksize_0, blocksize_1; + int codebook_count; + Codebook *codebooks; + int floor_count; + uint16 floor_types[64]; // varies + Floor *floor_config; + int residue_count; + uint16 residue_types[64]; // varies + Residue *residue_config; + int mapping_count; + Mapping *mapping; + int mode_count; + Mode mode_config[64]; // varies + + uint32 total_samples; + + // decode buffer + float *channel_buffers[STB_VORBIS_MAX_CHANNELS]; + float *outputs [STB_VORBIS_MAX_CHANNELS]; + + float *previous_window[STB_VORBIS_MAX_CHANNELS]; + int previous_length; + + #ifndef STB_VORBIS_NO_DEFER_FLOOR + int16 *finalY[STB_VORBIS_MAX_CHANNELS]; + #else + float *floor_buffers[STB_VORBIS_MAX_CHANNELS]; + #endif + + uint32 current_loc; // sample location of next frame to decode + int current_loc_valid; + + // per-blocksize precomputed data + + // twiddle factors + float *A[2],*B[2],*C[2]; + float *window[2]; + uint16 *bit_reverse[2]; + + // current page/packet/segment streaming info + uint32 serial; // stream serial number for verification + int last_page; + int segment_count; + uint8 segments[255]; + uint8 page_flag; + uint8 bytes_in_seg; + uint8 first_decode; + int next_seg; + int last_seg; // flag that we're on the last segment + int last_seg_which; // what was the segment number of the last seg? + uint32 acc; + int valid_bits; + int packet_bytes; + int end_seg_with_known_loc; + uint32 known_loc_for_packet; + int discard_samples_deferred; + uint32 samples_output; + + // push mode scanning + int page_crc_tests; // only in push_mode: number of tests active; -1 if not searching +#ifndef STB_VORBIS_NO_PUSHDATA_API + CRCscan scan[STB_VORBIS_PUSHDATA_CRC_COUNT]; +#endif + + // sample-access + int channel_buffer_start; + int channel_buffer_end; +}; + +#if defined(STB_VORBIS_NO_PUSHDATA_API) + #define IS_PUSH_MODE(f) FALSE +#elif defined(STB_VORBIS_NO_PULLDATA_API) + #define IS_PUSH_MODE(f) TRUE +#else + #define IS_PUSH_MODE(f) ((f)->push_mode) +#endif + +typedef struct stb_vorbis vorb; + +static int error(vorb *f, enum STBVorbisError e) +{ + f->error = e; + if (!f->eof && e != VORBIS_need_more_data) { + f->error=e; // breakpoint for debugging + } + return 0; +} + + +// these functions are used for allocating temporary memory +// while decoding. if you can afford the stack space, use +// alloca(); otherwise, provide a temp buffer and it will +// allocate out of those. + +#define array_size_required(count,size) (count*(sizeof(void *)+(size))) + +#define temp_alloc(f,size) (f->alloc.alloc_buffer ? setup_temp_malloc(f,size) : alloca(size)) +#define temp_free(f,p) (void)0 +#define temp_alloc_save(f) ((f)->temp_offset) +#define temp_alloc_restore(f,p) ((f)->temp_offset = (p)) + +#define temp_block_array(f,count,size) make_block_array(temp_alloc(f,array_size_required(count,size)), count, size) + +// given a sufficiently large block of memory, make an array of pointers to subblocks of it +static void *make_block_array(void *mem, int count, int size) +{ + int i; + void ** p = (void **) mem; + char *q = (char *) (p + count); + for (i=0; i < count; ++i) { + p[i] = q; + q += size; + } + return p; +} + +static void *setup_malloc(vorb *f, int sz) +{ + sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs. + f->setup_memory_required += sz; + if (f->alloc.alloc_buffer) { + void *p = (char *) f->alloc.alloc_buffer + f->setup_offset; + if (f->setup_offset + sz > f->temp_offset) return NULL; + f->setup_offset += sz; + return p; + } + return sz ? malloc(sz) : NULL; +} + +static void setup_free(vorb *f, void *p) +{ + if (f->alloc.alloc_buffer) return; // do nothing; setup mem is a stack + free(p); +} + +static void *setup_temp_malloc(vorb *f, int sz) +{ + sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs. + if (f->alloc.alloc_buffer) { + if (f->temp_offset - sz < f->setup_offset) return NULL; + f->temp_offset -= sz; + return (char *) f->alloc.alloc_buffer + f->temp_offset; + } + return malloc(sz); +} + +static void setup_temp_free(vorb *f, void *p, int sz) +{ + if (f->alloc.alloc_buffer) { + f->temp_offset += (sz+7)&~7; + return; + } + free(p); +} + +#define CRC32_POLY 0x04c11db7 // from spec + +static uint32 crc_table[256]; +static void crc32_init(void) +{ + int i,j; + uint32 s; + for(i=0; i < 256; i++) { + for (s=(uint32) i << 24, j=0; j < 8; ++j) + s = (s << 1) ^ (s >= (1U<<31) ? CRC32_POLY : 0); + crc_table[i] = s; + } +} + +static __forceinline uint32 crc32_update(uint32 crc, uint8 byte) +{ + return (crc << 8) ^ crc_table[byte ^ (crc >> 24)]; +} + + +// used in setup, and for huffman that doesn't go fast path +static unsigned int bit_reverse(unsigned int n) +{ + n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1); + n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2); + n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4); + n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8); + return (n >> 16) | (n << 16); +} + +static float square(float x) +{ + return x*x; +} + +// this is a weird definition of log2() for which log2(1) = 1, log2(2) = 2, log2(4) = 3 +// as required by the specification. fast(?) implementation from stb.h +// @OPTIMIZE: called multiple times per-packet with "constants"; move to setup +static int ilog(int32 n) +{ + static signed char log2_4[16] = { 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4 }; + + if (n < 0) return 0; // signed n returns 0 + + // 2 compares if n < 16, 3 compares otherwise (4 if signed or n > 1<<29) + if (n < (1 << 14)) + if (n < (1 << 4)) return 0 + log2_4[n ]; + else if (n < (1 << 9)) return 5 + log2_4[n >> 5]; + else return 10 + log2_4[n >> 10]; + else if (n < (1 << 24)) + if (n < (1 << 19)) return 15 + log2_4[n >> 15]; + else return 20 + log2_4[n >> 20]; + else if (n < (1 << 29)) return 25 + log2_4[n >> 25]; + else return 30 + log2_4[n >> 30]; +} + +#ifndef M_PI + #define M_PI 3.14159265358979323846264f // from CRC +#endif + +// code length assigned to a value with no huffman encoding +#define NO_CODE 255 + +/////////////////////// LEAF SETUP FUNCTIONS ////////////////////////// +// +// these functions are only called at setup, and only a few times +// per file + +static float float32_unpack(uint32 x) +{ + // from the specification + uint32 mantissa = x & 0x1fffff; + uint32 sign = x & 0x80000000; + uint32 exp = (x & 0x7fe00000) >> 21; + double res = sign ? -(double)mantissa : (double)mantissa; + return (float) ldexp((float)res, (int)exp-788); +} + + +// zlib & jpeg huffman tables assume that the output symbols +// can either be arbitrarily arranged, or have monotonically +// increasing frequencies--they rely on the lengths being sorted; +// this makes for a very simple generation algorithm. +// vorbis allows a huffman table with non-sorted lengths. This +// requires a more sophisticated construction, since symbols in +// order do not map to huffman codes "in order". +static void add_entry(Codebook *c, uint32 huff_code, int symbol, int count, int len, uint32 *values) +{ + if (!c->sparse) { + c->codewords [symbol] = huff_code; + } else { + c->codewords [count] = huff_code; + c->codeword_lengths[count] = len; + values [count] = symbol; + } +} + +static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values) +{ + int i,k,m=0; + uint32 available[32]; + + memset(available, 0, sizeof(available)); + // find the first entry + for (k=0; k < n; ++k) if (len[k] < NO_CODE) break; + if (k == n) { assert(c->sorted_entries == 0); return TRUE; } + assert(len[k] < 32); // no error return required, code reading lens checks this + // add to the list + add_entry(c, 0, k, m++, len[k], values); + // add all available leaves + for (i=1; i <= len[k]; ++i) + available[i] = 1U << (32-i); + // note that the above code treats the first case specially, + // but it's really the same as the following code, so they + // could probably be combined (except the initial code is 0, + // and I use 0 in available[] to mean 'empty') + for (i=k+1; i < n; ++i) { + uint32 res; + int z = len[i], y; + if (z == NO_CODE) continue; + assert(z < 32); // no error return required, code reading lens checks this + // find lowest available leaf (should always be earliest, + // which is what the specification calls for) + // note that this property, and the fact we can never have + // more than one free leaf at a given level, isn't totally + // trivial to prove, but it seems true and the assert never + // fires, so! + while (z > 0 && !available[z]) --z; + if (z == 0) { return FALSE; } + res = available[z]; + available[z] = 0; + add_entry(c, bit_reverse(res), i, m++, len[i], values); + // propagate availability up the tree + if (z != len[i]) { + for (y=len[i]; y > z; --y) { + assert(available[y] == 0); + available[y] = res + (1 << (32-y)); + } + } + } + return TRUE; +} + +// accelerated huffman table allows fast O(1) match of all symbols +// of length <= STB_VORBIS_FAST_HUFFMAN_LENGTH +static void compute_accelerated_huffman(Codebook *c) +{ + int i, len; + for (i=0; i < FAST_HUFFMAN_TABLE_SIZE; ++i) + c->fast_huffman[i] = -1; + + len = c->sparse ? c->sorted_entries : c->entries; + #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT + if (len > 32767) len = 32767; // largest possible value we can encode! + #endif + for (i=0; i < len; ++i) { + if (c->codeword_lengths[i] <= STB_VORBIS_FAST_HUFFMAN_LENGTH) { + uint32 z = c->sparse ? bit_reverse(c->sorted_codewords[i]) : c->codewords[i]; + // set table entries for all bit combinations in the higher bits + while (z < FAST_HUFFMAN_TABLE_SIZE) { + c->fast_huffman[z] = i; + z += 1 << c->codeword_lengths[i]; + } + } + } +} + +#ifdef _MSC_VER +#define STBV_CDECL __cdecl +#else +#define STBV_CDECL +#endif + +static int STBV_CDECL uint32_compare(const void *p, const void *q) +{ + uint32 x = * (uint32 *) p; + uint32 y = * (uint32 *) q; + return x < y ? -1 : x > y; +} + +static int include_in_sort(Codebook *c, uint8 len) +{ + if (c->sparse) { assert(len != NO_CODE); return TRUE; } + if (len == NO_CODE) return FALSE; + if (len > STB_VORBIS_FAST_HUFFMAN_LENGTH) return TRUE; + return FALSE; +} + +// if the fast table above doesn't work, we want to binary +// search them... need to reverse the bits +static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values) +{ + int i, len; + // build a list of all the entries + // OPTIMIZATION: don't include the short ones, since they'll be caught by FAST_HUFFMAN. + // this is kind of a frivolous optimization--I don't see any performance improvement, + // but it's like 4 extra lines of code, so. + if (!c->sparse) { + int k = 0; + for (i=0; i < c->entries; ++i) + if (include_in_sort(c, lengths[i])) + c->sorted_codewords[k++] = bit_reverse(c->codewords[i]); + assert(k == c->sorted_entries); + } else { + for (i=0; i < c->sorted_entries; ++i) + c->sorted_codewords[i] = bit_reverse(c->codewords[i]); + } + + qsort(c->sorted_codewords, c->sorted_entries, sizeof(c->sorted_codewords[0]), uint32_compare); + c->sorted_codewords[c->sorted_entries] = 0xffffffff; + + len = c->sparse ? c->sorted_entries : c->entries; + // now we need to indicate how they correspond; we could either + // #1: sort a different data structure that says who they correspond to + // #2: for each sorted entry, search the original list to find who corresponds + // #3: for each original entry, find the sorted entry + // #1 requires extra storage, #2 is slow, #3 can use binary search! + for (i=0; i < len; ++i) { + int huff_len = c->sparse ? lengths[values[i]] : lengths[i]; + if (include_in_sort(c,huff_len)) { + uint32 code = bit_reverse(c->codewords[i]); + int x=0, n=c->sorted_entries; + while (n > 1) { + // invariant: sc[x] <= code < sc[x+n] + int m = x + (n >> 1); + if (c->sorted_codewords[m] <= code) { + x = m; + n -= (n>>1); + } else { + n >>= 1; + } + } + assert(c->sorted_codewords[x] == code); + if (c->sparse) { + c->sorted_values[x] = values[i]; + c->codeword_lengths[x] = huff_len; + } else { + c->sorted_values[x] = i; + } + } + } +} + +// only run while parsing the header (3 times) +static int vorbis_validate(uint8 *data) +{ + static uint8 vorbis[6] = { 'v', 'o', 'r', 'b', 'i', 's' }; + return memcmp(data, vorbis, 6) == 0; +} + +// called from setup only, once per code book +// (formula implied by specification) +static int lookup1_values(int entries, int dim) +{ + int r = (int) floor(exp((float) log((float) entries) / dim)); + if ((int) floor(pow((float) r+1, dim)) <= entries) // (int) cast for MinGW warning; + ++r; // floor() to avoid _ftol() when non-CRT + if (pow((float) r+1, dim) <= entries) + return -1; + if ((int) floor(pow((float) r, dim)) > entries) + return -1; + return r; +} + +// called twice per file +static void compute_twiddle_factors(int n, float *A, float *B, float *C) +{ + int n4 = n >> 2, n8 = n >> 3; + int k,k2; + + for (k=k2=0; k < n4; ++k,k2+=2) { + A[k2 ] = (float) cos(4*k*M_PI/n); + A[k2+1] = (float) -sin(4*k*M_PI/n); + B[k2 ] = (float) cos((k2+1)*M_PI/n/2) * 0.5f; + B[k2+1] = (float) sin((k2+1)*M_PI/n/2) * 0.5f; + } + for (k=k2=0; k < n8; ++k,k2+=2) { + C[k2 ] = (float) cos(2*(k2+1)*M_PI/n); + C[k2+1] = (float) -sin(2*(k2+1)*M_PI/n); + } +} + +static void compute_window(int n, float *window) +{ + int n2 = n >> 1, i; + for (i=0; i < n2; ++i) + window[i] = (float) sin(0.5 * M_PI * square((float) sin((i - 0 + 0.5) / n2 * 0.5 * M_PI))); +} + +static void compute_bitreverse(int n, uint16 *rev) +{ + int ld = ilog(n) - 1; // ilog is off-by-one from normal definitions + int i, n8 = n >> 3; + for (i=0; i < n8; ++i) + rev[i] = (bit_reverse(i) >> (32-ld+3)) << 2; +} + +static int init_blocksize(vorb *f, int b, int n) +{ + int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3; + f->A[b] = (float *) setup_malloc(f, sizeof(float) * n2); + f->B[b] = (float *) setup_malloc(f, sizeof(float) * n2); + f->C[b] = (float *) setup_malloc(f, sizeof(float) * n4); + if (!f->A[b] || !f->B[b] || !f->C[b]) return error(f, VORBIS_outofmem); + compute_twiddle_factors(n, f->A[b], f->B[b], f->C[b]); + f->window[b] = (float *) setup_malloc(f, sizeof(float) * n2); + if (!f->window[b]) return error(f, VORBIS_outofmem); + compute_window(n, f->window[b]); + f->bit_reverse[b] = (uint16 *) setup_malloc(f, sizeof(uint16) * n8); + if (!f->bit_reverse[b]) return error(f, VORBIS_outofmem); + compute_bitreverse(n, f->bit_reverse[b]); + return TRUE; +} + +static void neighbors(uint16 *x, int n, int *plow, int *phigh) +{ + int low = -1; + int high = 65536; + int i; + for (i=0; i < n; ++i) { + if (x[i] > low && x[i] < x[n]) { *plow = i; low = x[i]; } + if (x[i] < high && x[i] > x[n]) { *phigh = i; high = x[i]; } + } +} + +// this has been repurposed so y is now the original index instead of y +typedef struct +{ + uint16 x,id; +} stbv__floor_ordering; + +static int STBV_CDECL point_compare(const void *p, const void *q) +{ + stbv__floor_ordering *a = (stbv__floor_ordering *) p; + stbv__floor_ordering *b = (stbv__floor_ordering *) q; + return a->x < b->x ? -1 : a->x > b->x; +} + +// +/////////////////////// END LEAF SETUP FUNCTIONS ////////////////////////// + + +#if defined(STB_VORBIS_NO_STDIO) + #define USE_MEMORY(z) TRUE +#else + #define USE_MEMORY(z) ((z)->stream) +#endif + +static uint8 get8(vorb *z) +{ + if (USE_MEMORY(z)) { + if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; } + return *z->stream++; + } + + #ifndef STB_VORBIS_NO_STDIO + { + int c = fgetc(z->f); + if (c == EOF) { z->eof = TRUE; return 0; } + return c; + } + #endif +} + +static uint32 get32(vorb *f) +{ + uint32 x; + x = get8(f); + x += get8(f) << 8; + x += get8(f) << 16; + x += (uint32) get8(f) << 24; + return x; +} + +static int getn(vorb *z, uint8 *data, int n) +{ + if (USE_MEMORY(z)) { + if (z->stream+n > z->stream_end) { z->eof = 1; return 0; } + memcpy(data, z->stream, n); + z->stream += n; + return 1; + } + + #ifndef STB_VORBIS_NO_STDIO + if (fread(data, n, 1, z->f) == 1) + return 1; + else { + z->eof = 1; + return 0; + } + #endif +} + +static void skip(vorb *z, int n) +{ + if (USE_MEMORY(z)) { + z->stream += n; + if (z->stream >= z->stream_end) z->eof = 1; + return; + } + #ifndef STB_VORBIS_NO_STDIO + { + long x = ftell(z->f); + fseek(z->f, x+n, SEEK_SET); + } + #endif +} + +static int set_file_offset(stb_vorbis *f, unsigned int loc) +{ + #ifndef STB_VORBIS_NO_PUSHDATA_API + if (f->push_mode) return 0; + #endif + f->eof = 0; + if (USE_MEMORY(f)) { + if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) { + f->stream = f->stream_end; + f->eof = 1; + return 0; + } else { + f->stream = f->stream_start + loc; + return 1; + } + } + #ifndef STB_VORBIS_NO_STDIO + if (loc + f->f_start < loc || loc >= 0x80000000) { + loc = 0x7fffffff; + f->eof = 1; + } else { + loc += f->f_start; + } + if (!fseek(f->f, loc, SEEK_SET)) + return 1; + f->eof = 1; + fseek(f->f, f->f_start, SEEK_END); + return 0; + #endif +} + + +static uint8 ogg_page_header[4] = { 0x4f, 0x67, 0x67, 0x53 }; + +static int capture_pattern(vorb *f) +{ + if (0x4f != get8(f)) return FALSE; + if (0x67 != get8(f)) return FALSE; + if (0x67 != get8(f)) return FALSE; + if (0x53 != get8(f)) return FALSE; + return TRUE; +} + +#define PAGEFLAG_continued_packet 1 +#define PAGEFLAG_first_page 2 +#define PAGEFLAG_last_page 4 + +static int start_page_no_capturepattern(vorb *f) +{ + uint32 loc0,loc1,n; + if (f->first_decode && !IS_PUSH_MODE(f)) { + f->p_first.page_start = stb_vorbis_get_file_offset(f) - 4; + } + // stream structure version + if (0 != get8(f)) return error(f, VORBIS_invalid_stream_structure_version); + // header flag + f->page_flag = get8(f); + // absolute granule position + loc0 = get32(f); + loc1 = get32(f); + // @TODO: validate loc0,loc1 as valid positions? + // stream serial number -- vorbis doesn't interleave, so discard + get32(f); + //if (f->serial != get32(f)) return error(f, VORBIS_incorrect_stream_serial_number); + // page sequence number + n = get32(f); + f->last_page = n; + // CRC32 + get32(f); + // page_segments + f->segment_count = get8(f); + if (!getn(f, f->segments, f->segment_count)) + return error(f, VORBIS_unexpected_eof); + // assume we _don't_ know any the sample position of any segments + f->end_seg_with_known_loc = -2; + if (loc0 != ~0U || loc1 != ~0U) { + int i; + // determine which packet is the last one that will complete + for (i=f->segment_count-1; i >= 0; --i) + if (f->segments[i] < 255) + break; + // 'i' is now the index of the _last_ segment of a packet that ends + if (i >= 0) { + f->end_seg_with_known_loc = i; + f->known_loc_for_packet = loc0; + } + } + if (f->first_decode) { + int i,len; + len = 0; + for (i=0; i < f->segment_count; ++i) + len += f->segments[i]; + len += 27 + f->segment_count; + f->p_first.page_end = f->p_first.page_start + len; + f->p_first.last_decoded_sample = loc0; + } + f->next_seg = 0; + return TRUE; +} + +static int start_page(vorb *f) +{ + if (!capture_pattern(f)) return error(f, VORBIS_missing_capture_pattern); + return start_page_no_capturepattern(f); +} + +static int start_packet(vorb *f) +{ + while (f->next_seg == -1) { + if (!start_page(f)) return FALSE; + if (f->page_flag & PAGEFLAG_continued_packet) + return error(f, VORBIS_continued_packet_flag_invalid); + } + f->last_seg = FALSE; + f->valid_bits = 0; + f->packet_bytes = 0; + f->bytes_in_seg = 0; + // f->next_seg is now valid + return TRUE; +} + +static int maybe_start_packet(vorb *f) +{ + if (f->next_seg == -1) { + int x = get8(f); + if (f->eof) return FALSE; // EOF at page boundary is not an error! + if (0x4f != x ) return error(f, VORBIS_missing_capture_pattern); + if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern); + if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern); + if (0x53 != get8(f)) return error(f, VORBIS_missing_capture_pattern); + if (!start_page_no_capturepattern(f)) return FALSE; + if (f->page_flag & PAGEFLAG_continued_packet) { + // set up enough state that we can read this packet if we want, + // e.g. during recovery + f->last_seg = FALSE; + f->bytes_in_seg = 0; + return error(f, VORBIS_continued_packet_flag_invalid); + } + } + return start_packet(f); +} + +static int next_segment(vorb *f) +{ + int len; + if (f->last_seg) return 0; + if (f->next_seg == -1) { + f->last_seg_which = f->segment_count-1; // in case start_page fails + if (!start_page(f)) { f->last_seg = 1; return 0; } + if (!(f->page_flag & PAGEFLAG_continued_packet)) return error(f, VORBIS_continued_packet_flag_invalid); + } + len = f->segments[f->next_seg++]; + if (len < 255) { + f->last_seg = TRUE; + f->last_seg_which = f->next_seg-1; + } + if (f->next_seg >= f->segment_count) + f->next_seg = -1; + assert(f->bytes_in_seg == 0); + f->bytes_in_seg = len; + return len; +} + +#define EOP (-1) +#define INVALID_BITS (-1) + +static int get8_packet_raw(vorb *f) +{ + if (!f->bytes_in_seg) { // CLANG! + if (f->last_seg) return EOP; + else if (!next_segment(f)) return EOP; + } + assert(f->bytes_in_seg > 0); + --f->bytes_in_seg; + ++f->packet_bytes; + return get8(f); +} + +static int get8_packet(vorb *f) +{ + int x = get8_packet_raw(f); + f->valid_bits = 0; + return x; +} + +static int get32_packet(vorb *f) +{ + uint32 x; + x = get8_packet(f); + x += get8_packet(f) << 8; + x += get8_packet(f) << 16; + x += (uint32) get8_packet(f) << 24; + return x; +} + +static void flush_packet(vorb *f) +{ + while (get8_packet_raw(f) != EOP); +} + +// @OPTIMIZE: this is the secondary bit decoder, so it's probably not as important +// as the huffman decoder? +static uint32 get_bits(vorb *f, int n) +{ + uint32 z; + + if (f->valid_bits < 0) return 0; + if (f->valid_bits < n) { + if (n > 24) { + // the accumulator technique below would not work correctly in this case + z = get_bits(f, 24); + z += get_bits(f, n-24) << 24; + return z; + } + if (f->valid_bits == 0) f->acc = 0; + while (f->valid_bits < n) { + int z = get8_packet_raw(f); + if (z == EOP) { + f->valid_bits = INVALID_BITS; + return 0; + } + f->acc += z << f->valid_bits; + f->valid_bits += 8; + } + } + + assert(f->valid_bits >= n); + z = f->acc & ((1 << n)-1); + f->acc >>= n; + f->valid_bits -= n; + return z; +} + +// @OPTIMIZE: primary accumulator for huffman +// expand the buffer to as many bits as possible without reading off end of packet +// it might be nice to allow f->valid_bits and f->acc to be stored in registers, +// e.g. cache them locally and decode locally +static __forceinline void prep_huffman(vorb *f) +{ + if (f->valid_bits <= 24) { + if (f->valid_bits == 0) f->acc = 0; + do { + int z; + if (f->last_seg && !f->bytes_in_seg) return; + z = get8_packet_raw(f); + if (z == EOP) return; + f->acc += (unsigned) z << f->valid_bits; + f->valid_bits += 8; + } while (f->valid_bits <= 24); + } +} + +enum +{ + VORBIS_packet_id = 1, + VORBIS_packet_comment = 3, + VORBIS_packet_setup = 5 +}; + +static int codebook_decode_scalar_raw(vorb *f, Codebook *c) +{ + int i; + prep_huffman(f); + + if (c->codewords == NULL && c->sorted_codewords == NULL) + return -1; + + // cases to use binary search: sorted_codewords && !c->codewords + // sorted_codewords && c->entries > 8 + if (c->entries > 8 ? c->sorted_codewords!=NULL : !c->codewords) { + // binary search + uint32 code = bit_reverse(f->acc); + int x=0, n=c->sorted_entries, len; + + while (n > 1) { + // invariant: sc[x] <= code < sc[x+n] + int m = x + (n >> 1); + if (c->sorted_codewords[m] <= code) { + x = m; + n -= (n>>1); + } else { + n >>= 1; + } + } + // x is now the sorted index + if (!c->sparse) x = c->sorted_values[x]; + // x is now sorted index if sparse, or symbol otherwise + len = c->codeword_lengths[x]; + if (f->valid_bits >= len) { + f->acc >>= len; + f->valid_bits -= len; + return x; + } + + f->valid_bits = 0; + return -1; + } + + // if small, linear search + assert(!c->sparse); + for (i=0; i < c->entries; ++i) { + if (c->codeword_lengths[i] == NO_CODE) continue; + if (c->codewords[i] == (f->acc & ((1 << c->codeword_lengths[i])-1))) { + if (f->valid_bits >= c->codeword_lengths[i]) { + f->acc >>= c->codeword_lengths[i]; + f->valid_bits -= c->codeword_lengths[i]; + return i; + } + f->valid_bits = 0; + return -1; + } + } + + error(f, VORBIS_invalid_stream); + f->valid_bits = 0; + return -1; +} + +#ifndef STB_VORBIS_NO_INLINE_DECODE + +#define DECODE_RAW(var, f,c) \ + if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH) \ + prep_huffman(f); \ + var = f->acc & FAST_HUFFMAN_TABLE_MASK; \ + var = c->fast_huffman[var]; \ + if (var >= 0) { \ + int n = c->codeword_lengths[var]; \ + f->acc >>= n; \ + f->valid_bits -= n; \ + if (f->valid_bits < 0) { f->valid_bits = 0; var = -1; } \ + } else { \ + var = codebook_decode_scalar_raw(f,c); \ + } + +#else + +static int codebook_decode_scalar(vorb *f, Codebook *c) +{ + int i; + if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH) + prep_huffman(f); + // fast huffman table lookup + i = f->acc & FAST_HUFFMAN_TABLE_MASK; + i = c->fast_huffman[i]; + if (i >= 0) { + f->acc >>= c->codeword_lengths[i]; + f->valid_bits -= c->codeword_lengths[i]; + if (f->valid_bits < 0) { f->valid_bits = 0; return -1; } + return i; + } + return codebook_decode_scalar_raw(f,c); +} + +#define DECODE_RAW(var,f,c) var = codebook_decode_scalar(f,c); + +#endif + +#define DECODE(var,f,c) \ + DECODE_RAW(var,f,c) \ + if (c->sparse) var = c->sorted_values[var]; + +#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK + #define DECODE_VQ(var,f,c) DECODE_RAW(var,f,c) +#else + #define DECODE_VQ(var,f,c) DECODE(var,f,c) +#endif + + + + + + +// CODEBOOK_ELEMENT_FAST is an optimization for the CODEBOOK_FLOATS case +// where we avoid one addition +#define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off]) +#define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off]) +#define CODEBOOK_ELEMENT_BASE(c) (0) + +static int codebook_decode_start(vorb *f, Codebook *c) +{ + int z = -1; + + // type 0 is only legal in a scalar context + if (c->lookup_type == 0) + error(f, VORBIS_invalid_stream); + else { + DECODE_VQ(z,f,c); + if (c->sparse) assert(z < c->sorted_entries); + if (z < 0) { // check for EOP + if (!f->bytes_in_seg) + if (f->last_seg) + return z; + error(f, VORBIS_invalid_stream); + } + } + return z; +} + +static int codebook_decode(vorb *f, Codebook *c, float *output, int len) +{ + int i,z = codebook_decode_start(f,c); + if (z < 0) return FALSE; + if (len > c->dimensions) len = c->dimensions; + +#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK + if (c->lookup_type == 1) { + float last = CODEBOOK_ELEMENT_BASE(c); + int div = 1; + for (i=0; i < len; ++i) { + int off = (z / div) % c->lookup_values; + float val = CODEBOOK_ELEMENT_FAST(c,off) + last; + output[i] += val; + if (c->sequence_p) last = val + c->minimum_value; + div *= c->lookup_values; + } + return TRUE; + } +#endif + + z *= c->dimensions; + if (c->sequence_p) { + float last = CODEBOOK_ELEMENT_BASE(c); + for (i=0; i < len; ++i) { + float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; + output[i] += val; + last = val + c->minimum_value; + } + } else { + float last = CODEBOOK_ELEMENT_BASE(c); + for (i=0; i < len; ++i) { + output[i] += CODEBOOK_ELEMENT_FAST(c,z+i) + last; + } + } + + return TRUE; +} + +static int codebook_decode_step(vorb *f, Codebook *c, float *output, int len, int step) +{ + int i,z = codebook_decode_start(f,c); + float last = CODEBOOK_ELEMENT_BASE(c); + if (z < 0) return FALSE; + if (len > c->dimensions) len = c->dimensions; + +#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK + if (c->lookup_type == 1) { + int div = 1; + for (i=0; i < len; ++i) { + int off = (z / div) % c->lookup_values; + float val = CODEBOOK_ELEMENT_FAST(c,off) + last; + output[i*step] += val; + if (c->sequence_p) last = val; + div *= c->lookup_values; + } + return TRUE; + } +#endif + + z *= c->dimensions; + for (i=0; i < len; ++i) { + float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; + output[i*step] += val; + if (c->sequence_p) last = val; + } + + return TRUE; +} + +static int codebook_decode_deinterleave_repeat(vorb *f, Codebook *c, float **outputs, int ch, int *c_inter_p, int *p_inter_p, int len, int total_decode) +{ + int c_inter = *c_inter_p; + int p_inter = *p_inter_p; + int i,z, effective = c->dimensions; + + // type 0 is only legal in a scalar context + if (c->lookup_type == 0) return error(f, VORBIS_invalid_stream); + + while (total_decode > 0) { + float last = CODEBOOK_ELEMENT_BASE(c); + DECODE_VQ(z,f,c); + #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK + assert(!c->sparse || z < c->sorted_entries); + #endif + if (z < 0) { + if (!f->bytes_in_seg) + if (f->last_seg) return FALSE; + return error(f, VORBIS_invalid_stream); + } + + // if this will take us off the end of the buffers, stop short! + // we check by computing the length of the virtual interleaved + // buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter), + // and the length we'll be using (effective) + if (c_inter + p_inter*ch + effective > len * ch) { + effective = len*ch - (p_inter*ch - c_inter); + } + + #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK + if (c->lookup_type == 1) { + int div = 1; + for (i=0; i < effective; ++i) { + int off = (z / div) % c->lookup_values; + float val = CODEBOOK_ELEMENT_FAST(c,off) + last; + if (outputs[c_inter]) + outputs[c_inter][p_inter] += val; + if (++c_inter == ch) { c_inter = 0; ++p_inter; } + if (c->sequence_p) last = val; + div *= c->lookup_values; + } + } else + #endif + { + z *= c->dimensions; + if (c->sequence_p) { + for (i=0; i < effective; ++i) { + float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; + if (outputs[c_inter]) + outputs[c_inter][p_inter] += val; + if (++c_inter == ch) { c_inter = 0; ++p_inter; } + last = val; + } + } else { + for (i=0; i < effective; ++i) { + float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; + if (outputs[c_inter]) + outputs[c_inter][p_inter] += val; + if (++c_inter == ch) { c_inter = 0; ++p_inter; } + } + } + } + + total_decode -= effective; + } + *c_inter_p = c_inter; + *p_inter_p = p_inter; + return TRUE; +} + +static int predict_point(int x, int x0, int x1, int y0, int y1) +{ + int dy = y1 - y0; + int adx = x1 - x0; + // @OPTIMIZE: force int division to round in the right direction... is this necessary on x86? + int err = abs(dy) * (x - x0); + int off = err / adx; + return dy < 0 ? y0 - off : y0 + off; +} + +// the following table is block-copied from the specification +static float 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.0f +}; + + +// @OPTIMIZE: if you want to replace this bresenham line-drawing routine, +// note that you must produce bit-identical output to decode correctly; +// this specific sequence of operations is specified in the spec (it's +// drawing integer-quantized frequency-space lines that the encoder +// expects to be exactly the same) +// ... also, isn't the whole point of Bresenham's algorithm to NOT +// have to divide in the setup? sigh. +#ifndef STB_VORBIS_NO_DEFER_FLOOR +#define LINE_OP(a,b) a *= b +#else +#define LINE_OP(a,b) a = b +#endif + +#ifdef STB_VORBIS_DIVIDE_TABLE +#define DIVTAB_NUMER 32 +#define DIVTAB_DENOM 64 +int8 integer_divide_table[DIVTAB_NUMER][DIVTAB_DENOM]; // 2KB +#endif + +static __forceinline void draw_line(float *output, int x0, int y0, int x1, int y1, int n) +{ + int dy = y1 - y0; + int adx = x1 - x0; + int ady = abs(dy); + int base; + int x=x0,y=y0; + int err = 0; + int sy; + +#ifdef STB_VORBIS_DIVIDE_TABLE + if (adx < DIVTAB_DENOM && ady < DIVTAB_NUMER) { + if (dy < 0) { + base = -integer_divide_table[ady][adx]; + sy = base-1; + } else { + base = integer_divide_table[ady][adx]; + sy = base+1; + } + } else { + base = dy / adx; + if (dy < 0) + sy = base - 1; + else + sy = base+1; + } +#else + base = dy / adx; + if (dy < 0) + sy = base - 1; + else + sy = base+1; +#endif + ady -= abs(base) * adx; + if (x1 > n) x1 = n; + if (x < x1) { + LINE_OP(output[x], inverse_db_table[y&255]); + for (++x; x < x1; ++x) { + err += ady; + if (err >= adx) { + err -= adx; + y += sy; + } else + y += base; + LINE_OP(output[x], inverse_db_table[y&255]); + } + } +} + +static int residue_decode(vorb *f, Codebook *book, float *target, int offset, int n, int rtype) +{ + int k; + if (rtype == 0) { + int step = n / book->dimensions; + for (k=0; k < step; ++k) + if (!codebook_decode_step(f, book, target+offset+k, n-offset-k, step)) + return FALSE; + } else { + for (k=0; k < n; ) { + if (!codebook_decode(f, book, target+offset, n-k)) + return FALSE; + k += book->dimensions; + offset += book->dimensions; + } + } + return TRUE; +} + +// n is 1/2 of the blocksize -- +// specification: "Correct per-vector decode length is [n]/2" +static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int rn, uint8 *do_not_decode) +{ + int i,j,pass; + Residue *r = f->residue_config + rn; + int rtype = f->residue_types[rn]; + int c = r->classbook; + int classwords = f->codebooks[c].dimensions; + unsigned int actual_size = rtype == 2 ? n*2 : n; + unsigned int limit_r_begin = (r->begin < actual_size ? r->begin : actual_size); + unsigned int limit_r_end = (r->end < actual_size ? r->end : actual_size); + int n_read = limit_r_end - limit_r_begin; + int part_read = n_read / r->part_size; + int temp_alloc_point = temp_alloc_save(f); + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + uint8 ***part_classdata = (uint8 ***) temp_block_array(f,f->channels, part_read * sizeof(**part_classdata)); + #else + int **classifications = (int **) temp_block_array(f,f->channels, part_read * sizeof(**classifications)); + #endif + + CHECK(f); + + for (i=0; i < ch; ++i) + if (!do_not_decode[i]) + memset(residue_buffers[i], 0, sizeof(float) * n); + + if (rtype == 2 && ch != 1) { + for (j=0; j < ch; ++j) + if (!do_not_decode[j]) + break; + if (j == ch) + goto done; + + for (pass=0; pass < 8; ++pass) { + int pcount = 0, class_set = 0; + if (ch == 2) { + while (pcount < part_read) { + int z = r->begin + pcount*r->part_size; + int c_inter = (z & 1), p_inter = z>>1; + if (pass == 0) { + Codebook *c = f->codebooks+r->classbook; + int q; + DECODE(q,f,c); + if (q == EOP) goto done; + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + part_classdata[0][class_set] = r->classdata[q]; + #else + for (i=classwords-1; i >= 0; --i) { + classifications[0][i+pcount] = q % r->classifications; + q /= r->classifications; + } + #endif + } + for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) { + int z = r->begin + pcount*r->part_size; + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + int c = part_classdata[0][class_set][i]; + #else + int c = classifications[0][pcount]; + #endif + int b = r->residue_books[c][pass]; + if (b >= 0) { + Codebook *book = f->codebooks + b; + #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK + if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size)) + goto done; + #else + // saves 1% + if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size)) + goto done; + #endif + } else { + z += r->part_size; + c_inter = z & 1; + p_inter = z >> 1; + } + } + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + ++class_set; + #endif + } + } else if (ch > 2) { + while (pcount < part_read) { + int z = r->begin + pcount*r->part_size; + int c_inter = z % ch, p_inter = z/ch; + if (pass == 0) { + Codebook *c = f->codebooks+r->classbook; + int q; + DECODE(q,f,c); + if (q == EOP) goto done; + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + part_classdata[0][class_set] = r->classdata[q]; + #else + for (i=classwords-1; i >= 0; --i) { + classifications[0][i+pcount] = q % r->classifications; + q /= r->classifications; + } + #endif + } + for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) { + int z = r->begin + pcount*r->part_size; + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + int c = part_classdata[0][class_set][i]; + #else + int c = classifications[0][pcount]; + #endif + int b = r->residue_books[c][pass]; + if (b >= 0) { + Codebook *book = f->codebooks + b; + if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size)) + goto done; + } else { + z += r->part_size; + c_inter = z % ch; + p_inter = z / ch; + } + } + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + ++class_set; + #endif + } + } + } + goto done; + } + CHECK(f); + + for (pass=0; pass < 8; ++pass) { + int pcount = 0, class_set=0; + while (pcount < part_read) { + if (pass == 0) { + for (j=0; j < ch; ++j) { + if (!do_not_decode[j]) { + Codebook *c = f->codebooks+r->classbook; + int temp; + DECODE(temp,f,c); + if (temp == EOP) goto done; + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + part_classdata[j][class_set] = r->classdata[temp]; + #else + for (i=classwords-1; i >= 0; --i) { + classifications[j][i+pcount] = temp % r->classifications; + temp /= r->classifications; + } + #endif + } + } + } + for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) { + for (j=0; j < ch; ++j) { + if (!do_not_decode[j]) { + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + int c = part_classdata[j][class_set][i]; + #else + int c = classifications[j][pcount]; + #endif + int b = r->residue_books[c][pass]; + if (b >= 0) { + float *target = residue_buffers[j]; + int offset = r->begin + pcount * r->part_size; + int n = r->part_size; + Codebook *book = f->codebooks + b; + if (!residue_decode(f, book, target, offset, n, rtype)) + goto done; + } + } + } + } + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + ++class_set; + #endif + } + } + done: + CHECK(f); + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + temp_free(f,part_classdata); + #else + temp_free(f,classifications); + #endif + temp_alloc_restore(f,temp_alloc_point); +} + + +#if 0 +// slow way for debugging +void inverse_mdct_slow(float *buffer, int n) +{ + int i,j; + int n2 = n >> 1; + float *x = (float *) malloc(sizeof(*x) * n2); + memcpy(x, buffer, sizeof(*x) * n2); + for (i=0; i < n; ++i) { + float acc = 0; + for (j=0; j < n2; ++j) + // formula from paper: + //acc += n/4.0f * x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n/2.0)*(2*j+1)); + // formula from wikipedia + //acc += 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5)); + // these are equivalent, except the formula from the paper inverts the multiplier! + // however, what actually works is NO MULTIPLIER!?! + //acc += 64 * 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5)); + acc += x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n/2.0)*(2*j+1)); + buffer[i] = acc; + } + free(x); +} +#elif 0 +// same as above, but just barely able to run in real time on modern machines +void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype) +{ + float mcos[16384]; + int i,j; + int n2 = n >> 1, nmask = (n << 2) -1; + float *x = (float *) malloc(sizeof(*x) * n2); + memcpy(x, buffer, sizeof(*x) * n2); + for (i=0; i < 4*n; ++i) + mcos[i] = (float) cos(M_PI / 2 * i / n); + + for (i=0; i < n; ++i) { + float acc = 0; + for (j=0; j < n2; ++j) + acc += x[j] * mcos[(2 * i + 1 + n2)*(2*j+1) & nmask]; + buffer[i] = acc; + } + free(x); +} +#elif 0 +// transform to use a slow dct-iv; this is STILL basically trivial, +// but only requires half as many ops +void dct_iv_slow(float *buffer, int n) +{ + float mcos[16384]; + float x[2048]; + int i,j; + int n2 = n >> 1, nmask = (n << 3) - 1; + memcpy(x, buffer, sizeof(*x) * n); + for (i=0; i < 8*n; ++i) + mcos[i] = (float) cos(M_PI / 4 * i / n); + for (i=0; i < n; ++i) { + float acc = 0; + for (j=0; j < n; ++j) + acc += x[j] * mcos[((2 * i + 1)*(2*j+1)) & nmask]; + buffer[i] = acc; + } +} + +void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype) +{ + int i, n4 = n >> 2, n2 = n >> 1, n3_4 = n - n4; + float temp[4096]; + + memcpy(temp, buffer, n2 * sizeof(float)); + dct_iv_slow(temp, n2); // returns -c'-d, a-b' + + for (i=0; i < n4 ; ++i) buffer[i] = temp[i+n4]; // a-b' + for ( ; i < n3_4; ++i) buffer[i] = -temp[n3_4 - i - 1]; // b-a', c+d' + for ( ; i < n ; ++i) buffer[i] = -temp[i - n3_4]; // c'+d +} +#endif + +#ifndef LIBVORBIS_MDCT +#define LIBVORBIS_MDCT 0 +#endif + +#if LIBVORBIS_MDCT +// directly call the vorbis MDCT using an interface documented +// by Jeff Roberts... useful for performance comparison +typedef struct +{ + int n; + int log2n; + + float *trig; + int *bitrev; + + float scale; +} mdct_lookup; + +extern void mdct_init(mdct_lookup *lookup, int n); +extern void mdct_clear(mdct_lookup *l); +extern void mdct_backward(mdct_lookup *init, float *in, float *out); + +mdct_lookup M1,M2; + +void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) +{ + mdct_lookup *M; + if (M1.n == n) M = &M1; + else if (M2.n == n) M = &M2; + else if (M1.n == 0) { mdct_init(&M1, n); M = &M1; } + else { + if (M2.n) __asm int 3; + mdct_init(&M2, n); + M = &M2; + } + + mdct_backward(M, buffer, buffer); +} +#endif + + +// the following were split out into separate functions while optimizing; +// they could be pushed back up but eh. __forceinline showed no change; +// they're probably already being inlined. +static void imdct_step3_iter0_loop(int n, float *e, int i_off, int k_off, float *A) +{ + float *ee0 = e + i_off; + float *ee2 = ee0 + k_off; + int i; + + assert((n & 3) == 0); + for (i=(n>>2); i > 0; --i) { + float k00_20, k01_21; + k00_20 = ee0[ 0] - ee2[ 0]; + k01_21 = ee0[-1] - ee2[-1]; + ee0[ 0] += ee2[ 0];//ee0[ 0] = ee0[ 0] + ee2[ 0]; + ee0[-1] += ee2[-1];//ee0[-1] = ee0[-1] + ee2[-1]; + ee2[ 0] = k00_20 * A[0] - k01_21 * A[1]; + ee2[-1] = k01_21 * A[0] + k00_20 * A[1]; + A += 8; + + k00_20 = ee0[-2] - ee2[-2]; + k01_21 = ee0[-3] - ee2[-3]; + ee0[-2] += ee2[-2];//ee0[-2] = ee0[-2] + ee2[-2]; + ee0[-3] += ee2[-3];//ee0[-3] = ee0[-3] + ee2[-3]; + ee2[-2] = k00_20 * A[0] - k01_21 * A[1]; + ee2[-3] = k01_21 * A[0] + k00_20 * A[1]; + A += 8; + + k00_20 = ee0[-4] - ee2[-4]; + k01_21 = ee0[-5] - ee2[-5]; + ee0[-4] += ee2[-4];//ee0[-4] = ee0[-4] + ee2[-4]; + ee0[-5] += ee2[-5];//ee0[-5] = ee0[-5] + ee2[-5]; + ee2[-4] = k00_20 * A[0] - k01_21 * A[1]; + ee2[-5] = k01_21 * A[0] + k00_20 * A[1]; + A += 8; + + k00_20 = ee0[-6] - ee2[-6]; + k01_21 = ee0[-7] - ee2[-7]; + ee0[-6] += ee2[-6];//ee0[-6] = ee0[-6] + ee2[-6]; + ee0[-7] += ee2[-7];//ee0[-7] = ee0[-7] + ee2[-7]; + ee2[-6] = k00_20 * A[0] - k01_21 * A[1]; + ee2[-7] = k01_21 * A[0] + k00_20 * A[1]; + A += 8; + ee0 -= 8; + ee2 -= 8; + } +} + +static void imdct_step3_inner_r_loop(int lim, float *e, int d0, int k_off, float *A, int k1) +{ + int i; + float k00_20, k01_21; + + float *e0 = e + d0; + float *e2 = e0 + k_off; + + for (i=lim >> 2; i > 0; --i) { + k00_20 = e0[-0] - e2[-0]; + k01_21 = e0[-1] - e2[-1]; + e0[-0] += e2[-0];//e0[-0] = e0[-0] + e2[-0]; + e0[-1] += e2[-1];//e0[-1] = e0[-1] + e2[-1]; + e2[-0] = (k00_20)*A[0] - (k01_21) * A[1]; + e2[-1] = (k01_21)*A[0] + (k00_20) * A[1]; + + A += k1; + + k00_20 = e0[-2] - e2[-2]; + k01_21 = e0[-3] - e2[-3]; + e0[-2] += e2[-2];//e0[-2] = e0[-2] + e2[-2]; + e0[-3] += e2[-3];//e0[-3] = e0[-3] + e2[-3]; + e2[-2] = (k00_20)*A[0] - (k01_21) * A[1]; + e2[-3] = (k01_21)*A[0] + (k00_20) * A[1]; + + A += k1; + + k00_20 = e0[-4] - e2[-4]; + k01_21 = e0[-5] - e2[-5]; + e0[-4] += e2[-4];//e0[-4] = e0[-4] + e2[-4]; + e0[-5] += e2[-5];//e0[-5] = e0[-5] + e2[-5]; + e2[-4] = (k00_20)*A[0] - (k01_21) * A[1]; + e2[-5] = (k01_21)*A[0] + (k00_20) * A[1]; + + A += k1; + + k00_20 = e0[-6] - e2[-6]; + k01_21 = e0[-7] - e2[-7]; + e0[-6] += e2[-6];//e0[-6] = e0[-6] + e2[-6]; + e0[-7] += e2[-7];//e0[-7] = e0[-7] + e2[-7]; + e2[-6] = (k00_20)*A[0] - (k01_21) * A[1]; + e2[-7] = (k01_21)*A[0] + (k00_20) * A[1]; + + e0 -= 8; + e2 -= 8; + + A += k1; + } +} + +static void imdct_step3_inner_s_loop(int n, float *e, int i_off, int k_off, float *A, int a_off, int k0) +{ + int i; + float A0 = A[0]; + float A1 = A[0+1]; + float A2 = A[0+a_off]; + float A3 = A[0+a_off+1]; + float A4 = A[0+a_off*2+0]; + float A5 = A[0+a_off*2+1]; + float A6 = A[0+a_off*3+0]; + float A7 = A[0+a_off*3+1]; + + float k00,k11; + + float *ee0 = e +i_off; + float *ee2 = ee0+k_off; + + for (i=n; i > 0; --i) { + k00 = ee0[ 0] - ee2[ 0]; + k11 = ee0[-1] - ee2[-1]; + ee0[ 0] = ee0[ 0] + ee2[ 0]; + ee0[-1] = ee0[-1] + ee2[-1]; + ee2[ 0] = (k00) * A0 - (k11) * A1; + ee2[-1] = (k11) * A0 + (k00) * A1; + + k00 = ee0[-2] - ee2[-2]; + k11 = ee0[-3] - ee2[-3]; + ee0[-2] = ee0[-2] + ee2[-2]; + ee0[-3] = ee0[-3] + ee2[-3]; + ee2[-2] = (k00) * A2 - (k11) * A3; + ee2[-3] = (k11) * A2 + (k00) * A3; + + k00 = ee0[-4] - ee2[-4]; + k11 = ee0[-5] - ee2[-5]; + ee0[-4] = ee0[-4] + ee2[-4]; + ee0[-5] = ee0[-5] + ee2[-5]; + ee2[-4] = (k00) * A4 - (k11) * A5; + ee2[-5] = (k11) * A4 + (k00) * A5; + + k00 = ee0[-6] - ee2[-6]; + k11 = ee0[-7] - ee2[-7]; + ee0[-6] = ee0[-6] + ee2[-6]; + ee0[-7] = ee0[-7] + ee2[-7]; + ee2[-6] = (k00) * A6 - (k11) * A7; + ee2[-7] = (k11) * A6 + (k00) * A7; + + ee0 -= k0; + ee2 -= k0; + } +} + +static __forceinline void iter_54(float *z) +{ + float k00,k11,k22,k33; + float y0,y1,y2,y3; + + k00 = z[ 0] - z[-4]; + y0 = z[ 0] + z[-4]; + y2 = z[-2] + z[-6]; + k22 = z[-2] - z[-6]; + + z[-0] = y0 + y2; // z0 + z4 + z2 + z6 + z[-2] = y0 - y2; // z0 + z4 - z2 - z6 + + // done with y0,y2 + + k33 = z[-3] - z[-7]; + + z[-4] = k00 + k33; // z0 - z4 + z3 - z7 + z[-6] = k00 - k33; // z0 - z4 - z3 + z7 + + // done with k33 + + k11 = z[-1] - z[-5]; + y1 = z[-1] + z[-5]; + y3 = z[-3] + z[-7]; + + z[-1] = y1 + y3; // z1 + z5 + z3 + z7 + z[-3] = y1 - y3; // z1 + z5 - z3 - z7 + z[-5] = k11 - k22; // z1 - z5 + z2 - z6 + z[-7] = k11 + k22; // z1 - z5 - z2 + z6 +} + +static void imdct_step3_inner_s_loop_ld654(int n, float *e, int i_off, float *A, int base_n) +{ + int a_off = base_n >> 3; + float A2 = A[0+a_off]; + float *z = e + i_off; + float *base = z - 16 * n; + + while (z > base) { + float k00,k11; + float l00,l11; + + k00 = z[-0] - z[ -8]; + k11 = z[-1] - z[ -9]; + l00 = z[-2] - z[-10]; + l11 = z[-3] - z[-11]; + z[ -0] = z[-0] + z[ -8]; + z[ -1] = z[-1] + z[ -9]; + z[ -2] = z[-2] + z[-10]; + z[ -3] = z[-3] + z[-11]; + z[ -8] = k00; + z[ -9] = k11; + z[-10] = (l00+l11) * A2; + z[-11] = (l11-l00) * A2; + + k00 = z[ -4] - z[-12]; + k11 = z[ -5] - z[-13]; + l00 = z[ -6] - z[-14]; + l11 = z[ -7] - z[-15]; + z[ -4] = z[ -4] + z[-12]; + z[ -5] = z[ -5] + z[-13]; + z[ -6] = z[ -6] + z[-14]; + z[ -7] = z[ -7] + z[-15]; + z[-12] = k11; + z[-13] = -k00; + z[-14] = (l11-l00) * A2; + z[-15] = (l00+l11) * -A2; + + iter_54(z); + iter_54(z-8); + z -= 16; + } +} + +static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) +{ + int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l; + int ld; + // @OPTIMIZE: reduce register pressure by using fewer variables? + int save_point = temp_alloc_save(f); + float *buf2 = (float *) temp_alloc(f, n2 * sizeof(*buf2)); + float *u=NULL,*v=NULL; + // twiddle factors + float *A = f->A[blocktype]; + + // IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio" + // See notes about bugs in that paper in less-optimal implementation 'inverse_mdct_old' after this function. + + // kernel from paper + + + // merged: + // copy and reflect spectral data + // step 0 + + // note that it turns out that the items added together during + // this step are, in fact, being added to themselves (as reflected + // by step 0). inexplicable inefficiency! this became obvious + // once I combined the passes. + + // so there's a missing 'times 2' here (for adding X to itself). + // this propagates through linearly to the end, where the numbers + // are 1/2 too small, and need to be compensated for. + + { + float *d,*e, *AA, *e_stop; + d = &buf2[n2-2]; + AA = A; + e = &buffer[0]; + e_stop = &buffer[n2]; + while (e != e_stop) { + d[1] = (e[0] * AA[0] - e[2]*AA[1]); + d[0] = (e[0] * AA[1] + e[2]*AA[0]); + d -= 2; + AA += 2; + e += 4; + } + + e = &buffer[n2-3]; + while (d >= buf2) { + d[1] = (-e[2] * AA[0] - -e[0]*AA[1]); + d[0] = (-e[2] * AA[1] + -e[0]*AA[0]); + d -= 2; + AA += 2; + e -= 4; + } + } + + // now we use symbolic names for these, so that we can + // possibly swap their meaning as we change which operations + // are in place + + u = buffer; + v = buf2; + + // step 2 (paper output is w, now u) + // this could be in place, but the data ends up in the wrong + // place... _somebody_'s got to swap it, so this is nominated + { + float *AA = &A[n2-8]; + float *d0,*d1, *e0, *e1; + + e0 = &v[n4]; + e1 = &v[0]; + + d0 = &u[n4]; + d1 = &u[0]; + + while (AA >= A) { + float v40_20, v41_21; + + v41_21 = e0[1] - e1[1]; + v40_20 = e0[0] - e1[0]; + d0[1] = e0[1] + e1[1]; + d0[0] = e0[0] + e1[0]; + d1[1] = v41_21*AA[4] - v40_20*AA[5]; + d1[0] = v40_20*AA[4] + v41_21*AA[5]; + + v41_21 = e0[3] - e1[3]; + v40_20 = e0[2] - e1[2]; + d0[3] = e0[3] + e1[3]; + d0[2] = e0[2] + e1[2]; + d1[3] = v41_21*AA[0] - v40_20*AA[1]; + d1[2] = v40_20*AA[0] + v41_21*AA[1]; + + AA -= 8; + + d0 += 4; + d1 += 4; + e0 += 4; + e1 += 4; + } + } + + // step 3 + ld = ilog(n) - 1; // ilog is off-by-one from normal definitions + + // optimized step 3: + + // the original step3 loop can be nested r inside s or s inside r; + // it's written originally as s inside r, but this is dumb when r + // iterates many times, and s few. So I have two copies of it and + // switch between them halfway. + + // this is iteration 0 of step 3 + imdct_step3_iter0_loop(n >> 4, u, n2-1-n4*0, -(n >> 3), A); + imdct_step3_iter0_loop(n >> 4, u, n2-1-n4*1, -(n >> 3), A); + + // this is iteration 1 of step 3 + imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*0, -(n >> 4), A, 16); + imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*1, -(n >> 4), A, 16); + imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*2, -(n >> 4), A, 16); + imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*3, -(n >> 4), A, 16); + + l=2; + for (; l < (ld-3)>>1; ++l) { + int k0 = n >> (l+2), k0_2 = k0>>1; + int lim = 1 << (l+1); + int i; + for (i=0; i < lim; ++i) + imdct_step3_inner_r_loop(n >> (l+4), u, n2-1 - k0*i, -k0_2, A, 1 << (l+3)); + } + + for (; l < ld-6; ++l) { + int k0 = n >> (l+2), k1 = 1 << (l+3), k0_2 = k0>>1; + int rlim = n >> (l+6), r; + int lim = 1 << (l+1); + int i_off; + float *A0 = A; + i_off = n2-1; + for (r=rlim; r > 0; --r) { + imdct_step3_inner_s_loop(lim, u, i_off, -k0_2, A0, k1, k0); + A0 += k1*4; + i_off -= 8; + } + } + + // iterations with count: + // ld-6,-5,-4 all interleaved together + // the big win comes from getting rid of needless flops + // due to the constants on pass 5 & 4 being all 1 and 0; + // combining them to be simultaneous to improve cache made little difference + imdct_step3_inner_s_loop_ld654(n >> 5, u, n2-1, A, n); + + // output is u + + // step 4, 5, and 6 + // cannot be in-place because of step 5 + { + uint16 *bitrev = f->bit_reverse[blocktype]; + // weirdly, I'd have thought reading sequentially and writing + // erratically would have been better than vice-versa, but in + // fact that's not what my testing showed. (That is, with + // j = bitreverse(i), do you read i and write j, or read j and write i.) + + float *d0 = &v[n4-4]; + float *d1 = &v[n2-4]; + while (d0 >= v) { + int k4; + + k4 = bitrev[0]; + d1[3] = u[k4+0]; + d1[2] = u[k4+1]; + d0[3] = u[k4+2]; + d0[2] = u[k4+3]; + + k4 = bitrev[1]; + d1[1] = u[k4+0]; + d1[0] = u[k4+1]; + d0[1] = u[k4+2]; + d0[0] = u[k4+3]; + + d0 -= 4; + d1 -= 4; + bitrev += 2; + } + } + // (paper output is u, now v) + + + // data must be in buf2 + assert(v == buf2); + + // step 7 (paper output is v, now v) + // this is now in place + { + float *C = f->C[blocktype]; + float *d, *e; + + d = v; + e = v + n2 - 4; + + while (d < e) { + float a02,a11,b0,b1,b2,b3; + + a02 = d[0] - e[2]; + a11 = d[1] + e[3]; + + b0 = C[1]*a02 + C[0]*a11; + b1 = C[1]*a11 - C[0]*a02; + + b2 = d[0] + e[ 2]; + b3 = d[1] - e[ 3]; + + d[0] = b2 + b0; + d[1] = b3 + b1; + e[2] = b2 - b0; + e[3] = b1 - b3; + + a02 = d[2] - e[0]; + a11 = d[3] + e[1]; + + b0 = C[3]*a02 + C[2]*a11; + b1 = C[3]*a11 - C[2]*a02; + + b2 = d[2] + e[ 0]; + b3 = d[3] - e[ 1]; + + d[2] = b2 + b0; + d[3] = b3 + b1; + e[0] = b2 - b0; + e[1] = b1 - b3; + + C += 4; + d += 4; + e -= 4; + } + } + + // data must be in buf2 + + + // step 8+decode (paper output is X, now buffer) + // this generates pairs of data a la 8 and pushes them directly through + // the decode kernel (pushing rather than pulling) to avoid having + // to make another pass later + + // this cannot POSSIBLY be in place, so we refer to the buffers directly + + { + float *d0,*d1,*d2,*d3; + + float *B = f->B[blocktype] + n2 - 8; + float *e = buf2 + n2 - 8; + d0 = &buffer[0]; + d1 = &buffer[n2-4]; + d2 = &buffer[n2]; + d3 = &buffer[n-4]; + while (e >= v) { + float p0,p1,p2,p3; + + p3 = e[6]*B[7] - e[7]*B[6]; + p2 = -e[6]*B[6] - e[7]*B[7]; + + d0[0] = p3; + d1[3] = - p3; + d2[0] = p2; + d3[3] = p2; + + p1 = e[4]*B[5] - e[5]*B[4]; + p0 = -e[4]*B[4] - e[5]*B[5]; + + d0[1] = p1; + d1[2] = - p1; + d2[1] = p0; + d3[2] = p0; + + p3 = e[2]*B[3] - e[3]*B[2]; + p2 = -e[2]*B[2] - e[3]*B[3]; + + d0[2] = p3; + d1[1] = - p3; + d2[2] = p2; + d3[1] = p2; + + p1 = e[0]*B[1] - e[1]*B[0]; + p0 = -e[0]*B[0] - e[1]*B[1]; + + d0[3] = p1; + d1[0] = - p1; + d2[3] = p0; + d3[0] = p0; + + B -= 8; + e -= 8; + d0 += 4; + d2 += 4; + d1 -= 4; + d3 -= 4; + } + } + + temp_free(f,buf2); + temp_alloc_restore(f,save_point); +} + +#if 0 +// this is the original version of the above code, if you want to optimize it from scratch +void inverse_mdct_naive(float *buffer, int n) +{ + float s; + float A[1 << 12], B[1 << 12], C[1 << 11]; + int i,k,k2,k4, n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l; + int n3_4 = n - n4, ld; + // how can they claim this only uses N words?! + // oh, because they're only used sparsely, whoops + float u[1 << 13], X[1 << 13], v[1 << 13], w[1 << 13]; + // set up twiddle factors + + for (k=k2=0; k < n4; ++k,k2+=2) { + A[k2 ] = (float) cos(4*k*M_PI/n); + A[k2+1] = (float) -sin(4*k*M_PI/n); + B[k2 ] = (float) cos((k2+1)*M_PI/n/2); + B[k2+1] = (float) sin((k2+1)*M_PI/n/2); + } + for (k=k2=0; k < n8; ++k,k2+=2) { + C[k2 ] = (float) cos(2*(k2+1)*M_PI/n); + C[k2+1] = (float) -sin(2*(k2+1)*M_PI/n); + } + + // IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio" + // Note there are bugs in that pseudocode, presumably due to them attempting + // to rename the arrays nicely rather than representing the way their actual + // implementation bounces buffers back and forth. As a result, even in the + // "some formulars corrected" version, a direct implementation fails. These + // are noted below as "paper bug". + + // copy and reflect spectral data + for (k=0; k < n2; ++k) u[k] = buffer[k]; + for ( ; k < n ; ++k) u[k] = -buffer[n - k - 1]; + // kernel from paper + // step 1 + for (k=k2=k4=0; k < n4; k+=1, 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=k4=0; k < n8; k+=1, 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 + ld = ilog(n) - 1; // ilog is off-by-one from normal definitions + for (l=0; l < ld-3; ++l) { + int k0 = n >> (l+2), k1 = 1 << (l+3); + int rlim = n >> (l+4), r4, r; + int s2lim = 1 << (l+2), s2; + for (r=r4=0; r < rlim; r4+=4,++r) { + for (s2=0; s2 < s2lim; 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-3) { + // paper bug: ping-ponging of u&w here is omitted + memcpy(w, u, sizeof(u)); + } + } + + // step 4 + for (i=0; i < n8; ++i) { + int j = bit_reverse(i) >> (32-ld+3); + assert(j < n8); + if (i == j) { + // paper bug: original code probably swapped in place; if copying, + // need to directly copy in this case + int 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) { + int 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; k < n2; ++k) { + w[k] = v[k*2+1]; + } + // step 6 + for (k=k2=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=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]))/2; + 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]))/2; + v[n2+1+ k2] = ( u[n2+1+k2] - u[n-1-k2] + C[k2+1]*(u[n2+1+k2]+u[n-1-k2]) - C[k2]*(u[n2+k2]-u[n-2-k2]))/2; + v[n-1 - k2] = (-u[n2+1+k2] + u[n-1-k2] + C[k2+1]*(u[n2+1+k2]+u[n-1-k2]) - C[k2]*(u[n2+k2]-u[n-2-k2]))/2; + } + // step 8 + for (k=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 ]; + } + + // decode kernel to output + // determined the following value experimentally + // (by first figuring out what made inverse_mdct_slow work); then matching that here + // (probably vorbis encoder premultiplies by n or n/2, to save it on the decoder?) + s = 0.5; // theoretically would be n4 + + // [[[ note! the s value of 0.5 is compensated for by the B[] in the current code, + // so it needs to use the "old" B values to behave correctly, or else + // set s to 1.0 ]]] + for (i=0; i < n4 ; ++i) buffer[i] = s * X[i+n4]; + for ( ; i < n3_4; ++i) buffer[i] = -s * X[n3_4 - i - 1]; + for ( ; i < n ; ++i) buffer[i] = -s * X[i - n3_4]; +} +#endif + +static float *get_window(vorb *f, int len) +{ + len <<= 1; + if (len == f->blocksize_0) return f->window[0]; + if (len == f->blocksize_1) return f->window[1]; + return NULL; +} + +#ifndef STB_VORBIS_NO_DEFER_FLOOR +typedef int16 YTYPE; +#else +typedef int YTYPE; +#endif +static int do_floor(vorb *f, Mapping *map, int i, int n, float *target, YTYPE *finalY, uint8 *step2_flag) +{ + int n2 = n >> 1; + int s = map->chan[i].mux, floor; + floor = map->submap_floor[s]; + if (f->floor_types[floor] == 0) { + return error(f, VORBIS_invalid_stream); + } else { + Floor1 *g = &f->floor_config[floor].floor1; + int j,q; + int lx = 0, ly = finalY[0] * g->floor1_multiplier; + for (q=1; q < g->values; ++q) { + j = g->sorted_order[q]; + #ifndef STB_VORBIS_NO_DEFER_FLOOR + STBV_NOTUSED(step2_flag); + if (finalY[j] >= 0) + #else + if (step2_flag[j]) + #endif + { + int hy = finalY[j] * g->floor1_multiplier; + int hx = g->Xlist[j]; + if (lx != hx) + draw_line(target, lx,ly, hx,hy, n2); + CHECK(f); + lx = hx, ly = hy; + } + } + if (lx < n2) { + // optimization of: draw_line(target, lx,ly, n,ly, n2); + for (j=lx; j < n2; ++j) + LINE_OP(target[j], inverse_db_table[ly]); + CHECK(f); + } + } + return TRUE; +} + +// The meaning of "left" and "right" +// +// For a given frame: +// we compute samples from 0..n +// window_center is n/2 +// we'll window and mix the samples from left_start to left_end with data from the previous frame +// all of the samples from left_end to right_start can be output without mixing; however, +// this interval is 0-length except when transitioning between short and long frames +// all of the samples from right_start to right_end need to be mixed with the next frame, +// which we don't have, so those get saved in a buffer +// frame N's right_end-right_start, the number of samples to mix with the next frame, +// has to be the same as frame N+1's left_end-left_start (which they are by +// construction) + +static int vorbis_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode) +{ + Mode *m; + int i, n, prev, next, window_center; + f->channel_buffer_start = f->channel_buffer_end = 0; + + retry: + if (f->eof) return FALSE; + if (!maybe_start_packet(f)) + return FALSE; + // check packet type + if (get_bits(f,1) != 0) { + if (IS_PUSH_MODE(f)) + return error(f,VORBIS_bad_packet_type); + while (EOP != get8_packet(f)); + goto retry; + } + + if (f->alloc.alloc_buffer) + assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset); + + i = get_bits(f, ilog(f->mode_count-1)); + if (i == EOP) return FALSE; + if (i >= f->mode_count) return FALSE; + *mode = i; + m = f->mode_config + i; + if (m->blockflag) { + n = f->blocksize_1; + prev = get_bits(f,1); + next = get_bits(f,1); + } else { + prev = next = 0; + n = f->blocksize_0; + } + +// WINDOWING + + window_center = n >> 1; + if (m->blockflag && !prev) { + *p_left_start = (n - f->blocksize_0) >> 2; + *p_left_end = (n + f->blocksize_0) >> 2; + } else { + *p_left_start = 0; + *p_left_end = window_center; + } + if (m->blockflag && !next) { + *p_right_start = (n*3 - f->blocksize_0) >> 2; + *p_right_end = (n*3 + f->blocksize_0) >> 2; + } else { + *p_right_start = window_center; + *p_right_end = n; + } + + return TRUE; +} + +static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, int left_end, int right_start, int right_end, int *p_left) +{ + Mapping *map; + int i,j,k,n,n2; + int zero_channel[256]; + int really_zero_channel[256]; + +// WINDOWING + + STBV_NOTUSED(left_end); + n = f->blocksize[m->blockflag]; + map = &f->mapping[m->mapping]; + +// FLOORS + n2 = n >> 1; + + CHECK(f); + + for (i=0; i < f->channels; ++i) { + int s = map->chan[i].mux, floor; + zero_channel[i] = FALSE; + floor = map->submap_floor[s]; + if (f->floor_types[floor] == 0) { + return error(f, VORBIS_invalid_stream); + } else { + Floor1 *g = &f->floor_config[floor].floor1; + if (get_bits(f, 1)) { + short *finalY; + uint8 step2_flag[256]; + static int range_list[4] = { 256, 128, 86, 64 }; + int range = range_list[g->floor1_multiplier-1]; + int offset = 2; + finalY = f->finalY[i]; + finalY[0] = get_bits(f, ilog(range)-1); + finalY[1] = get_bits(f, ilog(range)-1); + for (j=0; j < g->partitions; ++j) { + int pclass = g->partition_class_list[j]; + int cdim = g->class_dimensions[pclass]; + int cbits = g->class_subclasses[pclass]; + int csub = (1 << cbits)-1; + int cval = 0; + if (cbits) { + Codebook *c = f->codebooks + g->class_masterbooks[pclass]; + DECODE(cval,f,c); + } + for (k=0; k < cdim; ++k) { + int book = g->subclass_books[pclass][cval & csub]; + cval = cval >> cbits; + if (book >= 0) { + int temp; + Codebook *c = f->codebooks + book; + DECODE(temp,f,c); + finalY[offset++] = temp; + } else + finalY[offset++] = 0; + } + } + if (f->valid_bits == INVALID_BITS) goto error; // behavior according to spec + step2_flag[0] = step2_flag[1] = 1; + for (j=2; j < g->values; ++j) { + int low, high, pred, highroom, lowroom, room, val; + low = g->neighbors[j][0]; + high = g->neighbors[j][1]; + //neighbors(g->Xlist, j, &low, &high); + pred = predict_point(g->Xlist[j], g->Xlist[low], g->Xlist[high], finalY[low], finalY[high]); + val = finalY[j]; + highroom = range - pred; + lowroom = pred; + if (highroom < lowroom) + room = highroom * 2; + else + room = lowroom * 2; + if (val) { + step2_flag[low] = step2_flag[high] = 1; + step2_flag[j] = 1; + if (val >= room) + if (highroom > lowroom) + finalY[j] = val - lowroom + pred; + else + finalY[j] = pred - val + highroom - 1; + else + if (val & 1) + finalY[j] = pred - ((val+1)>>1); + else + finalY[j] = pred + (val>>1); + } else { + step2_flag[j] = 0; + finalY[j] = pred; + } + } + +#ifdef STB_VORBIS_NO_DEFER_FLOOR + do_floor(f, map, i, n, f->floor_buffers[i], finalY, step2_flag); +#else + // defer final floor computation until _after_ residue + for (j=0; j < g->values; ++j) { + if (!step2_flag[j]) + finalY[j] = -1; + } +#endif + } else { + error: + zero_channel[i] = TRUE; + } + // So we just defer everything else to later + + // at this point we've decoded the floor into buffer + } + } + CHECK(f); + // at this point we've decoded all floors + + if (f->alloc.alloc_buffer) + assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset); + + // re-enable coupled channels if necessary + memcpy(really_zero_channel, zero_channel, sizeof(really_zero_channel[0]) * f->channels); + for (i=0; i < map->coupling_steps; ++i) + if (!zero_channel[map->chan[i].magnitude] || !zero_channel[map->chan[i].angle]) { + zero_channel[map->chan[i].magnitude] = zero_channel[map->chan[i].angle] = FALSE; + } + + CHECK(f); +// RESIDUE DECODE + for (i=0; i < map->submaps; ++i) { + float *residue_buffers[STB_VORBIS_MAX_CHANNELS]; + int r; + uint8 do_not_decode[256]; + int ch = 0; + for (j=0; j < f->channels; ++j) { + if (map->chan[j].mux == i) { + if (zero_channel[j]) { + do_not_decode[ch] = TRUE; + residue_buffers[ch] = NULL; + } else { + do_not_decode[ch] = FALSE; + residue_buffers[ch] = f->channel_buffers[j]; + } + ++ch; + } + } + r = map->submap_residue[i]; + decode_residue(f, residue_buffers, ch, n2, r, do_not_decode); + } + + if (f->alloc.alloc_buffer) + assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset); + CHECK(f); + +// INVERSE COUPLING + for (i = map->coupling_steps-1; i >= 0; --i) { + int n2 = n >> 1; + float *m = f->channel_buffers[map->chan[i].magnitude]; + float *a = f->channel_buffers[map->chan[i].angle ]; + for (j=0; j < n2; ++j) { + float a2,m2; + if (m[j] > 0) + if (a[j] > 0) + m2 = m[j], a2 = m[j] - a[j]; + else + a2 = m[j], m2 = m[j] + a[j]; + else + if (a[j] > 0) + m2 = m[j], a2 = m[j] + a[j]; + else + a2 = m[j], m2 = m[j] - a[j]; + m[j] = m2; + a[j] = a2; + } + } + CHECK(f); + + // finish decoding the floors +#ifndef STB_VORBIS_NO_DEFER_FLOOR + for (i=0; i < f->channels; ++i) { + if (really_zero_channel[i]) { + memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2); + } else { + do_floor(f, map, i, n, f->channel_buffers[i], f->finalY[i], NULL); + } + } +#else + for (i=0; i < f->channels; ++i) { + if (really_zero_channel[i]) { + memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2); + } else { + for (j=0; j < n2; ++j) + f->channel_buffers[i][j] *= f->floor_buffers[i][j]; + } + } +#endif + +// INVERSE MDCT + CHECK(f); + for (i=0; i < f->channels; ++i) + inverse_mdct(f->channel_buffers[i], n, f, m->blockflag); + CHECK(f); + + // this shouldn't be necessary, unless we exited on an error + // and want to flush to get to the next packet + flush_packet(f); + + if (f->first_decode) { + // assume we start so first non-discarded sample is sample 0 + // this isn't to spec, but spec would require us to read ahead + // and decode the size of all current frames--could be done, + // but presumably it's not a commonly used feature + f->current_loc = 0u - n2; // start of first frame is positioned for discard (NB this is an intentional unsigned overflow/wrap-around) + // we might have to discard samples "from" the next frame too, + // if we're lapping a large block then a small at the start? + f->discard_samples_deferred = n - right_end; + f->current_loc_valid = TRUE; + f->first_decode = FALSE; + } else if (f->discard_samples_deferred) { + if (f->discard_samples_deferred >= right_start - left_start) { + f->discard_samples_deferred -= (right_start - left_start); + left_start = right_start; + *p_left = left_start; + } else { + left_start += f->discard_samples_deferred; + *p_left = left_start; + f->discard_samples_deferred = 0; + } + } else if (f->previous_length == 0 && f->current_loc_valid) { + // we're recovering from a seek... that means we're going to discard + // the samples from this packet even though we know our position from + // the last page header, so we need to update the position based on + // the discarded samples here + // but wait, the code below is going to add this in itself even + // on a discard, so we don't need to do it here... + } + + // check if we have ogg information about the sample # for this packet + if (f->last_seg_which == f->end_seg_with_known_loc) { + // if we have a valid current loc, and this is final: + if (f->current_loc_valid && (f->page_flag & PAGEFLAG_last_page)) { + uint32 current_end = f->known_loc_for_packet; + // then let's infer the size of the (probably) short final frame + if (current_end < f->current_loc + (right_end-left_start)) { + if (current_end < f->current_loc) { + // negative truncation, that's impossible! + *len = 0; + } else { + *len = current_end - f->current_loc; + } + *len += left_start; // this doesn't seem right, but has no ill effect on my test files + if (*len > right_end) *len = right_end; // this should never happen + f->current_loc += *len; + return TRUE; + } + } + // otherwise, just set our sample loc + // guess that the ogg granule pos refers to the _middle_ of the + // last frame? + // set f->current_loc to the position of left_start + f->current_loc = f->known_loc_for_packet - (n2-left_start); + f->current_loc_valid = TRUE; + } + if (f->current_loc_valid) + f->current_loc += (right_start - left_start); + + if (f->alloc.alloc_buffer) + assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset); + *len = right_end; // ignore samples after the window goes to 0 + CHECK(f); + + return TRUE; +} + +static int vorbis_decode_packet(vorb *f, int *len, int *p_left, int *p_right) +{ + int mode, left_end, right_end; + if (!vorbis_decode_initial(f, p_left, &left_end, p_right, &right_end, &mode)) return 0; + return vorbis_decode_packet_rest(f, len, f->mode_config + mode, *p_left, left_end, *p_right, right_end, p_left); +} + +static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right) +{ + int prev,i,j; + // we use right&left (the start of the right- and left-window sin()-regions) + // to determine how much to return, rather than inferring from the rules + // (same result, clearer code); 'left' indicates where our sin() window + // starts, therefore where the previous window's right edge starts, and + // therefore where to start mixing from the previous buffer. 'right' + // indicates where our sin() ending-window starts, therefore that's where + // we start saving, and where our returned-data ends. + + // mixin from previous window + if (f->previous_length) { + int i,j, n = f->previous_length; + float *w = get_window(f, n); + if (w == NULL) return 0; + for (i=0; i < f->channels; ++i) { + for (j=0; j < n; ++j) + f->channel_buffers[i][left+j] = + f->channel_buffers[i][left+j]*w[ j] + + f->previous_window[i][ j]*w[n-1-j]; + } + } + + prev = f->previous_length; + + // last half of this data becomes previous window + f->previous_length = len - right; + + // @OPTIMIZE: could avoid this copy by double-buffering the + // output (flipping previous_window with channel_buffers), but + // then previous_window would have to be 2x as large, and + // channel_buffers couldn't be temp mem (although they're NOT + // currently temp mem, they could be (unless we want to level + // performance by spreading out the computation)) + for (i=0; i < f->channels; ++i) + for (j=0; right+j < len; ++j) + f->previous_window[i][j] = f->channel_buffers[i][right+j]; + + if (!prev) + // there was no previous packet, so this data isn't valid... + // this isn't entirely true, only the would-have-overlapped data + // isn't valid, but this seems to be what the spec requires + return 0; + + // truncate a short frame + if (len < right) right = len; + + f->samples_output += right-left; + + return right - left; +} + +static int vorbis_pump_first_frame(stb_vorbis *f) +{ + int len, right, left, res; + res = vorbis_decode_packet(f, &len, &left, &right); + if (res) + vorbis_finish_frame(f, len, left, right); + return res; +} + +#ifndef STB_VORBIS_NO_PUSHDATA_API +static int is_whole_packet_present(stb_vorbis *f) +{ + // make sure that we have the packet available before continuing... + // this requires a full ogg parse, but we know we can fetch from f->stream + + // instead of coding this out explicitly, we could save the current read state, + // read the next packet with get8() until end-of-packet, check f->eof, then + // reset the state? but that would be slower, esp. since we'd have over 256 bytes + // of state to restore (primarily the page segment table) + + int s = f->next_seg, first = TRUE; + uint8 *p = f->stream; + + if (s != -1) { // if we're not starting the packet with a 'continue on next page' flag + for (; s < f->segment_count; ++s) { + p += f->segments[s]; + if (f->segments[s] < 255) // stop at first short segment + break; + } + // either this continues, or it ends it... + if (s == f->segment_count) + s = -1; // set 'crosses page' flag + if (p > f->stream_end) return error(f, VORBIS_need_more_data); + first = FALSE; + } + for (; s == -1;) { + uint8 *q; + int n; + + // check that we have the page header ready + if (p + 26 >= f->stream_end) return error(f, VORBIS_need_more_data); + // validate the page + if (memcmp(p, ogg_page_header, 4)) return error(f, VORBIS_invalid_stream); + if (p[4] != 0) return error(f, VORBIS_invalid_stream); + if (first) { // the first segment must NOT have 'continued_packet', later ones MUST + if (f->previous_length) + if ((p[5] & PAGEFLAG_continued_packet)) return error(f, VORBIS_invalid_stream); + // if no previous length, we're resynching, so we can come in on a continued-packet, + // which we'll just drop + } else { + if (!(p[5] & PAGEFLAG_continued_packet)) return error(f, VORBIS_invalid_stream); + } + n = p[26]; // segment counts + q = p+27; // q points to segment table + p = q + n; // advance past header + // make sure we've read the segment table + if (p > f->stream_end) return error(f, VORBIS_need_more_data); + for (s=0; s < n; ++s) { + p += q[s]; + if (q[s] < 255) + break; + } + if (s == n) + s = -1; // set 'crosses page' flag + if (p > f->stream_end) return error(f, VORBIS_need_more_data); + first = FALSE; + } + return TRUE; +} +#endif // !STB_VORBIS_NO_PUSHDATA_API + +static int start_decoder(vorb *f) +{ + uint8 header[6], x,y; + int len,i,j,k, max_submaps = 0; + int longest_floorlist=0; + + // first page, first packet + f->first_decode = TRUE; + + if (!start_page(f)) return FALSE; + // validate page flag + if (!(f->page_flag & PAGEFLAG_first_page)) return error(f, VORBIS_invalid_first_page); + if (f->page_flag & PAGEFLAG_last_page) return error(f, VORBIS_invalid_first_page); + if (f->page_flag & PAGEFLAG_continued_packet) return error(f, VORBIS_invalid_first_page); + // check for expected packet length + if (f->segment_count != 1) return error(f, VORBIS_invalid_first_page); + if (f->segments[0] != 30) { + // check for the Ogg skeleton fishead identifying header to refine our error + if (f->segments[0] == 64 && + getn(f, header, 6) && + header[0] == 'f' && + header[1] == 'i' && + header[2] == 's' && + header[3] == 'h' && + header[4] == 'e' && + header[5] == 'a' && + get8(f) == 'd' && + get8(f) == '\0') return error(f, VORBIS_ogg_skeleton_not_supported); + else + return error(f, VORBIS_invalid_first_page); + } + + // read packet + // check packet header + if (get8(f) != VORBIS_packet_id) return error(f, VORBIS_invalid_first_page); + if (!getn(f, header, 6)) return error(f, VORBIS_unexpected_eof); + if (!vorbis_validate(header)) return error(f, VORBIS_invalid_first_page); + // vorbis_version + if (get32(f) != 0) return error(f, VORBIS_invalid_first_page); + f->channels = get8(f); if (!f->channels) return error(f, VORBIS_invalid_first_page); + if (f->channels > STB_VORBIS_MAX_CHANNELS) return error(f, VORBIS_too_many_channels); + f->sample_rate = get32(f); if (!f->sample_rate) return error(f, VORBIS_invalid_first_page); + get32(f); // bitrate_maximum + get32(f); // bitrate_nominal + get32(f); // bitrate_minimum + x = get8(f); + { + int log0,log1; + log0 = x & 15; + log1 = x >> 4; + f->blocksize_0 = 1 << log0; + f->blocksize_1 = 1 << log1; + if (log0 < 6 || log0 > 13) return error(f, VORBIS_invalid_setup); + if (log1 < 6 || log1 > 13) return error(f, VORBIS_invalid_setup); + if (log0 > log1) return error(f, VORBIS_invalid_setup); + } + + // framing_flag + x = get8(f); + if (!(x & 1)) return error(f, VORBIS_invalid_first_page); + + // second packet! + if (!start_page(f)) return FALSE; + + if (!start_packet(f)) return FALSE; + + if (!next_segment(f)) return FALSE; + + if (get8_packet(f) != VORBIS_packet_comment) return error(f, VORBIS_invalid_setup); + for (i=0; i < 6; ++i) header[i] = get8_packet(f); + if (!vorbis_validate(header)) return error(f, VORBIS_invalid_setup); + //file vendor + len = get32_packet(f); + f->vendor = (char*)setup_malloc(f, sizeof(char) * (len+1)); + if (f->vendor == NULL) return error(f, VORBIS_outofmem); + for(i=0; i < len; ++i) { + f->vendor[i] = get8_packet(f); + } + f->vendor[len] = (char)'\0'; + //user comments + f->comment_list_length = get32_packet(f); + f->comment_list = NULL; + if (f->comment_list_length > 0) + { + f->comment_list = (char**) setup_malloc(f, sizeof(char*) * (f->comment_list_length)); + if (f->comment_list == NULL) return error(f, VORBIS_outofmem); + } + + for(i=0; i < f->comment_list_length; ++i) { + len = get32_packet(f); + f->comment_list[i] = (char*)setup_malloc(f, sizeof(char) * (len+1)); + if (f->comment_list[i] == NULL) return error(f, VORBIS_outofmem); + + for(j=0; j < len; ++j) { + f->comment_list[i][j] = get8_packet(f); + } + f->comment_list[i][len] = (char)'\0'; + } + + // framing_flag + x = get8_packet(f); + if (!(x & 1)) return error(f, VORBIS_invalid_setup); + + + skip(f, f->bytes_in_seg); + f->bytes_in_seg = 0; + + do { + len = next_segment(f); + skip(f, len); + f->bytes_in_seg = 0; + } while (len); + + // third packet! + if (!start_packet(f)) return FALSE; + + #ifndef STB_VORBIS_NO_PUSHDATA_API + if (IS_PUSH_MODE(f)) { + if (!is_whole_packet_present(f)) { + // convert error in ogg header to write type + if (f->error == VORBIS_invalid_stream) + f->error = VORBIS_invalid_setup; + return FALSE; + } + } + #endif + + crc32_init(); // always init it, to avoid multithread race conditions + + if (get8_packet(f) != VORBIS_packet_setup) return error(f, VORBIS_invalid_setup); + for (i=0; i < 6; ++i) header[i] = get8_packet(f); + if (!vorbis_validate(header)) return error(f, VORBIS_invalid_setup); + + // codebooks + + f->codebook_count = get_bits(f,8) + 1; + f->codebooks = (Codebook *) setup_malloc(f, sizeof(*f->codebooks) * f->codebook_count); + if (f->codebooks == NULL) return error(f, VORBIS_outofmem); + memset(f->codebooks, 0, sizeof(*f->codebooks) * f->codebook_count); + for (i=0; i < f->codebook_count; ++i) { + uint32 *values; + int ordered, sorted_count; + int total=0; + uint8 *lengths; + Codebook *c = f->codebooks+i; + CHECK(f); + x = get_bits(f, 8); if (x != 0x42) return error(f, VORBIS_invalid_setup); + x = get_bits(f, 8); if (x != 0x43) return error(f, VORBIS_invalid_setup); + x = get_bits(f, 8); if (x != 0x56) return error(f, VORBIS_invalid_setup); + x = get_bits(f, 8); + c->dimensions = (get_bits(f, 8)<<8) + x; + x = get_bits(f, 8); + y = get_bits(f, 8); + c->entries = (get_bits(f, 8)<<16) + (y<<8) + x; + ordered = get_bits(f,1); + c->sparse = ordered ? 0 : get_bits(f,1); + + if (c->dimensions == 0 && c->entries != 0) return error(f, VORBIS_invalid_setup); + + if (c->sparse) + lengths = (uint8 *) setup_temp_malloc(f, c->entries); + else + lengths = c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries); + + if (!lengths) return error(f, VORBIS_outofmem); + + if (ordered) { + int current_entry = 0; + int current_length = get_bits(f,5) + 1; + while (current_entry < c->entries) { + int limit = c->entries - current_entry; + int n = get_bits(f, ilog(limit)); + if (current_length >= 32) return error(f, VORBIS_invalid_setup); + if (current_entry + n > (int) c->entries) { return error(f, VORBIS_invalid_setup); } + memset(lengths + current_entry, current_length, n); + current_entry += n; + ++current_length; + } + } else { + for (j=0; j < c->entries; ++j) { + int present = c->sparse ? get_bits(f,1) : 1; + if (present) { + lengths[j] = get_bits(f, 5) + 1; + ++total; + if (lengths[j] == 32) + return error(f, VORBIS_invalid_setup); + } else { + lengths[j] = NO_CODE; + } + } + } + + if (c->sparse && total >= c->entries >> 2) { + // convert sparse items to non-sparse! + if (c->entries > (int) f->setup_temp_memory_required) + f->setup_temp_memory_required = c->entries; + + c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries); + if (c->codeword_lengths == NULL) return error(f, VORBIS_outofmem); + memcpy(c->codeword_lengths, lengths, c->entries); + setup_temp_free(f, lengths, c->entries); // note this is only safe if there have been no intervening temp mallocs! + lengths = c->codeword_lengths; + c->sparse = 0; + } + + // compute the size of the sorted tables + if (c->sparse) { + sorted_count = total; + } else { + sorted_count = 0; + #ifndef STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH + for (j=0; j < c->entries; ++j) + if (lengths[j] > STB_VORBIS_FAST_HUFFMAN_LENGTH && lengths[j] != NO_CODE) + ++sorted_count; + #endif + } + + c->sorted_entries = sorted_count; + values = NULL; + + CHECK(f); + if (!c->sparse) { + c->codewords = (uint32 *) setup_malloc(f, sizeof(c->codewords[0]) * c->entries); + if (!c->codewords) return error(f, VORBIS_outofmem); + } else { + unsigned int size; + if (c->sorted_entries) { + c->codeword_lengths = (uint8 *) setup_malloc(f, c->sorted_entries); + if (!c->codeword_lengths) return error(f, VORBIS_outofmem); + c->codewords = (uint32 *) setup_temp_malloc(f, sizeof(*c->codewords) * c->sorted_entries); + if (!c->codewords) return error(f, VORBIS_outofmem); + values = (uint32 *) setup_temp_malloc(f, sizeof(*values) * c->sorted_entries); + if (!values) return error(f, VORBIS_outofmem); + } + size = c->entries + (sizeof(*c->codewords) + sizeof(*values)) * c->sorted_entries; + if (size > f->setup_temp_memory_required) + f->setup_temp_memory_required = size; + } + + if (!compute_codewords(c, lengths, c->entries, values)) { + if (c->sparse) setup_temp_free(f, values, 0); + return error(f, VORBIS_invalid_setup); + } + + if (c->sorted_entries) { + // allocate an extra slot for sentinels + c->sorted_codewords = (uint32 *) setup_malloc(f, sizeof(*c->sorted_codewords) * (c->sorted_entries+1)); + if (c->sorted_codewords == NULL) return error(f, VORBIS_outofmem); + // allocate an extra slot at the front so that c->sorted_values[-1] is defined + // so that we can catch that case without an extra if + c->sorted_values = ( int *) setup_malloc(f, sizeof(*c->sorted_values ) * (c->sorted_entries+1)); + if (c->sorted_values == NULL) return error(f, VORBIS_outofmem); + ++c->sorted_values; + c->sorted_values[-1] = -1; + compute_sorted_huffman(c, lengths, values); + } + + if (c->sparse) { + setup_temp_free(f, values, sizeof(*values)*c->sorted_entries); + setup_temp_free(f, c->codewords, sizeof(*c->codewords)*c->sorted_entries); + setup_temp_free(f, lengths, c->entries); + c->codewords = NULL; + } + + compute_accelerated_huffman(c); + + CHECK(f); + c->lookup_type = get_bits(f, 4); + if (c->lookup_type > 2) return error(f, VORBIS_invalid_setup); + if (c->lookup_type > 0) { + uint16 *mults; + c->minimum_value = float32_unpack(get_bits(f, 32)); + c->delta_value = float32_unpack(get_bits(f, 32)); + c->value_bits = get_bits(f, 4)+1; + c->sequence_p = get_bits(f,1); + if (c->lookup_type == 1) { + int values = lookup1_values(c->entries, c->dimensions); + if (values < 0) return error(f, VORBIS_invalid_setup); + c->lookup_values = (uint32) values; + } else { + c->lookup_values = c->entries * c->dimensions; + } + if (c->lookup_values == 0) return error(f, VORBIS_invalid_setup); + mults = (uint16 *) setup_temp_malloc(f, sizeof(mults[0]) * c->lookup_values); + if (mults == NULL) return error(f, VORBIS_outofmem); + for (j=0; j < (int) c->lookup_values; ++j) { + int q = get_bits(f, c->value_bits); + if (q == EOP) { setup_temp_free(f,mults,sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_invalid_setup); } + mults[j] = q; + } + +#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK + if (c->lookup_type == 1) { + int len, sparse = c->sparse; + float last=0; + // pre-expand the lookup1-style multiplicands, to avoid a divide in the inner loop + if (sparse) { + if (c->sorted_entries == 0) goto skip; + c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->sorted_entries * c->dimensions); + } else + c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->entries * c->dimensions); + if (c->multiplicands == NULL) { setup_temp_free(f,mults,sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); } + len = sparse ? c->sorted_entries : c->entries; + for (j=0; j < len; ++j) { + unsigned int z = sparse ? c->sorted_values[j] : j; + unsigned int div=1; + for (k=0; k < c->dimensions; ++k) { + int off = (z / div) % c->lookup_values; + float val = mults[off]*c->delta_value + c->minimum_value + last; + c->multiplicands[j*c->dimensions + k] = val; + if (c->sequence_p) + last = val; + if (k+1 < c->dimensions) { + if (div > UINT_MAX / (unsigned int) c->lookup_values) { + setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values); + return error(f, VORBIS_invalid_setup); + } + div *= c->lookup_values; + } + } + } + c->lookup_type = 2; + } + else +#endif + { + float last=0; + CHECK(f); + c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->lookup_values); + if (c->multiplicands == NULL) { setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); } + for (j=0; j < (int) c->lookup_values; ++j) { + float val = mults[j] * c->delta_value + c->minimum_value + last; + c->multiplicands[j] = val; + if (c->sequence_p) + last = val; + } + } +#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK + skip:; +#endif + setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values); + + CHECK(f); + } + CHECK(f); + } + + // time domain transfers (notused) + + x = get_bits(f, 6) + 1; + for (i=0; i < x; ++i) { + uint32 z = get_bits(f, 16); + if (z != 0) return error(f, VORBIS_invalid_setup); + } + + // Floors + f->floor_count = get_bits(f, 6)+1; + f->floor_config = (Floor *) setup_malloc(f, f->floor_count * sizeof(*f->floor_config)); + if (f->floor_config == NULL) return error(f, VORBIS_outofmem); + for (i=0; i < f->floor_count; ++i) { + f->floor_types[i] = get_bits(f, 16); + if (f->floor_types[i] > 1) return error(f, VORBIS_invalid_setup); + if (f->floor_types[i] == 0) { + Floor0 *g = &f->floor_config[i].floor0; + g->order = get_bits(f,8); + g->rate = get_bits(f,16); + g->bark_map_size = get_bits(f,16); + g->amplitude_bits = get_bits(f,6); + g->amplitude_offset = get_bits(f,8); + g->number_of_books = get_bits(f,4) + 1; + for (j=0; j < g->number_of_books; ++j) + g->book_list[j] = get_bits(f,8); + return error(f, VORBIS_feature_not_supported); + } else { + stbv__floor_ordering p[31*8+2]; + Floor1 *g = &f->floor_config[i].floor1; + int max_class = -1; + g->partitions = get_bits(f, 5); + for (j=0; j < g->partitions; ++j) { + g->partition_class_list[j] = get_bits(f, 4); + if (g->partition_class_list[j] > max_class) + max_class = g->partition_class_list[j]; + } + for (j=0; j <= max_class; ++j) { + g->class_dimensions[j] = get_bits(f, 3)+1; + g->class_subclasses[j] = get_bits(f, 2); + if (g->class_subclasses[j]) { + g->class_masterbooks[j] = get_bits(f, 8); + if (g->class_masterbooks[j] >= f->codebook_count) return error(f, VORBIS_invalid_setup); + } + for (k=0; k < 1 << g->class_subclasses[j]; ++k) { + g->subclass_books[j][k] = (int16)get_bits(f,8)-1; + if (g->subclass_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup); + } + } + g->floor1_multiplier = get_bits(f,2)+1; + g->rangebits = get_bits(f,4); + g->Xlist[0] = 0; + g->Xlist[1] = 1 << g->rangebits; + g->values = 2; + for (j=0; j < g->partitions; ++j) { + int c = g->partition_class_list[j]; + for (k=0; k < g->class_dimensions[c]; ++k) { + g->Xlist[g->values] = get_bits(f, g->rangebits); + ++g->values; + } + } + // precompute the sorting + for (j=0; j < g->values; ++j) { + p[j].x = g->Xlist[j]; + p[j].id = j; + } + qsort(p, g->values, sizeof(p[0]), point_compare); + for (j=0; j < g->values-1; ++j) + if (p[j].x == p[j+1].x) + return error(f, VORBIS_invalid_setup); + for (j=0; j < g->values; ++j) + g->sorted_order[j] = (uint8) p[j].id; + // precompute the neighbors + for (j=2; j < g->values; ++j) { + int low = 0,hi = 0; + neighbors(g->Xlist, j, &low,&hi); + g->neighbors[j][0] = low; + g->neighbors[j][1] = hi; + } + + if (g->values > longest_floorlist) + longest_floorlist = g->values; + } + } + + // Residue + f->residue_count = get_bits(f, 6)+1; + f->residue_config = (Residue *) setup_malloc(f, f->residue_count * sizeof(f->residue_config[0])); + if (f->residue_config == NULL) return error(f, VORBIS_outofmem); + memset(f->residue_config, 0, f->residue_count * sizeof(f->residue_config[0])); + for (i=0; i < f->residue_count; ++i) { + uint8 residue_cascade[64]; + Residue *r = f->residue_config+i; + f->residue_types[i] = get_bits(f, 16); + if (f->residue_types[i] > 2) return error(f, VORBIS_invalid_setup); + r->begin = get_bits(f, 24); + r->end = get_bits(f, 24); + if (r->end < r->begin) return error(f, VORBIS_invalid_setup); + r->part_size = get_bits(f,24)+1; + r->classifications = get_bits(f,6)+1; + r->classbook = get_bits(f,8); + if (r->classbook >= f->codebook_count) return error(f, VORBIS_invalid_setup); + for (j=0; j < r->classifications; ++j) { + uint8 high_bits=0; + uint8 low_bits=get_bits(f,3); + if (get_bits(f,1)) + high_bits = get_bits(f,5); + residue_cascade[j] = high_bits*8 + low_bits; + } + r->residue_books = (short (*)[8]) setup_malloc(f, sizeof(r->residue_books[0]) * r->classifications); + if (r->residue_books == NULL) return error(f, VORBIS_outofmem); + for (j=0; j < r->classifications; ++j) { + for (k=0; k < 8; ++k) { + if (residue_cascade[j] & (1 << k)) { + r->residue_books[j][k] = get_bits(f, 8); + if (r->residue_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup); + } else { + r->residue_books[j][k] = -1; + } + } + } + // precompute the classifications[] array to avoid inner-loop mod/divide + // call it 'classdata' since we already have r->classifications + r->classdata = (uint8 **) setup_malloc(f, sizeof(*r->classdata) * f->codebooks[r->classbook].entries); + if (!r->classdata) return error(f, VORBIS_outofmem); + memset(r->classdata, 0, sizeof(*r->classdata) * f->codebooks[r->classbook].entries); + for (j=0; j < f->codebooks[r->classbook].entries; ++j) { + int classwords = f->codebooks[r->classbook].dimensions; + int temp = j; + r->classdata[j] = (uint8 *) setup_malloc(f, sizeof(r->classdata[j][0]) * classwords); + if (r->classdata[j] == NULL) return error(f, VORBIS_outofmem); + for (k=classwords-1; k >= 0; --k) { + r->classdata[j][k] = temp % r->classifications; + temp /= r->classifications; + } + } + } + + f->mapping_count = get_bits(f,6)+1; + f->mapping = (Mapping *) setup_malloc(f, f->mapping_count * sizeof(*f->mapping)); + if (f->mapping == NULL) return error(f, VORBIS_outofmem); + memset(f->mapping, 0, f->mapping_count * sizeof(*f->mapping)); + for (i=0; i < f->mapping_count; ++i) { + Mapping *m = f->mapping + i; + int mapping_type = get_bits(f,16); + if (mapping_type != 0) return error(f, VORBIS_invalid_setup); + m->chan = (MappingChannel *) setup_malloc(f, f->channels * sizeof(*m->chan)); + if (m->chan == NULL) return error(f, VORBIS_outofmem); + if (get_bits(f,1)) + m->submaps = get_bits(f,4)+1; + else + m->submaps = 1; + if (m->submaps > max_submaps) + max_submaps = m->submaps; + if (get_bits(f,1)) { + m->coupling_steps = get_bits(f,8)+1; + if (m->coupling_steps > f->channels) return error(f, VORBIS_invalid_setup); + for (k=0; k < m->coupling_steps; ++k) { + m->chan[k].magnitude = get_bits(f, ilog(f->channels-1)); + m->chan[k].angle = get_bits(f, ilog(f->channels-1)); + if (m->chan[k].magnitude >= f->channels) return error(f, VORBIS_invalid_setup); + if (m->chan[k].angle >= f->channels) return error(f, VORBIS_invalid_setup); + if (m->chan[k].magnitude == m->chan[k].angle) return error(f, VORBIS_invalid_setup); + } + } else + m->coupling_steps = 0; + + // reserved field + if (get_bits(f,2)) return error(f, VORBIS_invalid_setup); + if (m->submaps > 1) { + for (j=0; j < f->channels; ++j) { + m->chan[j].mux = get_bits(f, 4); + if (m->chan[j].mux >= m->submaps) return error(f, VORBIS_invalid_setup); + } + } else + // @SPECIFICATION: this case is missing from the spec + for (j=0; j < f->channels; ++j) + m->chan[j].mux = 0; + + for (j=0; j < m->submaps; ++j) { + get_bits(f,8); // discard + m->submap_floor[j] = get_bits(f,8); + m->submap_residue[j] = get_bits(f,8); + if (m->submap_floor[j] >= f->floor_count) return error(f, VORBIS_invalid_setup); + if (m->submap_residue[j] >= f->residue_count) return error(f, VORBIS_invalid_setup); + } + } + + // Modes + f->mode_count = get_bits(f, 6)+1; + for (i=0; i < f->mode_count; ++i) { + Mode *m = f->mode_config+i; + m->blockflag = get_bits(f,1); + m->windowtype = get_bits(f,16); + m->transformtype = get_bits(f,16); + m->mapping = get_bits(f,8); + if (m->windowtype != 0) return error(f, VORBIS_invalid_setup); + if (m->transformtype != 0) return error(f, VORBIS_invalid_setup); + if (m->mapping >= f->mapping_count) return error(f, VORBIS_invalid_setup); + } + + flush_packet(f); + + f->previous_length = 0; + + for (i=0; i < f->channels; ++i) { + f->channel_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1); + f->previous_window[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1/2); + f->finalY[i] = (int16 *) setup_malloc(f, sizeof(int16) * longest_floorlist); + if (f->channel_buffers[i] == NULL || f->previous_window[i] == NULL || f->finalY[i] == NULL) return error(f, VORBIS_outofmem); + memset(f->channel_buffers[i], 0, sizeof(float) * f->blocksize_1); + #ifdef STB_VORBIS_NO_DEFER_FLOOR + f->floor_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1/2); + if (f->floor_buffers[i] == NULL) return error(f, VORBIS_outofmem); + #endif + } + + if (!init_blocksize(f, 0, f->blocksize_0)) return FALSE; + if (!init_blocksize(f, 1, f->blocksize_1)) return FALSE; + f->blocksize[0] = f->blocksize_0; + f->blocksize[1] = f->blocksize_1; + +#ifdef STB_VORBIS_DIVIDE_TABLE + if (integer_divide_table[1][1]==0) + for (i=0; i < DIVTAB_NUMER; ++i) + for (j=1; j < DIVTAB_DENOM; ++j) + integer_divide_table[i][j] = i / j; +#endif + + // compute how much temporary memory is needed + + // 1. + { + uint32 imdct_mem = (f->blocksize_1 * sizeof(float) >> 1); + uint32 classify_mem; + int i,max_part_read=0; + for (i=0; i < f->residue_count; ++i) { + Residue *r = f->residue_config + i; + unsigned int actual_size = f->blocksize_1 / 2; + unsigned int limit_r_begin = r->begin < actual_size ? r->begin : actual_size; + unsigned int limit_r_end = r->end < actual_size ? r->end : actual_size; + int n_read = limit_r_end - limit_r_begin; + int part_read = n_read / r->part_size; + if (part_read > max_part_read) + max_part_read = part_read; + } + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(uint8 *)); + #else + classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(int *)); + #endif + + // maximum reasonable partition size is f->blocksize_1 + + f->temp_memory_required = classify_mem; + if (imdct_mem > f->temp_memory_required) + f->temp_memory_required = imdct_mem; + } + + + if (f->alloc.alloc_buffer) { + assert(f->temp_offset == f->alloc.alloc_buffer_length_in_bytes); + // check if there's enough temp memory so we don't error later + if (f->setup_offset + sizeof(*f) + f->temp_memory_required > (unsigned) f->temp_offset) + return error(f, VORBIS_outofmem); + } + + // @TODO: stb_vorbis_seek_start expects first_audio_page_offset to point to a page + // without PAGEFLAG_continued_packet, so this either points to the first page, or + // the page after the end of the headers. It might be cleaner to point to a page + // in the middle of the headers, when that's the page where the first audio packet + // starts, but we'd have to also correctly skip the end of any continued packet in + // stb_vorbis_seek_start. + if (f->next_seg == -1) { + f->first_audio_page_offset = stb_vorbis_get_file_offset(f); + } else { + f->first_audio_page_offset = 0; + } + + return TRUE; +} + +static void vorbis_deinit(stb_vorbis *p) +{ + int i,j; + + setup_free(p, p->vendor); + for (i=0; i < p->comment_list_length; ++i) { + setup_free(p, p->comment_list[i]); + } + setup_free(p, p->comment_list); + + if (p->residue_config) { + for (i=0; i < p->residue_count; ++i) { + Residue *r = p->residue_config+i; + if (r->classdata) { + for (j=0; j < p->codebooks[r->classbook].entries; ++j) + setup_free(p, r->classdata[j]); + setup_free(p, r->classdata); + } + setup_free(p, r->residue_books); + } + } + + if (p->codebooks) { + CHECK(p); + for (i=0; i < p->codebook_count; ++i) { + Codebook *c = p->codebooks + i; + setup_free(p, c->codeword_lengths); + setup_free(p, c->multiplicands); + setup_free(p, c->codewords); + setup_free(p, c->sorted_codewords); + // c->sorted_values[-1] is the first entry in the array + setup_free(p, c->sorted_values ? c->sorted_values-1 : NULL); + } + setup_free(p, p->codebooks); + } + setup_free(p, p->floor_config); + setup_free(p, p->residue_config); + if (p->mapping) { + for (i=0; i < p->mapping_count; ++i) + setup_free(p, p->mapping[i].chan); + setup_free(p, p->mapping); + } + CHECK(p); + for (i=0; i < p->channels && i < STB_VORBIS_MAX_CHANNELS; ++i) { + setup_free(p, p->channel_buffers[i]); + setup_free(p, p->previous_window[i]); + #ifdef STB_VORBIS_NO_DEFER_FLOOR + setup_free(p, p->floor_buffers[i]); + #endif + setup_free(p, p->finalY[i]); + } + for (i=0; i < 2; ++i) { + setup_free(p, p->A[i]); + setup_free(p, p->B[i]); + setup_free(p, p->C[i]); + setup_free(p, p->window[i]); + setup_free(p, p->bit_reverse[i]); + } + #ifndef STB_VORBIS_NO_STDIO + if (p->close_on_free) fclose(p->f); + #endif +} + +void stb_vorbis_close(stb_vorbis *p) +{ + if (p == NULL) return; + vorbis_deinit(p); + setup_free(p,p); +} + +static void vorbis_init(stb_vorbis *p, const stb_vorbis_alloc *z) +{ + memset(p, 0, sizeof(*p)); // NULL out all malloc'd pointers to start + if (z) { + p->alloc = *z; + p->alloc.alloc_buffer_length_in_bytes &= ~7; + p->temp_offset = p->alloc.alloc_buffer_length_in_bytes; + } + p->eof = 0; + p->error = VORBIS__no_error; + p->stream = NULL; + p->codebooks = NULL; + p->page_crc_tests = -1; + #ifndef STB_VORBIS_NO_STDIO + p->close_on_free = FALSE; + p->f = NULL; + #endif +} + +int stb_vorbis_get_sample_offset(stb_vorbis *f) +{ + if (f->current_loc_valid) + return f->current_loc; + else + return -1; +} + +stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f) +{ + stb_vorbis_info d; + d.channels = f->channels; + d.sample_rate = f->sample_rate; + d.setup_memory_required = f->setup_memory_required; + d.setup_temp_memory_required = f->setup_temp_memory_required; + d.temp_memory_required = f->temp_memory_required; + d.max_frame_size = f->blocksize_1 >> 1; + return d; +} + +stb_vorbis_comment stb_vorbis_get_comment(stb_vorbis *f) +{ + stb_vorbis_comment d; + d.vendor = f->vendor; + d.comment_list_length = f->comment_list_length; + d.comment_list = f->comment_list; + return d; +} + +int stb_vorbis_get_error(stb_vorbis *f) +{ + int e = f->error; + f->error = VORBIS__no_error; + return e; +} + +static stb_vorbis * vorbis_alloc(stb_vorbis *f) +{ + stb_vorbis *p = (stb_vorbis *) setup_malloc(f, sizeof(*p)); + return p; +} + +#ifndef STB_VORBIS_NO_PUSHDATA_API + +void stb_vorbis_flush_pushdata(stb_vorbis *f) +{ + f->previous_length = 0; + f->page_crc_tests = 0; + f->discard_samples_deferred = 0; + f->current_loc_valid = FALSE; + f->first_decode = FALSE; + f->samples_output = 0; + f->channel_buffer_start = 0; + f->channel_buffer_end = 0; +} + +static int vorbis_search_for_page_pushdata(vorb *f, uint8 *data, int data_len) +{ + int i,n; + for (i=0; i < f->page_crc_tests; ++i) + f->scan[i].bytes_done = 0; + + // if we have room for more scans, search for them first, because + // they may cause us to stop early if their header is incomplete + if (f->page_crc_tests < STB_VORBIS_PUSHDATA_CRC_COUNT) { + if (data_len < 4) return 0; + data_len -= 3; // need to look for 4-byte sequence, so don't miss + // one that straddles a boundary + for (i=0; i < data_len; ++i) { + if (data[i] == 0x4f) { + if (0==memcmp(data+i, ogg_page_header, 4)) { + int j,len; + uint32 crc; + // make sure we have the whole page header + if (i+26 >= data_len || i+27+data[i+26] >= data_len) { + // only read up to this page start, so hopefully we'll + // have the whole page header start next time + data_len = i; + break; + } + // ok, we have it all; compute the length of the page + len = 27 + data[i+26]; + for (j=0; j < data[i+26]; ++j) + len += data[i+27+j]; + // scan everything up to the embedded crc (which we must 0) + crc = 0; + for (j=0; j < 22; ++j) + crc = crc32_update(crc, data[i+j]); + // now process 4 0-bytes + for ( ; j < 26; ++j) + crc = crc32_update(crc, 0); + // len is the total number of bytes we need to scan + n = f->page_crc_tests++; + f->scan[n].bytes_left = len-j; + f->scan[n].crc_so_far = crc; + f->scan[n].goal_crc = data[i+22] + (data[i+23] << 8) + (data[i+24]<<16) + (data[i+25]<<24); + // if the last frame on a page is continued to the next, then + // we can't recover the sample_loc immediately + if (data[i+27+data[i+26]-1] == 255) + f->scan[n].sample_loc = ~0; + else + f->scan[n].sample_loc = data[i+6] + (data[i+7] << 8) + (data[i+ 8]<<16) + (data[i+ 9]<<24); + f->scan[n].bytes_done = i+j; + if (f->page_crc_tests == STB_VORBIS_PUSHDATA_CRC_COUNT) + break; + // keep going if we still have room for more + } + } + } + } + + for (i=0; i < f->page_crc_tests;) { + uint32 crc; + int j; + int n = f->scan[i].bytes_done; + int m = f->scan[i].bytes_left; + if (m > data_len - n) m = data_len - n; + // m is the bytes to scan in the current chunk + crc = f->scan[i].crc_so_far; + for (j=0; j < m; ++j) + crc = crc32_update(crc, data[n+j]); + f->scan[i].bytes_left -= m; + f->scan[i].crc_so_far = crc; + if (f->scan[i].bytes_left == 0) { + // does it match? + if (f->scan[i].crc_so_far == f->scan[i].goal_crc) { + // Houston, we have page + data_len = n+m; // consumption amount is wherever that scan ended + f->page_crc_tests = -1; // drop out of page scan mode + f->previous_length = 0; // decode-but-don't-output one frame + f->next_seg = -1; // start a new page + f->current_loc = f->scan[i].sample_loc; // set the current sample location + // to the amount we'd have decoded had we decoded this page + f->current_loc_valid = f->current_loc != ~0U; + return data_len; + } + // delete entry + f->scan[i] = f->scan[--f->page_crc_tests]; + } else { + ++i; + } + } + + return data_len; +} + +// return value: number of bytes we used +int stb_vorbis_decode_frame_pushdata( + stb_vorbis *f, // the file we're decoding + const uint8 *data, int data_len, // the memory available for decoding + int *channels, // place to write number of float * buffers + float ***output, // place to write float ** array of float * buffers + int *samples // place to write number of output samples + ) +{ + int i; + int len,right,left; + + if (!IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing); + + if (f->page_crc_tests >= 0) { + *samples = 0; + return vorbis_search_for_page_pushdata(f, (uint8 *) data, data_len); + } + + f->stream = (uint8 *) data; + f->stream_end = (uint8 *) data + data_len; + f->error = VORBIS__no_error; + + // check that we have the entire packet in memory + if (!is_whole_packet_present(f)) { + *samples = 0; + return 0; + } + + if (!vorbis_decode_packet(f, &len, &left, &right)) { + // save the actual error we encountered + enum STBVorbisError error = f->error; + if (error == VORBIS_bad_packet_type) { + // flush and resynch + f->error = VORBIS__no_error; + while (get8_packet(f) != EOP) + if (f->eof) break; + *samples = 0; + return (int) (f->stream - data); + } + if (error == VORBIS_continued_packet_flag_invalid) { + if (f->previous_length == 0) { + // we may be resynching, in which case it's ok to hit one + // of these; just discard the packet + f->error = VORBIS__no_error; + while (get8_packet(f) != EOP) + if (f->eof) break; + *samples = 0; + return (int) (f->stream - data); + } + } + // if we get an error while parsing, what to do? + // well, it DEFINITELY won't work to continue from where we are! + stb_vorbis_flush_pushdata(f); + // restore the error that actually made us bail + f->error = error; + *samples = 0; + return 1; + } + + // success! + len = vorbis_finish_frame(f, len, left, right); + for (i=0; i < f->channels; ++i) + f->outputs[i] = f->channel_buffers[i] + left; + + if (channels) *channels = f->channels; + *samples = len; + *output = f->outputs; + return (int) (f->stream - data); +} + +stb_vorbis *stb_vorbis_open_pushdata( + const unsigned char *data, int data_len, // the memory available for decoding + int *data_used, // only defined if result is not NULL + int *error, const stb_vorbis_alloc *alloc) +{ + stb_vorbis *f, p; + vorbis_init(&p, alloc); + p.stream = (uint8 *) data; + p.stream_end = (uint8 *) data + data_len; + p.push_mode = TRUE; + if (!start_decoder(&p)) { + if (p.eof) + *error = VORBIS_need_more_data; + else + *error = p.error; + vorbis_deinit(&p); + return NULL; + } + f = vorbis_alloc(&p); + if (f) { + *f = p; + *data_used = (int) (f->stream - data); + *error = 0; + return f; + } else { + vorbis_deinit(&p); + return NULL; + } +} +#endif // STB_VORBIS_NO_PUSHDATA_API + +unsigned int stb_vorbis_get_file_offset(stb_vorbis *f) +{ + #ifndef STB_VORBIS_NO_PUSHDATA_API + if (f->push_mode) return 0; + #endif + if (USE_MEMORY(f)) return (unsigned int) (f->stream - f->stream_start); + #ifndef STB_VORBIS_NO_STDIO + return (unsigned int) (ftell(f->f) - f->f_start); + #endif +} + +#ifndef STB_VORBIS_NO_PULLDATA_API +// +// DATA-PULLING API +// + +static uint32 vorbis_find_page(stb_vorbis *f, uint32 *end, uint32 *last) +{ + for(;;) { + int n; + if (f->eof) return 0; + n = get8(f); + if (n == 0x4f) { // page header candidate + unsigned int retry_loc = stb_vorbis_get_file_offset(f); + int i; + // check if we're off the end of a file_section stream + if (retry_loc - 25 > f->stream_len) + return 0; + // check the rest of the header + for (i=1; i < 4; ++i) + if (get8(f) != ogg_page_header[i]) + break; + if (f->eof) return 0; + if (i == 4) { + uint8 header[27]; + uint32 i, crc, goal, len; + for (i=0; i < 4; ++i) + header[i] = ogg_page_header[i]; + for (; i < 27; ++i) + header[i] = get8(f); + if (f->eof) return 0; + if (header[4] != 0) goto invalid; + goal = header[22] + (header[23] << 8) + (header[24]<<16) + ((uint32)header[25]<<24); + for (i=22; i < 26; ++i) + header[i] = 0; + crc = 0; + for (i=0; i < 27; ++i) + crc = crc32_update(crc, header[i]); + len = 0; + for (i=0; i < header[26]; ++i) { + int s = get8(f); + crc = crc32_update(crc, s); + len += s; + } + if (len && f->eof) return 0; + for (i=0; i < len; ++i) + crc = crc32_update(crc, get8(f)); + // finished parsing probable page + if (crc == goal) { + // we could now check that it's either got the last + // page flag set, OR it's followed by the capture + // pattern, but I guess TECHNICALLY you could have + // a file with garbage between each ogg page and recover + // from it automatically? So even though that paranoia + // might decrease the chance of an invalid decode by + // another 2^32, not worth it since it would hose those + // invalid-but-useful files? + if (end) + *end = stb_vorbis_get_file_offset(f); + if (last) { + if (header[5] & 0x04) + *last = 1; + else + *last = 0; + } + set_file_offset(f, retry_loc-1); + return 1; + } + } + invalid: + // not a valid page, so rewind and look for next one + set_file_offset(f, retry_loc); + } + } +} + + +#define SAMPLE_unknown 0xffffffff + +// seeking is implemented with a binary search, which narrows down the range to +// 64K, before using a linear search (because finding the synchronization +// pattern can be expensive, and the chance we'd find the end page again is +// relatively high for small ranges) +// +// two initial interpolation-style probes are used at the start of the search +// to try to bound either side of the binary search sensibly, while still +// working in O(log n) time if they fail. + +static int get_seek_page_info(stb_vorbis *f, ProbedPage *z) +{ + uint8 header[27], lacing[255]; + int i,len; + + // record where the page starts + z->page_start = stb_vorbis_get_file_offset(f); + + // parse the header + getn(f, header, 27); + if (header[0] != 'O' || header[1] != 'g' || header[2] != 'g' || header[3] != 'S') + return 0; + getn(f, lacing, header[26]); + + // determine the length of the payload + len = 0; + for (i=0; i < header[26]; ++i) + len += lacing[i]; + + // this implies where the page ends + z->page_end = z->page_start + 27 + header[26] + len; + + // read the last-decoded sample out of the data + z->last_decoded_sample = header[6] + (header[7] << 8) + (header[8] << 16) + (header[9] << 24); + + // restore file state to where we were + set_file_offset(f, z->page_start); + return 1; +} + +// rarely used function to seek back to the preceding page while finding the +// start of a packet +static int go_to_page_before(stb_vorbis *f, unsigned int limit_offset) +{ + unsigned int previous_safe, end; + + // now we want to seek back 64K from the limit + if (limit_offset >= 65536 && limit_offset-65536 >= f->first_audio_page_offset) + previous_safe = limit_offset - 65536; + else + previous_safe = f->first_audio_page_offset; + + set_file_offset(f, previous_safe); + + while (vorbis_find_page(f, &end, NULL)) { + if (end >= limit_offset && stb_vorbis_get_file_offset(f) < limit_offset) + return 1; + set_file_offset(f, end); + } + + return 0; +} + +// implements the search logic for finding a page and starting decoding. if +// the function succeeds, current_loc_valid will be true and current_loc will +// be less than or equal to the provided sample number (the closer the +// better). +static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number) +{ + ProbedPage left, right, mid; + int i, start_seg_with_known_loc, end_pos, page_start; + uint32 delta, stream_length, padding, last_sample_limit; + double offset = 0.0, bytes_per_sample = 0.0; + int probe = 0; + + // find the last page and validate the target sample + stream_length = stb_vorbis_stream_length_in_samples(f); + if (stream_length == 0) return error(f, VORBIS_seek_without_length); + if (sample_number > stream_length) return error(f, VORBIS_seek_invalid); + + // this is the maximum difference between the window-center (which is the + // actual granule position value), and the right-start (which the spec + // indicates should be the granule position (give or take one)). + padding = ((f->blocksize_1 - f->blocksize_0) >> 2); + if (sample_number < padding) + last_sample_limit = 0; + else + last_sample_limit = sample_number - padding; + + left = f->p_first; + while (left.last_decoded_sample == ~0U) { + // (untested) the first page does not have a 'last_decoded_sample' + set_file_offset(f, left.page_end); + if (!get_seek_page_info(f, &left)) goto error; + } + + right = f->p_last; + assert(right.last_decoded_sample != ~0U); + + // starting from the start is handled differently + if (last_sample_limit <= left.last_decoded_sample) { + if (stb_vorbis_seek_start(f)) { + if (f->current_loc > sample_number) + return error(f, VORBIS_seek_failed); + return 1; + } + return 0; + } + + while (left.page_end != right.page_start) { + assert(left.page_end < right.page_start); + // search range in bytes + delta = right.page_start - left.page_end; + if (delta <= 65536) { + // there's only 64K left to search - handle it linearly + set_file_offset(f, left.page_end); + } else { + if (probe < 2) { + if (probe == 0) { + // first probe (interpolate) + double data_bytes = right.page_end - left.page_start; + bytes_per_sample = data_bytes / right.last_decoded_sample; + offset = left.page_start + bytes_per_sample * (last_sample_limit - left.last_decoded_sample); + } else { + // second probe (try to bound the other side) + double error = ((double) last_sample_limit - mid.last_decoded_sample) * bytes_per_sample; + if (error >= 0 && error < 8000) error = 8000; + if (error < 0 && error > -8000) error = -8000; + offset += error * 2; + } + + // ensure the offset is valid + if (offset < left.page_end) + offset = left.page_end; + if (offset > right.page_start - 65536) + offset = right.page_start - 65536; + + set_file_offset(f, (unsigned int) offset); + } else { + // binary search for large ranges (offset by 32K to ensure + // we don't hit the right page) + set_file_offset(f, left.page_end + (delta / 2) - 32768); + } + + if (!vorbis_find_page(f, NULL, NULL)) goto error; + } + + for (;;) { + if (!get_seek_page_info(f, &mid)) goto error; + if (mid.last_decoded_sample != ~0U) break; + // (untested) no frames end on this page + set_file_offset(f, mid.page_end); + assert(mid.page_start < right.page_start); + } + + // if we've just found the last page again then we're in a tricky file, + // and we're close enough (if it wasn't an interpolation probe). + if (mid.page_start == right.page_start) { + if (probe >= 2 || delta <= 65536) + break; + } else { + if (last_sample_limit < mid.last_decoded_sample) + right = mid; + else + left = mid; + } + + ++probe; + } + + // seek back to start of the last packet + page_start = left.page_start; + set_file_offset(f, page_start); + if (!start_page(f)) return error(f, VORBIS_seek_failed); + end_pos = f->end_seg_with_known_loc; + assert(end_pos >= 0); + + for (;;) { + for (i = end_pos; i > 0; --i) + if (f->segments[i-1] != 255) + break; + + start_seg_with_known_loc = i; + + if (start_seg_with_known_loc > 0 || !(f->page_flag & PAGEFLAG_continued_packet)) + break; + + // (untested) the final packet begins on an earlier page + if (!go_to_page_before(f, page_start)) + goto error; + + page_start = stb_vorbis_get_file_offset(f); + if (!start_page(f)) goto error; + end_pos = f->segment_count - 1; + } + + // prepare to start decoding + f->current_loc_valid = FALSE; + f->last_seg = FALSE; + f->valid_bits = 0; + f->packet_bytes = 0; + f->bytes_in_seg = 0; + f->previous_length = 0; + f->next_seg = start_seg_with_known_loc; + + for (i = 0; i < start_seg_with_known_loc; i++) + skip(f, f->segments[i]); + + // start decoding (optimizable - this frame is generally discarded) + if (!vorbis_pump_first_frame(f)) + return 0; + if (f->current_loc > sample_number) + return error(f, VORBIS_seek_failed); + return 1; + +error: + // try to restore the file to a valid state + stb_vorbis_seek_start(f); + return error(f, VORBIS_seek_failed); +} + +// the same as vorbis_decode_initial, but without advancing +static int peek_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode) +{ + int bits_read, bytes_read; + + if (!vorbis_decode_initial(f, p_left_start, p_left_end, p_right_start, p_right_end, mode)) + return 0; + + // either 1 or 2 bytes were read, figure out which so we can rewind + bits_read = 1 + ilog(f->mode_count-1); + if (f->mode_config[*mode].blockflag) + bits_read += 2; + bytes_read = (bits_read + 7) / 8; + + f->bytes_in_seg += bytes_read; + f->packet_bytes -= bytes_read; + skip(f, -bytes_read); + if (f->next_seg == -1) + f->next_seg = f->segment_count - 1; + else + f->next_seg--; + f->valid_bits = 0; + + return 1; +} + +int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number) +{ + uint32 max_frame_samples; + + if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing); + + // fast page-level search + if (!seek_to_sample_coarse(f, sample_number)) + return 0; + + assert(f->current_loc_valid); + assert(f->current_loc <= sample_number); + + // linear search for the relevant packet + max_frame_samples = (f->blocksize_1*3 - f->blocksize_0) >> 2; + while (f->current_loc < sample_number) { + int left_start, left_end, right_start, right_end, mode, frame_samples; + if (!peek_decode_initial(f, &left_start, &left_end, &right_start, &right_end, &mode)) + return error(f, VORBIS_seek_failed); + // calculate the number of samples returned by the next frame + frame_samples = right_start - left_start; + if (f->current_loc + frame_samples > sample_number) { + return 1; // the next frame will contain the sample + } else if (f->current_loc + frame_samples + max_frame_samples > sample_number) { + // there's a chance the frame after this could contain the sample + vorbis_pump_first_frame(f); + } else { + // this frame is too early to be relevant + f->current_loc += frame_samples; + f->previous_length = 0; + maybe_start_packet(f); + flush_packet(f); + } + } + // the next frame should start with the sample + if (f->current_loc != sample_number) return error(f, VORBIS_seek_failed); + return 1; +} + +int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number) +{ + if (!stb_vorbis_seek_frame(f, sample_number)) + return 0; + + if (sample_number != f->current_loc) { + int n; + uint32 frame_start = f->current_loc; + stb_vorbis_get_frame_float(f, &n, NULL); + assert(sample_number > frame_start); + assert(f->channel_buffer_start + (int) (sample_number-frame_start) <= f->channel_buffer_end); + f->channel_buffer_start += (sample_number - frame_start); + } + + return 1; +} + +int stb_vorbis_seek_start(stb_vorbis *f) +{ + if (IS_PUSH_MODE(f)) { return error(f, VORBIS_invalid_api_mixing); } + set_file_offset(f, f->first_audio_page_offset); + f->previous_length = 0; + f->first_decode = TRUE; + f->next_seg = -1; + return vorbis_pump_first_frame(f); +} + +unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f) +{ + unsigned int restore_offset, previous_safe; + unsigned int end, last_page_loc; + + if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing); + if (!f->total_samples) { + unsigned int last; + uint32 lo,hi; + char header[6]; + + // first, store the current decode position so we can restore it + restore_offset = stb_vorbis_get_file_offset(f); + + // now we want to seek back 64K from the end (the last page must + // be at most a little less than 64K, but let's allow a little slop) + if (f->stream_len >= 65536 && f->stream_len-65536 >= f->first_audio_page_offset) + previous_safe = f->stream_len - 65536; + else + previous_safe = f->first_audio_page_offset; + + set_file_offset(f, previous_safe); + // previous_safe is now our candidate 'earliest known place that seeking + // to will lead to the final page' + + if (!vorbis_find_page(f, &end, &last)) { + // if we can't find a page, we're hosed! + f->error = VORBIS_cant_find_last_page; + f->total_samples = 0xffffffff; + goto done; + } + + // check if there are more pages + last_page_loc = stb_vorbis_get_file_offset(f); + + // stop when the last_page flag is set, not when we reach eof; + // this allows us to stop short of a 'file_section' end without + // explicitly checking the length of the section + while (!last) { + set_file_offset(f, end); + if (!vorbis_find_page(f, &end, &last)) { + // the last page we found didn't have the 'last page' flag + // set. whoops! + break; + } + //previous_safe = last_page_loc+1; // NOTE: not used after this point, but note for debugging + last_page_loc = stb_vorbis_get_file_offset(f); + } + + set_file_offset(f, last_page_loc); + + // parse the header + getn(f, (unsigned char *)header, 6); + // extract the absolute granule position + lo = get32(f); + hi = get32(f); + if (lo == 0xffffffff && hi == 0xffffffff) { + f->error = VORBIS_cant_find_last_page; + f->total_samples = SAMPLE_unknown; + goto done; + } + if (hi) + lo = 0xfffffffe; // saturate + f->total_samples = lo; + + f->p_last.page_start = last_page_loc; + f->p_last.page_end = end; + f->p_last.last_decoded_sample = lo; + + done: + set_file_offset(f, restore_offset); + } + return f->total_samples == SAMPLE_unknown ? 0 : f->total_samples; +} + +float stb_vorbis_stream_length_in_seconds(stb_vorbis *f) +{ + return stb_vorbis_stream_length_in_samples(f) / (float) f->sample_rate; +} + + + +int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output) +{ + int len, right,left,i; + if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing); + + if (!vorbis_decode_packet(f, &len, &left, &right)) { + f->channel_buffer_start = f->channel_buffer_end = 0; + return 0; + } + + len = vorbis_finish_frame(f, len, left, right); + for (i=0; i < f->channels; ++i) + f->outputs[i] = f->channel_buffers[i] + left; + + f->channel_buffer_start = left; + f->channel_buffer_end = left+len; + + if (channels) *channels = f->channels; + if (output) *output = f->outputs; + return len; +} + +#ifndef STB_VORBIS_NO_STDIO + +stb_vorbis * stb_vorbis_open_file_section(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc, unsigned int length) +{ + stb_vorbis *f, p; + vorbis_init(&p, alloc); + p.f = file; + p.f_start = (uint32) ftell(file); + p.stream_len = length; + p.close_on_free = close_on_free; + if (start_decoder(&p)) { + f = vorbis_alloc(&p); + if (f) { + *f = p; + vorbis_pump_first_frame(f); + return f; + } + } + if (error) *error = p.error; + vorbis_deinit(&p); + return NULL; +} + +stb_vorbis * stb_vorbis_open_file(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc) +{ + unsigned int len, start; + start = (unsigned int) ftell(file); + fseek(file, 0, SEEK_END); + len = (unsigned int) (ftell(file) - start); + fseek(file, start, SEEK_SET); + return stb_vorbis_open_file_section(file, close_on_free, error, alloc, len); +} + +stb_vorbis * stb_vorbis_open_filename(const char *filename, int *error, const stb_vorbis_alloc *alloc) +{ + FILE *f; +#if defined(_WIN32) && defined(__STDC_WANT_SECURE_LIB__) + if (0 != fopen_s(&f, filename, "rb")) + f = NULL; +#else + f = fopen(filename, "rb"); +#endif + if (f) + return stb_vorbis_open_file(f, TRUE, error, alloc); + if (error) *error = VORBIS_file_open_failure; + return NULL; +} +#endif // STB_VORBIS_NO_STDIO + +stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *error, const stb_vorbis_alloc *alloc) +{ + stb_vorbis *f, p; + if (!data) { + if (error) *error = VORBIS_unexpected_eof; + return NULL; + } + vorbis_init(&p, alloc); + p.stream = (uint8 *) data; + p.stream_end = (uint8 *) data + len; + p.stream_start = (uint8 *) p.stream; + p.stream_len = len; + p.push_mode = FALSE; + if (start_decoder(&p)) { + f = vorbis_alloc(&p); + if (f) { + *f = p; + vorbis_pump_first_frame(f); + if (error) *error = VORBIS__no_error; + return f; + } + } + if (error) *error = p.error; + vorbis_deinit(&p); + return NULL; +} + +#ifndef STB_VORBIS_NO_INTEGER_CONVERSION +#define PLAYBACK_MONO 1 +#define PLAYBACK_LEFT 2 +#define PLAYBACK_RIGHT 4 + +#define L (PLAYBACK_LEFT | PLAYBACK_MONO) +#define C (PLAYBACK_LEFT | PLAYBACK_RIGHT | PLAYBACK_MONO) +#define R (PLAYBACK_RIGHT | PLAYBACK_MONO) + +static int8 channel_position[7][6] = +{ + { 0 }, + { C }, + { L, R }, + { L, C, R }, + { L, R, L, R }, + { L, C, R, L, R }, + { L, C, R, L, R, C }, +}; + + +#ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT + typedef union { + float f; + int i; + } float_conv; + typedef char stb_vorbis_float_size_test[sizeof(float)==4 && sizeof(int) == 4]; + #define FASTDEF(x) float_conv x + // add (1<<23) to convert to int, then divide by 2^SHIFT, then add 0.5/2^SHIFT to round + #define MAGIC(SHIFT) (1.5f * (1 << (23-SHIFT)) + 0.5f/(1 << SHIFT)) + #define ADDEND(SHIFT) (((150-SHIFT) << 23) + (1 << 22)) + #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) (temp.f = (x) + MAGIC(s), temp.i - ADDEND(s)) + #define check_endianness() +#else + #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) ((int) ((x) * (1 << (s)))) + #define check_endianness() + #define FASTDEF(x) +#endif + +static void copy_samples(short *dest, float *src, int len) +{ + int i; + check_endianness(); + for (i=0; i < len; ++i) { + FASTDEF(temp); + int v = FAST_SCALED_FLOAT_TO_INT(temp, src[i],15); + if ((unsigned int) (v + 32768) > 65535) + v = v < 0 ? -32768 : 32767; + dest[i] = v; + } +} + +static void compute_samples(int mask, short *output, int num_c, float **data, int d_offset, int len) +{ + #define STB_BUFFER_SIZE 32 + float buffer[STB_BUFFER_SIZE]; + int i,j,o,n = STB_BUFFER_SIZE; + check_endianness(); + for (o = 0; o < len; o += STB_BUFFER_SIZE) { + memset(buffer, 0, sizeof(buffer)); + if (o + n > len) n = len - o; + for (j=0; j < num_c; ++j) { + if (channel_position[num_c][j] & mask) { + for (i=0; i < n; ++i) + buffer[i] += data[j][d_offset+o+i]; + } + } + for (i=0; i < n; ++i) { + FASTDEF(temp); + int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15); + if ((unsigned int) (v + 32768) > 65535) + v = v < 0 ? -32768 : 32767; + output[o+i] = v; + } + } + #undef STB_BUFFER_SIZE +} + +static void compute_stereo_samples(short *output, int num_c, float **data, int d_offset, int len) +{ + #define STB_BUFFER_SIZE 32 + float buffer[STB_BUFFER_SIZE]; + int i,j,o,n = STB_BUFFER_SIZE >> 1; + // o is the offset in the source data + check_endianness(); + for (o = 0; o < len; o += STB_BUFFER_SIZE >> 1) { + // o2 is the offset in the output data + int o2 = o << 1; + memset(buffer, 0, sizeof(buffer)); + if (o + n > len) n = len - o; + for (j=0; j < num_c; ++j) { + int m = channel_position[num_c][j] & (PLAYBACK_LEFT | PLAYBACK_RIGHT); + if (m == (PLAYBACK_LEFT | PLAYBACK_RIGHT)) { + for (i=0; i < n; ++i) { + buffer[i*2+0] += data[j][d_offset+o+i]; + buffer[i*2+1] += data[j][d_offset+o+i]; + } + } else if (m == PLAYBACK_LEFT) { + for (i=0; i < n; ++i) { + buffer[i*2+0] += data[j][d_offset+o+i]; + } + } else if (m == PLAYBACK_RIGHT) { + for (i=0; i < n; ++i) { + buffer[i*2+1] += data[j][d_offset+o+i]; + } + } + } + for (i=0; i < (n<<1); ++i) { + FASTDEF(temp); + int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15); + if ((unsigned int) (v + 32768) > 65535) + v = v < 0 ? -32768 : 32767; + output[o2+i] = v; + } + } + #undef STB_BUFFER_SIZE +} + +static void convert_samples_short(int buf_c, short **buffer, int b_offset, int data_c, float **data, int d_offset, int samples) +{ + int i; + if (buf_c != data_c && buf_c <= 2 && data_c <= 6) { + static int channel_selector[3][2] = { {0}, {PLAYBACK_MONO}, {PLAYBACK_LEFT, PLAYBACK_RIGHT} }; + for (i=0; i < buf_c; ++i) + compute_samples(channel_selector[buf_c][i], buffer[i]+b_offset, data_c, data, d_offset, samples); + } else { + int limit = buf_c < data_c ? buf_c : data_c; + for (i=0; i < limit; ++i) + copy_samples(buffer[i]+b_offset, data[i]+d_offset, samples); + for ( ; i < buf_c; ++i) + memset(buffer[i]+b_offset, 0, sizeof(short) * samples); + } +} + +int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples) +{ + float **output = NULL; + int len = stb_vorbis_get_frame_float(f, NULL, &output); + if (len > num_samples) len = num_samples; + if (len) + convert_samples_short(num_c, buffer, 0, f->channels, output, 0, len); + return len; +} + +static void convert_channels_short_interleaved(int buf_c, short *buffer, int data_c, float **data, int d_offset, int len) +{ + int i; + check_endianness(); + if (buf_c != data_c && buf_c <= 2 && data_c <= 6) { + assert(buf_c == 2); + for (i=0; i < buf_c; ++i) + compute_stereo_samples(buffer, data_c, data, d_offset, len); + } else { + int limit = buf_c < data_c ? buf_c : data_c; + int j; + for (j=0; j < len; ++j) { + for (i=0; i < limit; ++i) { + FASTDEF(temp); + float f = data[i][d_offset+j]; + int v = FAST_SCALED_FLOAT_TO_INT(temp, f,15);//data[i][d_offset+j],15); + if ((unsigned int) (v + 32768) > 65535) + v = v < 0 ? -32768 : 32767; + *buffer++ = v; + } + for ( ; i < buf_c; ++i) + *buffer++ = 0; + } + } +} + +int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts) +{ + float **output; + int len; + if (num_c == 1) return stb_vorbis_get_frame_short(f,num_c,&buffer, num_shorts); + len = stb_vorbis_get_frame_float(f, NULL, &output); + if (len) { + if (len*num_c > num_shorts) len = num_shorts / num_c; + convert_channels_short_interleaved(num_c, buffer, f->channels, output, 0, len); + } + return len; +} + +int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts) +{ + float **outputs; + int len = num_shorts / channels; + int n=0; + while (n < len) { + int k = f->channel_buffer_end - f->channel_buffer_start; + if (n+k >= len) k = len - n; + if (k) + convert_channels_short_interleaved(channels, buffer, f->channels, f->channel_buffers, f->channel_buffer_start, k); + buffer += k*channels; + n += k; + f->channel_buffer_start += k; + if (n == len) break; + if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break; + } + return n; +} + +int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int len) +{ + float **outputs; + int n=0; + while (n < len) { + int k = f->channel_buffer_end - f->channel_buffer_start; + if (n+k >= len) k = len - n; + if (k) + convert_samples_short(channels, buffer, n, f->channels, f->channel_buffers, f->channel_buffer_start, k); + n += k; + f->channel_buffer_start += k; + if (n == len) break; + if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break; + } + return n; +} + +#ifndef STB_VORBIS_NO_STDIO +int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output) +{ + int data_len, offset, total, limit, error; + short *data; + stb_vorbis *v = stb_vorbis_open_filename(filename, &error, NULL); + if (v == NULL) return -1; + limit = v->channels * 4096; + *channels = v->channels; + if (sample_rate) + *sample_rate = v->sample_rate; + offset = data_len = 0; + total = limit; + data = (short *) malloc(total * sizeof(*data)); + if (data == NULL) { + stb_vorbis_close(v); + return -2; + } + for (;;) { + int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data+offset, total-offset); + if (n == 0) break; + data_len += n; + offset += n * v->channels; + if (offset + limit > total) { + short *data2; + total *= 2; + data2 = (short *) realloc(data, total * sizeof(*data)); + if (data2 == NULL) { + free(data); + stb_vorbis_close(v); + return -2; + } + data = data2; + } + } + *output = data; + stb_vorbis_close(v); + return data_len; +} +#endif // NO_STDIO + +int stb_vorbis_decode_memory(const uint8 *mem, int len, int *channels, int *sample_rate, short **output) +{ + int data_len, offset, total, limit, error; + short *data; + stb_vorbis *v = stb_vorbis_open_memory(mem, len, &error, NULL); + if (v == NULL) return -1; + limit = v->channels * 4096; + *channels = v->channels; + if (sample_rate) + *sample_rate = v->sample_rate; + offset = data_len = 0; + total = limit; + data = (short *) malloc(total * sizeof(*data)); + if (data == NULL) { + stb_vorbis_close(v); + return -2; + } + for (;;) { + int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data+offset, total-offset); + if (n == 0) break; + data_len += n; + offset += n * v->channels; + if (offset + limit > total) { + short *data2; + total *= 2; + data2 = (short *) realloc(data, total * sizeof(*data)); + if (data2 == NULL) { + free(data); + stb_vorbis_close(v); + return -2; + } + data = data2; + } + } + *output = data; + stb_vorbis_close(v); + return data_len; +} +#endif // STB_VORBIS_NO_INTEGER_CONVERSION + +int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats) +{ + float **outputs; + int len = num_floats / channels; + int n=0; + int z = f->channels; + if (z > channels) z = channels; + while (n < len) { + int i,j; + int k = f->channel_buffer_end - f->channel_buffer_start; + if (n+k >= len) k = len - n; + for (j=0; j < k; ++j) { + for (i=0; i < z; ++i) + *buffer++ = f->channel_buffers[i][f->channel_buffer_start+j]; + for ( ; i < channels; ++i) + *buffer++ = 0; + } + n += k; + f->channel_buffer_start += k; + if (n == len) + break; + if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) + break; + } + return n; +} + +int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples) +{ + float **outputs; + int n=0; + int z = f->channels; + if (z > channels) z = channels; + while (n < num_samples) { + int i; + int k = f->channel_buffer_end - f->channel_buffer_start; + if (n+k >= num_samples) k = num_samples - n; + if (k) { + for (i=0; i < z; ++i) + memcpy(buffer[i]+n, f->channel_buffers[i]+f->channel_buffer_start, sizeof(float)*k); + for ( ; i < channels; ++i) + memset(buffer[i]+n, 0, sizeof(float) * k); + } + n += k; + f->channel_buffer_start += k; + if (n == num_samples) + break; + if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) + break; + } + return n; +} +#endif // STB_VORBIS_NO_PULLDATA_API + +/* Version history + 1.17 - 2019-07-08 - fix CVE-2019-13217, -13218, -13219, -13220, -13221, -13222, -13223 + found with Mayhem by ForAllSecure + 1.16 - 2019-03-04 - fix warnings + 1.15 - 2019-02-07 - explicit failure if Ogg Skeleton data is found + 1.14 - 2018-02-11 - delete bogus dealloca usage + 1.13 - 2018-01-29 - fix truncation of last frame (hopefully) + 1.12 - 2017-11-21 - limit residue begin/end to blocksize/2 to avoid large temp allocs in bad/corrupt files + 1.11 - 2017-07-23 - fix MinGW compilation + 1.10 - 2017-03-03 - more robust seeking; fix negative ilog(); clear error in open_memory + 1.09 - 2016-04-04 - back out 'avoid discarding last frame' fix from previous version + 1.08 - 2016-04-02 - fixed multiple warnings; fix setup memory leaks; + avoid discarding last frame of audio data + 1.07 - 2015-01-16 - fixed some warnings, fix mingw, const-correct API + some more crash fixes when out of memory or with corrupt files + 1.06 - 2015-08-31 - full, correct support for seeking API (Dougall Johnson) + some crash fixes when out of memory or with corrupt files + 1.05 - 2015-04-19 - don't define __forceinline if it's redundant + 1.04 - 2014-08-27 - fix missing const-correct case in API + 1.03 - 2014-08-07 - Warning fixes + 1.02 - 2014-07-09 - Declare qsort compare function _cdecl on windows + 1.01 - 2014-06-18 - fix stb_vorbis_get_samples_float + 1.0 - 2014-05-26 - fix memory leaks; fix warnings; fix bugs in multichannel + (API change) report sample rate for decode-full-file funcs + 0.99996 - bracket #include for macintosh compilation by Laurent Gomila + 0.99995 - use union instead of pointer-cast for fast-float-to-int to avoid alias-optimization problem + 0.99994 - change fast-float-to-int to work in single-precision FPU mode, remove endian-dependence + 0.99993 - remove assert that fired on legal files with empty tables + 0.99992 - rewind-to-start + 0.99991 - bugfix to stb_vorbis_get_samples_short by Bernhard Wodo + 0.9999 - (should have been 0.99990) fix no-CRT support, compiling as C++ + 0.9998 - add a full-decode function with a memory source + 0.9997 - fix a bug in the read-from-FILE case in 0.9996 addition + 0.9996 - query length of vorbis stream in samples/seconds + 0.9995 - bugfix to another optimization that only happened in certain files + 0.9994 - bugfix to one of the optimizations that caused significant (but inaudible?) errors + 0.9993 - performance improvements; runs in 99% to 104% of time of reference implementation + 0.9992 - performance improvement of IMDCT; now performs close to reference implementation + 0.9991 - performance improvement of IMDCT + 0.999 - (should have been 0.9990) performance improvement of IMDCT + 0.998 - no-CRT support from Casey Muratori + 0.997 - bugfixes for bugs found by Terje Mathisen + 0.996 - bugfix: fast-huffman decode initialized incorrectly for sparse codebooks; fixing gives 10% speedup - found by Terje Mathisen + 0.995 - bugfix: fix to 'effective' overrun detection - found by Terje Mathisen + 0.994 - bugfix: garbage decode on final VQ symbol of a non-multiple - found by Terje Mathisen + 0.993 - bugfix: pushdata API required 1 extra byte for empty page (failed to consume final page if empty) - found by Terje Mathisen + 0.992 - fixes for MinGW warning + 0.991 - turn fast-float-conversion on by default + 0.990 - fix push-mode seek recovery if you seek into the headers + 0.98b - fix to bad release of 0.98 + 0.98 - fix push-mode seek recovery; robustify float-to-int and support non-fast mode + 0.97 - builds under c++ (typecasting, don't use 'class' keyword) + 0.96 - somehow MY 0.95 was right, but the web one was wrong, so here's my 0.95 rereleased as 0.96, fixes a typo in the clamping code + 0.95 - clamping code for 16-bit functions + 0.94 - not publically released + 0.93 - fixed all-zero-floor case (was decoding garbage) + 0.92 - fixed a memory leak + 0.91 - conditional compiles to omit parts of the API and the infrastructure to support them: STB_VORBIS_NO_PULLDATA_API, STB_VORBIS_NO_PUSHDATA_API, STB_VORBIS_NO_STDIO, STB_VORBIS_NO_INTEGER_CONVERSION + 0.90 - first public release +*/ + +#endif // STB_VORBIS_HEADER_ONLY + + +/* +------------------------------------------------------------------------------ +This software is available under 2 licenses -- choose whichever you prefer. +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License +Copyright (c) 2017 Sean Barrett +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +------------------------------------------------------------------------------ +ALTERNATIVE B - Public Domain (www.unlicense.org) +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------ +*/ diff --git a/source_files/edge/CMakeLists.txt b/source_files/edge/CMakeLists.txt index fadb35748..3ea3234a4 100644 --- a/source_files/edge/CMakeLists.txt +++ b/source_files/edge/CMakeLists.txt @@ -86,7 +86,6 @@ set (EDGE_SOURCE_FILES s_music.cc s_ogg.cc s_fluid.cc - s_fmm.cc s_m4p.cc s_opl.cc s_rad.cc @@ -141,13 +140,11 @@ set (EDGE_LINK_LIBRARIES crsid dr_libs fluidlite - fmmidi HandmadeMath libRAD libvwad lua m4p - minivorbis miniz pl_mpeg prns diff --git a/source_files/edge/i_movie.cc b/source_files/edge/i_movie.cc index 7b86326a4..608e074e9 100644 --- a/source_files/edge/i_movie.cc +++ b/source_files/edge/i_movie.cc @@ -36,13 +36,11 @@ #include "w_wad.h" extern bool sound_device_stereo; -extern int sound_device_frequency; bool playing_movie = false; static bool need_canvas_update = false; static bool skip_bar_active = false; static plm_t *decoder = nullptr; -static SDL_AudioStream *movie_audio_stream = nullptr; static int movie_sample_rate = 0; static float skip_time; static uint8_t *movie_bytes = nullptr; @@ -53,17 +51,8 @@ static GLuint texture_y = 0; static GLuint texture_cb = 0; static GLuint texture_cr = 0; -static bool MovieSetupAudioStream(int rate) +static void MovieSetupAudio(int rate) { - movie_audio_stream = - SDL_NewAudioStream(AUDIO_F32, 2, rate, AUDIO_S16, sound_device_stereo ? 2 : 1, sound_device_frequency); - - if (!movie_audio_stream) - { - LogWarning("PlayMovie: Failed to setup audio stream: %s\n", SDL_GetError()); - return false; - } - plm_set_audio_lead_time(decoder, (double)1024 / (double)rate); PauseMusic(); @@ -71,27 +60,31 @@ static bool MovieSetupAudioStream(int rate) // think) - Dasho SoundQueueStop(); SoundQueueInitialize(); - - return true; } -static void MovieAudioCallback(plm_t *mpeg, plm_samples_t *samples, void *user) +void MovieAudioCallback(plm_t *mpeg, plm_samples_t *samples, void *user) { (void)mpeg; (void)user; - SDL_AudioStreamPut(movie_audio_stream, samples->interleaved, sizeof(float) * samples->count * 2); - int avail = SDL_AudioStreamAvailable(movie_audio_stream); - if (avail) + SoundData *movie_buf = SoundQueueGetFreeBuffer(PLM_AUDIO_SAMPLES_PER_FRAME, sound_device_stereo ? kMixInterleaved : kMixMono); + if (movie_buf) { - SoundData *movie_buf = SoundQueueGetFreeBuffer(avail / 2, sound_device_stereo ? kMixInterleaved : kMixMono); - if (movie_buf) + movie_buf->length_ = PLM_AUDIO_SAMPLES_PER_FRAME; + if (sound_device_stereo) + { + memcpy(movie_buf->data_, samples->interleaved, PLM_AUDIO_SAMPLES_PER_FRAME * 2 * sizeof(float)); + SoundQueueAddBuffer(movie_buf, movie_sample_rate); + } + else { - movie_buf->length_ = - SDL_AudioStreamGet(movie_audio_stream, movie_buf->data_left_, avail) / (sound_device_stereo ? 4 : 2); - if (movie_buf->length_ > 0) - SoundQueueAddBuffer(movie_buf, sound_device_frequency); - else - SoundQueueReturnBuffer(movie_buf); + float *src = samples->interleaved; + float *dest = movie_buf->data_; + float *dest_end = movie_buf->data_ + PLM_AUDIO_SAMPLES_PER_FRAME; + for (; dest < dest_end; src += 2) + { + *dest++ = (src[0] + src[1]) * 0.5f; + } + SoundQueueAddBuffer(movie_buf, movie_sample_rate); } } } @@ -236,12 +229,6 @@ void PlayMovie(const std::string &name) decoder = nullptr; } - if (movie_audio_stream) - { - SDL_FreeAudioStream(movie_audio_stream); - movie_audio_stream = nullptr; - } - decoder = plm_create_with_memory(movie_bytes, length, 0); if (!decoder) @@ -255,14 +242,7 @@ void PlayMovie(const std::string &name) if (!no_sound && !(movie->special_ & kMovieSpecialMute) && plm_get_num_audio_streams(decoder) > 0) { movie_sample_rate = plm_get_samplerate(decoder); - if (!MovieSetupAudioStream(movie_sample_rate)) - { - plm_destroy(decoder); - delete[] movie_bytes; - movie_bytes = nullptr; - decoder = nullptr; - return; - } + MovieSetupAudio(movie_sample_rate); } int movie_width = plm_get_width(decoder); @@ -314,7 +294,7 @@ void PlayMovie(const std::string &name) plm_set_video_decode_callback(decoder, MovieVideoCallback, nullptr); plm_set_audio_decode_callback(decoder, MovieAudioCallback, nullptr); - if (!no_sound && movie_audio_stream) + if (!no_sound) { plm_set_audio_enabled(decoder, 1); plm_set_audio_stream(decoder, 0); @@ -459,11 +439,6 @@ void PlayMovie(const std::string &name) decoder = nullptr; delete[] movie_bytes; movie_bytes = nullptr; - if (movie_audio_stream) - { - SDL_FreeAudioStream(movie_audio_stream); - movie_audio_stream = nullptr; - } glClearColor(0, 0, 0, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); FinishFrame(); diff --git a/source_files/edge/i_sound.cc b/source_files/edge/i_sound.cc index 9815f7b99..100553646 100644 --- a/source_files/edge/i_sound.cc +++ b/source_files/edge/i_sound.cc @@ -67,9 +67,9 @@ static bool TryOpenSound(int want_freq, bool want_stereo) LogPrint("StartupSound: trying %d Hz %s\n", want_freq, want_stereo ? "Stereo" : "Mono"); trydev.freq = want_freq; - trydev.format = AUDIO_S16SYS; + trydev.format = AUDIO_F32SYS; trydev.channels = want_stereo ? 2 : 1; - trydev.samples = 1024; + trydev.samples = 512; trydev.callback = SoundFillCallback; current_sound_device = SDL_OpenAudioDevice(nullptr, 0, &trydev, &sound_device_check, 0); @@ -137,7 +137,7 @@ void StartupAudio(void) // desired format and convert silently if needed, but they might end up // being a good safety net - Dasho - if (sound_device_check.format != AUDIO_S16SYS) + if (sound_device_check.format != AUDIO_F32SYS) { LogPrint("StartupSound: unsupported format: %d\n", sound_device_check.format); SDL_CloseAudioDevice(current_sound_device); @@ -159,13 +159,12 @@ void StartupAudio(void) else if (!want_stereo && sound_device_check.channels != 1) LogPrint("StartupSound: mono sound not available.\n"); - if (sound_device_check.freq < (want_freq - want_freq / 100) || - sound_device_check.freq > (want_freq + want_freq / 100)) + if (sound_device_check.freq != want_freq) { LogPrint("StartupSound: %d Hz sound not available.\n", want_freq); } - sound_device_bytes_per_sample = (sound_device_check.channels) * 2; + sound_device_bytes_per_sample = sound_device_check.channels * sizeof(float); sound_device_samples_per_buffer = sound_device_check.size / sound_device_bytes_per_sample; EPI_ASSERT(sound_device_bytes_per_sample > 0); diff --git a/source_files/edge/m_option.cc b/source_files/edge/m_option.cc index 62f614082..cd3ab3644 100644 --- a/source_files/edge/m_option.cc +++ b/source_files/edge/m_option.cc @@ -108,7 +108,6 @@ #include "s_blit.h" #include "s_cache.h" #include "s_fluid.h" -#include "s_fmm.h" #include "s_music.h" #include "s_opl.h" #include "s_sound.h" @@ -527,7 +526,7 @@ static OptionMenuItem soundoptions[] = { {kOptionMenuItemTypePlain, "", nullptr, 0, nullptr, nullptr, nullptr}, {kOptionMenuItemTypeSwitch, "Stereo", "Off/On/Swapped", 3, &var_sound_stereo, nullptr, "NeedRestart"}, {kOptionMenuItemTypePlain, "", nullptr, 0, nullptr, nullptr, nullptr}, - {kOptionMenuItemTypeSwitch, "MIDI Player", "Fluidlite/Opal/FMMIDI", 3, &var_midi_player, OptionMenuChangeMidiPlayer, + {kOptionMenuItemTypeSwitch, "MIDI Player", "Fluidlite/Opal", 2, &var_midi_player, OptionMenuChangeMidiPlayer, nullptr}, {kOptionMenuItemTypeFunction, "Fluidlite Soundfont", nullptr, 0, nullptr, OptionMenuChangeSoundfont, nullptr}, {kOptionMenuItemTypeBoolean, "PC Speaker Mode", YesNo, 2, &pc_speaker_mode, OptionMenuChangePCSpeakerMode, @@ -2039,10 +2038,8 @@ static void OptionMenuChangeMidiPlayer(int key_pressed, ConsoleVariable *console if (var_midi_player == 1 || (playing && (playing->type_ == kDDFMusicIMF280 || playing->type_ == kDDFMusicIMF560 || playing->type_ == kDDFMusicIMF700))) RestartOpal(); - else if (var_midi_player == 0) - RestartFluid(); else - RestartFMM(); + RestartFluid(); } // diff --git a/source_files/edge/s_blit.cc b/source_files/edge/s_blit.cc index 94e92e334..eb0a7f680 100644 --- a/source_files/edge/s_blit.cc +++ b/source_files/edge/s_blit.cc @@ -27,6 +27,7 @@ #include +#include "AlmostEquals.h" #include "dm_state.h" #include "epi.h" #include "epi_sdl.h" @@ -46,8 +47,8 @@ // The more safe bits there are, the less likely the final // output sum will overflow into white noise, but the less // precision you have left for the volume multiplier. -static constexpr uint8_t kSafeClippingBits = 4; -static constexpr int32_t kSoundClipThreshold = ((1 << (31 - kSafeClippingBits)) - 1); +static constexpr float kSoundClipMaximum = 1.0f; +static constexpr float kSoundClipMinimum = -1.0f; static constexpr uint8_t kMinimumSoundChannels = 32; static constexpr uint16_t kMaximumSoundChannels = 256; @@ -65,8 +66,8 @@ int ddf_reverb_ratio = 0; int ddf_reverb_delay = 0; float music_player_gain = 1.0f; -static int *mix_buffer; -static int mix_buffer_length; +static float *mix_buffer; +static int mix_buffer_length; static constexpr uint8_t kMaximumQueueBuffers = 16; @@ -103,16 +104,7 @@ SoundChannel::~SoundChannel() void SoundChannel::ComputeDelta() { - // frequency close enough ? - if (data_->frequency_ > (sound_device_frequency - sound_device_frequency / 100) && - data_->frequency_ < (sound_device_frequency + sound_device_frequency / 100)) - { - delta_ = (1 << 10); - } - else - { - delta_ = (uint32_t)floor((float)data_->frequency_ * 1024.0f / sound_device_frequency); - } + delta_ = (float)data_->frequency_ * (1.0f / (float)sound_device_frequency); } void SoundChannel::ComputeVolume() @@ -144,7 +136,7 @@ void SoundChannel::ComputeVolume() } } - float MAX_VOL = (1 << (16 - kSafeClippingBits)) - 3; + float MAX_VOL = 1.0f; MAX_VOL = (boss_ ? MAX_VOL : MAX_VOL / dist) * sound_effect_volume.f_; @@ -152,21 +144,21 @@ void SoundChannel::ComputeVolume() MAX_VOL *= definition_->volume_; // strictly linear equations - volume_left_ = (int)(MAX_VOL * (1.0 - sep)); - volume_right_ = (int)(MAX_VOL * (0.0 + sep)); + volume_left_ = (MAX_VOL * (1.0 - sep)); + volume_right_ = (MAX_VOL * (0.0 + sep)); if (var_sound_stereo == 2) /* SWAP ! */ { if (!fliplevels.d_) { - int tmp = volume_left_; + float tmp = volume_left_; volume_left_ = volume_right_; volume_right_ = tmp; } } else if (fliplevels.d_) { - int tmp = volume_left_; + float tmp = volume_left_; volume_left_ = volume_right_; volume_right_ = tmp; } @@ -174,114 +166,56 @@ void SoundChannel::ComputeVolume() void SoundChannel::ComputeMusicVolume() { - float MAX_VOL = (1 << (16 - kSafeClippingBits)) - 3; + float MAX_VOL = 1.0f; - MAX_VOL = MAX_VOL * music_volume.f_ * - music_player_gain; // This last one is an internal value that depends on music format + MAX_VOL = MAX_VOL * music_volume.f_ * music_player_gain; // This last one is an internal value that depends on music format - volume_left_ = (int)MAX_VOL; - volume_right_ = (int)MAX_VOL; + volume_left_ = MAX_VOL; + volume_right_ = MAX_VOL; } //---------------------------------------------------------------------------- -static void BlitToS16(const int *src, int16_t *dest, int length) +static void BlitToF32(const float *src, float *dest, int length) { - const int *s_end = src + length; - - while (src < s_end) - { - int val = *src++; - - if (val > kSoundClipThreshold) - val = kSoundClipThreshold; - else if (val < -kSoundClipThreshold) - val = -kSoundClipThreshold; - - *dest++ = (int16_t)(val >> (16 - kSafeClippingBits)); - } -} - -static void MixMono(SoundChannel *chan, int *dest, int pairs) -{ - EPI_ASSERT(pairs > 0); - - int16_t *src_L; - - if (paused || menu_active) - src_L = chan->data_->data_left_; - else - { - if (!chan->data_->is_sound_effect_ || chan->category_ == kCategoryUi || - chan->data_->current_filter_ == kFilterNone) - src_L = chan->data_->data_left_; - else - src_L = chan->data_->filter_data_left_; - } - - int *d_pos = dest; - int *d_end = d_pos + pairs; - - uint32_t offset = chan->offset_; - - while (d_pos < d_end) - { - *d_pos++ += src_L[offset >> 10] * chan->volume_left_; - - offset += chan->delta_; - } - - chan->offset_ = offset; - - EPI_ASSERT(offset - chan->delta_ < chan->length_); + memcpy(dest, src, length * sizeof(float)); } -static void MixStereo(SoundChannel *chan, int *dest, int pairs) +static void MixMono(SoundChannel *chan, float *dest, int pairs) { EPI_ASSERT(pairs > 0); - int16_t *src_L; - int16_t *src_R; + float *src = chan->data_->data_; - if (paused || menu_active) - { + /*if (paused || menu_active) src_L = chan->data_->data_left_; - src_R = chan->data_->data_right_; - } else { if (!chan->data_->is_sound_effect_ || chan->category_ == kCategoryUi || chan->data_->current_filter_ == kFilterNone) - { src_L = chan->data_->data_left_; - src_R = chan->data_->data_right_; - } else - { src_L = chan->data_->filter_data_left_; - src_R = chan->data_->filter_data_right_; - } - } + }*/ - int *d_pos = dest; - int *d_end = d_pos + pairs * 2; + float *d_pos = dest; + float *d_end = d_pos + pairs; - uint32_t offset = chan->offset_; + float offset = chan->offset_; while (d_pos < d_end) { - *d_pos++ += src_L[offset >> 10] * chan->volume_left_; - *d_pos++ += src_R[offset >> 10] * chan->volume_right_; + *d_pos++ += src[(uint32_t)offset] * chan->volume_left_; offset += chan->delta_; } chan->offset_ = offset; - EPI_ASSERT(offset - chan->delta_ < chan->length_); + EPI_ASSERT((uint32_t)(offset - chan->delta_) < chan->length_); } -static void MixInterleaved(SoundChannel *chan, int *dest, int pairs) +static void MixInterleaved(SoundChannel *chan, float *dest, int pairs) { if (!sound_device_stereo) FatalError("INTERNAL ERROR: tried to mix an interleaved buffer in MONO " @@ -289,9 +223,9 @@ static void MixInterleaved(SoundChannel *chan, int *dest, int pairs) EPI_ASSERT(pairs > 0); - int16_t *src_L; + float *src = chan->data_->data_; - if (paused || menu_active) + /*if (paused || menu_active) src_L = chan->data_->data_left_; else { @@ -300,26 +234,25 @@ static void MixInterleaved(SoundChannel *chan, int *dest, int pairs) src_L = chan->data_->data_left_; else src_L = chan->data_->filter_data_left_; - } + }*/ - int *d_pos = dest; - int *d_end = d_pos + pairs * 2; + float *d_pos = dest; + float *d_end = d_pos + pairs * 2; - uint32_t offset = chan->offset_; + float offset = chan->offset_; while (d_pos < d_end) { - uint32_t pos = (offset >> 9) & ~1; - - *d_pos++ += src_L[pos] * chan->volume_left_; - *d_pos++ += src_L[pos | 1] * chan->volume_right_; + uint32_t pos = (uint32_t)(offset * 2.0f) & ~1; + *d_pos++ += src[pos] * chan->volume_left_; + *d_pos++ += src[pos|1] * chan->volume_right_; offset += chan->delta_; } chan->offset_ = offset; - EPI_ASSERT(offset - chan->delta_ < chan->length_); + EPI_ASSERT((uint32_t)(offset - chan->delta_) < chan->length_); } static void MixOneChannel(SoundChannel *chan, int pairs) @@ -327,39 +260,37 @@ static void MixOneChannel(SoundChannel *chan, int pairs) if (sound_effects_paused && chan->category_ >= kCategoryPlayer) return; - if (chan->volume_left_ == 0 && chan->volume_right_ == 0) + if (AlmostEquals(chan->volume_left_, 0.0f) && AlmostEquals(chan->volume_right_, 0.0f)) return; - EPI_ASSERT(chan->offset_ < chan->length_); + EPI_ASSERT((uint32_t)chan->offset_ < chan->length_); - int *dest = mix_buffer; + float *dest = mix_buffer; while (pairs > 0) { int count = pairs; // check if enough sound data is left - if (chan->offset_ + count * chan->delta_ >= chan->length_) + if ((uint32_t)(chan->offset_ + count * chan->delta_) >= chan->length_) { // find minimum number of samples we can play - double avail = (chan->length_ - chan->offset_ + chan->delta_ - 1) / (double)chan->delta_; + double avail = (chan->length_ - (uint32_t)(chan->offset_)) / (double)chan->delta_; count = (int)floor(avail); EPI_ASSERT(count > 0); EPI_ASSERT(count <= pairs); - EPI_ASSERT(chan->offset_ + count * chan->delta_ >= chan->length_); + EPI_ASSERT((uint32_t)RoundToInteger(chan->offset_ + count * chan->delta_) >= chan->length_); } if (chan->data_->mode_ == kMixInterleaved) MixInterleaved(chan, dest, count); - else if (sound_device_stereo) - MixStereo(chan, dest, count); else MixMono(chan, dest, count); - if (chan->offset_ >= chan->length_) + if ((uint32_t)chan->offset_ >= chan->length_) { if (!chan->loop_) { @@ -393,7 +324,7 @@ static bool QueueNextBuffer(void) queue_channel->data_ = buf; queue_channel->offset_ = 0; - queue_channel->length_ = buf->length_ << 10; + queue_channel->length_ = buf->length_; queue_channel->ComputeDelta(); @@ -411,36 +342,34 @@ static void MixQueues(int pairs) if (chan->volume_left_ == 0 && chan->volume_right_ == 0) return; - EPI_ASSERT(chan->offset_ < chan->length_); + EPI_ASSERT((uint32_t)chan->offset_ < chan->length_); - int *dest = mix_buffer; + float *dest = mix_buffer; while (pairs > 0) { int count = pairs; // check if enough sound data is left - if (chan->offset_ + count * chan->delta_ >= chan->length_) + if ((uint32_t)(chan->offset_ + count * chan->delta_) >= chan->length_) { // find minimum number of samples we can play - double avail = (chan->length_ - chan->offset_ + chan->delta_ - 1) / (double)chan->delta_; + double avail = (chan->length_ - (uint32_t)(chan->offset_)) / (double)chan->delta_; count = (int)floor(avail); EPI_ASSERT(count > 0); EPI_ASSERT(count <= pairs); - EPI_ASSERT(chan->offset_ + count * chan->delta_ >= chan->length_); + EPI_ASSERT((uint32_t)RoundToInteger(chan->offset_ + count * chan->delta_) >= chan->length_); } if (chan->data_->mode_ == kMixInterleaved) MixInterleaved(chan, dest, count); - else if (sound_device_stereo) - MixStereo(chan, dest, count); else MixMono(chan, dest, count); - if (chan->offset_ >= chan->length_) + if ((uint32_t)chan->offset_ >= chan->length_) { // reached end of current queued buffer. // Place current buffer onto free list, @@ -479,7 +408,7 @@ void MixAllSoundChannels(void *stream, int len) EPI_ASSERT(mix_buffer && samples <= mix_buffer_length); // clear mixer buffer - memset(mix_buffer, 0, mix_buffer_length * sizeof(int)); + memset(mix_buffer, 0, mix_buffer_length * sizeof(float)); // add each channel for (int i = 0; i < total_channels; i++) @@ -493,7 +422,7 @@ void MixAllSoundChannels(void *stream, int len) MixQueues(pairs); // blit to the SDL stream - BlitToS16(mix_buffer, (int16_t *)stream, samples); + BlitToF32(mix_buffer, (float *)stream, samples); } //---------------------------------------------------------------------------- @@ -512,7 +441,7 @@ void InitializeSoundChannels(int total) // allocate mixer buffer mix_buffer_length = sound_device_samples_per_buffer * (sound_device_stereo ? 2 : 1); - mix_buffer = new int[mix_buffer_length]; + mix_buffer = new float[mix_buffer_length]; } void FreeSoundChannels(void) diff --git a/source_files/edge/s_blit.h b/source_files/edge/s_blit.h index 4cc551a75..a3c25775a 100644 --- a/source_files/edge/s_blit.h +++ b/source_files/edge/s_blit.h @@ -52,14 +52,12 @@ class SoundChannel SoundEffectDefinition *definition_; Position *position_; - // We use a 22.10 fixed point for sound offsets. It's a reasonable - // compromise between longest sound and accumulated round-off error. - uint32_t offset_; + float offset_; + float delta_; uint32_t length_; - uint32_t delta_; - int volume_left_; // mixing volume - int volume_right_; + float volume_left_; // mixing volume + float volume_right_; bool loop_; // will loop *one* more time bool boss_; diff --git a/source_files/edge/s_cache.cc b/source_files/edge/s_cache.cc index 97b33c88d..d625512e7 100644 --- a/source_files/edge/s_cache.cc +++ b/source_files/edge/s_cache.cc @@ -49,6 +49,7 @@ #include "w_files.h" #include "w_wad.h" +extern bool sound_device_stereo; extern int sound_device_frequency; extern bool pc_speaker_mode; @@ -59,9 +60,9 @@ static void LoadSilence(SoundData *buf) int length = 256; buf->frequency_ = sound_device_frequency; - buf->Allocate(length, kMixMono); + buf->Allocate(length, sound_device_stereo ? kMixInterleaved : kMixMono); - memset(buf->data_left_, 0, length * sizeof(int16_t)); + memset(buf->data_, 0, length * sizeof(float) * (sound_device_stereo ? 2 : 1)); } static bool LoadDoom(SoundData *buf, const uint8_t *lump, int length) @@ -79,16 +80,37 @@ static bool LoadDoom(SoundData *buf, const uint8_t *lump, int length) if (length <= 0) return false; - buf->Allocate(length, kMixMono); + buf->Allocate(length, sound_device_stereo ? kMixInterleaved : kMixMono); // convert to signed 16-bit format const uint8_t *src = lump + 8; const uint8_t *s_end = src + length; - int16_t *dest = buf->data_left_; + float *dest = buf->data_; - for (; src < s_end; src++) - *dest++ = (*src ^ 0x80) << 8; + if (sound_device_stereo) + { + float src_f = 0; + for (; src < s_end; src++) + { + int16_t in = ((*src ^ 0x80) << 8); + *(uint32_t *)&src_f=0x43818000^((uint16_t)in); + src_f -= 259.0f; + *dest++ = src_f; + *dest++ = src_f; + } + } + else + { + for (; src < s_end; src++) + { + int16_t in = ((*src ^ 0x80) << 8); + *(uint32_t *)dest=0x43818000^((uint16_t)in); + *dest++ -= 259.0f; + } + } + +//65540 return true; } diff --git a/source_files/edge/s_flac.cc b/source_files/edge/s_flac.cc index a9f02466a..a2b72d09d 100644 --- a/source_files/edge/s_flac.cc +++ b/source_files/edge/s_flac.cc @@ -50,12 +50,13 @@ class FLACPlayer : public AbstractMusicPlayer int status_; bool looping_; + bool is_stereo_; drflac *flac_track_; // I had to make it rhyme uint8_t *flac_data_; // Passed in from s_music; must be deleted on close - int16_t *mono_buffer_; + float *mono_buffer_; public: bool OpenMemory(uint8_t *data, int length); @@ -80,7 +81,7 @@ class FLACPlayer : public AbstractMusicPlayer FLACPlayer::FLACPlayer() : status_(kNotLoaded) { - mono_buffer_ = new int16_t[kMusicBuffer * 2]; + mono_buffer_ = new float[kMusicBuffer * 2]; } FLACPlayer::~FLACPlayer() @@ -93,34 +94,43 @@ FLACPlayer::~FLACPlayer() void FLACPlayer::PostOpen() { + if (flac_track_->channels == 1) + { + is_stereo_ = false; + } + else + { + is_stereo_ = true; + } + // Loaded, but not playing status_ = kStopped; } -static void ConvertToMono(int16_t *dest, const int16_t *src, int len) +static void ConvertToMono(float *dest, const float *src, int len) { - const int16_t *s_end = src + len * 2; + const float *s_end = src + len * 2; for (; src < s_end; src += 2) { // compute average of samples - *dest++ = ((int)src[0] + (int)src[1]) >> 1; + *dest++ = (src[0] + src[1]) * 0.5f; } } bool FLACPlayer::StreamIntoBuffer(SoundData *buf) { - int16_t *data_buf; + float *data_buf; bool song_done = false; if (!sound_device_stereo) data_buf = mono_buffer_; else - data_buf = buf->data_left_; + data_buf = buf->data_; - drflac_uint64 frames = drflac_read_pcm_frames_s16(flac_track_, kMusicBuffer, data_buf); + drflac_uint64 frames = drflac_read_pcm_frames_f32(flac_track_, kMusicBuffer, data_buf); if (frames < kMusicBuffer) song_done = true; @@ -129,8 +139,8 @@ bool FLACPlayer::StreamIntoBuffer(SoundData *buf) buf->frequency_ = flac_track_->sampleRate; - if (!sound_device_stereo) - ConvertToMono(buf->data_left_, mono_buffer_, buf->length_); + if (is_stereo_ && !sound_device_stereo) + ConvertToMono(buf->data_, mono_buffer_, buf->length_); if (song_done) /* EOF */ { diff --git a/source_files/edge/s_fluid.cc b/source_files/edge/s_fluid.cc index 850c76182..1d49cb0fa 100644 --- a/source_files/edge/s_fluid.cc +++ b/source_files/edge/s_fluid.cc @@ -68,14 +68,14 @@ static void *edge_fluid_fopen(fluid_fileapi_t *fileapi, const char *filename) return fp; } -static void ConvertToMono(int16_t *dest, const int16_t *src, int len) +static void ConvertToMono(float *dest, const float *src, int len) { - const int16_t *s_end = src + len * 2; + const float *s_end = src + len * 2; for (; src < s_end; src += 2) { // compute average of samples - *dest++ = ((int)src[0] + (int)src[1]) >> 1; + *dest++ = (src[0] + src[1]) * 0.5f; } } @@ -186,12 +186,12 @@ class FluidPlayer : public AbstractMusicPlayer FluidInterface *fluid_interface_; - int16_t *mono_buffer_; + float *mono_buffer_; public: FluidPlayer(uint8_t *data, int _length, bool looping) : status_(kNotLoaded), looping_(looping) { - mono_buffer_ = new int16_t[kMusicBuffer * 2]; + mono_buffer_ = new float[kMusicBuffer * 2]; SequencerInit(); } @@ -263,7 +263,7 @@ class FluidPlayer : public AbstractMusicPlayer static void playSynth(void *userdata, uint8_t *stream, size_t length) { - fluid_synth_write_s16(edge_fluid, (int)length / 4, stream, 0, 2, stream + 2, 0, 2); + fluid_synth_write_float(edge_fluid, (int)length / 8, stream, 0, 2, stream + 4, 0, 2); } void SequencerInit() @@ -286,7 +286,7 @@ class FluidPlayer : public AbstractMusicPlayer fluid_interface_->onPcmRender_userdata = this; fluid_interface_->pcmSampleRate = sound_device_frequency; - fluid_interface_->pcmFrameSize = 2 /*channels*/ * 2 /*size of one sample*/; + fluid_interface_->pcmFrameSize = 2 /*channels*/ * 4 /*size of one sample*/; fluid_interface_->rt_deviceSwitch = rtDeviceSwitch; fluid_interface_->rt_currentDevice = rtCurrentDevice; @@ -397,24 +397,24 @@ class FluidPlayer : public AbstractMusicPlayer private: bool StreamIntoBuffer(SoundData *buf) { - int16_t *data_buf; + float *data_buf; bool song_done = false; if (!sound_device_stereo) data_buf = mono_buffer_; else - data_buf = buf->data_left_; + data_buf = buf->data_; int played = fluid_sequencer_->PlayStream((uint8_t *)data_buf, kMusicBuffer); if (fluid_sequencer_->PositionAtEnd()) song_done = true; - buf->length_ = played / 4; + buf->length_ = played / 8; if (!sound_device_stereo) - ConvertToMono(buf->data_left_, mono_buffer_, buf->length_); + ConvertToMono(buf->data_, mono_buffer_, buf->length_); if (song_done) /* EOF */ { diff --git a/source_files/edge/s_fmm.cc b/source_files/edge/s_fmm.cc deleted file mode 100644 index fa23b933e..000000000 --- a/source_files/edge/s_fmm.cc +++ /dev/null @@ -1,383 +0,0 @@ -//---------------------------------------------------------------------------- -// EDGE FMMIDI Music Player -//---------------------------------------------------------------------------- -// -// Copyright (c) 2023-2024 The EDGE Team. -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 3 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -//---------------------------------------------------------------------------- - -#include "s_fmm.h" - -#include -#include - -#include "dm_state.h" -#include "epi_file.h" -#include "epi_filesystem.h" -#include "epi_str_util.h" -#include "i_system.h" -#include "m_misc.h" -#include "midisynth.hpp" -// clang-format off -#define MidiFraction FMMFraction -#define MidiSequencer FMMSequencer -typedef struct MidiRealTimeInterface FMMInterface; -#include "midi_sequencer_impl.hpp" -// clang-format on -#include "s_blit.h" - -extern bool sound_device_stereo; -extern int sound_device_frequency; - -// Should only be invoked when switching MIDI players -void RestartFMM(void) -{ - int old_entry = entry_playing; - - StopMusic(); - - ChangeMusic(old_entry, true); // Restart track that was kPlaying when switched - - return; // OK! -} - -static void ConvertToMono(int16_t *dest, const int16_t *src, int len) -{ - const int16_t *s_end = src + len * 2; - - for (; src < s_end; src += 2) - { - // compute average of samples - *dest++ = ((int)src[0] + (int)src[1]) >> 1; - } -} - -class FMMPlayer : public AbstractMusicPlayer -{ - private: - private: - enum status_ - { - kNotLoaded, - kPlaying, - kPaused, - kStopped - }; - - int status_; - bool looping_; - - FMMInterface *fmm_interface_; - - int16_t *mono_buffer_; - - public: - FMMPlayer(uint8_t *data, int length, bool looping) : status_(kNotLoaded), looping_(looping) - { - mono_buffer_ = new int16_t[kMusicBuffer * 2]; - SequencerInit(); - } - - ~FMMPlayer() - { - Close(); - - if (mono_buffer_) - delete[] mono_buffer_; - } - - public: - FMMSequencer *fmm_sequencer_; - midisynth::synthesizer *fmm_synth_; - midisynth::fm_note_factory *fmm_note_factory_; - - static void rtNoteOn(void *userdata, uint8_t channel, uint8_t note, uint8_t velocity) - { - FMMPlayer *player = (FMMPlayer *)userdata; - player->fmm_synth_->note_on(channel, note, velocity); - } - - static void rtNoteOff(void *userdata, uint8_t channel, uint8_t note) - { - FMMPlayer *player = (FMMPlayer *)userdata; - player->fmm_synth_->note_off(channel, note, 0); - } - - static void rtNoteAfterTouch(void *userdata, uint8_t channel, uint8_t note, uint8_t atVal) - { - FMMPlayer *player = (FMMPlayer *)userdata; - player->fmm_synth_->polyphonic_key_pressure(channel, note, atVal); - } - - static void rtChannelAfterTouch(void *userdata, uint8_t channel, uint8_t atVal) - { - FMMPlayer *player = (FMMPlayer *)userdata; - player->fmm_synth_->channel_pressure(channel, atVal); - } - - static void rtControllerChange(void *userdata, uint8_t channel, uint8_t type, uint8_t value) - { - FMMPlayer *player = (FMMPlayer *)userdata; - player->fmm_synth_->control_change(channel, type, value); - } - - static void rtPatchChange(void *userdata, uint8_t channel, uint8_t patch) - { - FMMPlayer *player = (FMMPlayer *)userdata; - player->fmm_synth_->program_change(channel, patch); - } - - static void rtPitchBend(void *userdata, uint8_t channel, uint8_t msb, uint8_t lsb) - { - FMMPlayer *player = (FMMPlayer *)userdata; - player->fmm_synth_->pitch_bend_change(channel, (msb << 7) | lsb); - } - - static void rtSysEx(void *userdata, const uint8_t *msg, size_t size) - { - FMMPlayer *player = (FMMPlayer *)userdata; - player->fmm_synth_->sysex_message(msg, size); - } - - static void rtDeviceSwitch(void *userdata, size_t track, const char *data, size_t length) - { - (void)userdata; - (void)track; - (void)data; - (void)length; - } - - static size_t rtCurrentDevice(void *userdata, size_t track) - { - (void)userdata; - (void)track; - return 0; - } - - static void playSynth(void *userdata, uint8_t *stream, size_t length) - { - FMMPlayer *player = (FMMPlayer *)userdata; - player->fmm_synth_->synthesize(reinterpret_cast(stream), length / 4, sound_device_frequency); - } - - void SequencerInit() - { - fmm_sequencer_ = new FMMSequencer; - fmm_interface_ = new FMMInterface; - memset(fmm_interface_, 0, sizeof(MidiRealTimeInterface)); - - fmm_interface_->rtUserData = this; - fmm_interface_->rt_noteOn = rtNoteOn; - fmm_interface_->rt_noteOff = rtNoteOff; - fmm_interface_->rt_noteAfterTouch = rtNoteAfterTouch; - fmm_interface_->rt_channelAfterTouch = rtChannelAfterTouch; - fmm_interface_->rt_controllerChange = rtControllerChange; - fmm_interface_->rt_patchChange = rtPatchChange; - fmm_interface_->rt_pitchBend = rtPitchBend; - fmm_interface_->rt_systemExclusive = rtSysEx; - - fmm_interface_->onPcmRender = playSynth; - fmm_interface_->onPcmRender_userdata = this; - - fmm_interface_->pcmSampleRate = sound_device_frequency; - fmm_interface_->pcmFrameSize = 2 /*channels*/ * 2 /*size of one sample*/; - - fmm_interface_->rt_deviceSwitch = rtDeviceSwitch; - fmm_interface_->rt_currentDevice = rtCurrentDevice; - - fmm_sequencer_->SetInterface(fmm_interface_); - } - - bool LoadTrack(const uint8_t *data, int length) - { - return fmm_sequencer_->LoadMidi(data, length); - } - - void Close(void) - { - if (status_ == kNotLoaded) - return; - - // Stop playback - if (status_ != kStopped) - Stop(); - - if (fmm_sequencer_) - { - delete fmm_sequencer_; - fmm_sequencer_ = nullptr; - } - if (fmm_interface_) - { - delete fmm_interface_; - fmm_interface_ = nullptr; - } - if (fmm_note_factory_) - { - delete fmm_note_factory_; - fmm_note_factory_ = nullptr; - } - if (fmm_synth_) - { - delete fmm_synth_; - fmm_synth_ = nullptr; - } - - status_ = kNotLoaded; - } - - void Play(bool loop) - { - if (!(status_ == kNotLoaded || status_ == kStopped)) - return; - - status_ = kPlaying; - looping_ = loop; - - // Load up initial buffer data - Ticker(); - } - - void Stop(void) - { - if (!(status_ == kPlaying || status_ == kPaused)) - return; - - fmm_synth_->all_sound_off_immediately(); - - SoundQueueStop(); - - status_ = kStopped; - } - - void Pause(void) - { - if (status_ != kPlaying) - return; - - fmm_synth_->all_sound_off(); - - status_ = kPaused; - } - - void Resume(void) - { - if (status_ != kPaused) - return; - - status_ = kPlaying; - } - - void Ticker(void) - { - while (status_ == kPlaying && !pc_speaker_mode) - { - SoundData *buf = SoundQueueGetFreeBuffer(kMusicBuffer, sound_device_stereo ? kMixInterleaved : kMixMono); - - if (!buf) - break; - - if (StreamIntoBuffer(buf)) - { - SoundQueueAddBuffer(buf, sound_device_frequency); - } - else - { - // finished playing - SoundQueueReturnBuffer(buf); - - Stop(); - } - } - } - - private: - bool StreamIntoBuffer(SoundData *buf) - { - int16_t *data_buf; - - bool song_done = false; - - if (!sound_device_stereo) - data_buf = mono_buffer_; - else - data_buf = buf->data_left_; - - int played = fmm_sequencer_->PlayStream((uint8_t *)data_buf, kMusicBuffer); - - if (fmm_sequencer_->PositionAtEnd()) - song_done = true; - - buf->length_ = played / 4; - - if (!sound_device_stereo) - ConvertToMono(buf->data_left_, mono_buffer_, buf->length_); - - if (song_done) /* EOF */ - { - if (!looping_) - return false; - fmm_sequencer_->Rewind(); - return true; - } - - return true; - } -}; - -AbstractMusicPlayer *PlayFMMMusic(uint8_t *data, int length, bool loop) -{ - FMMPlayer *player = new FMMPlayer(data, length, loop); - - if (!player) - { - LogDebug("FMMIDI player: error initializing!\n"); - delete[] data; - return nullptr; - } - - player->fmm_note_factory_ = new midisynth::fm_note_factory; - if (!player->fmm_note_factory_) - { - LogDebug("FMMIDI player: error initializing!\n"); - delete[] data; - delete player; - return nullptr; - } - - player->fmm_synth_ = new midisynth::synthesizer(player->fmm_note_factory_); - if (!player->fmm_synth_) - { - LogDebug("FMMIDI player: error initializing!\n"); - delete[] data; - delete player; - return nullptr; - } - - if (!player->LoadTrack(data, length)) // Lobo: quietly log it instead of completely exiting EDGE - { - LogDebug("FMMIDI player: failed to load MIDI file!\n"); - delete[] data; - delete player; - return nullptr; - } - - delete[] data; - - player->Play(loop); - - return player; -} - -//--- editor settings --- -// vi:ts=4:sw=4:noexpandtab diff --git a/source_files/edge/s_fmm.h b/source_files/edge/s_fmm.h deleted file mode 100644 index c37b81187..000000000 --- a/source_files/edge/s_fmm.h +++ /dev/null @@ -1,28 +0,0 @@ -//---------------------------------------------------------------------------- -// EDGE FMMIDI Music Player -//---------------------------------------------------------------------------- -// -// Copyright (c) 2023-2024 The EDGE Team. -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 3 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -//---------------------------------------------------------------------------- - -#pragma once - -#include "s_music.h" - -void RestartFMM(void); - -AbstractMusicPlayer *PlayFMMMusic(uint8_t *data, int length, bool loop); - -//--- editor settings --- -// vi:ts=4:sw=4:noexpandtab diff --git a/source_files/edge/s_m4p.cc b/source_files/edge/s_m4p.cc index c9511674b..e366ec186 100644 --- a/source_files/edge/s_m4p.cc +++ b/source_files/edge/s_m4p.cc @@ -49,7 +49,7 @@ class M4PPlayer : public AbstractMusicPlayer int status_; bool looping_; - int16_t *mono_buffer_; + float *mono_buffer_; public: bool OpenMemory(uint8_t *data, int length); @@ -74,7 +74,7 @@ class M4PPlayer : public AbstractMusicPlayer M4PPlayer::M4PPlayer() : status_(kNotLoaded) { - mono_buffer_ = new int16_t[kMusicBuffer * 2]; + mono_buffer_ = new float[kMusicBuffer * 2]; } M4PPlayer::~M4PPlayer() @@ -91,34 +91,34 @@ void M4PPlayer::PostOpen() status_ = kStopped; } -static void ConvertToMono(int16_t *dest, const int16_t *src, int len) +static void ConvertToMono(float *dest, const float *src, int len) { - const int16_t *s_end = src + len * 2; + const float *s_end = src + len * 2; for (; src < s_end; src += 2) { // compute average of samples - *dest++ = ((int)src[0] + (int)src[1]) >> 1; + *dest++ = (src[0] + src[1]) * 0.5f; } } bool M4PPlayer::StreamIntoBuffer(SoundData *buf) { - int16_t *data_buf; + float *data_buf; bool song_done = false; if (!sound_device_stereo) data_buf = mono_buffer_; else - data_buf = buf->data_left_; + data_buf = buf->data_; - m4p_GenerateSamples(data_buf, kMusicBuffer / sizeof(int16_t)); + m4p_GenerateFloatSamples(data_buf, kMusicBuffer / sizeof(float)); - buf->length_ = kMusicBuffer / 2; + buf->length_ = kMusicBuffer / 4; if (!sound_device_stereo) - ConvertToMono(buf->data_left_, mono_buffer_, buf->length_); + ConvertToMono(buf->data_, mono_buffer_, buf->length_); if (song_done) /* EOF */ { diff --git a/source_files/edge/s_mp3.cc b/source_files/edge/s_mp3.cc index 44eeb5051..2f7898eb8 100644 --- a/source_files/edge/s_mp3.cc +++ b/source_files/edge/s_mp3.cc @@ -55,7 +55,7 @@ class MP3Player : public AbstractMusicPlayer uint8_t *mp3_data_ = nullptr; drmp3 *mp3_decoder_ = nullptr; - int16_t *mono_buffer_; + float *mono_buffer_; public: bool OpenMemory(uint8_t *data, int length); @@ -80,7 +80,7 @@ class MP3Player : public AbstractMusicPlayer MP3Player::MP3Player() : status_(kNotLoaded) { - mono_buffer_ = new int16_t[kMusicBuffer * 2]; + mono_buffer_ = new float[kMusicBuffer * 2]; } MP3Player::~MP3Player() @@ -106,27 +106,27 @@ void MP3Player::PostOpen() status_ = kStopped; } -static void ConvertToMono(int16_t *dest, const int16_t *src, int len) +static void ConvertToMono(float *dest, const float *src, int len) { - const int16_t *s_end = src + len * 2; + const float *s_end = src + len * 2; for (; src < s_end; src += 2) { // compute average of samples - *dest++ = ((int)src[0] + (int)src[1]) >> 1; + *dest++ = (src[0] + src[1]) * 0.5f; } } bool MP3Player::StreamIntoBuffer(SoundData *buf) { - int16_t *data_buf; + float *data_buf; if (is_stereo_ && !sound_device_stereo) data_buf = mono_buffer_; else - data_buf = buf->data_left_; + data_buf = buf->data_; - int got_size = drmp3_read_pcm_frames_s16(mp3_decoder_, kMusicBuffer, data_buf); + int got_size = drmp3_read_pcm_frames_f32(mp3_decoder_, kMusicBuffer, data_buf); if (got_size == 0) /* EOF */ { @@ -145,7 +145,7 @@ bool MP3Player::StreamIntoBuffer(SoundData *buf) buf->length_ = got_size; if (is_stereo_ && !sound_device_stereo) - ConvertToMono(buf->data_left_, mono_buffer_, got_size); + ConvertToMono(buf->data_, mono_buffer_, got_size); return (true); } @@ -312,17 +312,19 @@ bool LoadMP3Sound(SoundData *buf, const uint8_t *data, int length) LogDebug("MP3 SFX Loader: freq %d Hz, %d channels\n", mp3.sampleRate, mp3.channels); - bool is_stereo_ = (mp3.channels > 1); + bool is_stereo = (mp3.channels > 1); buf->frequency_ = mp3.sampleRate; + buf->mode_ = sound_device_stereo ? kMixInterleaved : kMixMono; + SoundGatherer gather; - int16_t *buffer = gather.MakeChunk(framecount, is_stereo_); + float *buffer = gather.MakeChunk(framecount, is_stereo); - gather.CommitChunk(drmp3_read_pcm_frames_s16(&mp3, framecount, buffer)); + gather.CommitChunk(drmp3_read_pcm_frames_f32(&mp3, framecount, buffer)); - if (!gather.Finalise(buf, is_stereo_)) + if (!gather.Finalise(buf, sound_device_stereo)) LogWarning("MP3 SFX Loader: no samples!\n"); drmp3_uninit(&mp3); diff --git a/source_files/edge/s_music.cc b/source_files/edge/s_music.cc index a2c6e4cfe..79ca894d2 100644 --- a/source_files/edge/s_music.cc +++ b/source_files/edge/s_music.cc @@ -32,7 +32,6 @@ #include "m_misc.h" #include "s_flac.h" #include "s_fluid.h" -#include "s_fmm.h" #include "s_m4p.h" #include "s_mp3.h" #include "s_ogg.h" @@ -216,13 +215,9 @@ void ChangeMusic(int entry_number, bool loop) { music_player = PlayFluidMusic(data, length, loop); } - else if (var_midi_player == 1) - { - music_player = PlayOPLMusic(data, length, loop, play->type_); - } else { - music_player = PlayFMMMusic(data, length, loop); + music_player = PlayOPLMusic(data, length, loop, play->type_); } break; diff --git a/source_files/edge/s_ogg.cc b/source_files/edge/s_ogg.cc index fe14d56a5..ea22ad063 100644 --- a/source_files/edge/s_ogg.cc +++ b/source_files/edge/s_ogg.cc @@ -15,36 +15,25 @@ // GNU General Public License for more details. // //---------------------------------------------------------------------------- -// -// -ACB- 2004/08/18 Written: -// -// Based on a tutorial at DevMaster.net: -// http://www.devmaster.net/articles/openal-tutorials/lesson8.php -// #include "s_ogg.h" -#include "ddf_playlist.h" #include "epi.h" #include "epi_endian.h" -#include "epi_file.h" #include "epi_filesystem.h" -#include "minivorbis.h" #include "s_blit.h" #include "s_cache.h" #include "s_music.h" #include "snd_gather.h" -#include "w_wad.h" +// clang-format off +#define STB_VORBIS_NO_INTEGER_CONVERSION +#define STB_VORBIS_NO_PUSHDATA_API +#define STB_VORBIS_NO_STDIO +#include "stb_vorbis.h" +// clang-format on extern bool sound_device_stereo; // FIXME: encapsulation -struct OGGDataLump -{ - const uint8_t *data; - size_t position; - size_t size; -}; - class OGGPlayer : public AbstractMusicPlayer { public: @@ -65,11 +54,7 @@ class OGGPlayer : public AbstractMusicPlayer bool looping_; bool is_stereo_; - OGGDataLump *ogg_lump_ = nullptr; - OggVorbis_File ogg_stream_; - vorbis_info *vorbis_info_ = nullptr; - - int16_t *mono_buffer_; + stb_vorbis *ogg_decoder_; public: bool OpenMemory(uint8_t *data, int length); @@ -85,206 +70,49 @@ class OGGPlayer : public AbstractMusicPlayer virtual void Ticker(void); private: - const char *GetError(int code); - void PostOpen(void); bool StreamIntoBuffer(SoundData *buf); }; -//---------------------------------------------------------------------------- -// -// oggplayer memory operations -// - -size_t oggplayer_memread(void *ptr, size_t size, size_t nmemb, void *datasource) -{ - OGGDataLump *d = (OGGDataLump *)datasource; - size_t rb = size * nmemb; - - if (d->position >= d->size) - return 0; - - if (d->position + rb > d->size) - rb = d->size - d->position; - - memcpy(ptr, d->data + d->position, rb); - d->position += rb; - - return rb / size; -} - -int oggplayer_memseek(void *datasource, ogg_int64_t offset, int whence) -{ - OGGDataLump *d = (OGGDataLump *)datasource; - size_t newpos; - - switch (whence) - { - case SEEK_SET: { - newpos = (int)offset; - break; - } - case SEEK_CUR: { - newpos = d->position + (int)offset; - break; - } - case SEEK_END: { - newpos = d->size + (int)offset; - break; - } - default: { - return -1; - } // WTF? - } - - if (newpos > d->size) - return -1; - - d->position = newpos; - return 0; -} - -int oggplayer_memclose(void *datasource) -{ - // we don't free the data here - - return 0; -} - -long oggplayer_memtell(void *datasource) -{ - OGGDataLump *d = (OGGDataLump *)datasource; - - if (d->position > d->size) - return -1; - - return d->position; -} - //---------------------------------------------------------------------------- -OGGPlayer::OGGPlayer() : status_(kNotLoaded), vorbis_info_(nullptr) +OGGPlayer::OGGPlayer() : status_(kNotLoaded), ogg_decoder_(nullptr) { - mono_buffer_ = new int16_t[kMusicBuffer * 2]; } OGGPlayer::~OGGPlayer() { Close(); - - if (mono_buffer_) - delete[] mono_buffer_; -} - -const char *OGGPlayer::GetError(int code) -{ - switch (code) - { - case OV_EREAD: - return ("Read from media error."); - - case OV_ENOTVORBIS: - return ("Not Vorbis data."); - - case OV_EVERSION: - return ("Vorbis version mismatch."); - - case OV_EBADHEADER: - return ("Invalid Vorbis header."); - - case OV_EFAULT: - return ("Internal error."); - - default: - break; - } - - return ("Unknown OGG error."); } void OGGPlayer::PostOpen() { - vorbis_info_ = ov_info(&ogg_stream_, -1); - EPI_ASSERT(vorbis_info_); - - if (vorbis_info_->channels == 1) - { - is_stereo_ = false; - } - else - { - is_stereo_ = true; - } - // Loaded, but not playing status_ = kStopped; } -static void ConvertToMono(int16_t *dest, const int16_t *src, int len) +bool OGGPlayer::StreamIntoBuffer(SoundData *buf) { - const int16_t *s_end = src + len * 2; + int got_size = stb_vorbis_get_samples_float_interleaved(ogg_decoder_, sound_device_stereo ? 2 : 1, buf->data_, kMusicBuffer); - for (; src < s_end; src += 2) + if (got_size == 0) /* EOF */ { - // compute average of samples - *dest++ = ((int)src[0] + (int)src[1]) >> 1; + if (!looping_) + return false; + stb_vorbis_seek_start(ogg_decoder_); + return true; } -} - -bool OGGPlayer::StreamIntoBuffer(SoundData *buf) -{ - int ogg_endian = (kByteOrder == kLittleEndian) ? 0 : 1; - int samples = 0; - - while (samples < kMusicBuffer) + if (got_size < 0) /* ERROR */ { - int16_t *data_buf; - - if (is_stereo_ && !sound_device_stereo) - data_buf = mono_buffer_; - else - data_buf = buf->data_left_ + samples * (is_stereo_ ? 2 : 1); - - int section; - int got_size = - ov_read(&ogg_stream_, (char *)data_buf, (kMusicBuffer - samples) * (is_stereo_ ? 2 : 1) * sizeof(int16_t), - ogg_endian, sizeof(int16_t), 1 /* signed data */, §ion); - - if (got_size == OV_HOLE) // ignore corruption - continue; - - if (got_size == 0) /* EOF */ - { - if (!looping_) - break; - - ov_raw_seek(&ogg_stream_, 0); - continue; // try again - } - - if (got_size < 0) /* ERROR */ - { - // Construct an error message - std::string err_msg("[oggplayer_c::StreamIntoBuffer] Failed: "); - - err_msg += GetError(got_size); - - // FIXME: using FatalError is too harsh - FatalError("%s", err_msg.c_str()); - return false; /* NOT REACHED */ - } - - got_size /= (is_stereo_ ? 2 : 1) * sizeof(int16_t); - - if (is_stereo_ && !sound_device_stereo) - ConvertToMono(buf->data_left_ + samples, mono_buffer_, got_size); - - samples += got_size; + LogDebug("[oggplayer_c::StreamIntoBuffer] Failed\n"); + return false; } - return (samples > 0); + buf->length_ = got_size; + + return true; } bool OGGPlayer::OpenMemory(uint8_t *data, int length) @@ -292,30 +120,25 @@ bool OGGPlayer::OpenMemory(uint8_t *data, int length) if (status_ != kNotLoaded) Close(); - ogg_lump_ = new OGGDataLump; - - ogg_lump_->data = data; - ogg_lump_->size = length; - ogg_lump_->position = 0; + int ogg_error = 0; + ogg_decoder_ = stb_vorbis_open_memory(data, length, &ogg_error, nullptr); - ov_callbacks CB; - - CB.read_func = oggplayer_memread; - CB.seek_func = oggplayer_memseek; - CB.close_func = oggplayer_memclose; - CB.tell_func = oggplayer_memtell; - - int result = ov_open_callbacks((void *)ogg_lump_, &ogg_stream_, nullptr, 0, CB); - - if (result < 0) + if (ogg_error || !ogg_decoder_) { - std::string err_msg("[oggplayer_c::OpenMemory] Failed: "); + LogWarning("OGGPlayer: unable to load file\n"); + if (ogg_decoder_) + { + stb_vorbis_close(ogg_decoder_); + ogg_decoder_ = nullptr; + } + return false; + } - err_msg += GetError(result); - LogWarning("%s\n", err_msg.c_str()); - ov_clear(&ogg_stream_); - ogg_lump_->data = nullptr; // this is deleted after the function returns false - delete ogg_lump_; + if (ogg_decoder_->channels > 2 || ogg_decoder_->channels < 1) + { + LogWarning("OGGPlayer: unsupported number of channels: %d\n", ogg_decoder_->channels); + stb_vorbis_close(ogg_decoder_); + ogg_decoder_ = nullptr; return false; } @@ -331,11 +154,11 @@ void OGGPlayer::Close() // Stop playback Stop(); - ov_clear(&ogg_stream_); - - delete[] ogg_lump_->data; - delete ogg_lump_; - ogg_lump_ = nullptr; + if (ogg_decoder_) + { + stb_vorbis_close(ogg_decoder_); + ogg_decoder_ = nullptr; + } // Reset player gain music_player_gain = 1.0f; @@ -386,17 +209,17 @@ void OGGPlayer::Stop() void OGGPlayer::Ticker() { - while (status_ == kPlaying && !pc_speaker_mode) + while (status_ == kPlaying) { SoundData *buf = - SoundQueueGetFreeBuffer(kMusicBuffer, (is_stereo_ && sound_device_stereo) ? kMixInterleaved : kMixMono); + SoundQueueGetFreeBuffer(kMusicBuffer, sound_device_stereo ? kMixInterleaved : kMixMono); if (!buf) break; if (StreamIntoBuffer(buf)) { - SoundQueueAddBuffer(buf, vorbis_info_->rate); + SoundQueueAddBuffer(buf, ogg_decoder_->sample_rate); } else { @@ -427,90 +250,42 @@ AbstractMusicPlayer *PlayOGGMusic(uint8_t *data, int length, bool looping) bool LoadOGGSound(SoundData *buf, const uint8_t *data, int length) { - OGGDataLump ogg_lump; - - ogg_lump.data = data; - ogg_lump.size = length; - ogg_lump.position = 0; - - ov_callbacks CB; - - CB.read_func = oggplayer_memread; - CB.seek_func = oggplayer_memseek; - CB.close_func = oggplayer_memclose; - CB.tell_func = oggplayer_memtell; - - OggVorbis_File ogg_stream; + int ogg_error = 0; + stb_vorbis *ogg = stb_vorbis_open_memory(data, length, &ogg_error, nullptr); - int result = ov_open_callbacks((void *)&ogg_lump, &ogg_stream, nullptr, 0, CB); - - if (result < 0) + if (ogg_error || !ogg) { - LogWarning("Failed to load OGG sound (corrupt ogg?) error=%d\n", result); - + LogWarning("OGG SFX Loader: unable to load file\n"); + if (ogg) + stb_vorbis_close(ogg); return false; } - vorbis_info *vorbis_inf = ov_info(&ogg_stream, -1); - EPI_ASSERT(vorbis_inf); - - LogDebug("OGG SFX Loader: freq %d Hz, %d channels\n", (int)vorbis_inf->rate, (int)vorbis_inf->channels); - - if (vorbis_inf->channels > 2) + if (ogg->channels > 2 || ogg->channels < 1) { - LogWarning("OGG Sfx Loader: too many channels: %d\n", vorbis_inf->channels); - - ogg_lump.size = 0; - ov_clear(&ogg_stream); - + LogWarning("OGG SFX Loader: unsupported number of channels: %d\n", ogg->channels); + stb_vorbis_close(ogg); return false; } - bool is_stereo = (vorbis_inf->channels > 1); - int ogg_endian = (kByteOrder == kLittleEndian) ? 0 : 1; - - buf->frequency_ = vorbis_inf->rate; + LogDebug("OGG SFX Loader: freq %d Hz, %d channels\n", ogg->sample_rate, ogg->channels); - SoundGatherer gather; - - for (;;) - { - int want = 2048; - - int16_t *buffer = gather.MakeChunk(want, is_stereo); + buf->frequency_ = ogg->sample_rate; - int section; - int got_size = ov_read(&ogg_stream, (char *)buffer, want * (is_stereo ? 2 : 1) * sizeof(int16_t), ogg_endian, - sizeof(int16_t), 1 /* signed data */, §ion); + buf->mode_ = sound_device_stereo ? kMixInterleaved : kMixMono; - if (got_size == OV_HOLE) // ignore corruption - { - gather.DiscardChunk(); - continue; - } + uint32_t total_samples = stb_vorbis_stream_length_in_samples(ogg); - if (got_size == 0) /* EOF */ - { - gather.DiscardChunk(); - break; - } - else if (got_size < 0) /* ERROR */ - { - gather.DiscardChunk(); - - LogWarning("Problem occurred while loading OGG (%d)\n", got_size); - break; - } + SoundGatherer gather; - got_size /= (is_stereo ? 2 : 1) * sizeof(int16_t); + float *buffer = gather.MakeChunk(total_samples, sound_device_stereo); - gather.CommitChunk(got_size); - } + gather.CommitChunk(stb_vorbis_get_samples_float_interleaved(ogg, sound_device_stereo ? 2 : 1, buffer, total_samples)); - if (!gather.Finalise(buf, is_stereo)) + if (!gather.Finalise(buf, sound_device_stereo)) FatalError("OGG SFX Loader: no samples!\n"); - ov_clear(&ogg_stream); + stb_vorbis_close(ogg); return true; } diff --git a/source_files/edge/s_opl.cc b/source_files/edge/s_opl.cc index 1f3fad2be..339fb73c1 100644 --- a/source_files/edge/s_opl.cc +++ b/source_files/edge/s_opl.cc @@ -90,14 +90,14 @@ void RestartOpal(void) return; // OK! } -static void ConvertToMono(int16_t *dest, const int16_t *src, int len) +static void ConvertToMono(float *dest, const float *src, int len) { - const int16_t *s_end = src + len * 2; + const float *s_end = src + len * 2; for (; src < s_end; src += 2) { // compute average of samples - *dest++ = ((int)src[0] + (int)src[1]) >> 1; + *dest++ = (src[0] + src[1]) * 0.5f; } } @@ -115,14 +115,14 @@ class OpalPlayer : public AbstractMusicPlayer int status_; bool looping_; - int16_t *mono_buffer_; + float *mono_buffer_; OPLInterface *opl_interface_; public: OpalPlayer(bool looping) : status_(kNotLoaded), looping_(looping) { - mono_buffer_ = new int16_t[2 * kMusicBuffer]; + mono_buffer_ = new float[2 * kMusicBuffer]; SequencerInit(); } @@ -205,7 +205,7 @@ class OpalPlayer : public AbstractMusicPlayer static void playSynth(void *userdata, uint8_t *stream, size_t length) { (void)userdata; - edge_opl->generate((int16_t *)(stream), length / (2 * sizeof(int16_t))); + edge_opl->generate((float *)(stream), length / (2 * sizeof(float))); } void SequencerInit() @@ -228,7 +228,7 @@ class OpalPlayer : public AbstractMusicPlayer opl_interface_->onPcmRender_userdata = this; opl_interface_->pcmSampleRate = sound_device_frequency; - opl_interface_->pcmFrameSize = 2 /*channels*/ * 2 /*size of one sample*/; // OPL3 is 2 'channels' regardless of + opl_interface_->pcmFrameSize = 2 /*channels*/ * 4 /*size of one sample*/; // OPL3 is 2 'channels' regardless of // the sound_device_stereo setting opl_interface_->rt_deviceSwitch = rtDeviceSwitch; @@ -336,24 +336,24 @@ class OpalPlayer : public AbstractMusicPlayer private: bool StreamIntoBuffer(SoundData *buf) { - int16_t *data_buf; + float *data_buf; bool song_done = false; if (!sound_device_stereo) data_buf = mono_buffer_; else - data_buf = buf->data_left_; + data_buf = buf->data_; int played = opl_sequencer_->PlayStream((uint8_t *)(data_buf), kMusicBuffer); if (opl_sequencer_->PositionAtEnd()) song_done = true; - buf->length_ = played / (2 * sizeof(int16_t)); + buf->length_ = played / 8; if (!sound_device_stereo) - ConvertToMono(buf->data_left_, mono_buffer_, buf->length_); + ConvertToMono(buf->data_, mono_buffer_, buf->length_); if (song_done) /* EOF */ { diff --git a/source_files/edge/s_rad.cc b/source_files/edge/s_rad.cc index 7e3e2fb2d..f6724a2b8 100644 --- a/source_files/edge/s_rad.cc +++ b/source_files/edge/s_rad.cc @@ -54,7 +54,7 @@ class RadPlayer : public AbstractMusicPlayer int status_; bool looping_; - int16_t *mono_buffer_; + float *mono_buffer_; int sample_count_; int sample_update_; @@ -84,7 +84,7 @@ class RadPlayer : public AbstractMusicPlayer RadPlayer::RadPlayer() : status_(kNotLoaded), tune_(nullptr) { - mono_buffer_ = new int16_t[kMusicBuffer * 2]; + mono_buffer_ = new float[kMusicBuffer * 2]; } RadPlayer::~RadPlayer() @@ -103,20 +103,20 @@ void RadPlayer::PostOpen() status_ = kStopped; } -static void ConvertToMono(int16_t *dest, const int16_t *src, int len) +static void ConvertToMono(float *dest, const float *src, int len) { - const int16_t *s_end = src + len * 2; + const float *s_end = src + len * 2; for (; src < s_end; src += 2) { // compute average of samples - *dest++ = ((int)src[0] + (int)src[1]) >> 1; + *dest++ = (src[0] + src[1]) * 0.5f; } } bool RadPlayer::StreamIntoBuffer(SoundData *buf) { - int16_t *data_buf; + float *data_buf; bool song_done = false; int samples = 0; @@ -124,7 +124,7 @@ bool RadPlayer::StreamIntoBuffer(SoundData *buf) if (!sound_device_stereo) data_buf = mono_buffer_; else - data_buf = buf->data_left_; + data_buf = buf->data_; for (int i = 0; i < kMusicBuffer; i += 2) { @@ -141,7 +141,7 @@ bool RadPlayer::StreamIntoBuffer(SoundData *buf) buf->length_ = samples; if (!sound_device_stereo) - ConvertToMono(buf->data_left_, mono_buffer_, buf->length_); + ConvertToMono(buf->data_, mono_buffer_, buf->length_); if (song_done) /* EOF */ { diff --git a/source_files/edge/s_sid.cc b/source_files/edge/s_sid.cc index 23eff4454..99c1adef8 100644 --- a/source_files/edge/s_sid.cc +++ b/source_files/edge/s_sid.cc @@ -52,7 +52,7 @@ class SIDPlayer : public AbstractMusicPlayer int status_; bool looping_; - int16_t *mono_buffer_; + float *mono_buffer_; cRSID_C64instance *C64_ = nullptr; cRSID_SIDheader *C64_song_ = nullptr; @@ -80,7 +80,7 @@ class SIDPlayer : public AbstractMusicPlayer SIDPlayer::SIDPlayer() : status_(kNotLoaded) { - mono_buffer_ = new int16_t[kMusicBuffer * 2]; + mono_buffer_ = new float[kMusicBuffer * 2]; } SIDPlayer::~SIDPlayer() @@ -99,32 +99,32 @@ void SIDPlayer::PostOpenInit() status_ = kStopped; } -static void ConvertToMono(int16_t *dest, const int16_t *src, int len) +static void ConvertToMono(float *dest, const float *src, int len) { - const int16_t *s_end = src + len * 2; + const float *s_end = src + len * 2; for (; src < s_end; src += 2) { // compute average of samples - *dest++ = ((int)src[0] + (int)src[1]) >> 1; + *dest++ = (src[0] + src[1]) * 0.5f; } } bool SIDPlayer::StreamIntoBuffer(SoundData *buf) { - int16_t *data_buf; + float *data_buf; if (!sound_device_stereo) data_buf = mono_buffer_; else - data_buf = buf->data_left_; + data_buf = buf->data_; - cRSID_generateSound(C64_, (unsigned char *)data_buf, kMusicBuffer); + cRSID_generateFloat(C64_, data_buf, kMusicBuffer); - buf->length_ = kMusicBuffer / sizeof(int16_t) / 2; + buf->length_ = kMusicBuffer / sizeof(float) / 2; if (!sound_device_stereo) - ConvertToMono(buf->data_left_, mono_buffer_, buf->length_); + ConvertToMono(buf->data_, mono_buffer_, buf->length_); return true; } diff --git a/source_files/edge/s_sound.cc b/source_files/edge/s_sound.cc index d3326206e..1371f200c 100644 --- a/source_files/edge/s_sound.cc +++ b/source_files/edge/s_sound.cc @@ -332,7 +332,7 @@ static void S_PlaySound(int idx, SoundEffectDefinition *def, int category, Posit chan->volume_right_ = 0; chan->offset_ = 0; - chan->length_ = chan->data_->length_ << 10; + chan->length_ = chan->data_->length_; chan->loop_ = false; chan->boss_ = (flags & kSoundEffectBoss) ? true : false; @@ -452,19 +452,6 @@ void StartSoundEffect(SoundEffect *sfx, int category, Position *pos, int flags) if (!buf) return; - if (vacuum_sound_effects) - buf->MixVacuum(); - else if (submerged_sound_effects) - buf->MixSubmerged(); - else - { - if (ddf_reverb) - buf->MixReverb(dynamic_reverb, room_area, outdoor_reverb, ddf_reverb_type, ddf_reverb_ratio, - ddf_reverb_delay); - else - buf->MixReverb(dynamic_reverb, room_area, outdoor_reverb, 0, 0, 0); - } - LockAudio(); { DoStartFX(def, category, pos, flags, buf); diff --git a/source_files/edge/s_wav.cc b/source_files/edge/s_wav.cc index 52db4d94f..020f50d08 100644 --- a/source_files/edge/s_wav.cc +++ b/source_files/edge/s_wav.cc @@ -232,13 +232,15 @@ bool LoadWAVSound(SoundData *buf, uint8_t *data, int length, bool pc_speaker) buf->frequency_ = wav.sampleRate; + buf->mode_ = sound_device_stereo ? kMixInterleaved : kMixMono; + SoundGatherer gather; - int16_t *buffer = gather.MakeChunk(wav.totalPCMFrameCount, is_stereo); + float *buffer = gather.MakeChunk(wav.totalPCMFrameCount, is_stereo); - gather.CommitChunk(drwav_read_pcm_frames_s16(&wav, wav.totalPCMFrameCount, buffer)); + gather.CommitChunk(drwav_read_pcm_frames_f32(&wav, wav.totalPCMFrameCount, buffer)); - if (!gather.Finalise(buf, is_stereo)) + if (!gather.Finalise(buf, sound_device_stereo)) LogWarning("WAV SFX Loader: no samples!\n"); drwav_uninit(&wav); diff --git a/source_files/edge/snd_data.cc b/source_files/edge/snd_data.cc index 9c1fd7296..2701bc79d 100644 --- a/source_files/edge/snd_data.cc +++ b/source_files/edge/snd_data.cc @@ -18,16 +18,9 @@ #include "snd_data.h" -#include - -#include "HandmadeMath.h" -#include "epi.h" - SoundData::SoundData() - : length_(0), frequency_(0), mode_(0), data_left_(nullptr), data_right_(nullptr), filter_data_left_(nullptr), - filter_data_right_(nullptr), definition_data_(nullptr), is_sound_effect_(false), current_filter_(kFilterNone), - reverbed_room_size_(kRoomReverbNone), current_ddf_reverb_ratio_(0), current_ddf_reverb_delay_(0), - current_ddf_reverb_type_(0), reverb_is_outdoors_(false) + : length_(0), frequency_(0), mode_(0), data_(nullptr), + definition_data_(nullptr), is_sound_effect_(false) { } @@ -40,40 +33,22 @@ void SoundData::Free() { length_ = 0; - if (data_right_ && data_right_ != data_left_) - delete[] data_right_; - - if (data_left_) - delete[] data_left_; + if (data_) + delete[] data_; - data_left_ = nullptr; - data_right_ = nullptr; - - FreeFilter(); -} - -void SoundData::FreeFilter() -{ - if (filter_data_right_ && filter_data_right_ != filter_data_left_) - delete[] filter_data_right_; - - if (filter_data_left_) - delete[] filter_data_left_; - - filter_data_left_ = nullptr; - filter_data_right_ = nullptr; + data_ = nullptr; } void SoundData::Allocate(int samples, int buf_mode) { // early out when requirements are already met - if (data_left_ && length_ >= samples && mode_ == buf_mode) + if (data_ && length_ >= samples && mode_ == buf_mode) { length_ = samples; // FIXME: perhaps keep allocated count return; } - if (data_left_ || data_right_) + if (data_) { Free(); } @@ -81,429 +56,7 @@ void SoundData::Allocate(int samples, int buf_mode) length_ = samples; mode_ = buf_mode; - switch (buf_mode) - { - case kMixMono: - data_left_ = new int16_t[samples]; - data_right_ = data_left_; - break; - - case kMixStereo: - data_left_ = new int16_t[samples]; - data_right_ = new int16_t[samples]; - break; - - case kMixInterleaved: - data_left_ = new int16_t[samples * 2]; - data_right_ = data_left_; - break; - - default: - break; - } -} - -void SoundData::MixSubmerged() -{ - if (current_filter_ != kFilterSubmerged) - { - // Setup lowpass + reverb parameters - int out_L = 0; - int accum_L = 0; - int out_R = 0; - int accum_R = 0; - int k = 4; - int *reverb_buffer_L; - int *reverb_buffer_R; - int write_pos = 0; - int read_pos = 0; - int reverb_ratio = 25; - int reverb_delay = 100; - - switch (mode_) - { - case kMixMono: - if (!filter_data_left_) - filter_data_left_ = new int16_t[length_]; - filter_data_right_ = filter_data_left_; - reverb_buffer_L = new int[length_]; - memset(reverb_buffer_L, 0, length_ * sizeof(int)); - read_pos = ((write_pos - reverb_delay * frequency_ / 1000) + length_) % (length_); - for (int i = 0; i < length_; i++) - { - filter_data_left_[i] = out_L = accum_L >> k; - accum_L = accum_L - out_L + data_left_[i]; - int reverbed = filter_data_left_[i] + reverb_buffer_L[HMM_MAX(0, read_pos)] * reverb_ratio / 100; - filter_data_left_[i] = HMM_Clamp(INT16_MIN, reverbed, INT16_MAX); - reverb_buffer_L[write_pos] = filter_data_left_[i]; - write_pos = (write_pos + 1) % (length_); - read_pos = (read_pos + 1) % (length_); - } - current_filter_ = kFilterSubmerged; - delete[] reverb_buffer_L; - reverb_buffer_L = nullptr; - break; - - case kMixStereo: - if (!filter_data_left_) - filter_data_left_ = new int16_t[length_]; - if (!filter_data_right_) - filter_data_right_ = new int16_t[length_]; - reverb_buffer_L = new int[length_]; - reverb_buffer_R = new int[length_]; - memset(reverb_buffer_L, 0, length_ * sizeof(int)); - memset(reverb_buffer_R, 0, length_ * sizeof(int)); - read_pos = ((write_pos - reverb_delay * frequency_ / 1000) + length_) % (length_); - for (int i = 0; i < length_; i++) - { - filter_data_left_[i] = out_L = accum_L >> k; - accum_L = accum_L - out_L + data_left_[i]; - filter_data_right_[i] = out_R = accum_R >> k; - accum_R = accum_R - out_R + data_right_[i]; - int reverbed_L = filter_data_left_[i] + reverb_buffer_L[HMM_MAX(0, read_pos)] * reverb_ratio / 100; - int reverbed_R = filter_data_right_[i] + reverb_buffer_R[HMM_MAX(0, read_pos)] * reverb_ratio / 100; - filter_data_left_[i] = HMM_Clamp(INT16_MIN, reverbed_L, INT16_MAX); - filter_data_right_[i] = HMM_Clamp(INT16_MIN, reverbed_R, INT16_MAX); - reverb_buffer_L[write_pos] = filter_data_left_[i]; - reverb_buffer_R[write_pos] = filter_data_right_[i]; - write_pos = (write_pos + 1) % (length_); - read_pos = (read_pos + 1) % (length_); - } - current_filter_ = kFilterSubmerged; - delete[] reverb_buffer_L; - delete[] reverb_buffer_R; - reverb_buffer_L = nullptr; - reverb_buffer_R = nullptr; - break; - - case kMixInterleaved: - if (!filter_data_left_) - filter_data_left_ = new int16_t[length_ * 2]; - filter_data_right_ = filter_data_left_; - reverb_buffer_L = new int[length_ * 2]; - memset(reverb_buffer_L, 0, length_ * sizeof(int) * 2); - read_pos = ((write_pos - reverb_delay * frequency_ / 1000) + length_) % (length_ * 2); - for (int i = 0; i < length_ * 2; i++) - { - filter_data_left_[i] = out_L = accum_L >> k; - accum_L = accum_L - out_L + data_left_[i]; - int reverbed = filter_data_left_[i] + reverb_buffer_L[HMM_MAX(0, read_pos)] * reverb_ratio / 100; - filter_data_left_[i] = HMM_Clamp(INT16_MIN, reverbed, INT16_MAX); - reverb_buffer_L[write_pos] = filter_data_left_[i]; - write_pos = (write_pos + 1) % (length_ * 2); - read_pos = (read_pos + 1) % (length_ * 2); - } - current_filter_ = kFilterSubmerged; - delete[] reverb_buffer_L; - reverb_buffer_L = nullptr; - break; - } - } -} - -void SoundData::MixVacuum() -{ - if (current_filter_ != kFilterVacuum) - { - // Setup lowpass parameters - int out_L = 0; - int accum_L = 0; - int out_R = 0; - int accum_R = 0; - int k = 5; - - switch (mode_) - { - case kMixMono: - if (!filter_data_left_) - filter_data_left_ = new int16_t[length_]; - filter_data_right_ = filter_data_left_; - for (int i = 0; i < length_; i++) - { - filter_data_left_[i] = out_L = accum_L >> k; - accum_L = accum_L - out_L + data_left_[i]; - } - current_filter_ = kFilterVacuum; - break; - - case kMixStereo: - if (!filter_data_left_) - filter_data_left_ = new int16_t[length_]; - if (!filter_data_right_) - filter_data_right_ = new int16_t[length_]; - for (int i = 0; i < length_; i++) - { - filter_data_left_[i] = out_L = accum_L >> k; - accum_L = accum_L - out_L + data_left_[i]; - filter_data_right_[i] = out_R = accum_R >> k; - accum_R = accum_R - out_R + data_right_[i]; - } - current_filter_ = kFilterVacuum; - break; - - case kMixInterleaved: - if (!filter_data_left_) - filter_data_left_ = new int16_t[length_ * 2]; - filter_data_right_ = filter_data_left_; - for (int i = 0; i < length_ * 2; i++) - { - filter_data_left_[i] = out_L = accum_L >> k; - accum_L = accum_L - out_L + data_left_[i]; - } - current_filter_ = kFilterVacuum; - break; - } - } -} - -void SoundData::MixReverb(bool dynamic_reverb, float room_area, bool outdoor_reverb, int ddf_reverb_type, - int ddf_reverb_ratio, int ddf_reverb_delay) -{ - if (ddf_reverb_ratio > 0 && ddf_reverb_delay > 0 && ddf_reverb_type > 0) - { - if (current_filter_ != kFilterReverb || ddf_reverb_ratio != current_ddf_reverb_ratio_ || - ddf_reverb_delay != current_ddf_reverb_delay_ || ddf_reverb_type != current_ddf_reverb_type_) - { - // Setup reverb parameters - int *reverb_buffer_L; - int *reverb_buffer_R; - int write_pos = 0; - int read_pos = 0; - int reverb_ratio = ddf_reverb_ratio; - int reverb_delay = ddf_reverb_delay; - switch (mode_) - { - case kMixMono: - if (!filter_data_left_) - filter_data_left_ = new int16_t[length_]; - filter_data_right_ = filter_data_left_; - reverb_buffer_L = new int[length_]; - memset(reverb_buffer_L, 0, length_ * sizeof(int)); - read_pos = ((write_pos - reverb_delay * frequency_ / 1000) + length_) % (length_); - for (int i = 0; i < length_; i++) - { - if (ddf_reverb_type == 2) - reverb_buffer_L[write_pos] = data_left_[i]; - int reverbed = data_left_[i] + reverb_buffer_L[HMM_MAX(0, read_pos)] * reverb_ratio / 100; - filter_data_left_[i] = HMM_Clamp(INT16_MIN, reverbed, INT16_MAX); - if (ddf_reverb_type == 1) - reverb_buffer_L[write_pos] = reverbed; - write_pos = (write_pos + 1) % (length_); - read_pos = (read_pos + 1) % (length_); - } - current_filter_ = kFilterReverb; - current_ddf_reverb_delay_ = ddf_reverb_delay; - current_ddf_reverb_ratio_ = ddf_reverb_ratio; - current_ddf_reverb_type_ = ddf_reverb_type; - reverbed_room_size_ = kRoomReverbNone; - delete[] reverb_buffer_L; - reverb_buffer_L = nullptr; - break; - - case kMixStereo: - if (!filter_data_left_) - filter_data_left_ = new int16_t[length_]; - if (!filter_data_right_) - filter_data_right_ = new int16_t[length_]; - reverb_buffer_L = new int[length_]; - reverb_buffer_R = new int[length_]; - memset(reverb_buffer_L, 0, length_ * sizeof(int)); - memset(reverb_buffer_R, 0, length_ * sizeof(int)); - read_pos = ((write_pos - reverb_delay * frequency_ / 1000) + length_) % (length_); - for (int i = 0; i < length_; i++) - { - if (ddf_reverb_type == 2) - { - reverb_buffer_L[write_pos] = data_left_[i]; - reverb_buffer_R[write_pos] = data_right_[i]; - } - int reverbed_L = data_left_[i] + reverb_buffer_L[HMM_MAX(0, read_pos)] * reverb_ratio / 100; - int reverbed_R = data_right_[i] + reverb_buffer_R[HMM_MAX(0, read_pos)] * reverb_ratio / 100; - filter_data_left_[i] = HMM_Clamp(INT16_MIN, reverbed_L, INT16_MAX); - filter_data_right_[i] = HMM_Clamp(INT16_MIN, reverbed_R, INT16_MAX); - if (ddf_reverb_type == 1) - { - reverb_buffer_L[write_pos] = reverbed_L; - reverb_buffer_R[write_pos] = reverbed_R; - } - write_pos = (write_pos + 1) % (length_); - read_pos = (read_pos + 1) % (length_); - } - current_filter_ = kFilterReverb; - current_ddf_reverb_delay_ = ddf_reverb_delay; - current_ddf_reverb_ratio_ = ddf_reverb_ratio; - current_ddf_reverb_type_ = ddf_reverb_type; - reverbed_room_size_ = kRoomReverbNone; - delete[] reverb_buffer_L; - delete[] reverb_buffer_R; - reverb_buffer_L = nullptr; - reverb_buffer_R = nullptr; - break; - - case kMixInterleaved: - if (!filter_data_left_) - filter_data_left_ = new int16_t[length_ * 2]; - filter_data_right_ = filter_data_left_; - reverb_buffer_L = new int[length_ * 2]; - memset(reverb_buffer_L, 0, length_ * sizeof(int) * 2); - read_pos = ((write_pos - reverb_delay * frequency_ / 1000) + length_ * 2) % (length_ * 2); - for (int i = 0; i < length_ * 2; i++) - { - if (ddf_reverb_type == 2) - reverb_buffer_L[write_pos] = data_left_[i]; - int reverbed = data_left_[i] + reverb_buffer_L[HMM_MAX(0, read_pos)] * reverb_ratio / 100; - filter_data_left_[i] = HMM_Clamp(INT16_MIN, reverbed, INT16_MAX); - if (ddf_reverb_type == 1) - reverb_buffer_L[write_pos] = reverbed; - write_pos = (write_pos + 1) % (length_ * 2); - read_pos = (read_pos + 1) % (length_ * 2); - } - current_filter_ = kFilterReverb; - current_ddf_reverb_delay_ = ddf_reverb_delay; - current_ddf_reverb_ratio_ = ddf_reverb_ratio; - current_ddf_reverb_type_ = ddf_reverb_type; - reverbed_room_size_ = kRoomReverbNone; - delete[] reverb_buffer_L; - reverb_buffer_L = nullptr; - break; - } - } - } - else if (dynamic_reverb) - { - ReverbRoomSize current_room_size; - if (room_area > 700) - { - current_room_size = kRoomReverbLarge; - } - else if (room_area > 350) - { - current_room_size = kRoomReverbMedium; - } - else - { - current_room_size = kRoomReverbSmall; - } - - if (current_filter_ != kFilterReverb || reverbed_room_size_ != current_room_size || - reverb_is_outdoors_ != outdoor_reverb) - { - // Setup reverb parameters - int *reverb_buffer_L; - int *reverb_buffer_R; - int write_pos = 0; - int read_pos = 0; - int reverb_ratio = outdoor_reverb ? 25 : 30; - int reverb_delay; - if (outdoor_reverb) - reverb_delay = 50 * current_room_size + 25; - else - reverb_delay = 20 * current_room_size + 10; - switch (mode_) - { - case kMixMono: - if (!filter_data_left_) - filter_data_left_ = new int16_t[length_]; - filter_data_right_ = filter_data_left_; - reverb_buffer_L = new int[length_]; - memset(reverb_buffer_L, 0, length_ * sizeof(int)); - read_pos = ((write_pos - reverb_delay * frequency_ / 1000) + length_) % (length_); - for (int i = 0; i < length_; i++) - { - if (outdoor_reverb) - reverb_buffer_L[write_pos] = data_left_[i]; - int reverbed = data_left_[i] + reverb_buffer_L[HMM_MAX(0, read_pos)] * reverb_ratio / 100; - filter_data_left_[i] = HMM_Clamp(INT16_MIN, reverbed, INT16_MAX); - if (!outdoor_reverb) - reverb_buffer_L[write_pos] = reverbed; - write_pos = (write_pos + 1) % (length_); - read_pos = (read_pos + 1) % (length_); - } - current_filter_ = kFilterReverb; - reverbed_room_size_ = current_room_size; - if (outdoor_reverb) - reverb_is_outdoors_ = true; - else - reverb_is_outdoors_ = false; - delete[] reverb_buffer_L; - reverb_buffer_L = nullptr; - break; - - case kMixStereo: - if (!filter_data_left_) - filter_data_left_ = new int16_t[length_]; - if (!filter_data_right_) - filter_data_right_ = new int16_t[length_]; - reverb_buffer_L = new int[length_]; - reverb_buffer_R = new int[length_]; - memset(reverb_buffer_L, 0, length_ * sizeof(int)); - memset(reverb_buffer_R, 0, length_ * sizeof(int)); - read_pos = ((write_pos - reverb_delay * frequency_ / 1000) + length_) % (length_); - for (int i = 0; i < length_; i++) - { - if (outdoor_reverb) - { - reverb_buffer_L[write_pos] = data_left_[i]; - reverb_buffer_R[write_pos] = data_right_[i]; - } - int reverbed_L = data_left_[i] + reverb_buffer_L[HMM_MAX(0, read_pos)] * reverb_ratio / 100; - int reverbed_R = data_right_[i] + reverb_buffer_R[HMM_MAX(0, read_pos)] * reverb_ratio / 100; - filter_data_left_[i] = HMM_Clamp(INT16_MIN, reverbed_L, INT16_MAX); - filter_data_right_[i] = HMM_Clamp(INT16_MIN, reverbed_R, INT16_MAX); - if (!outdoor_reverb) - { - reverb_buffer_L[write_pos] = reverbed_L; - reverb_buffer_R[write_pos] = reverbed_R; - } - write_pos = (write_pos + 1) % (length_); - read_pos = (read_pos + 1) % (length_); - } - current_filter_ = kFilterReverb; - reverbed_room_size_ = current_room_size; - if (outdoor_reverb) - reverb_is_outdoors_ = true; - else - reverb_is_outdoors_ = false; - delete[] reverb_buffer_L; - delete[] reverb_buffer_R; - reverb_buffer_L = nullptr; - reverb_buffer_R = nullptr; - break; - - case kMixInterleaved: - if (!filter_data_left_) - filter_data_left_ = new int16_t[length_ * 2]; - filter_data_right_ = filter_data_left_; - reverb_buffer_L = new int[length_ * 2]; - memset(reverb_buffer_L, 0, length_ * sizeof(int) * 2); - read_pos = ((write_pos - reverb_delay * frequency_ / 1000) + length_ * 2) % (length_ * 2); - for (int i = 0; i < length_ * 2; i++) - { - if (outdoor_reverb) - reverb_buffer_L[write_pos] = data_left_[i]; - int reverbed = data_left_[i] + reverb_buffer_L[HMM_MAX(0, read_pos)] * reverb_ratio / 100; - filter_data_left_[i] = HMM_Clamp(INT16_MIN, reverbed, INT16_MAX); - if (!outdoor_reverb) - reverb_buffer_L[write_pos] = reverbed; - write_pos = (write_pos + 1) % (length_ * 2); - read_pos = (read_pos + 1) % (length_ * 2); - } - current_filter_ = kFilterReverb; - reverbed_room_size_ = current_room_size; - if (outdoor_reverb) - reverb_is_outdoors_ = true; - else - reverb_is_outdoors_ = false; - delete[] reverb_buffer_L; - reverb_buffer_L = nullptr; - break; - } - } - } - else // Just use the original buffer - Dasho - { - current_filter_ = kFilterNone; - } + data_ = new float[samples * (buf_mode == kMixMono ? 1 : 2)]; } //--- editor settings --- diff --git a/source_files/edge/snd_data.h b/source_files/edge/snd_data.h index cf328eda5..ef9ca014b 100644 --- a/source_files/edge/snd_data.h +++ b/source_files/edge/snd_data.h @@ -23,8 +23,7 @@ enum SoundBufferMix { kMixMono = 0, - kMixStereo = 1, - kMixInterleaved = 2 + kMixInterleaved = 1 }; enum SoundFilter @@ -50,43 +49,20 @@ class SoundData int frequency_; // frequency int mode_; // one of the kMixxxx values - // signed 16-bit samples. - // For kMixMono, both pointers refer to the same memory. - // For kMixInterleaved, only data_left_ is used and contains - // both channels, left samples before right samples. - int16_t *data_left_; - int16_t *data_right_; - - // Temp buffer for mixed SFX. Will be overwritten as needed. - int16_t *filter_data_left_; - int16_t *filter_data_right_; + // 32-bit floating point samples. + float *data_; // values for the engine to use void *definition_data_; bool is_sound_effect_; - SoundFilter current_filter_; - - ReverbRoomSize reverbed_room_size_; - - int current_ddf_reverb_ratio_; - int current_ddf_reverb_delay_; - int current_ddf_reverb_type_; - - bool reverb_is_outdoors_; - public: SoundData(); ~SoundData(); void Allocate(int samples, int buf_mode); void Free(); - void FreeFilter(); - void MixVacuum(); - void MixSubmerged(); - void MixReverb(bool dynamic_reverb, float room_area, bool outdoor_reverb, int ddf_reverb_type, int ddf_reverb_ratio, - int ddf_reverb_delay); }; //--- editor settings --- diff --git a/source_files/edge/snd_gather.cc b/source_files/edge/snd_gather.cc index 76120d3c3..692166d41 100644 --- a/source_files/edge/snd_gather.cc +++ b/source_files/edge/snd_gather.cc @@ -23,7 +23,7 @@ class GatherChunk { public: - int16_t *samples_; + float *samples_; int total_samples_; // total number is *2 for stereo @@ -34,7 +34,7 @@ class GatherChunk { EPI_ASSERT(total_samples_ > 0); - samples_ = new int16_t[total_samples_ * (is_stereo_ ? 2 : 1)]; + samples_ = new float[total_samples_ * (is_stereo_ ? 2 : 1)]; } ~GatherChunk() @@ -58,7 +58,7 @@ SoundGatherer::~SoundGatherer() delete chunks_[i]; } -int16_t *SoundGatherer::MakeChunk(int max_samples, bool _stereo) +float *SoundGatherer::MakeChunk(int max_samples, bool _stereo) { EPI_ASSERT(!request_); EPI_ASSERT(max_samples > 0); @@ -101,7 +101,7 @@ bool SoundGatherer::Finalise(SoundData *buf, bool want_stereo) if (total_samples_ == 0) return false; - buf->Allocate(total_samples_, want_stereo ? kMixStereo : kMixMono); + buf->Allocate(total_samples_, want_stereo ? kMixInterleaved : kMixMono); int pos = 0; @@ -124,21 +124,21 @@ void SoundGatherer::TransferMono(GatherChunk *chunk, SoundData *buf, int pos) { int count = chunk->total_samples_; - int16_t *dest = buf->data_left_ + pos; - int16_t *dest_end = dest + count; + float *dest = buf->data_ + pos; + float *dest_end = dest + count; - const int16_t *src = chunk->samples_; + float *src = chunk->samples_; if (chunk->is_stereo_) { - for (; dest < dest_end; src += 2) + for (;dest < dest_end; src += 2) { - *dest++ = ((int)src[0] + (int)src[1]) >> 1; + *dest++ = (src[0] + src[1]) * 0.5f; } } else { - memcpy(dest, src, count * sizeof(int16_t)); + memcpy(dest, src, count * sizeof(float)); } } @@ -146,26 +146,25 @@ void SoundGatherer::TransferStereo(GatherChunk *chunk, SoundData *buf, int pos) { int count = chunk->total_samples_; - int16_t *dest_L = buf->data_left_ + pos; - int16_t *dest_R = buf->data_right_ + pos; + float *dest = buf->data_ + pos; - const int16_t *src = chunk->samples_; - const int16_t *src_end = src + count * (chunk->is_stereo_ ? 2 : 1); + const float *src = chunk->samples_; + const float *src_end = src + count * (chunk->is_stereo_ ? 2 : 1); if (chunk->is_stereo_) { for (; src < src_end; src += 2) { - *dest_L++ = src[0]; - *dest_R++ = src[1]; + *dest++ = src[0]; + *dest++ = src[1]; } } else { while (src < src_end) { - *dest_L++ = *src; - *dest_R++ = *src++; + *dest++ = *src; + *dest++ = *src++; } } } diff --git a/source_files/edge/snd_gather.h b/source_files/edge/snd_gather.h index 1ff074c58..0480f3032 100644 --- a/source_files/edge/snd_gather.h +++ b/source_files/edge/snd_gather.h @@ -38,7 +38,7 @@ class SoundGatherer SoundGatherer(); ~SoundGatherer(); - int16_t *MakeChunk(int max_samples, bool stereo); + float *MakeChunk(int max_samples, bool stereo); // prepare to add a chunk of sound samples. Returns a buffer // containing the number of samples (* 2 for stereo) which the // user can fill up. From 05be568bcdfcb06956a328e3805571085039e70c Mon Sep 17 00:00:00 2001 From: dashodanger Date: Tue, 15 Oct 2024 20:20:37 -0600 Subject: [PATCH 2/6] Add some defines for new FP outputs --- libraries/crsid/host/audio.c | 5 +++++ libraries/libRAD/opal.cpp | 6 +++++- libraries/m4p/it2drivers/sb16.c | 5 ++++- libraries/m4p/pmp_mix.c | 30 ++++++++++++++++++++++++++---- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/libraries/crsid/host/audio.c b/libraries/crsid/host/audio.c index f1e2c45c3..c84fb0fd9 100644 --- a/libraries/crsid/host/audio.c +++ b/libraries/crsid/host/audio.c @@ -31,10 +31,15 @@ void cRSID_generateFloat(cRSID_C64instance *C64, float *buf, unsigned short len) { for (j = 0; j < C64->PlaybackSpeed; ++j) Output = cRSID_generateSample(C64); +#if defined _MSC_VER || (defined __SIZEOF_FLOAT__ && __SIZEOF_FLOAT__ == 4) *(uint32_t *)buf=0x43818000^((uint16_t)Output.L * C64->MainVolume / 256); *buf++ -= 259.0f; *(uint32_t *)buf=0x43818000^((uint16_t)Output.R * C64->MainVolume / 256); *buf++ -= 259.0f; +#else + *buf++ = (float)(Output.L * C64->MainVolume / 256) * 0.000030517578125f; + *buf++ = (float)(Output.R * C64->MainVolume / 256) * 0.000030517578125f; +#endif } } diff --git a/libraries/libRAD/opal.cpp b/libraries/libRAD/opal.cpp index 3e646a250..c15b9f3d2 100644 --- a/libraries/libRAD/opal.cpp +++ b/libraries/libRAD/opal.cpp @@ -419,11 +419,15 @@ void Opal::Sample(float *left, float *right) // Mix with the partial accumulation int32_t omblend = SampleRate - SampleAccum; +#if defined _MSC_VER || (defined __SIZEOF_FLOAT__ && __SIZEOF_FLOAT__ == 4) *(uint32_t *)left = 0x43818000^(uint16_t)((LastOutput[0] * omblend + CurrOutput[0] * SampleAccum) / SampleRate); *left -= 259.0f; *(uint32_t *)right = 0x43818000^(uint16_t)((LastOutput[1] * omblend + CurrOutput[1] * SampleAccum) / SampleRate); *right -= 259.0f; - +#else + *left = (float)((LastOutput[0] * omblend + CurrOutput[0] * SampleAccum) / SampleRate) * 0.000030517578125f; + *right = (float)((LastOutput[1] * omblend + CurrOutput[1] * SampleAccum) / SampleRate) * 0.000030517578125f; +#endif SampleAccum += OPL3SampleRate; } diff --git a/libraries/m4p/it2drivers/sb16.c b/libraries/m4p/it2drivers/sb16.c index 4955d73d0..f795371e9 100644 --- a/libraries/m4p/it2drivers/sb16.c +++ b/libraries/m4p/it2drivers/sb16.c @@ -285,9 +285,12 @@ static int32_t SB16_PostMix_Float(float *AudioOut32, int32_t SamplesToOutput) Sample = INT16_MIN; else if (Sample > INT16_MAX) Sample = INT16_MAX; - +#if defined _MSC_VER || (defined __SIZEOF_FLOAT__ && __SIZEOF_FLOAT__ == 4) *(uint32_t *)AudioOut32=0x43818000^((uint16_t)Sample); *AudioOut32++ -= 259.0f; +#else + *AudioOut32++ = (float)Sample * 0.000030517578125f; +#endif } return SamplesTodo; diff --git a/libraries/m4p/pmp_mix.c b/libraries/m4p/pmp_mix.c index 13bd9a157..7f7075e85 100644 --- a/libraries/m4p/pmp_mix.c +++ b/libraries/m4p/pmp_mix.c @@ -358,14 +358,15 @@ void mix_UpdateBufferFloat(float *buffer, int32_t numSamples) ** Instead we change the amplitude here. */ +#if defined _MSC_VER || (defined __SIZEOF_FLOAT__ && __SIZEOF_FLOAT__ == 4) if (masterVol == 256) // 8bb: max master volume, no need to change amp { for (int32_t i = 0; i < numSamples; i++) { int32_t out32 = CDA_MixBuffer[i] >> 8; CLAMP16(out32); - *(uint32_t *)&buffer[i] = 0x43818000^((uint16_t)out32); - buffer[i] -= 259.0f; + *(uint32_t *)buffer = 0x43818000^((uint16_t)out32); + *buffer++ -= 259.0f; } } else @@ -375,10 +376,31 @@ void mix_UpdateBufferFloat(float *buffer, int32_t numSamples) int32_t out32 = CDA_MixBuffer[i] >> 8; CLAMP16(out32); out32 = (out32 * masterVol) >> 8; - *(uint32_t *)&buffer[i] = 0x43818000^((uint16_t)out32); - buffer[i] -= 259.0f; + *(uint32_t *)buffer = 0x43818000^((uint16_t)out32); + *buffer++ -= 259.0f; } } +#else + if (masterVol == 256) // 8bb: max master volume, no need to change amp + { + for (int32_t i = 0; i < numSamples; i++) + { + int32_t out32 = CDA_MixBuffer[i] >> 8; + CLAMP16(out32); + *buffer++ = (float)out32 * 0.000030517578125f; + } + } + else + { + for (int32_t i = 0; i < numSamples; i++) + { + int32_t out32 = CDA_MixBuffer[i] >> 8; + CLAMP16(out32); + out32 = (out32 * masterVol) >> 8; + *buffer++ = (float)out32 * 0.000030517578125f; + } + } +#endif } bool dump_Init(int32_t frq, int32_t amp, int16_t songPos) From f4ecc45271e5e051543b48fc221b9a62c8f100b1 Mon Sep 17 00:00:00 2001 From: dashodanger Date: Tue, 15 Oct 2024 23:56:42 -0600 Subject: [PATCH 3/6] Blitter/Gather/OGG fixes and updates --- source_files/edge/s_blit.cc | 50 ++++++++++++++++++++------------- source_files/edge/s_blit.h | 6 ++-- source_files/edge/s_ogg.cc | 6 ++-- source_files/edge/s_sound.cc | 2 +- source_files/edge/snd_gather.cc | 16 ++++------- 5 files changed, 44 insertions(+), 36 deletions(-) diff --git a/source_files/edge/s_blit.cc b/source_files/edge/s_blit.cc index eb0a7f680..28d9eb2fc 100644 --- a/source_files/edge/s_blit.cc +++ b/source_files/edge/s_blit.cc @@ -104,7 +104,16 @@ SoundChannel::~SoundChannel() void SoundChannel::ComputeDelta() { - delta_ = (float)data_->frequency_ * (1.0f / (float)sound_device_frequency); + // frequency close enough ? + if (data_->frequency_ > (sound_device_frequency - sound_device_frequency / 100) && + data_->frequency_ < (sound_device_frequency + sound_device_frequency / 100)) + { + delta_ = (1 << 10); + } + else + { + delta_ = (uint32_t)floor((float)data_->frequency_ * 1024.0f / sound_device_frequency); + } } void SoundChannel::ComputeVolume() @@ -201,18 +210,18 @@ static void MixMono(SoundChannel *chan, float *dest, int pairs) float *d_pos = dest; float *d_end = d_pos + pairs; - float offset = chan->offset_; + uint32_t offset = chan->offset_; while (d_pos < d_end) { - *d_pos++ += src[(uint32_t)offset] * chan->volume_left_; + *d_pos++ += src[offset >> 10] * chan->volume_left_; offset += chan->delta_; } chan->offset_ = offset; - EPI_ASSERT((uint32_t)(offset - chan->delta_) < chan->length_); + EPI_ASSERT(offset - chan->delta_ < chan->length_); } static void MixInterleaved(SoundChannel *chan, float *dest, int pairs) @@ -239,20 +248,21 @@ static void MixInterleaved(SoundChannel *chan, float *dest, int pairs) float *d_pos = dest; float *d_end = d_pos + pairs * 2; - float offset = chan->offset_; + uint32_t offset = chan->offset_; while (d_pos < d_end) { - uint32_t pos = (uint32_t)(offset * 2.0f) & ~1; + uint32_t pos = (offset >> 9) & ~1; *d_pos++ += src[pos] * chan->volume_left_; - *d_pos++ += src[pos|1] * chan->volume_right_; + *d_pos++ += src[pos | 1] * chan->volume_right_; + offset += chan->delta_; } chan->offset_ = offset; - EPI_ASSERT((uint32_t)(offset - chan->delta_) < chan->length_); + EPI_ASSERT(offset - chan->delta_ < chan->length_); } static void MixOneChannel(SoundChannel *chan, int pairs) @@ -263,7 +273,7 @@ static void MixOneChannel(SoundChannel *chan, int pairs) if (AlmostEquals(chan->volume_left_, 0.0f) && AlmostEquals(chan->volume_right_, 0.0f)) return; - EPI_ASSERT((uint32_t)chan->offset_ < chan->length_); + EPI_ASSERT(chan->offset_ < chan->length_); float *dest = mix_buffer; @@ -272,17 +282,17 @@ static void MixOneChannel(SoundChannel *chan, int pairs) int count = pairs; // check if enough sound data is left - if ((uint32_t)(chan->offset_ + count * chan->delta_) >= chan->length_) + if (chan->offset_ + count * chan->delta_ >= chan->length_) { // find minimum number of samples we can play - double avail = (chan->length_ - (uint32_t)(chan->offset_)) / (double)chan->delta_; + double avail = (chan->length_ - chan->offset_ + chan->delta_ - 1) / (double)chan->delta_; count = (int)floor(avail); EPI_ASSERT(count > 0); EPI_ASSERT(count <= pairs); - EPI_ASSERT((uint32_t)RoundToInteger(chan->offset_ + count * chan->delta_) >= chan->length_); + EPI_ASSERT(chan->offset_ + count * chan->delta_ >= chan->length_); } if (chan->data_->mode_ == kMixInterleaved) @@ -290,7 +300,7 @@ static void MixOneChannel(SoundChannel *chan, int pairs) else MixMono(chan, dest, count); - if ((uint32_t)chan->offset_ >= chan->length_) + if (chan->offset_ >= chan->length_) { if (!chan->loop_) { @@ -324,7 +334,7 @@ static bool QueueNextBuffer(void) queue_channel->data_ = buf; queue_channel->offset_ = 0; - queue_channel->length_ = buf->length_; + queue_channel->length_ = buf->length_ << 10; queue_channel->ComputeDelta(); @@ -339,10 +349,10 @@ static void MixQueues(int pairs) if (!chan || !chan->data_ || chan->state_ != kChannelPlaying) return; - if (chan->volume_left_ == 0 && chan->volume_right_ == 0) + if (AlmostEquals(chan->volume_left_, 0.0f) && AlmostEquals(chan->volume_right_, 0.0f)) return; - EPI_ASSERT((uint32_t)chan->offset_ < chan->length_); + EPI_ASSERT(chan->offset_ < chan->length_); float *dest = mix_buffer; @@ -351,17 +361,17 @@ static void MixQueues(int pairs) int count = pairs; // check if enough sound data is left - if ((uint32_t)(chan->offset_ + count * chan->delta_) >= chan->length_) + if (chan->offset_ + count * chan->delta_ >= chan->length_) { // find minimum number of samples we can play - double avail = (chan->length_ - (uint32_t)(chan->offset_)) / (double)chan->delta_; + double avail = (chan->length_ - chan->offset_ + chan->delta_ - 1) / (double)chan->delta_; count = (int)floor(avail); EPI_ASSERT(count > 0); EPI_ASSERT(count <= pairs); - EPI_ASSERT((uint32_t)RoundToInteger(chan->offset_ + count * chan->delta_) >= chan->length_); + EPI_ASSERT(chan->offset_ + count * chan->delta_ >= chan->length_); } if (chan->data_->mode_ == kMixInterleaved) @@ -369,7 +379,7 @@ static void MixQueues(int pairs) else MixMono(chan, dest, count); - if ((uint32_t)chan->offset_ >= chan->length_) + if (chan->offset_ >= chan->length_) { // reached end of current queued buffer. // Place current buffer onto free list, diff --git a/source_files/edge/s_blit.h b/source_files/edge/s_blit.h index a3c25775a..cc128f877 100644 --- a/source_files/edge/s_blit.h +++ b/source_files/edge/s_blit.h @@ -52,9 +52,11 @@ class SoundChannel SoundEffectDefinition *definition_; Position *position_; - float offset_; - float delta_; + // We use a 22.10 fixed point for sound offsets. It's a reasonable + // compromise between longest sound and accumulated round-off error. + uint32_t offset_; uint32_t length_; + uint32_t delta_; float volume_left_; // mixing volume float volume_right_; diff --git a/source_files/edge/s_ogg.cc b/source_files/edge/s_ogg.cc index ea22ad063..1b26482bb 100644 --- a/source_files/edge/s_ogg.cc +++ b/source_files/edge/s_ogg.cc @@ -270,6 +270,8 @@ bool LoadOGGSound(SoundData *buf, const uint8_t *data, int length) LogDebug("OGG SFX Loader: freq %d Hz, %d channels\n", ogg->sample_rate, ogg->channels); + bool is_stereo = ogg->channels > 1; + buf->frequency_ = ogg->sample_rate; buf->mode_ = sound_device_stereo ? kMixInterleaved : kMixMono; @@ -278,9 +280,9 @@ bool LoadOGGSound(SoundData *buf, const uint8_t *data, int length) SoundGatherer gather; - float *buffer = gather.MakeChunk(total_samples, sound_device_stereo); + float *buffer = gather.MakeChunk(total_samples, is_stereo); - gather.CommitChunk(stb_vorbis_get_samples_float_interleaved(ogg, sound_device_stereo ? 2 : 1, buffer, total_samples)); + gather.CommitChunk(stb_vorbis_get_samples_float_interleaved(ogg, is_stereo ? 2 : 1, buffer, total_samples)); if (!gather.Finalise(buf, sound_device_stereo)) FatalError("OGG SFX Loader: no samples!\n"); diff --git a/source_files/edge/s_sound.cc b/source_files/edge/s_sound.cc index 1371f200c..a53cac88f 100644 --- a/source_files/edge/s_sound.cc +++ b/source_files/edge/s_sound.cc @@ -332,7 +332,7 @@ static void S_PlaySound(int idx, SoundEffectDefinition *def, int category, Posit chan->volume_right_ = 0; chan->offset_ = 0; - chan->length_ = chan->data_->length_; + chan->length_ = chan->data_->length_ << 10; chan->loop_ = false; chan->boss_ = (flags & kSoundEffectBoss) ? true : false; diff --git a/source_files/edge/snd_gather.cc b/source_files/edge/snd_gather.cc index 692166d41..ab06e197e 100644 --- a/source_files/edge/snd_gather.cc +++ b/source_files/edge/snd_gather.cc @@ -124,13 +124,12 @@ void SoundGatherer::TransferMono(GatherChunk *chunk, SoundData *buf, int pos) { int count = chunk->total_samples_; - float *dest = buf->data_ + pos; - float *dest_end = dest + count; - + float *dest = buf->data_ + pos; float *src = chunk->samples_; if (chunk->is_stereo_) { + const float *dest_end = dest + count; for (;dest < dest_end; src += 2) { *dest++ = (src[0] + src[1]) * 0.5f; @@ -147,20 +146,15 @@ void SoundGatherer::TransferStereo(GatherChunk *chunk, SoundData *buf, int pos) int count = chunk->total_samples_; float *dest = buf->data_ + pos; - - const float *src = chunk->samples_; - const float *src_end = src + count * (chunk->is_stereo_ ? 2 : 1); + float *src = chunk->samples_; if (chunk->is_stereo_) { - for (; src < src_end; src += 2) - { - *dest++ = src[0]; - *dest++ = src[1]; - } + memcpy(dest, src, count * 2 * sizeof(float)); } else { + const float *src_end = src + count; while (src < src_end) { *dest++ = *src; From 02acfc6ca1ab66f798b6bfd5e006c9a7468b1c64 Mon Sep 17 00:00:00 2001 From: dashodanger Date: Wed, 16 Oct 2024 09:41:16 -0600 Subject: [PATCH 4/6] Simplify stereo/mono blitting --- source_files/edge/i_movie.cc | 22 ++-------- source_files/edge/s_blit.cc | 72 ++++++++++----------------------- source_files/edge/s_blit.h | 6 +-- source_files/edge/s_cache.cc | 37 +++++------------ source_files/edge/s_flac.cc | 32 +-------------- source_files/edge/s_fluid.cc | 36 ++--------------- source_files/edge/s_m4p.cc | 34 ++-------------- source_files/edge/s_mp3.cc | 39 ++---------------- source_files/edge/s_ogg.cc | 10 ++--- source_files/edge/s_opl.cc | 26 ++---------- source_files/edge/s_rad.cc | 30 +------------- source_files/edge/s_sid.cc | 34 ++-------------- source_files/edge/s_wav.cc | 6 +-- source_files/edge/snd_data.cc | 9 ++--- source_files/edge/snd_data.h | 9 +---- source_files/edge/snd_gather.cc | 31 ++------------ source_files/edge/snd_gather.h | 3 +- 17 files changed, 73 insertions(+), 363 deletions(-) diff --git a/source_files/edge/i_movie.cc b/source_files/edge/i_movie.cc index 608e074e9..46c3232c1 100644 --- a/source_files/edge/i_movie.cc +++ b/source_files/edge/i_movie.cc @@ -35,8 +35,6 @@ #include "w_files.h" #include "w_wad.h" -extern bool sound_device_stereo; - bool playing_movie = false; static bool need_canvas_update = false; static bool skip_bar_active = false; @@ -66,26 +64,12 @@ void MovieAudioCallback(plm_t *mpeg, plm_samples_t *samples, void *user) { (void)mpeg; (void)user; - SoundData *movie_buf = SoundQueueGetFreeBuffer(PLM_AUDIO_SAMPLES_PER_FRAME, sound_device_stereo ? kMixInterleaved : kMixMono); + SoundData *movie_buf = SoundQueueGetFreeBuffer(PLM_AUDIO_SAMPLES_PER_FRAME); if (movie_buf) { movie_buf->length_ = PLM_AUDIO_SAMPLES_PER_FRAME; - if (sound_device_stereo) - { - memcpy(movie_buf->data_, samples->interleaved, PLM_AUDIO_SAMPLES_PER_FRAME * 2 * sizeof(float)); - SoundQueueAddBuffer(movie_buf, movie_sample_rate); - } - else - { - float *src = samples->interleaved; - float *dest = movie_buf->data_; - float *dest_end = movie_buf->data_ + PLM_AUDIO_SAMPLES_PER_FRAME; - for (; dest < dest_end; src += 2) - { - *dest++ = (src[0] + src[1]) * 0.5f; - } - SoundQueueAddBuffer(movie_buf, movie_sample_rate); - } + memcpy(movie_buf->data_, samples->interleaved, PLM_AUDIO_SAMPLES_PER_FRAME * 2 * sizeof(float)); + SoundQueueAddBuffer(movie_buf, movie_sample_rate); } } diff --git a/source_files/edge/s_blit.cc b/source_files/edge/s_blit.cc index 28d9eb2fc..c3b511bc9 100644 --- a/source_files/edge/s_blit.cc +++ b/source_files/edge/s_blit.cc @@ -190,7 +190,7 @@ static void BlitToF32(const float *src, float *dest, int length) memcpy(dest, src, length * sizeof(float)); } -static void MixMono(SoundChannel *chan, float *dest, int pairs) +static void MixInterleaved(SoundChannel *chan, float *dest, int pairs) { EPI_ASSERT(pairs > 0); @@ -208,56 +208,32 @@ static void MixMono(SoundChannel *chan, float *dest, int pairs) }*/ float *d_pos = dest; - float *d_end = d_pos + pairs; + float *d_end = d_pos + pairs * (sound_device_stereo ? 2 : 1); uint32_t offset = chan->offset_; - while (d_pos < d_end) + if (sound_device_stereo) { - *d_pos++ += src[offset >> 10] * chan->volume_left_; - - offset += chan->delta_; - } - - chan->offset_ = offset; - - EPI_ASSERT(offset - chan->delta_ < chan->length_); -} - -static void MixInterleaved(SoundChannel *chan, float *dest, int pairs) -{ - if (!sound_device_stereo) - FatalError("INTERNAL ERROR: tried to mix an interleaved buffer in MONO " - "mode.\n"); - - EPI_ASSERT(pairs > 0); + while (d_pos < d_end) + { + uint32_t pos = (offset >> 9) & ~1; - float *src = chan->data_->data_; + *d_pos++ += src[pos] * chan->volume_left_; + *d_pos++ += src[pos | 1] * chan->volume_right_; - /*if (paused || menu_active) - src_L = chan->data_->data_left_; + offset += chan->delta_; + } + } else { - if (!chan->data_->is_sound_effect_ || chan->category_ == kCategoryUi || - chan->data_->current_filter_ == kFilterNone) - src_L = chan->data_->data_left_; - else - src_L = chan->data_->filter_data_left_; - }*/ - - float *d_pos = dest; - float *d_end = d_pos + pairs * 2; - - uint32_t offset = chan->offset_; - - while (d_pos < d_end) - { - uint32_t pos = (offset >> 9) & ~1; + while (d_pos < d_end) + { + uint32_t pos = (offset >> 9) & ~1; - *d_pos++ += src[pos] * chan->volume_left_; - *d_pos++ += src[pos | 1] * chan->volume_right_; + *d_pos++ += ((src[pos] * chan->volume_left_) + (src[pos | 1] * chan->volume_right_)) * 0.5f; - offset += chan->delta_; + offset += chan->delta_; + } } chan->offset_ = offset; @@ -295,10 +271,7 @@ static void MixOneChannel(SoundChannel *chan, int pairs) EPI_ASSERT(chan->offset_ + count * chan->delta_ >= chan->length_); } - if (chan->data_->mode_ == kMixInterleaved) - MixInterleaved(chan, dest, count); - else - MixMono(chan, dest, count); + MixInterleaved(chan, dest, count); if (chan->offset_ >= chan->length_) { @@ -374,10 +347,7 @@ static void MixQueues(int pairs) EPI_ASSERT(chan->offset_ + count * chan->delta_ >= chan->length_); } - if (chan->data_->mode_ == kMixInterleaved) - MixInterleaved(chan, dest, count); - else - MixMono(chan, dest, count); + MixInterleaved(chan, dest, count); if (chan->offset_ >= chan->length_) { @@ -653,7 +623,7 @@ void SoundQueueStop(void) UnlockAudio(); } -SoundData *SoundQueueGetFreeBuffer(int samples, int buf_mode) +SoundData *SoundQueueGetFreeBuffer(int samples) { if (no_sound) return nullptr; @@ -667,7 +637,7 @@ SoundData *SoundQueueGetFreeBuffer(int samples, int buf_mode) buf = free_queue_buffers.front(); free_queue_buffers.pop_front(); - buf->Allocate(samples, buf_mode); + buf->Allocate(samples); } } UnlockAudio(); diff --git a/source_files/edge/s_blit.h b/source_files/edge/s_blit.h index cc128f877..46b035e3e 100644 --- a/source_files/edge/s_blit.h +++ b/source_files/edge/s_blit.h @@ -114,11 +114,11 @@ void SoundQueueStop(void); // stop the currently playing queue. All playing buffers // are moved into the free list. -SoundData *SoundQueueGetFreeBuffer(int samples, int buf_mode); +SoundData *SoundQueueGetFreeBuffer(int samples); // returns the next unused (or finished) buffer, or nullptr -// if there are none. The data_left_/data_right_ fields will be +// if there are none. The data_ field will be // updated to ensure they hold the requested number of -// samples and conform to the wanted buffer mode. +// samples. void SoundQueueAddBuffer(SoundData *buf, int freq); // add a new buffer to be end of the queue. diff --git a/source_files/edge/s_cache.cc b/source_files/edge/s_cache.cc index d625512e7..bc3db992c 100644 --- a/source_files/edge/s_cache.cc +++ b/source_files/edge/s_cache.cc @@ -49,7 +49,6 @@ #include "w_files.h" #include "w_wad.h" -extern bool sound_device_stereo; extern int sound_device_frequency; extern bool pc_speaker_mode; @@ -60,9 +59,9 @@ static void LoadSilence(SoundData *buf) int length = 256; buf->frequency_ = sound_device_frequency; - buf->Allocate(length, sound_device_stereo ? kMixInterleaved : kMixMono); + buf->Allocate(length); - memset(buf->data_, 0, length * sizeof(float) * (sound_device_stereo ? 2 : 1)); + memset(buf->data_, 0, length * sizeof(float) * 2); } static bool LoadDoom(SoundData *buf, const uint8_t *lump, int length) @@ -80,37 +79,23 @@ static bool LoadDoom(SoundData *buf, const uint8_t *lump, int length) if (length <= 0) return false; - buf->Allocate(length, sound_device_stereo ? kMixInterleaved : kMixMono); + buf->Allocate(length); // convert to signed 16-bit format const uint8_t *src = lump + 8; const uint8_t *s_end = src + length; float *dest = buf->data_; - - if (sound_device_stereo) + float src_f = 0; + + for (; src < s_end; src++) { - float src_f = 0; - for (; src < s_end; src++) - { - int16_t in = ((*src ^ 0x80) << 8); - *(uint32_t *)&src_f=0x43818000^((uint16_t)in); - src_f -= 259.0f; - *dest++ = src_f; - *dest++ = src_f; - } + int16_t in = ((*src ^ 0x80) << 8); + *(uint32_t *)&src_f=0x43818000^((uint16_t)in); + src_f -= 259.0f; + *dest++ = src_f; + *dest++ = src_f; } - else - { - for (; src < s_end; src++) - { - int16_t in = ((*src ^ 0x80) << 8); - *(uint32_t *)dest=0x43818000^((uint16_t)in); - *dest++ -= 259.0f; - } - } - -//65540 return true; } diff --git a/source_files/edge/s_flac.cc b/source_files/edge/s_flac.cc index a2b72d09d..7df684633 100644 --- a/source_files/edge/s_flac.cc +++ b/source_files/edge/s_flac.cc @@ -30,7 +30,6 @@ #include "snd_gather.h" #include "w_wad.h" -extern bool sound_device_stereo; // FIXME: encapsulation extern int sound_device_frequency; class FLACPlayer : public AbstractMusicPlayer @@ -56,8 +55,6 @@ class FLACPlayer : public AbstractMusicPlayer uint8_t *flac_data_; // Passed in from s_music; must be deleted on close - float *mono_buffer_; - public: bool OpenMemory(uint8_t *data, int length); @@ -81,15 +78,11 @@ class FLACPlayer : public AbstractMusicPlayer FLACPlayer::FLACPlayer() : status_(kNotLoaded) { - mono_buffer_ = new float[kMusicBuffer * 2]; } FLACPlayer::~FLACPlayer() { Close(); - - if (mono_buffer_) - delete[] mono_buffer_; } void FLACPlayer::PostOpen() @@ -108,29 +101,11 @@ void FLACPlayer::PostOpen() status_ = kStopped; } -static void ConvertToMono(float *dest, const float *src, int len) -{ - const float *s_end = src + len * 2; - - for (; src < s_end; src += 2) - { - // compute average of samples - *dest++ = (src[0] + src[1]) * 0.5f; - } -} - bool FLACPlayer::StreamIntoBuffer(SoundData *buf) { - float *data_buf; - bool song_done = false; - if (!sound_device_stereo) - data_buf = mono_buffer_; - else - data_buf = buf->data_; - - drflac_uint64 frames = drflac_read_pcm_frames_f32(flac_track_, kMusicBuffer, data_buf); + drflac_uint64 frames = drflac_read_pcm_frames_f32(flac_track_, kMusicBuffer, buf->data_); if (frames < kMusicBuffer) song_done = true; @@ -139,9 +114,6 @@ bool FLACPlayer::StreamIntoBuffer(SoundData *buf) buf->frequency_ = flac_track_->sampleRate; - if (is_stereo_ && !sound_device_stereo) - ConvertToMono(buf->data_, mono_buffer_, buf->length_); - if (song_done) /* EOF */ { if (!looping_) @@ -235,7 +207,7 @@ void FLACPlayer::Ticker() { while (status_ == kPlaying && !pc_speaker_mode) { - SoundData *buf = SoundQueueGetFreeBuffer(kMusicBuffer, (sound_device_stereo) ? kMixInterleaved : kMixMono); + SoundData *buf = SoundQueueGetFreeBuffer(kMusicBuffer); if (!buf) break; diff --git a/source_files/edge/s_fluid.cc b/source_files/edge/s_fluid.cc index 1d49cb0fa..d9f449507 100644 --- a/source_files/edge/s_fluid.cc +++ b/source_files/edge/s_fluid.cc @@ -38,7 +38,6 @@ typedef struct MidiRealTimeInterface FluidInterface; #include "s_blit.h" #include "s_music.h" -extern bool sound_device_stereo; extern int sound_device_frequency; bool fluid_disabled = false; @@ -68,17 +67,6 @@ static void *edge_fluid_fopen(fluid_fileapi_t *fileapi, const char *filename) return fp; } -static void ConvertToMono(float *dest, const float *src, int len) -{ - const float *s_end = src + len * 2; - - for (; src < s_end; src += 2) - { - // compute average of samples - *dest++ = (src[0] + src[1]) * 0.5f; - } -} - bool StartupFluid(void) { LogPrint("Initializing FluidLite...\n"); @@ -186,21 +174,15 @@ class FluidPlayer : public AbstractMusicPlayer FluidInterface *fluid_interface_; - float *mono_buffer_; - public: FluidPlayer(uint8_t *data, int _length, bool looping) : status_(kNotLoaded), looping_(looping) { - mono_buffer_ = new float[kMusicBuffer * 2]; SequencerInit(); } ~FluidPlayer() { Close(); - - if (mono_buffer_) - delete[] mono_buffer_; } public: @@ -286,7 +268,7 @@ class FluidPlayer : public AbstractMusicPlayer fluid_interface_->onPcmRender_userdata = this; fluid_interface_->pcmSampleRate = sound_device_frequency; - fluid_interface_->pcmFrameSize = 2 /*channels*/ * 4 /*size of one sample*/; + fluid_interface_->pcmFrameSize = 2 /*channels*/ * sizeof(float) /*size of one sample*/; fluid_interface_->rt_deviceSwitch = rtDeviceSwitch; fluid_interface_->rt_currentDevice = rtCurrentDevice; @@ -375,7 +357,7 @@ class FluidPlayer : public AbstractMusicPlayer while (status_ == kPlaying && !pc_speaker_mode) { - SoundData *buf = SoundQueueGetFreeBuffer(kMusicBuffer, sound_device_stereo ? kMixInterleaved : kMixMono); + SoundData *buf = SoundQueueGetFreeBuffer(kMusicBuffer); if (!buf) break; @@ -397,24 +379,14 @@ class FluidPlayer : public AbstractMusicPlayer private: bool StreamIntoBuffer(SoundData *buf) { - float *data_buf; - bool song_done = false; - if (!sound_device_stereo) - data_buf = mono_buffer_; - else - data_buf = buf->data_; - - int played = fluid_sequencer_->PlayStream((uint8_t *)data_buf, kMusicBuffer); + int played = fluid_sequencer_->PlayStream((uint8_t *)buf->data_, kMusicBuffer); if (fluid_sequencer_->PositionAtEnd()) song_done = true; - buf->length_ = played / 8; - - if (!sound_device_stereo) - ConvertToMono(buf->data_, mono_buffer_, buf->length_); + buf->length_ = played / 2 / sizeof(float); if (song_done) /* EOF */ { diff --git a/source_files/edge/s_m4p.cc b/source_files/edge/s_m4p.cc index e366ec186..4f7edf006 100644 --- a/source_files/edge/s_m4p.cc +++ b/source_files/edge/s_m4p.cc @@ -28,7 +28,6 @@ #include "snd_gather.h" #include "w_wad.h" -extern bool sound_device_stereo; // FIXME: encapsulation extern int sound_device_frequency; class M4PPlayer : public AbstractMusicPlayer @@ -49,8 +48,6 @@ class M4PPlayer : public AbstractMusicPlayer int status_; bool looping_; - float *mono_buffer_; - public: bool OpenMemory(uint8_t *data, int length); @@ -74,15 +71,11 @@ class M4PPlayer : public AbstractMusicPlayer M4PPlayer::M4PPlayer() : status_(kNotLoaded) { - mono_buffer_ = new float[kMusicBuffer * 2]; } M4PPlayer::~M4PPlayer() { Close(); - - if (mono_buffer_) - delete[] mono_buffer_; } void M4PPlayer::PostOpen() @@ -91,34 +84,13 @@ void M4PPlayer::PostOpen() status_ = kStopped; } -static void ConvertToMono(float *dest, const float *src, int len) -{ - const float *s_end = src + len * 2; - - for (; src < s_end; src += 2) - { - // compute average of samples - *dest++ = (src[0] + src[1]) * 0.5f; - } -} - bool M4PPlayer::StreamIntoBuffer(SoundData *buf) { - float *data_buf; - bool song_done = false; - if (!sound_device_stereo) - data_buf = mono_buffer_; - else - data_buf = buf->data_; - - m4p_GenerateFloatSamples(data_buf, kMusicBuffer / sizeof(float)); - - buf->length_ = kMusicBuffer / 4; + m4p_GenerateFloatSamples(buf->data_, kMusicBuffer / sizeof(float)); - if (!sound_device_stereo) - ConvertToMono(buf->data_, mono_buffer_, buf->length_); + buf->length_ = kMusicBuffer / sizeof(float); if (song_done) /* EOF */ { @@ -207,7 +179,7 @@ void M4PPlayer::Ticker() { while (status_ == kPlaying && !pc_speaker_mode) { - SoundData *buf = SoundQueueGetFreeBuffer(kMusicBuffer, (sound_device_stereo) ? kMixInterleaved : kMixMono); + SoundData *buf = SoundQueueGetFreeBuffer(kMusicBuffer); if (!buf) break; diff --git a/source_files/edge/s_mp3.cc b/source_files/edge/s_mp3.cc index 2f7898eb8..4057de483 100644 --- a/source_files/edge/s_mp3.cc +++ b/source_files/edge/s_mp3.cc @@ -30,8 +30,6 @@ #include "snd_gather.h" #include "w_wad.h" -extern bool sound_device_stereo; // FIXME: encapsulation - class MP3Player : public AbstractMusicPlayer { public: @@ -55,8 +53,6 @@ class MP3Player : public AbstractMusicPlayer uint8_t *mp3_data_ = nullptr; drmp3 *mp3_decoder_ = nullptr; - float *mono_buffer_; - public: bool OpenMemory(uint8_t *data, int length); @@ -80,15 +76,11 @@ class MP3Player : public AbstractMusicPlayer MP3Player::MP3Player() : status_(kNotLoaded) { - mono_buffer_ = new float[kMusicBuffer * 2]; } MP3Player::~MP3Player() { Close(); - - if (mono_buffer_) - delete[] mono_buffer_; } void MP3Player::PostOpen() @@ -106,27 +98,9 @@ void MP3Player::PostOpen() status_ = kStopped; } -static void ConvertToMono(float *dest, const float *src, int len) -{ - const float *s_end = src + len * 2; - - for (; src < s_end; src += 2) - { - // compute average of samples - *dest++ = (src[0] + src[1]) * 0.5f; - } -} - bool MP3Player::StreamIntoBuffer(SoundData *buf) { - float *data_buf; - - if (is_stereo_ && !sound_device_stereo) - data_buf = mono_buffer_; - else - data_buf = buf->data_; - - int got_size = drmp3_read_pcm_frames_f32(mp3_decoder_, kMusicBuffer, data_buf); + int got_size = drmp3_read_pcm_frames_f32(mp3_decoder_, kMusicBuffer, buf->data_); if (got_size == 0) /* EOF */ { @@ -144,10 +118,7 @@ bool MP3Player::StreamIntoBuffer(SoundData *buf) buf->length_ = got_size; - if (is_stereo_ && !sound_device_stereo) - ConvertToMono(buf->data_, mono_buffer_, got_size); - - return (true); + return true; } bool MP3Player::OpenMemory(uint8_t *data, int length) @@ -244,7 +215,7 @@ void MP3Player::Ticker() while (status_ == kPlaying && !pc_speaker_mode) { SoundData *buf = - SoundQueueGetFreeBuffer(kMusicBuffer, (is_stereo_ && sound_device_stereo) ? kMixInterleaved : kMixMono); + SoundQueueGetFreeBuffer(kMusicBuffer); if (!buf) break; @@ -316,15 +287,13 @@ bool LoadMP3Sound(SoundData *buf, const uint8_t *data, int length) buf->frequency_ = mp3.sampleRate; - buf->mode_ = sound_device_stereo ? kMixInterleaved : kMixMono; - SoundGatherer gather; float *buffer = gather.MakeChunk(framecount, is_stereo); gather.CommitChunk(drmp3_read_pcm_frames_f32(&mp3, framecount, buffer)); - if (!gather.Finalise(buf, sound_device_stereo)) + if (!gather.Finalise(buf)) LogWarning("MP3 SFX Loader: no samples!\n"); drmp3_uninit(&mp3); diff --git a/source_files/edge/s_ogg.cc b/source_files/edge/s_ogg.cc index 1b26482bb..f600c43dc 100644 --- a/source_files/edge/s_ogg.cc +++ b/source_files/edge/s_ogg.cc @@ -32,8 +32,6 @@ #include "stb_vorbis.h" // clang-format on -extern bool sound_device_stereo; // FIXME: encapsulation - class OGGPlayer : public AbstractMusicPlayer { public: @@ -94,7 +92,7 @@ void OGGPlayer::PostOpen() bool OGGPlayer::StreamIntoBuffer(SoundData *buf) { - int got_size = stb_vorbis_get_samples_float_interleaved(ogg_decoder_, sound_device_stereo ? 2 : 1, buf->data_, kMusicBuffer); + int got_size = stb_vorbis_get_samples_float_interleaved(ogg_decoder_, ogg_decoder_->channels, buf->data_, kMusicBuffer); if (got_size == 0) /* EOF */ { @@ -212,7 +210,7 @@ void OGGPlayer::Ticker() while (status_ == kPlaying) { SoundData *buf = - SoundQueueGetFreeBuffer(kMusicBuffer, sound_device_stereo ? kMixInterleaved : kMixMono); + SoundQueueGetFreeBuffer(kMusicBuffer); if (!buf) break; @@ -274,8 +272,6 @@ bool LoadOGGSound(SoundData *buf, const uint8_t *data, int length) buf->frequency_ = ogg->sample_rate; - buf->mode_ = sound_device_stereo ? kMixInterleaved : kMixMono; - uint32_t total_samples = stb_vorbis_stream_length_in_samples(ogg); SoundGatherer gather; @@ -284,7 +280,7 @@ bool LoadOGGSound(SoundData *buf, const uint8_t *data, int length) gather.CommitChunk(stb_vorbis_get_samples_float_interleaved(ogg, is_stereo ? 2 : 1, buffer, total_samples)); - if (!gather.Finalise(buf, sound_device_stereo)) + if (!gather.Finalise(buf)) FatalError("OGG SFX Loader: no samples!\n"); stb_vorbis_close(ogg); diff --git a/source_files/edge/s_opl.cc b/source_files/edge/s_opl.cc index 339fb73c1..83db1c3d2 100644 --- a/source_files/edge/s_opl.cc +++ b/source_files/edge/s_opl.cc @@ -41,7 +41,6 @@ typedef struct MidiRealTimeInterface OPLInterface; OPLPlayer *edge_opl = nullptr; -extern bool sound_device_stereo; extern int sound_device_frequency; bool opl_disabled = false; @@ -115,23 +114,17 @@ class OpalPlayer : public AbstractMusicPlayer int status_; bool looping_; - float *mono_buffer_; - OPLInterface *opl_interface_; public: OpalPlayer(bool looping) : status_(kNotLoaded), looping_(looping) { - mono_buffer_ = new float[2 * kMusicBuffer]; SequencerInit(); } ~OpalPlayer() { Close(); - - if (mono_buffer_) - delete[] mono_buffer_; } public: @@ -228,8 +221,7 @@ class OpalPlayer : public AbstractMusicPlayer opl_interface_->onPcmRender_userdata = this; opl_interface_->pcmSampleRate = sound_device_frequency; - opl_interface_->pcmFrameSize = 2 /*channels*/ * 4 /*size of one sample*/; // OPL3 is 2 'channels' regardless of - // the sound_device_stereo setting + opl_interface_->pcmFrameSize = 2 /*channels*/ * sizeof(float) /*size of one sample*/; opl_interface_->rt_deviceSwitch = rtDeviceSwitch; opl_interface_->rt_currentDevice = rtCurrentDevice; @@ -314,7 +306,7 @@ class OpalPlayer : public AbstractMusicPlayer { while (status_ == kPlaying && !pc_speaker_mode) { - SoundData *buf = SoundQueueGetFreeBuffer(kMusicBuffer, sound_device_stereo ? kMixInterleaved : kMixMono); + SoundData *buf = SoundQueueGetFreeBuffer(kMusicBuffer); if (!buf) break; @@ -336,24 +328,14 @@ class OpalPlayer : public AbstractMusicPlayer private: bool StreamIntoBuffer(SoundData *buf) { - float *data_buf; - bool song_done = false; - if (!sound_device_stereo) - data_buf = mono_buffer_; - else - data_buf = buf->data_; - - int played = opl_sequencer_->PlayStream((uint8_t *)(data_buf), kMusicBuffer); + int played = opl_sequencer_->PlayStream((uint8_t *)(buf->data_), kMusicBuffer); if (opl_sequencer_->PositionAtEnd()) song_done = true; - buf->length_ = played / 8; - - if (!sound_device_stereo) - ConvertToMono(buf->data_, mono_buffer_, buf->length_); + buf->length_ = played / 2 / sizeof(float); if (song_done) /* EOF */ { diff --git a/source_files/edge/s_rad.cc b/source_files/edge/s_rad.cc index f6724a2b8..0679872c6 100644 --- a/source_files/edge/s_rad.cc +++ b/source_files/edge/s_rad.cc @@ -29,7 +29,6 @@ #include "snd_gather.h" #include "w_wad.h" -extern bool sound_device_stereo; // FIXME: encapsulation extern int sound_device_frequency; // Works better with the RAD code if these are 'global' @@ -54,8 +53,6 @@ class RadPlayer : public AbstractMusicPlayer int status_; bool looping_; - float *mono_buffer_; - int sample_count_; int sample_update_; int sample_rate_; @@ -84,15 +81,11 @@ class RadPlayer : public AbstractMusicPlayer RadPlayer::RadPlayer() : status_(kNotLoaded), tune_(nullptr) { - mono_buffer_ = new float[kMusicBuffer * 2]; } RadPlayer::~RadPlayer() { Close(); - - if (mono_buffer_) - delete[] mono_buffer_; } void RadPlayer::PostOpen() @@ -103,29 +96,13 @@ void RadPlayer::PostOpen() status_ = kStopped; } -static void ConvertToMono(float *dest, const float *src, int len) -{ - const float *s_end = src + len * 2; - - for (; src < s_end; src += 2) - { - // compute average of samples - *dest++ = (src[0] + src[1]) * 0.5f; - } -} - bool RadPlayer::StreamIntoBuffer(SoundData *buf) { - float *data_buf; + float *data_buf = buf->data_; bool song_done = false; int samples = 0; - if (!sound_device_stereo) - data_buf = mono_buffer_; - else - data_buf = buf->data_; - for (int i = 0; i < kMusicBuffer; i += 2) { edge_opal->Sample(data_buf + i, data_buf + i + 1); @@ -140,9 +117,6 @@ bool RadPlayer::StreamIntoBuffer(SoundData *buf) buf->length_ = samples; - if (!sound_device_stereo) - ConvertToMono(buf->data_, mono_buffer_, buf->length_); - if (song_done) /* EOF */ { if (!looping_) @@ -250,7 +224,7 @@ void RadPlayer::Ticker() { while (status_ == kPlaying && !pc_speaker_mode) { - SoundData *buf = SoundQueueGetFreeBuffer(kMusicBuffer, (sound_device_stereo) ? kMixInterleaved : kMixMono); + SoundData *buf = SoundQueueGetFreeBuffer(kMusicBuffer); if (!buf) break; diff --git a/source_files/edge/s_sid.cc b/source_files/edge/s_sid.cc index 99c1adef8..32f60f575 100644 --- a/source_files/edge/s_sid.cc +++ b/source_files/edge/s_sid.cc @@ -31,7 +31,6 @@ #include "snd_gather.h" #include "w_wad.h" -extern bool sound_device_stereo; // FIXME: encapsulation extern int sound_device_frequency; class SIDPlayer : public AbstractMusicPlayer @@ -52,8 +51,6 @@ class SIDPlayer : public AbstractMusicPlayer int status_; bool looping_; - float *mono_buffer_; - cRSID_C64instance *C64_ = nullptr; cRSID_SIDheader *C64_song_ = nullptr; @@ -80,15 +77,11 @@ class SIDPlayer : public AbstractMusicPlayer SIDPlayer::SIDPlayer() : status_(kNotLoaded) { - mono_buffer_ = new float[kMusicBuffer * 2]; } SIDPlayer::~SIDPlayer() { Close(); - - if (mono_buffer_) - delete[] mono_buffer_; } void SIDPlayer::PostOpenInit() @@ -99,32 +92,11 @@ void SIDPlayer::PostOpenInit() status_ = kStopped; } -static void ConvertToMono(float *dest, const float *src, int len) -{ - const float *s_end = src + len * 2; - - for (; src < s_end; src += 2) - { - // compute average of samples - *dest++ = (src[0] + src[1]) * 0.5f; - } -} - bool SIDPlayer::StreamIntoBuffer(SoundData *buf) { - float *data_buf; - - if (!sound_device_stereo) - data_buf = mono_buffer_; - else - data_buf = buf->data_; - - cRSID_generateFloat(C64_, data_buf, kMusicBuffer); - - buf->length_ = kMusicBuffer / sizeof(float) / 2; + cRSID_generateFloat(C64_, buf->data_, kMusicBuffer); - if (!sound_device_stereo) - ConvertToMono(buf->data_, mono_buffer_, buf->length_); + buf->length_ = kMusicBuffer / 2 / sizeof(float); return true; } @@ -216,7 +188,7 @@ void SIDPlayer::Ticker() { while (status_ == kPlaying && !pc_speaker_mode) { - SoundData *buf = SoundQueueGetFreeBuffer(kMusicBuffer, (sound_device_stereo) ? kMixInterleaved : kMixMono); + SoundData *buf = SoundQueueGetFreeBuffer(kMusicBuffer); if (!buf) break; diff --git a/source_files/edge/s_wav.cc b/source_files/edge/s_wav.cc index 020f50d08..bcbbce621 100644 --- a/source_files/edge/s_wav.cc +++ b/source_files/edge/s_wav.cc @@ -28,8 +28,6 @@ #include "snd_gather.h" #include "w_wad.h" -extern bool sound_device_stereo; // FIXME: encapsulation - // The following structs and PC Speaker Conversion routine are adapted from the // SLADE codebase, specifically // https://github.com/sirjuddington/SLADE/blob/master/src/MainEditor/Conversions.cpp @@ -232,15 +230,13 @@ bool LoadWAVSound(SoundData *buf, uint8_t *data, int length, bool pc_speaker) buf->frequency_ = wav.sampleRate; - buf->mode_ = sound_device_stereo ? kMixInterleaved : kMixMono; - SoundGatherer gather; float *buffer = gather.MakeChunk(wav.totalPCMFrameCount, is_stereo); gather.CommitChunk(drwav_read_pcm_frames_f32(&wav, wav.totalPCMFrameCount, buffer)); - if (!gather.Finalise(buf, sound_device_stereo)) + if (!gather.Finalise(buf)) LogWarning("WAV SFX Loader: no samples!\n"); drwav_uninit(&wav); diff --git a/source_files/edge/snd_data.cc b/source_files/edge/snd_data.cc index 2701bc79d..5954b4c9c 100644 --- a/source_files/edge/snd_data.cc +++ b/source_files/edge/snd_data.cc @@ -19,7 +19,7 @@ #include "snd_data.h" SoundData::SoundData() - : length_(0), frequency_(0), mode_(0), data_(nullptr), + : length_(0), frequency_(0), data_(nullptr), definition_data_(nullptr), is_sound_effect_(false) { } @@ -39,10 +39,10 @@ void SoundData::Free() data_ = nullptr; } -void SoundData::Allocate(int samples, int buf_mode) +void SoundData::Allocate(int samples) { // early out when requirements are already met - if (data_ && length_ >= samples && mode_ == buf_mode) + if (data_ && length_ >= samples) { length_ = samples; // FIXME: perhaps keep allocated count return; @@ -54,9 +54,8 @@ void SoundData::Allocate(int samples, int buf_mode) } length_ = samples; - mode_ = buf_mode; - data_ = new float[samples * (buf_mode == kMixMono ? 1 : 2)]; + data_ = new float[samples * 2]; } //--- editor settings --- diff --git a/source_files/edge/snd_data.h b/source_files/edge/snd_data.h index ef9ca014b..fd7bd45c2 100644 --- a/source_files/edge/snd_data.h +++ b/source_files/edge/snd_data.h @@ -20,12 +20,6 @@ #include -enum SoundBufferMix -{ - kMixMono = 0, - kMixInterleaved = 1 -}; - enum SoundFilter { kFilterNone = 0, @@ -47,7 +41,6 @@ class SoundData public: int length_; // number of samples int frequency_; // frequency - int mode_; // one of the kMixxxx values // 32-bit floating point samples. float *data_; @@ -61,7 +54,7 @@ class SoundData SoundData(); ~SoundData(); - void Allocate(int samples, int buf_mode); + void Allocate(int samples); void Free(); }; diff --git a/source_files/edge/snd_gather.cc b/source_files/edge/snd_gather.cc index ab06e197e..13413f4ce 100644 --- a/source_files/edge/snd_gather.cc +++ b/source_files/edge/snd_gather.cc @@ -96,22 +96,18 @@ void SoundGatherer::DiscardChunk() request_ = nullptr; } -bool SoundGatherer::Finalise(SoundData *buf, bool want_stereo) +bool SoundGatherer::Finalise(SoundData *buf) { if (total_samples_ == 0) return false; - buf->Allocate(total_samples_, want_stereo ? kMixInterleaved : kMixMono); + buf->Allocate(total_samples_); int pos = 0; for (unsigned int i = 0; i < chunks_.size(); i++) { - if (want_stereo) - TransferStereo(chunks_[i], buf, pos); - else - TransferMono(chunks_[i], buf, pos); - + TransferStereo(chunks_[i], buf, pos); pos += chunks_[i]->total_samples_; } @@ -120,27 +116,6 @@ bool SoundGatherer::Finalise(SoundData *buf, bool want_stereo) return true; } -void SoundGatherer::TransferMono(GatherChunk *chunk, SoundData *buf, int pos) -{ - int count = chunk->total_samples_; - - float *dest = buf->data_ + pos; - float *src = chunk->samples_; - - if (chunk->is_stereo_) - { - const float *dest_end = dest + count; - for (;dest < dest_end; src += 2) - { - *dest++ = (src[0] + src[1]) * 0.5f; - } - } - else - { - memcpy(dest, src, count * sizeof(float)); - } -} - void SoundGatherer::TransferStereo(GatherChunk *chunk, SoundData *buf, int pos) { int count = chunk->total_samples_; diff --git a/source_files/edge/snd_gather.h b/source_files/edge/snd_gather.h index 0480f3032..7b58e11c1 100644 --- a/source_files/edge/snd_gather.h +++ b/source_files/edge/snd_gather.h @@ -53,7 +53,7 @@ class SoundGatherer // get rid of current chunk (because it wasn't needed, e.g. // the sound file you were reading hit EOF). - bool Finalise(SoundData *buf, bool want_stereo); + bool Finalise(SoundData *buf); // take all the stored sound data and transfer it to the // SoundData object, making it all contiguous, and // converting from/to stereoness where needed. @@ -62,7 +62,6 @@ class SoundGatherer // otherwise returns true (success). private: - void TransferMono(GatherChunk *chunk, SoundData *buf, int pos); void TransferStereo(GatherChunk *chunk, SoundData *buf, int pos); }; From 25600885db00dddf31497af28f428ed77494cde9 Mon Sep 17 00:00:00 2001 From: dashodanger Date: Wed, 16 Oct 2024 21:32:06 -0600 Subject: [PATCH 5/6] Restore lowpass/reverb/echo SFX processing --- source_files/edge/s_blit.cc | 12 +-- source_files/edge/s_sound.cc | 13 +++ source_files/edge/snd_data.cc | 191 ++++++++++++++++++++++++++++++++-- source_files/edge/snd_data.h | 20 ++++ 4 files changed, 223 insertions(+), 13 deletions(-) diff --git a/source_files/edge/s_blit.cc b/source_files/edge/s_blit.cc index c3b511bc9..e103afb22 100644 --- a/source_files/edge/s_blit.cc +++ b/source_files/edge/s_blit.cc @@ -194,18 +194,18 @@ static void MixInterleaved(SoundChannel *chan, float *dest, int pairs) { EPI_ASSERT(pairs > 0); - float *src = chan->data_->data_; + float *src = nullptr; - /*if (paused || menu_active) - src_L = chan->data_->data_left_; + if (paused || menu_active) + src = chan->data_->data_; else { if (!chan->data_->is_sound_effect_ || chan->category_ == kCategoryUi || chan->data_->current_filter_ == kFilterNone) - src_L = chan->data_->data_left_; + src = chan->data_->data_; else - src_L = chan->data_->filter_data_left_; - }*/ + src = chan->data_->filter_data_; + } float *d_pos = dest; float *d_end = d_pos + pairs * (sound_device_stereo ? 2 : 1); diff --git a/source_files/edge/s_sound.cc b/source_files/edge/s_sound.cc index a53cac88f..d3326206e 100644 --- a/source_files/edge/s_sound.cc +++ b/source_files/edge/s_sound.cc @@ -452,6 +452,19 @@ void StartSoundEffect(SoundEffect *sfx, int category, Position *pos, int flags) if (!buf) return; + if (vacuum_sound_effects) + buf->MixVacuum(); + else if (submerged_sound_effects) + buf->MixSubmerged(); + else + { + if (ddf_reverb) + buf->MixReverb(dynamic_reverb, room_area, outdoor_reverb, ddf_reverb_type, ddf_reverb_ratio, + ddf_reverb_delay); + else + buf->MixReverb(dynamic_reverb, room_area, outdoor_reverb, 0, 0, 0); + } + LockAudio(); { DoStartFX(def, category, pos, flags, buf); diff --git a/source_files/edge/snd_data.cc b/source_files/edge/snd_data.cc index 5954b4c9c..6fdc00876 100644 --- a/source_files/edge/snd_data.cc +++ b/source_files/edge/snd_data.cc @@ -18,9 +18,18 @@ #include "snd_data.h" +#include + +#include "HandmadeMath.h" + +static constexpr float kVacuumLowpassFactor = 0.03125f; +static constexpr float kSubmergedLowpassFactor = 0.025f; + SoundData::SoundData() - : length_(0), frequency_(0), data_(nullptr), - definition_data_(nullptr), is_sound_effect_(false) + : length_(0), frequency_(0), data_(nullptr), filter_data_(nullptr), reverb_buffer_(nullptr), + definition_data_(nullptr), is_sound_effect_(false), current_filter_(kFilterNone), + reverbed_room_size_(kRoomReverbNone), current_ddf_reverb_ratio_(0), current_ddf_reverb_delay_(0), + current_ddf_reverb_type_(0), reverb_is_outdoors_(false) { } @@ -35,8 +44,15 @@ void SoundData::Free() if (data_) delete[] data_; - data_ = nullptr; + + if (filter_data_) + delete[] filter_data_; + filter_data_ = nullptr; + + if (reverb_buffer_) + delete[] reverb_buffer_; + reverb_buffer_ = nullptr; } void SoundData::Allocate(int samples) @@ -48,15 +64,176 @@ void SoundData::Allocate(int samples) return; } - if (data_) - { - Free(); - } + Free(); length_ = samples; data_ = new float[samples * 2]; } +void SoundData::MixSubmerged() +{ + if (current_filter_ != kFilterSubmerged) + { + // Setup lowpass + reverb parameters + float out_L = 0; + float accum_L = 0; + float out_R = 0; + float accum_R = 0; + int read_pos = ((-25 * frequency_ / 1000) + length_ * 2) % (length_ * 2); + int write_pos = 0; + if (!filter_data_) + filter_data_ = new float[length_ * 2]; + memset(filter_data_, 0, length_ * sizeof(float) * 2); + if (!reverb_buffer_) + reverb_buffer_ = new float[length_ * 2]; + memset(reverb_buffer_, 0, length_ * sizeof(float) * 2); + for (int i = 0; i < length_ * 2; i += 2) + { + out_L = accum_L * kSubmergedLowpassFactor; + accum_L = accum_L - out_L + data_[i]; + filter_data_[i] = out_L + reverb_buffer_[HMM_MAX(0, read_pos)] * 25 / 100; + reverb_buffer_[write_pos] = filter_data_[i]; + write_pos = (write_pos + 1) % (length_ * 2); + read_pos = (read_pos + 1) % (length_ * 2); + out_R = accum_R * kSubmergedLowpassFactor; + accum_R = accum_R - out_R + data_[i+1]; + filter_data_[i+1] = out_R + reverb_buffer_[HMM_MAX(0, read_pos)] * 25 / 100; + reverb_buffer_[write_pos] = filter_data_[i+1]; + write_pos = (write_pos + 1) % (length_ * 2); + read_pos = (read_pos + 1) % (length_ * 2); + } + current_filter_ = kFilterSubmerged; + } +} + +void SoundData::MixVacuum() +{ + if (current_filter_ != kFilterVacuum) + { + // Setup lowpass parameters + float out_L = 0; + float accum_L = 0; + float out_R = 0; + float accum_R = 0; + if (!filter_data_) + filter_data_ = new float[length_ * 2]; + memset(filter_data_, 0, length_ * sizeof(float) * 2); + for (int i = 0; i < length_ * 2; i += 2) + { + filter_data_[i] = out_L = accum_L * kVacuumLowpassFactor; + accum_L = accum_L - out_L + data_[i]; + filter_data_[i+1] = out_R = accum_R * kVacuumLowpassFactor; + accum_R = accum_R - out_R + data_[i+1]; + } + current_filter_ = kFilterVacuum; + } +} + +void SoundData::MixReverb(bool dynamic_reverb, float room_area, bool outdoor_reverb, int ddf_reverb_type, + int ddf_reverb_ratio, int ddf_reverb_delay) +{ + if (ddf_reverb_ratio > 0 && ddf_reverb_delay > 0 && ddf_reverb_type > 0) + { + if (current_filter_ != kFilterReverb || ddf_reverb_ratio != current_ddf_reverb_ratio_ || + ddf_reverb_delay != current_ddf_reverb_delay_ || ddf_reverb_type != current_ddf_reverb_type_) + { + // Setup reverb parameters + int write_pos = 0; + int read_pos = ((-ddf_reverb_delay * frequency_ / 1000) + length_ * 2) % (length_ * 2); + if (!filter_data_) + filter_data_ = new float[length_ * 2]; + memset(filter_data_, 0, length_ * sizeof(float) * 2); + if (!reverb_buffer_) + reverb_buffer_ = new float[length_ * 2]; + memset(reverb_buffer_, 0, length_ * sizeof(float) * 2); + for (int i = 0; i < length_ * 2; i++) + { + if (ddf_reverb_type == 2) + reverb_buffer_[write_pos] = data_[i]; + filter_data_[i] = data_[i] + reverb_buffer_[HMM_MAX(0, read_pos)] * ddf_reverb_ratio / 100; + if (ddf_reverb_type == 1) + reverb_buffer_[write_pos] = filter_data_[i]; + write_pos = (write_pos + 1) % (length_ * 2); + read_pos = (read_pos + 1) % (length_ * 2); + if (ddf_reverb_type == 2) + reverb_buffer_[write_pos] = data_[i+1]; + filter_data_[i+1] = data_[i+1] + reverb_buffer_[HMM_MAX(0, read_pos)] * ddf_reverb_ratio / 100; + if (ddf_reverb_type == 1) + reverb_buffer_[write_pos] = filter_data_[i+1]; + write_pos = (write_pos + 1) % (length_ * 2); + read_pos = (read_pos + 1) % (length_ * 2); + } + current_filter_ = kFilterReverb; + current_ddf_reverb_delay_ = ddf_reverb_delay; + current_ddf_reverb_ratio_ = ddf_reverb_ratio; + current_ddf_reverb_type_ = ddf_reverb_type; + reverbed_room_size_ = kRoomReverbNone; + } + } + else if (dynamic_reverb) + { + ReverbRoomSize current_room_size; + if (room_area > 700) + { + current_room_size = kRoomReverbLarge; + } + else if (room_area > 350) + { + current_room_size = kRoomReverbMedium; + } + else + { + current_room_size = kRoomReverbSmall; + } + if (current_filter_ != kFilterReverb || reverbed_room_size_ != current_room_size || + reverb_is_outdoors_ != outdoor_reverb) + { + // Setup reverb parameters + int reverb_ratio = outdoor_reverb ? 25 : 30; + int reverb_delay = 0; + if (outdoor_reverb) + reverb_delay = 50 * current_room_size + 25; + else + reverb_delay = 20 * current_room_size + 10; + int write_pos = 0; + int read_pos = ((-reverb_delay * frequency_ / 1000) + length_ * 2) % (length_ * 2); + if (!filter_data_) + filter_data_ = new float[length_ * 2]; + memset(filter_data_, 0, length_ * sizeof(float) * 2); + if (!reverb_buffer_) + reverb_buffer_ = new float[length_ * 2]; + memset(reverb_buffer_, 0, length_ * sizeof(float) * 2); + for (int i = 0; i < length_ * 2; i += 2) + { + if (outdoor_reverb) + reverb_buffer_[write_pos] = data_[i]; + filter_data_[i] = data_[i] + reverb_buffer_[HMM_MAX(0, read_pos)] * reverb_ratio / 100; + if (!outdoor_reverb) + reverb_buffer_[write_pos] = filter_data_[i]; + write_pos = (write_pos + 1) % (length_ * 2); + read_pos = (read_pos + 1) % (length_ * 2); + if (outdoor_reverb) + reverb_buffer_[write_pos] = data_[i+1]; + filter_data_[i+1] = data_[i+1] + reverb_buffer_[HMM_MAX(0, read_pos)] * reverb_ratio / 100; + if (!outdoor_reverb) + reverb_buffer_[write_pos] = filter_data_[i+1]; + write_pos = (write_pos + 1) % (length_ * 2); + read_pos = (read_pos + 1) % (length_ * 2); + } + current_filter_ = kFilterReverb; + reverbed_room_size_ = current_room_size; + if (outdoor_reverb) + reverb_is_outdoors_ = true; + else + reverb_is_outdoors_ = false; + } + } + else // Just use the original buffer - Dasho + { + current_filter_ = kFilterNone; + } +} + //--- editor settings --- // vi:ts=4:sw=4:noexpandtab diff --git a/source_files/edge/snd_data.h b/source_files/edge/snd_data.h index fd7bd45c2..270287bb9 100644 --- a/source_files/edge/snd_data.h +++ b/source_files/edge/snd_data.h @@ -45,17 +45,37 @@ class SoundData // 32-bit floating point samples. float *data_; + // Temp buffer for mixed SFX. Will be overwritten as needed. + float *filter_data_; + + // Circular buffer used for reverb processing, if needed + float *reverb_buffer_; + // values for the engine to use void *definition_data_; bool is_sound_effect_; + SoundFilter current_filter_; + + ReverbRoomSize reverbed_room_size_; + + int current_ddf_reverb_ratio_; + int current_ddf_reverb_delay_; + int current_ddf_reverb_type_; + + bool reverb_is_outdoors_; + public: SoundData(); ~SoundData(); void Allocate(int samples); void Free(); + void MixVacuum(); + void MixSubmerged(); + void MixReverb(bool dynamic_reverb, float room_area, bool outdoor_reverb, int ddf_reverb_type, int ddf_reverb_ratio, + int ddf_reverb_delay); }; //--- editor settings --- From d0ed06bd31b2b48a63be5cfd38efd7830ced7986 Mon Sep 17 00:00:00 2001 From: dashodanger Date: Wed, 16 Oct 2024 21:36:29 -0600 Subject: [PATCH 6/6] Blitter tweak --- source_files/edge/s_blit.cc | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/source_files/edge/s_blit.cc b/source_files/edge/s_blit.cc index e103afb22..f94f3497a 100644 --- a/source_files/edge/s_blit.cc +++ b/source_files/edge/s_blit.cc @@ -40,16 +40,6 @@ #include "s_music.h" #include "s_sound.h" -// Sound must be clipped to prevent distortion (clipping is -// a kind of distortion of course, but it's much better than -// the "white noise" you get when values overflow). -// -// The more safe bits there are, the less likely the final -// output sum will overflow into white noise, but the less -// precision you have left for the volume multiplier. -static constexpr float kSoundClipMaximum = 1.0f; -static constexpr float kSoundClipMinimum = -1.0f; - static constexpr uint8_t kMinimumSoundChannels = 32; static constexpr uint16_t kMaximumSoundChannels = 256; @@ -185,11 +175,6 @@ void SoundChannel::ComputeMusicVolume() //---------------------------------------------------------------------------- -static void BlitToF32(const float *src, float *dest, int length) -{ - memcpy(dest, src, length * sizeof(float)); -} - static void MixInterleaved(SoundChannel *chan, float *dest, int pairs) { EPI_ASSERT(pairs > 0); @@ -401,8 +386,8 @@ void MixAllSoundChannels(void *stream, int len) MixQueues(pairs); - // blit to the SDL stream - BlitToF32(mix_buffer, (float *)stream, samples); + // copy to the SDL stream + memcpy((float *)stream, mix_buffer, samples * sizeof(float)); } //----------------------------------------------------------------------------