stb_hexwave added, stretchy_buffer.h deprecated
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parent b42009b · Browse files · 9 files·+944 added−324 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+944 added−324 removed
- tests:+18 added−61 removed
- other:+926 added−263 removed
Provenance
- Commit
559d759- Tree
dfd3652- Author
Sean Barrett <seanb@radgametools.com>- Committer
Sean Barrett <seanb@radgametools.com>- Parent
b42009b
9 files changed+944 added−324 removed
added:deprecated/stretch_test.c+29 added
1+// check that stb_truetype compiles with no stb_rect_pack.h2+#define STB_TRUETYPE_IMPLEMENTATION3+#include "stb_truetype.h"4+5+#define STB_DS_IMPLEMENTATION6+#include "stb_ds.h"7+#include <assert.h>8+9+int main(int arg, char **argv)10+{11+ int i;12+ int *arr = NULL;13+14+ for (i=0; i < 1000000; ++i)15+ arrput(arr, i);16+17+ assert(arrlen(arr) == 1000000);18+ for (i=0; i < 1000000; ++i)19+ assert(arr[i] == i);20+21+ arrfree(arr);22+ arr = NULL;23+24+ for (i=0; i < 1000; ++i)25+ arrput(arr, 1000);26+ assert(arrlen(arr) == 1000000);27+28+ return 0;29+}added:deprecated/stretchy_buffer.h+263 added
1+// stretchy_buffer.h - v1.04 - public domain - nothings.org/stb2+// a vector<>-like dynamic array for C3+//4+// version history:5+// 1.04 - fix warning6+// 1.03 - compile as C++ maybe7+// 1.02 - tweaks to syntax for no good reason8+// 1.01 - added a "common uses" documentation section9+// 1.0 - fixed bug in the version I posted prematurely10+// 0.9 - rewrite to try to avoid strict-aliasing optimization11+// issues, but won't compile as C++12+//13+// Will probably not work correctly with strict-aliasing optimizations.14+//15+// The idea:16+//17+// This implements an approximation to C++ vector<> for C, in that it18+// provides a generic definition for dynamic arrays which you can19+// still access in a typesafe way using arr[i] or *(arr+i). However,20+// it is simply a convenience wrapper around the common idiom of21+// of keeping a set of variables (in a struct or globals) which store22+// - pointer to array23+// - the length of the "in-use" part of the array24+// - the current size of the allocated array25+//26+// I find it to be the single most useful non-built-in-structure when27+// programming in C (hash tables a close second), but to be clear28+// it lacks many of the capabilities of C++ vector<>: there is no29+// range checking, the object address isn't stable (see next section30+// for details), the set of methods available is small (although31+// the file stb.h has another implementation of stretchy buffers32+// called 'stb_arr' which provides more methods, e.g. for insertion33+// and deletion).34+//35+// How to use:36+//37+// Unlike other stb header file libraries, there is no need to38+// define an _IMPLEMENTATION symbol. Every #include creates as39+// much implementation is needed.40+//41+// stretchy_buffer.h does not define any types, so you do not42+// need to #include it to before defining data types that are43+// stretchy buffers, only in files that *manipulate* stretchy44+// buffers.45+//46+// If you want a stretchy buffer aka dynamic array containing47+// objects of TYPE, declare such an array as:48+//49+// TYPE *myarray = NULL;50+//51+// (There is no typesafe way to distinguish between stretchy52+// buffers and regular arrays/pointers; this is necessary to53+// make ordinary array indexing work on these objects.)54+//55+// Unlike C++ vector<>, the stretchy_buffer has the same56+// semantics as an object that you manually malloc and realloc.57+// The pointer may relocate every time you add a new object58+// to it, so you:59+//60+// 1. can't take long-term pointers to elements of the array61+// 2. have to return the pointer from functions which might expand it62+// (either as a return value or by storing it to a ptr-to-ptr)63+//64+// Now you can do the following things with this array:65+//66+// sb_free(TYPE *a) free the array67+// sb_count(TYPE *a) the number of elements in the array68+// sb_push(TYPE *a, TYPE v) adds v on the end of the array, a la push_back69+// sb_add(TYPE *a, int n) adds n uninitialized elements at end of array & returns pointer to first added70+// sb_last(TYPE *a) returns an lvalue of the last item in the array71+// a[n] access the nth (counting from 0) element of the array72+//73+// #define STRETCHY_BUFFER_NO_SHORT_NAMES to only export74+// names of the form 'stb_sb_' if you have a name that would75+// otherwise collide.76+//77+// Note that these are all macros and many of them evaluate78+// their arguments more than once, so the arguments should79+// be side-effect-free.80+//81+// Note that 'TYPE *a' in sb_push and sb_add must be lvalues82+// so that the library can overwrite the existing pointer if83+// the object has to be reallocated.84+//85+// In an out-of-memory condition, the code will try to86+// set up a null-pointer or otherwise-invalid-pointer87+// exception to happen later. It's possible optimizing88+// compilers could detect this write-to-null statically89+// and optimize away some of the code, but it should only90+// be along the failure path. Nevertheless, for more security91+// in the face of such compilers, #define STRETCHY_BUFFER_OUT_OF_MEMORY92+// to a statement such as assert(0) or exit(1) or something93+// to force a failure when out-of-memory occurs.94+//95+// Common use:96+//97+// The main application for this is when building a list of98+// things with an unknown quantity, either due to loading from99+// a file or through a process which produces an unpredictable100+// number.101+//102+// My most common idiom is something like:103+//104+// SomeStruct *arr = NULL;105+// while (something)106+// {107+// SomeStruct new_one;108+// new_one.whatever = whatever;109+// new_one.whatup = whatup;110+// new_one.foobar = barfoo;111+// sb_push(arr, new_one);112+// }113+//114+// and various closely-related factorings of that. For example,115+// you might have several functions to create/init new SomeStructs,116+// and if you use the above idiom, you might prefer to make them117+// return structs rather than take non-const-pointers-to-structs,118+// so you can do things like:119+//120+// SomeStruct *arr = NULL;121+// while (something)122+// {123+// if (case_A) {124+// sb_push(arr, some_func1());125+// } else if (case_B) {126+// sb_push(arr, some_func2());127+// } else {128+// sb_push(arr, some_func3());129+// }130+// }131+//132+// Note that the above relies on the fact that sb_push doesn't133+// evaluate its second argument more than once. The macros do134+// evaluate the *array* argument multiple times, and numeric135+// arguments may be evaluated multiple times, but you can rely136+// on the second argument of sb_push being evaluated only once.137+//138+// Of course, you don't have to store bare objects in the array;139+// if you need the objects to have stable pointers, store an array140+// of pointers instead:141+//142+// SomeStruct **arr = NULL;143+// while (something)144+// {145+// SomeStruct *new_one = malloc(sizeof(*new_one));146+// new_one->whatever = whatever;147+// new_one->whatup = whatup;148+// new_one->foobar = barfoo;149+// sb_push(arr, new_one);150+// }151+//152+// How it works:153+//154+// A long-standing tradition in things like malloc implementations155+// is to store extra data before the beginning of the block returned156+// to the user. The stretchy buffer implementation here uses the157+// same trick; the current-count and current-allocation-size are158+// stored before the beginning of the array returned to the user.159+// (This means you can't directly free() the pointer, because the160+// allocated pointer is different from the type-safe pointer provided161+// to the user.)162+//163+// The details are trivial and implementation is straightforward;164+// the main trick is in realizing in the first place that it's165+// possible to do this in a generic, type-safe way in C.166+//167+// Contributors:168+//169+// Timothy Wright (github:ZenToad)170+//171+// LICENSE172+//173+// See end of file for license information.174+175+#ifndef STB_STRETCHY_BUFFER_H_INCLUDED176+#define STB_STRETCHY_BUFFER_H_INCLUDED177+178+#ifndef NO_STRETCHY_BUFFER_SHORT_NAMES179+#define sb_free stb_sb_free180+#define sb_push stb_sb_push181+#define sb_count stb_sb_count182+#define sb_add stb_sb_add183+#define sb_last stb_sb_last184+#endif185+186+#define stb_sb_free(a) ((a) ? free(stb__sbraw(a)),0 : 0)187+#define stb_sb_push(a,v) (stb__sbmaybegrow(a,1), (a)[stb__sbn(a)++] = (v))188+#define stb_sb_count(a) ((a) ? stb__sbn(a) : 0)189+#define stb_sb_add(a,n) (stb__sbmaybegrow(a,n), stb__sbn(a)+=(n), &(a)[stb__sbn(a)-(n)])190+#define stb_sb_last(a) ((a)[stb__sbn(a)-1])191+192+#define stb__sbraw(a) ((int *) (void *) (a) - 2)193+#define stb__sbm(a) stb__sbraw(a)[0]194+#define stb__sbn(a) stb__sbraw(a)[1]195+196+#define stb__sbneedgrow(a,n) ((a)==0 || stb__sbn(a)+(n) >= stb__sbm(a))197+#define stb__sbmaybegrow(a,n) (stb__sbneedgrow(a,(n)) ? stb__sbgrow(a,n) : 0)198+#define stb__sbgrow(a,n) (*((void **)&(a)) = stb__sbgrowf((a), (n), sizeof(*(a))))199+200+#include <stdlib.h>201+202+static void * stb__sbgrowf(void *arr, int increment, int itemsize)203+{204+ int dbl_cur = arr ? 2*stb__sbm(arr) : 0;205+ int min_needed = stb_sb_count(arr) + increment;206+ int m = dbl_cur > min_needed ? dbl_cur : min_needed;207+ int *p = (int *) realloc(arr ? stb__sbraw(arr) : 0, itemsize * m + sizeof(int)*2);208+ if (p) {209+ if (!arr)210+ p[1] = 0;211+ p[0] = m;212+ return p+2;213+ } else {214+ #ifdef STRETCHY_BUFFER_OUT_OF_MEMORY215+ STRETCHY_BUFFER_OUT_OF_MEMORY ;216+ #endif217+ return (void *) (2*sizeof(int)); // try to force a NULL pointer exception later218+ }219+}220+#endif // STB_STRETCHY_BUFFER_H_INCLUDED221+222+223+/*224+------------------------------------------------------------------------------225+This software is available under 2 licenses -- choose whichever you prefer.226+------------------------------------------------------------------------------227+ALTERNATIVE A - MIT License228+Copyright (c) 2017 Sean Barrett229+Permission is hereby granted, free of charge, to any person obtaining a copy of230+this software and associated documentation files (the "Software"), to deal in231+the Software without restriction, including without limitation the rights to232+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies233+of the Software, and to permit persons to whom the Software is furnished to do234+so, subject to the following conditions:235+The above copyright notice and this permission notice shall be included in all236+copies or substantial portions of the Software.237+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR238+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,239+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE240+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER241+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,242+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE243+SOFTWARE.244+------------------------------------------------------------------------------245+ALTERNATIVE B - Public Domain (www.unlicense.org)246+This is free and unencumbered software released into the public domain.247+Anyone is free to copy, modify, publish, use, compile, sell, or distribute this248+software, either in source code form or as a compiled binary, for any purpose,249+commercial or non-commercial, and by any means.250+In jurisdictions that recognize copyright laws, the author or authors of this251+software dedicate any and all copyright interest in the software to the public252+domain. We make this dedication for the benefit of the public at large and to253+the detriment of our heirs and successors. We intend this dedication to be an254+overt act of relinquishment in perpetuity of all present and future rights to255+this software under copyright law.256+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR257+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,258+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE259+AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN260+ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION261+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.262+------------------------------------------------------------------------------263+*/modified:tests/stb.dsp+1 added-9 removed
64 unmodified lines65 # PROP Intermediate_Dir "Debug\stb"66 # PROP Ignore_Export_Lib 067 # PROP Target_Dir ""68 # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c69-# ADD CPP /nologo /MTd /W3 /GX /Zi /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "DS_TEST" /FR /FD /GZ /c69+# ADD CPP /nologo /MTd /W3 /GX /Zi /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "TT_TEST" /FR /FD /GZ /c70 # SUBTRACT CPP /YX71 # ADD BASE RSC /l 0x409 /d "_DEBUG"72 # ADD RSC /l 0x409 /d "_DEBUG"73 BSC32=bscmake.exe119 unmodified lines193 SOURCE=..\stb_voxel_render.h194 # End Source File195 # Begin Source File196 197-SOURCE=..\stretchy_buffer.h198-# End Source File199-# Begin Source File200-201-SOURCE=.\stretchy_buffer_test.c202-# End Source File203-# Begin Source File204-197 SOURCE=.\test_c_compilation.c198 # End Source File199 # Begin Source File200 52 unmodified linesmodified:tests/stb.dsw-15 removed
88 unmodified lines89 Begin Project Dependency90 Project_Dep_Name image_test91 End Project Dependency92 Begin Project Dependency93- Project_Dep_Name stretch_test94- End Project Dependency95- Begin Project Dependency93 Project_Dep_Name c_lexer_test94 End Project Dependency95 }}}96 22 unmodified lines119 }}}120 121 ###############################################################################122 126-Project: "stretch_test"=.\stretch_test.dsp - Package Owner=<4>127-128-Package=<5>129-{{{130-}}}131-132-Package=<4>133-{{{134-}}}135-136-###############################################################################137-123 Project: "unicode"=..\tools\unicode\unicode.dsp - Package Owner=<4>124 125 Package=<5>126 {{{32 unmodified linesdeleted:tests/stretch_test.c-28 removed
1-// check that stb_truetype compiles with no stb_rect_pack.h2-#define STB_TRUETYPE_IMPLEMENTATION3-#include "stb_truetype.h"4-5-#include "stretchy_buffer.h"6-#include <assert.h>7-8-int main(int arg, char **argv)9-{10- int i;11- int *arr = NULL;12-13- for (i=0; i < 1000000; ++i)14- sb_push(arr, i);15-16- assert(sb_count(arr) == 1000000);17- for (i=0; i < 1000000; ++i)18- assert(arr[i] == i);19-20- sb_free(arr);21- arr = NULL;22-23- for (i=0; i < 1000; ++i)24- sb_add(arr, 1000);25- assert(sb_count(arr) == 1000000);26-27- return 0;28-}deleted:tests/stretchy_buffer_test.c-7 removed
1-#include "stretchy_buffer.h"2-3-void test_sb(void)4-{5- char *x = NULL;6- sb_push(x, 'x');7-}modified:tests/test_cpp_compilation.cpp+17 added-2 removed
1+#define STB_IMAGE_WRITE_STATIC2+#define STBIWDEF static inline3+4+#include "stb_image.h"5+#include "stb_rect_pack.h"6+#include "stb_truetype.h"7+#include "stb_image_write.h"8+#include "stb_perlin.h"9+#include "stb_dxt.h"10+#include "stb_c_lexer.h"11+#include "stb_divide.h"12+#include "stb_herringbone_wang_tile.h"13+#include "stb_ds.h"14+#include "stb_hexwave.h"15+16 #include "stb_sprintf.h"17 #define STB_SPRINTF_IMPLEMENTATION18 #include "stb_sprintf.h"19 5-#define STB_IMAGE_WRITE_STATIC6-#define STBIWDEF static inline20 21 #define STB_IMAGE_WRITE_IMPLEMENTATION22 #define STB_TRUETYPE_IMPLEMENTATION23 #define STB_PERLIN_IMPLEMENTATION4 unmodified lines28 #define STB_HERRINGBONE_WANG_TILE_IMPLEMENTATION29 #define STB_RECT_PACK_IMPLEMENTATION30 #define STB_VOXEL_RENDER_IMPLEMENTATION31 #define STB_CONNECTED_COMPONENTS_IMPLEMENTATION32+#define STB_HEXWAVE_IMPLEMENTATION33 #define STB_DS_IMPLEMENTATION34 #define STBDS_UNIT_TESTS35 36 #define STBI_MALLOC my_malloc13 unmodified lines50 #include "stb_c_lexer.h"51 #include "stb_divide.h"52 #include "stb_herringbone_wang_tile.h"53 #include "stb_ds.h"54+#include "stb_hexwave.h"55 56 #define STBCC_GRID_COUNT_X_LOG2 1057 #define STBCC_GRID_COUNT_Y_LOG2 1058 #include "stb_connected_components.h"127 unmodified linesadded:stb_hexwave.h+634 added
1+// stb_hexwave - v0.5 - public domain, initial release 2021-04-012+//3+// A flexible anti-aliased (bandlimited) digital audio oscillator.4+//5+// This library generates waveforms of a variety of shapes made of6+// line segments. It does not do envelopes, LFO effects, etc.; it7+// merely tries to solve the problem of generating an artifact-free8+// morphable digital waveform with a variety of spectra, and leaves9+// it to the user to rescale the waveform and mix multiple voices, etc.10+//11+// Compiling:12+//13+// In one C/CPP file that #includes this file, do14+//15+// #define STB_HEXWAVE_IMPLEMENTATION16+// #include "stb_hexwave.h"17+//18+// Optionally, #define STB_HEXWAVE_STATIC before including19+// the header to cause the definitions to be private to the20+// implementation file (i.e. to be "static" instead of "extern").21+//22+// Notes:23+//24+// Optionally performs memory allocation during initialization,25+// never allocates otherwise.26+//27+// Usage:28+//29+// Initialization:30+//31+// hexwave_init(32,16,NULL); // read "header section" for alternatives32+//33+// Create oscillator:34+//35+// HexWave *osc = malloc(sizeof(*osc)); // or "new HexWave", or declare globally or on stack36+// hexwave_create(osc, reflect_flag, peak_time, half_height, zero_wait);37+// see "Waveform shapes" below for the meaning of these parameters38+//39+// Generate audio:40+//41+// hexwave_generate_samples(output, number_of_samples, osc, oscillator_freq)42+// where:43+// output is a buffer where the library will store floating point audio samples44+// number_of_samples is the number of audio samples to generate45+// osc is a pointer to a Hexwave46+// oscillator_freq is the frequency of the oscillator divided by the sample rate47+//48+// The output samples will continue from where the samples generated by the49+// previous hexwave_generate_samples() on this oscillator ended.50+//51+// Change oscillator waveform:52+//53+// hexwave_change(osc, reflect_flag, peak_time, half_height, zero_wait);54+// can call in between calls to hexwave_generate_samples55+//56+// Waveform shapes:57+//58+// All waveforms generated by hexwave are constructed from six line segments59+// characterized by 3 parameters.60+//61+// See demonstration: https://www.youtube.com/watch?v=hsUCrAsDN-M62+//63+// reflect=0 reflect=164+//65+// 0-----P---1 0-----P---1 peak_time = P66+// . 1 . 167+// /\_ : /\_ :68+// / \_ : / \_ :69+// / \.H / \.H half_height = H70+// / | : / | :71+// _____/ |_:___ _____/ | : _____72+// . : \ | . | : /73+// . : \ | . | : /74+// . : \ _/ . \_: /75+// . : \ _/ . :_ /76+// . -1 \/ . -1 \/77+// 0 - Z - - - - 1 0 - Z - - - - 1 zero_wait = Z78+//79+// Classic waveforms:80+// peak half zero81+// reflect time height wait82+// Sawtooth 1 0 0 083+// Square 1 0 1 084+// Triangle 1 0.5 0 085+//86+// Some waveforms can be produced in multiple ways, which is useful when morphing87+// into other waveforms, and there are a few more notable shapes:88+//89+// peak half zero90+// reflect time height wait91+// Sawtooth 1 1 any 092+// Sawtooth (8va) 1 0 -1 093+// Triangle 1 0.5 0 094+// Square 1 0 1 095+// Square 0 0 1 096+// Triangle 0 0.5 0 097+// Triangle 0 0 -1 098+// AlternatingSaw 0 0 0 099+// AlternatingSaw 0 1 any 0100+// Stairs 0 0 1 0.5101+//102+// The "Sawtooth (8va)" waveform is identical to a sawtooth wave with 2x the103+// frequency, but when morphed with other values, it becomes an overtone of104+// the base frequency.105+//106+// Morphing waveforms:107+//108+// Sweeping peak_time morphs the waveform while producing various spectra.109+// Sweeping half_height effectively crossfades between two waveforms; useful, but less exciting.110+// Sweeping zero_wait produces a similar effect no matter the reset of the waveform,111+// a sort of high-pass/PWM effect where the wave becomes silent at zero_wait=1.112+//113+// You can trivially morph between any two waveforms from the above table114+// which only differ in one column.115+//116+// Crossfade between classic waveforms:117+// peak half zero118+// Start End reflect time height wait119+// ----- --- ------- ---- ------ ----120+// Triangle Square 0 0 -1..1 0121+// Saw Square 1 0 0..1 0122+// Triangle Saw 1 0.5 0..2 0123+//124+// The last morph uses uses half-height values larger than 1, which means it will125+// be louder and the output should be scaled down by half to compensate, or better126+// by dynamically tracking the morph: volume_scale = 1 - half_height/4127+//128+// Non-crossfade morph between classic waveforms, most require changing129+// two parameters at the same time:130+// peak half zero131+// Start End reflect time height wait132+// ----- --- ------- ---- ------ ----133+// Square Triangle any 0..0.5 1..0 0134+// Square Saw 1 0..1 1..any 0135+// Triangle Saw 1 0.5..1 0..-1 0136+//137+// Other noteworthy morphs between simple shapes:138+// peak half zero139+// Start Halfway End reflect time height wait140+// ----- --------- --- ------- ---- ------ ----141+// Saw (8va,neg) Saw (pos) 1 0..1 -1 0142+// Saw (neg) Saw (pos) 1 0..1 0 0143+// Triangle AlternatingSaw 0 0..1 -1 0144+// AlternatingSaw Triangle AlternatingSaw 0 0..1 0 0145+// Square AlternatingSaw 0 0..1 1 0146+// Triangle Triangle AlternatingSaw 0 0..1 -1..1 0147+// Square AlternatingSaw 0 0..1 1..0 0148+// Saw (8va) Triangle Saw 1 0..1 -1..1 0149+// Saw (neg) Saw (pos) 1 0..1 0..1 0150+// AlternatingSaw AlternatingSaw 0 0..1 0..any 0151+//152+// The last entry is noteworthy because the morph from the halfway point to either153+// endpoint sounds very different. For example, an LFO sweeping back and forth over154+// the whole range will morph between the middle timbre and the AlternatingSaw155+// timbre in two different ways, alternating.156+//157+// Entries with "any" for half_height are whole families of morphs, as you can pick158+// any value you want as the endpoint for half_height.159+//160+// You can always morph between any two waveforms with the same value of 'reflect'161+// by just sweeping the parameters simultaneously. There will never be artifacts162+// and the result will always be useful, if not necessarily what you want.163+//164+// You can vary the sound of two-parameter morphs by ramping them differently,165+// e.g. if the morph goes from t=0..1, then square-to-triangle looks like:166+// peak_time = lerp(t, 0, 0.5)167+// half_height = lerp(t, 1, 0 )168+// but you can also do things like:169+// peak_time = lerp(smoothstep(t), 0, 0.5)170+// half_height = cos(PI/2 * t)171+//172+// How it works:173+//174+// hexwave use BLEP to bandlimit discontinuities and BLAMP175+// to bandlimit C1 discontinuities. This is not polyBLEP176+// (polynomial BLEP), it is table-driven BLEP. It is177+// also not minBLEP (minimum-phase BLEP), as that complicates178+// things for little benefit once BLAMP is involved.179+//180+// The previous oscillator frequency is remembered, and when181+// the frequency changes, a BLAMP is generated to remove the182+// C1 discontinuity, which reduces artifacts for sweeps/LFO.183+//184+// Changes to an oscillator timbre using hexwave_change() actually185+// wait until the oscillator finishes its current cycle. All186+// waveforms with non-zero "zero_wait" settings pass through 0187+// and have 0-slope at the start of a cycle, which means changing188+// the settings is artifact free at that time. (If zero_wait is 0,189+// the code still treats it as passing through 0 with 0-slope; it'll190+// apply the necessary fixups to make it artifact free as if it does191+// transition to 0 with 0-slope vs. the waveform at the end of192+// the cycle, then adds the fixups for a non-0 and non-0 slope193+// at the start of the cycle, which cancels out if zero_wait is 0,194+// and still does the right thing if zero_wait is 0 when the195+// settings are updated.)196+//197+// BLEP/BLAMP normally requires overlapping buffers, but this198+// is hidden from the user by generating the waveform to a199+// temporary buffer and saving the overlap regions internally200+// between calls. (It is slightly more complicated; see code.)201+//202+// By design all shapes have 0 DC offset; this is one reason203+// hexwave uses zero_wait instead of standard PWM.204+//205+// The internals of hexwave could support any arbitrary shape206+// made of line segments, but I chose not to expose this207+// generality in favor of a simple, easy-to-use API.208+209+#ifndef STB_INCLUDE_STB_HEXWAVE_H210+#define STB_INCLUDE_STB_HEXWAVE_H211+212+#ifndef STB_HEXWAVE_MAX_BLEP_LENGTH213+#define STB_HEXWAVE_MAX_BLEP_LENGTH 64 // good enough for anybody214+#endif215+216+#ifdef STB_HEXWAVE_STATIC217+#define STB_HEXWAVE_DEF static218+#else219+#define STB_HEXWAVE_DEF extern220+#endif221+222+typedef struct HexWave HexWave;223+224+STB_HEXWAVE_DEF void hexwave_init(int width, int oversample, float *user_buffer);225+// width: size of BLEP, from 4..64, larger is slower & more memory but less aliasing226+// oversample: 2+, number of subsample positions, larger uses more memory but less noise227+// user_buffer: optional, if provided the library will perform no allocations.228+// 16*width*(oversample+1) bytes, must stay allocated as long as library is used229+// technically it only needs: 8*( width * (oversample + 1))230+// + 8*((width * oversample) + 1) bytes231+//232+// width can be larger than 64 if you define STB_HEXWAVE_MAX_BLEP_LENGTH to a larger value233+234+STB_HEXWAVE_DEF void hexwave_shutdown(float *user_buffer);235+// user_buffer: pass in same parameter as passed to hexwave_init236+237+STB_HEXWAVE_DEF void hexwave_create(HexWave *hex, int reflect, float peak_time, float half_height, float zero_wait);238+// see docs above for description239+//240+// reflect is tested as 0 or non-zero241+// peak_time is clamped to 0..1242+// half_height is not clamped243+// zero_wait is clamped to 0..1244+245+STB_HEXWAVE_DEF void hexwave_change(HexWave *hex, int reflect, float peak_time, float half_height, float zero_wait);246+// see docs247+248+STB_HEXWAVE_DEF void hexwave_generate_samples(float *output, int num_samples, HexWave *hex, float freq);249+// output: buffer where the library will store generated floating point audio samples250+// number_of_samples: the number of audio samples to generate251+// osc: pointer to a Hexwave initialized with 'hexwave_create'252+// oscillator_freq: frequency of the oscillator divided by the sample rate253+254+// private:255+typedef struct256+{257+ int reflect;258+ float peak_time;259+ float zero_wait;260+ float half_height;261+} HexWaveParameters;262+263+struct HexWave264+{265+ float t, prev_dt;266+ HexWaveParameters current, pending;267+ int have_pending;268+ float buffer[STB_HEXWAVE_MAX_BLEP_LENGTH];269+}; 270+#endif271+272+#ifdef STB_HEXWAVE_IMPLEMENTATION273+274+#ifndef STB_HEXWAVE_NO_ALLOCATION275+#include <stdlib.h> // malloc,free276+#endif277+278+#include <string.h> // memset,memcpy,memmove279+#include <math.h> // sin,cos,fabs280+281+#define hexwave_clamp(v,a,b) ((v) < (a) ? (a) : (v) > (b) ? (b) : (v))282+283+STB_HEXWAVE_DEF void hexwave_change(HexWave *hex, int reflect, float peak_time, float half_height, float zero_wait)284+{285+ hex->pending.reflect = reflect;286+ hex->pending.peak_time = hexwave_clamp(peak_time,0,1);287+ hex->pending.half_height = half_height;288+ hex->pending.zero_wait = hexwave_clamp(zero_wait,0,1);289+ // put a barrier here to allow changing from a different thread than the generator290+ hex->have_pending = 1;291+}292+293+STB_HEXWAVE_DEF void hexwave_create(HexWave *hex, int reflect, float peak_time, float half_height, float zero_wait)294+{295+ memset(hex, 0, sizeof(*hex));296+ hexwave_change(hex, reflect, peak_time, half_height, zero_wait);297+ hex->current = hex->pending;298+ hex->have_pending = 0;299+ hex->t = 0;300+ hex->prev_dt = 0;301+}302+303+static struct304+{305+ int width; // width of fixup in samples306+ int oversample; // number of oversampled versions (there's actually one more to allow lerpign)307+ float *blep;308+ float *blamp;309+} hexblep;310+311+static void hex_add_oversampled_bleplike(float *output, float time_since_transition, float scale, float *data)312+{313+ float *d1,*d2;314+ float lerpweight;315+ int i, bw = hexblep.width;316+317+ int slot = (int) (time_since_transition * hexblep.oversample);318+ if (slot >= hexblep.oversample)319+ slot = hexblep.oversample-1; // clamp in case the floats overshoot320+321+ d1 = &data[ slot *bw];322+ d2 = &data[(slot+1)*bw];323+324+ lerpweight = time_since_transition * hexblep.oversample - slot;325+ for (i=0; i < bw; ++i)326+ output[i] += scale * (d1[i] + (d2[i]-d1[i])*lerpweight);327+}328+329+static void hex_blep (float *output, float time_since_transition, float scale)330+{331+ hex_add_oversampled_bleplike(output, time_since_transition, scale, hexblep.blep);332+}333+334+static void hex_blamp(float *output, float time_since_transition, float scale)335+{336+ hex_add_oversampled_bleplike(output, time_since_transition, scale, hexblep.blamp);337+}338+339+typedef struct340+{341+ float t,v,s; // time, value, slope342+} hexvert;343+344+// each half of the waveform needs 4 vertices to represent 3 line345+// segments, plus 1 more for wraparound346+static void hexwave_generate_linesegs(hexvert vert[9], HexWave *hex, float dt)347+{348+ int j;349+ float min_len = dt / 256.0f;350+351+ vert[0].t = 0;352+ vert[0].v = 0;353+ vert[1].t = hex->current.zero_wait*0.5f;354+ vert[1].v = 0;355+ vert[2].t = 0.5f*hex->current.peak_time + vert[1].t*(1-hex->current.peak_time);356+ vert[2].v = 1;357+ vert[3].t = 0.5f;358+ vert[3].v = hex->current.half_height;359+360+ if (hex->current.reflect) {361+ for (j=4; j <= 7; ++j) {362+ vert[j].t = 1 - vert[7-j].t;363+ vert[j].v = - vert[7-j].v;364+ }365+ } else {366+ for (j=4; j <= 7; ++j) {367+ vert[j].t = 0.5f + vert[j-4].t;368+ vert[j].v = - vert[j-4].v;369+ }370+ }371+ vert[8].t = 1;372+ vert[8].v = 0;373+374+ for (j=0; j < 8; ++j) {375+ if (vert[j+1].t <= vert[j].t + min_len) {376+ // if change takes place over less than a fraction of a sample treat as discontinuity377+ //378+ // otherwise the slope computation can blow up to arbitrarily large and we379+ // try to generate a huge BLAMP and the result is wrong.380+ // 381+ // why does this happen if the math is right? i believe if done perfectly,382+ // the two BLAMPs on either side of the slope would cancel out, but our383+ // BLAMPs have only limited sub-sample precision and limited integration384+ // accuracy. or maybe it's just the math blowing up w/ floating point precision385+ // limits as we try to make x * (1/x) cancel out386+ //387+ // min_len verified artifact-free even near nyquist with only oversample=4388+ vert[j+1].t = vert[j].t;389+ }390+ }391+392+ if (vert[8].t != 1.0f) {393+ // if the above fixup moved the endpoint away from 1.0, move it back,394+ // along with any other vertices that got moved to the same time395+ float t = vert[8].t;396+ for (j=5; j <= 8; ++j)397+ if (vert[j].t == t)398+ vert[j].t = 1.0f;399+ }400+401+ // compute the exact slopes from the final fixed-up positions402+ for (j=0; j < 8; ++j)403+ if (vert[j+1].t == vert[j].t)404+ vert[j].s = 0;405+ else406+ vert[j].s = (vert[j+1].v - vert[j].v) / (vert[j+1].t - vert[j].t);407+408+ // wraparound at end409+ vert[8].t = 1;410+ vert[8].v = vert[0].v;411+ vert[8].s = vert[0].s;412+}413+414+STB_HEXWAVE_DEF void hexwave_generate_samples(float *output, int num_samples, HexWave *hex, float freq)415+{416+ hexvert vert[9];417+ int pass,i,j;418+ float t = hex->t;419+ float temp_output[2*STB_HEXWAVE_MAX_BLEP_LENGTH];420+ int buffered_length = sizeof(float)*hexblep.width;421+ float dt = (float) fabs(freq);422+ float recip_dt = (dt == 0.0f) ? 0.0f : 1.0f / dt;423+424+ int halfw = hexblep.width/2;425+ // all sample times are biased by halfw to leave room for BLEP/BLAMP to go back in time426+427+ if (num_samples <= 0)428+ return;429+430+ // convert parameters to times and slopes431+ hexwave_generate_linesegs(vert, hex, dt);432+433+ if (hex->prev_dt != dt) {434+ // if frequency changes, add a fixup at the derivative discontinuity starting at now435+ float slope;436+ for (j=1; j < 6; ++j)437+ if (t < vert[j].t)438+ break;439+ slope = vert[j].s;440+ if (slope != 0)441+ hex_blamp(output, 0, (dt - hex->prev_dt)*slope);442+ hex->prev_dt = dt;443+ }444+445+ // copy the buffered data from last call and clear the rest of the output array446+ memset(output, 0, sizeof(float)*num_samples);447+ memset(temp_output, 0, 2*hexblep.width*sizeof(float));448+449+ if (num_samples >= hexblep.width) {450+ memcpy(output, hex->buffer, buffered_length);451+ } else {452+ // if the output is shorter than hexblep.width, we do all synthesis to temp_output453+ memcpy(temp_output, hex->buffer, buffered_length);454+ }455+456+ for (pass=0; pass < 2; ++pass) {457+ int i0,i1;458+ float *out;459+460+ // we want to simulate having one buffer that is num_output + hexblep.width461+ // samples long, without putting that requirement on the user, and without462+ // allocating a temp buffer that's as long as the whole thing. so we use two463+ // overlapping buffers, one the user's buffer and one a fixed-length temp464+ // buffer.465+466+ if (pass == 0) {467+ if (num_samples < hexblep.width)468+ continue;469+ // run as far as we can without overwriting the end of the user's buffer 470+ out = output;471+ i0 = 0;472+ i1 = num_samples - hexblep.width;473+ } else {474+ // generate the rest into a temp buffer475+ out = temp_output;476+ i0 = 0;477+ if (num_samples >= hexblep.width)478+ i1 = hexblep.width;479+ else480+ i1 = num_samples;481+ }482+483+ // determine current segment484+ for (j=0; j < 8; ++j)485+ if (t < vert[j+1].t) 486+ break;487+488+ i = i0;489+ for(;;) {490+ while (t < vert[j+1].t) {491+ if (i == i1)492+ goto done;493+ out[i+halfw] += vert[j].v + vert[j].s*(t - vert[j].t);494+ t += dt;495+ ++i;496+ }497+ // transition from lineseg starting at j to lineseg starting at j+1498+499+ if (vert[j].t == vert[j+1].t)500+ hex_blep(out+i, recip_dt*(t-vert[j+1].t), (vert[j+1].v - vert[j].v));501+ hex_blamp(out+i, recip_dt*(t-vert[j+1].t), dt*(vert[j+1].s - vert[j].s));502+ ++j;503+504+ if (j == 8) {505+ // change to different waveform if there's a change pending506+ j = 0;507+ t -= 1.0; // t was >= 1.f if j==8508+ if (hex->have_pending) {509+ float prev_s0 = vert[j].s;510+ float prev_v0 = vert[j].v;511+ hex->current = hex->pending;512+ hex->have_pending = 0;513+ hexwave_generate_linesegs(vert, hex, dt);514+ // the following never occurs with this oscillator, but it makes515+ // the code work in more general cases516+ if (vert[j].v != prev_v0)517+ hex_blep (out+i, recip_dt*t, (vert[j].v - prev_v0));518+ if (vert[j].s != prev_s0)519+ hex_blamp(out+i, recip_dt*t, dt*(vert[j].s - prev_s0));520+ }521+ }522+ }523+ done:524+ ;525+ }526+527+ // at this point, we've written output[] and temp_output[]528+ if (num_samples >= hexblep.width) {529+ // the first half of temp[] overlaps the end of output, the second half will be the new start overlap530+ for (i=0; i < hexblep.width; ++i)531+ output[num_samples-hexblep.width + i] += temp_output[i];532+ memcpy(hex->buffer, temp_output+hexblep.width, buffered_length);533+ } else {534+ for (i=0; i < num_samples; ++i)535+ output[i] = temp_output[i];536+ memcpy(hex->buffer, temp_output+num_samples, buffered_length);537+ }538+539+ hex->t = t;540+}541+542+STB_HEXWAVE_DEF void hexwave_shutdown(float *user_buffer)543+{544+ #ifndef STB_HEXWAVE_NO_ALLOCATION545+ if (user_buffer != 0) {546+ free(hexblep.blep);547+ free(hexblep.blamp);548+ }549+ #endif550+}551+552+// buffer should be NULL or must be 4*(width*(oversample+1)*2 + 553+STB_HEXWAVE_DEF void hexwave_init(int width, int oversample, float *user_buffer)554+{555+ int halfwidth = width/2;556+ int half = halfwidth*oversample;557+ int blep_buffer_count = width*(oversample+1);558+ int n = 2*half+1;559+#ifdef STB_HEXWAVE_NO_ALLOCATION560+ float *buffers = user_buffer;561+#else562+ float *buffers = user_buffer ? user_buffer : (float *) malloc(sizeof(float) * n * 2);563+#endif564+ float *step = buffers+0*n;565+ float *ramp = buffers+1*n;566+ float *blep_buffer, *blamp_buffer;567+ double integrate_impulse=0, integrate_step=0;568+ int i,j;569+570+ if (width > STB_HEXWAVE_MAX_BLEP_LENGTH)571+ width = STB_HEXWAVE_MAX_BLEP_LENGTH;572+573+ if (user_buffer == 0) {574+ #ifndef STB_HEXWAVE_NO_ALLOCATION575+ blep_buffer = (float *) malloc(sizeof(float)*blep_buffer_count);576+ blamp_buffer = (float *) malloc(sizeof(float)*blep_buffer_count);577+ #endif578+ } else {579+ blep_buffer = ramp+n;580+ blamp_buffer = blep_buffer + blep_buffer_count;581+ }582+583+ // compute BLEP and BLAMP by integerating windowed sinc584+ for (i=0; i < n; ++i) {585+ for (j=0; j < 16; ++j) {586+ float sinc_t = 3.141592f* (i-half) / oversample;587+ float sinc = (i==half) ? 1.0f : (float) sin(sinc_t) / (sinc_t);588+ float wt = 2.0f*3.1415926f * i / (n-1);589+ float window = (float) (0.355768 - 0.487396*cos(wt) + 0.144232*cos(2*wt) - 0.012604*cos(3*wt)); // Nuttall590+ double value = window * sinc;591+ integrate_impulse += value/16;592+ integrate_step += integrate_impulse/16;593+ }594+ step[i] = (float) integrate_impulse;595+ ramp[i] = (float) integrate_step;596+ }597+598+ // renormalize599+ for (i=0; i < n; ++i) {600+ step[i] = step[i] * (float) (1.0 / step[n-1]); // step needs to reach to 1.0601+ ramp[i] = ramp[i] * (float) (halfwidth / ramp[n-1]); // ramp needs to become a slope of 1.0 after oversampling602+ }603+604+ // deinterleave to allow efficient interpolation e.g. w/SIMD605+ for (j=0; j <= oversample; ++j) {606+ for (i=0; i < width; ++i) {607+ blep_buffer [j*width+i] = step[j+i*oversample];608+ blamp_buffer[j*width+i] = ramp[j+i*oversample];609+ }610+ }611+612+ // subtract out the naive waveform; note we can't do this to the raw data613+ // above, because we want the discontinuity to be in a different locations614+ // for j=0 and j=oversample (which exists to provide something to interpolate against)615+ for (j=0; j <= oversample; ++j) {616+ // subtract step617+ for (i=halfwidth; i < width; ++i)618+ blep_buffer [j*width+i] -= 1.0f;619+ // subtract ramp620+ for (i=halfwidth; i < width; ++i)621+ blamp_buffer[j*width+i] -= (j+i*oversample-half)*(1.0f/oversample);622+ }623+624+ hexblep.blep = blep_buffer;625+ hexblep.blamp = blamp_buffer;626+ hexblep.width = width;627+ hexblep.oversample = oversample;628+629+ #ifndef STB_HEXWAVE_NO_ALLOCATION630+ if (user_buffer == 0)631+ free(buffers);632+ #endif633+}634+#endif // STB_HEXWAVE_IMPLEMENTATIONdeleted:stretchy_buffer.h-263 removed
1-// stretchy_buffer.h - v1.04 - public domain - nothings.org/stb2-// a vector<>-like dynamic array for C3-//4-// version history:5-// 1.04 - fix warning6-// 1.03 - compile as C++ maybe7-// 1.02 - tweaks to syntax for no good reason8-// 1.01 - added a "common uses" documentation section9-// 1.0 - fixed bug in the version I posted prematurely10-// 0.9 - rewrite to try to avoid strict-aliasing optimization11-// issues, but won't compile as C++12-//13-// Will probably not work correctly with strict-aliasing optimizations.14-//15-// The idea:16-//17-// This implements an approximation to C++ vector<> for C, in that it18-// provides a generic definition for dynamic arrays which you can19-// still access in a typesafe way using arr[i] or *(arr+i). However,20-// it is simply a convenience wrapper around the common idiom of21-// of keeping a set of variables (in a struct or globals) which store22-// - pointer to array23-// - the length of the "in-use" part of the array24-// - the current size of the allocated array25-//26-// I find it to be the single most useful non-built-in-structure when27-// programming in C (hash tables a close second), but to be clear28-// it lacks many of the capabilities of C++ vector<>: there is no29-// range checking, the object address isn't stable (see next section30-// for details), the set of methods available is small (although31-// the file stb.h has another implementation of stretchy buffers32-// called 'stb_arr' which provides more methods, e.g. for insertion33-// and deletion).34-//35-// How to use:36-//37-// Unlike other stb header file libraries, there is no need to38-// define an _IMPLEMENTATION symbol. Every #include creates as39-// much implementation is needed.40-//41-// stretchy_buffer.h does not define any types, so you do not42-// need to #include it to before defining data types that are43-// stretchy buffers, only in files that *manipulate* stretchy44-// buffers.45-//46-// If you want a stretchy buffer aka dynamic array containing47-// objects of TYPE, declare such an array as:48-//49-// TYPE *myarray = NULL;50-//51-// (There is no typesafe way to distinguish between stretchy52-// buffers and regular arrays/pointers; this is necessary to53-// make ordinary array indexing work on these objects.)54-//55-// Unlike C++ vector<>, the stretchy_buffer has the same56-// semantics as an object that you manually malloc and realloc.57-// The pointer may relocate every time you add a new object58-// to it, so you:59-//60-// 1. can't take long-term pointers to elements of the array61-// 2. have to return the pointer from functions which might expand it62-// (either as a return value or by storing it to a ptr-to-ptr)63-//64-// Now you can do the following things with this array:65-//66-// sb_free(TYPE *a) free the array67-// sb_count(TYPE *a) the number of elements in the array68-// sb_push(TYPE *a, TYPE v) adds v on the end of the array, a la push_back69-// sb_add(TYPE *a, int n) adds n uninitialized elements at end of array & returns pointer to first added70-// sb_last(TYPE *a) returns an lvalue of the last item in the array71-// a[n] access the nth (counting from 0) element of the array72-//73-// #define STRETCHY_BUFFER_NO_SHORT_NAMES to only export74-// names of the form 'stb_sb_' if you have a name that would75-// otherwise collide.76-//77-// Note that these are all macros and many of them evaluate78-// their arguments more than once, so the arguments should79-// be side-effect-free.80-//81-// Note that 'TYPE *a' in sb_push and sb_add must be lvalues82-// so that the library can overwrite the existing pointer if83-// the object has to be reallocated.84-//85-// In an out-of-memory condition, the code will try to86-// set up a null-pointer or otherwise-invalid-pointer87-// exception to happen later. It's possible optimizing88-// compilers could detect this write-to-null statically89-// and optimize away some of the code, but it should only90-// be along the failure path. Nevertheless, for more security91-// in the face of such compilers, #define STRETCHY_BUFFER_OUT_OF_MEMORY92-// to a statement such as assert(0) or exit(1) or something93-// to force a failure when out-of-memory occurs.94-//95-// Common use:96-//97-// The main application for this is when building a list of98-// things with an unknown quantity, either due to loading from99-// a file or through a process which produces an unpredictable100-// number.101-//102-// My most common idiom is something like:103-//104-// SomeStruct *arr = NULL;105-// while (something)106-// {107-// SomeStruct new_one;108-// new_one.whatever = whatever;109-// new_one.whatup = whatup;110-// new_one.foobar = barfoo;111-// sb_push(arr, new_one);112-// }113-//114-// and various closely-related factorings of that. For example,115-// you might have several functions to create/init new SomeStructs,116-// and if you use the above idiom, you might prefer to make them117-// return structs rather than take non-const-pointers-to-structs,118-// so you can do things like:119-//120-// SomeStruct *arr = NULL;121-// while (something)122-// {123-// if (case_A) {124-// sb_push(arr, some_func1());125-// } else if (case_B) {126-// sb_push(arr, some_func2());127-// } else {128-// sb_push(arr, some_func3());129-// }130-// }131-//132-// Note that the above relies on the fact that sb_push doesn't133-// evaluate its second argument more than once. The macros do134-// evaluate the *array* argument multiple times, and numeric135-// arguments may be evaluated multiple times, but you can rely136-// on the second argument of sb_push being evaluated only once.137-//138-// Of course, you don't have to store bare objects in the array;139-// if you need the objects to have stable pointers, store an array140-// of pointers instead:141-//142-// SomeStruct **arr = NULL;143-// while (something)144-// {145-// SomeStruct *new_one = malloc(sizeof(*new_one));146-// new_one->whatever = whatever;147-// new_one->whatup = whatup;148-// new_one->foobar = barfoo;149-// sb_push(arr, new_one);150-// }151-//152-// How it works:153-//154-// A long-standing tradition in things like malloc implementations155-// is to store extra data before the beginning of the block returned156-// to the user. The stretchy buffer implementation here uses the157-// same trick; the current-count and current-allocation-size are158-// stored before the beginning of the array returned to the user.159-// (This means you can't directly free() the pointer, because the160-// allocated pointer is different from the type-safe pointer provided161-// to the user.)162-//163-// The details are trivial and implementation is straightforward;164-// the main trick is in realizing in the first place that it's165-// possible to do this in a generic, type-safe way in C.166-//167-// Contributors:168-//169-// Timothy Wright (github:ZenToad)170-//171-// LICENSE172-//173-// See end of file for license information.174-175-#ifndef STB_STRETCHY_BUFFER_H_INCLUDED176-#define STB_STRETCHY_BUFFER_H_INCLUDED177-178-#ifndef NO_STRETCHY_BUFFER_SHORT_NAMES179-#define sb_free stb_sb_free180-#define sb_push stb_sb_push181-#define sb_count stb_sb_count182-#define sb_add stb_sb_add183-#define sb_last stb_sb_last184-#endif185-186-#define stb_sb_free(a) ((a) ? free(stb__sbraw(a)),0 : 0)187-#define stb_sb_push(a,v) (stb__sbmaybegrow(a,1), (a)[stb__sbn(a)++] = (v))188-#define stb_sb_count(a) ((a) ? stb__sbn(a) : 0)189-#define stb_sb_add(a,n) (stb__sbmaybegrow(a,n), stb__sbn(a)+=(n), &(a)[stb__sbn(a)-(n)])190-#define stb_sb_last(a) ((a)[stb__sbn(a)-1])191-192-#define stb__sbraw(a) ((int *) (void *) (a) - 2)193-#define stb__sbm(a) stb__sbraw(a)[0]194-#define stb__sbn(a) stb__sbraw(a)[1]195-196-#define stb__sbneedgrow(a,n) ((a)==0 || stb__sbn(a)+(n) >= stb__sbm(a))197-#define stb__sbmaybegrow(a,n) (stb__sbneedgrow(a,(n)) ? stb__sbgrow(a,n) : 0)198-#define stb__sbgrow(a,n) (*((void **)&(a)) = stb__sbgrowf((a), (n), sizeof(*(a))))199-200-#include <stdlib.h>201-202-static void * stb__sbgrowf(void *arr, int increment, int itemsize)203-{204- int dbl_cur = arr ? 2*stb__sbm(arr) : 0;205- int min_needed = stb_sb_count(arr) + increment;206- int m = dbl_cur > min_needed ? dbl_cur : min_needed;207- int *p = (int *) realloc(arr ? stb__sbraw(arr) : 0, itemsize * m + sizeof(int)*2);208- if (p) {209- if (!arr)210- p[1] = 0;211- p[0] = m;212- return p+2;213- } else {214- #ifdef STRETCHY_BUFFER_OUT_OF_MEMORY215- STRETCHY_BUFFER_OUT_OF_MEMORY ;216- #endif217- return (void *) (2*sizeof(int)); // try to force a NULL pointer exception later218- }219-}220-#endif // STB_STRETCHY_BUFFER_H_INCLUDED221-222-223-/*224-------------------------------------------------------------------------------225-This software is available under 2 licenses -- choose whichever you prefer.226-------------------------------------------------------------------------------227-ALTERNATIVE A - MIT License228-Copyright (c) 2017 Sean Barrett229-Permission is hereby granted, free of charge, to any person obtaining a copy of230-this software and associated documentation files (the "Software"), to deal in231-the Software without restriction, including without limitation the rights to232-use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies233-of the Software, and to permit persons to whom the Software is furnished to do234-so, subject to the following conditions:235-The above copyright notice and this permission notice shall be included in all236-copies or substantial portions of the Software.237-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR238-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,239-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE240-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER241-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,242-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE243-SOFTWARE.244-------------------------------------------------------------------------------245-ALTERNATIVE B - Public Domain (www.unlicense.org)246-This is free and unencumbered software released into the public domain.247-Anyone is free to copy, modify, publish, use, compile, sell, or distribute this248-software, either in source code form or as a compiled binary, for any purpose,249-commercial or non-commercial, and by any means.250-In jurisdictions that recognize copyright laws, the author or authors of this251-software dedicate any and all copyright interest in the software to the public252-domain. We make this dedication for the benefit of the public at large and to253-the detriment of our heirs and successors. We intend this dedication to be an254-overt act of relinquishment in perpetuity of all present and future rights to255-this software under copyright law.256-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR257-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,258-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE259-AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN260-ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION261-WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.262-------------------------------------------------------------------------------263-*/