Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363
  1. #include "mus-config.h"
  2. #if (defined(__GNUC__)) && (!(defined(__cplusplus)))
  3. #define _GNU_SOURCE
  4. /* this is needed to get the vasprintf declaration */
  5. #endif
  6. #if USE_SND
  7. #include "snd.h"
  8. #endif
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include <fcntl.h>
  12. #include <limits.h>
  13. #include <errno.h>
  14. #include <stdlib.h>
  15. #ifndef _MSC_VER
  16. #include <unistd.h>
  17. #else
  18. #include <io.h>
  19. #pragma warning(disable: 4244)
  20. #endif
  21. #include <string.h>
  22. #include <stdarg.h>
  23. #ifdef _MSC_VER
  24. #include <direct.h>
  25. #endif
  26. #include <sys/stat.h>
  27. #include "_sndlib.h"
  28. #define HAVE_BYTESWAP_H __linux__
  29. #define mus_byte_to_sample(n) (((mus_float_t)(n) / (mus_float_t)(1 << 7)))
  30. #define mus_short_to_sample(n) (((mus_float_t)(n) / (mus_float_t)(1 << 15)))
  31. #define mus_int_to_sample(n) (((mus_float_t)(n) / (mus_float_t)(1 << 23)))
  32. #define mus_int24_to_sample(n) (((mus_float_t)(n) / (mus_float_t)(1 << 23)))
  33. #define mus_sample_to_int(n) ((int)((n) * (1 << 23)))
  34. #define mus_sample_to_int24(n) ((int)((n) * (1 << 23)))
  35. #define mus_sample_to_short(n) ((short)((n) * (1 << 15)))
  36. #define mus_sample_to_byte(n) ((char)((n) * (1 << 7)))
  37. #if defined(__x86_64__) || defined(__i386__)
  38. #define bint24_to_sample(n) ((mus_float_t)(big_endian_int(jchar) >> 8) / (mus_float_t)(1 << 23))
  39. #define int24_to_sample(n) ((mus_float_t)(little_endian_int(jchar) >> 8) / (mus_float_t)(1 << 23))
  40. #else
  41. #define bint24_to_sample(n) ((mus_float_t)(mus_char_to_bint(jchar) >> 8) / (mus_float_t)(1 << 23))
  42. #define int24_to_sample(n) ((mus_float_t)(mus_char_to_lint(jchar) >> 8) / (mus_float_t)(1 << 23))
  43. #endif
  44. static mus_long_t mus_maximum_malloc = MUS_MAX_MALLOC_DEFAULT;
  45. mus_long_t mus_max_malloc(void)
  46. {
  47. return(mus_maximum_malloc);
  48. }
  49. mus_long_t mus_set_max_malloc(mus_long_t new_max)
  50. {
  51. mus_maximum_malloc = new_max;
  52. return(new_max);
  53. }
  54. static mus_long_t mus_maximum_table_size = MUS_MAX_TABLE_SIZE_DEFAULT;
  55. mus_long_t mus_max_table_size(void)
  56. {
  57. return(mus_maximum_table_size);
  58. }
  59. mus_long_t mus_set_max_table_size(mus_long_t new_max)
  60. {
  61. mus_maximum_table_size = new_max;
  62. return(new_max);
  63. }
  64. void mus_bint_to_char(unsigned char *j, int x)
  65. {
  66. unsigned char *ox = (unsigned char *)&x;
  67. #if MUS_LITTLE_ENDIAN
  68. j[0] = ox[3]; j[1] = ox[2]; j[2] = ox[1]; j[3] = ox[0];
  69. #else
  70. memcpy((void *)j, (void *)ox, 4);
  71. #endif
  72. }
  73. int mus_char_to_bint(const unsigned char *inp)
  74. {
  75. int o;
  76. unsigned char *outp = (unsigned char *)&o;
  77. #if MUS_LITTLE_ENDIAN
  78. outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];
  79. #else
  80. memcpy((void *)outp, (void *)inp, 4);
  81. #endif
  82. return(o);
  83. }
  84. void mus_lint_to_char(unsigned char *j, int x)
  85. {
  86. unsigned char *ox = (unsigned char *)&x;
  87. #if (!MUS_LITTLE_ENDIAN)
  88. j[0] = ox[3]; j[1] = ox[2]; j[2] = ox[1]; j[3] = ox[0];
  89. #else
  90. memcpy((void *)j, (void *)ox, 4);
  91. #endif
  92. }
  93. int mus_char_to_lint(const unsigned char *inp)
  94. {
  95. int o;
  96. unsigned char *outp = (unsigned char *)&o;
  97. #if (!MUS_LITTLE_ENDIAN)
  98. outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];
  99. #else
  100. memcpy((void *)outp, (void *)inp, 4);
  101. #endif
  102. return(o);
  103. }
  104. void mus_blong_to_char(unsigned char *j, mus_long_t x)
  105. {
  106. unsigned char *ox = (unsigned char *)&x;
  107. #if (!MUS_LITTLE_ENDIAN)
  108. memcpy((void *)j, (void *)ox, 8);
  109. #else
  110. j[0] = ox[7]; j[1] = ox[6]; j[2] = ox[5]; j[3] = ox[4]; j[4] = ox[3]; j[5] = ox[2]; j[6] = ox[1]; j[7] = ox[0];
  111. #endif
  112. }
  113. mus_long_t mus_char_to_blong(const unsigned char *inp)
  114. {
  115. mus_long_t o;
  116. unsigned char *outp = (unsigned char *)&o;
  117. #if MUS_LITTLE_ENDIAN
  118. outp[0] = inp[7]; outp[1] = inp[6]; outp[2] = inp[5]; outp[3] = inp[4]; outp[4] = inp[3]; outp[5] = inp[2]; outp[6] = inp[1]; outp[7] = inp[0];
  119. #else
  120. memcpy((void *)outp, (void *)inp, 8);
  121. #endif
  122. return(o);
  123. }
  124. void mus_llong_to_char(unsigned char *j, mus_long_t x)
  125. {
  126. unsigned char *ox = (unsigned char *)&x;
  127. #if (MUS_LITTLE_ENDIAN)
  128. memcpy((void *)j, (void *)ox, 8);
  129. #else
  130. j[0] = ox[7]; j[1] = ox[6]; j[2] = ox[5]; j[3] = ox[4]; j[4] = ox[3]; j[5] = ox[2]; j[6] = ox[1]; j[7] = ox[0];
  131. #endif
  132. }
  133. mus_long_t mus_char_to_llong(const unsigned char *inp)
  134. {
  135. mus_long_t o;
  136. unsigned char *outp = (unsigned char *)&o;
  137. #if (!MUS_LITTLE_ENDIAN)
  138. outp[0] = inp[7]; outp[1] = inp[6]; outp[2] = inp[5]; outp[3] = inp[4]; outp[4] = inp[3]; outp[5] = inp[2]; outp[6] = inp[1]; outp[7] = inp[0];
  139. #else
  140. memcpy((void *)outp, (void *)inp, 8);
  141. #endif
  142. return(o);
  143. }
  144. void mus_bfloat_to_char(unsigned char *j, float x)
  145. {
  146. unsigned char *ox = (unsigned char *)&x;
  147. #if MUS_LITTLE_ENDIAN
  148. j[0] = ox[3]; j[1] = ox[2]; j[2] = ox[1]; j[3] = ox[0];
  149. #else
  150. memcpy((void *)j, (void *)ox, 4);
  151. #endif
  152. }
  153. float mus_char_to_bfloat(const unsigned char *inp)
  154. {
  155. float o;
  156. unsigned char *outp = (unsigned char *)&o;
  157. #if MUS_LITTLE_ENDIAN
  158. outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];
  159. #else
  160. memcpy((void *)outp, (void *)inp, 4);
  161. #endif
  162. return(o);
  163. }
  164. void mus_lfloat_to_char(unsigned char *j, float x)
  165. {
  166. unsigned char *ox = (unsigned char *)&x;
  167. #if (!MUS_LITTLE_ENDIAN)
  168. j[0] = ox[3]; j[1] = ox[2]; j[2] = ox[1]; j[3] = ox[0];
  169. #else
  170. memcpy((void *)j, (void *)ox, 4);
  171. #endif
  172. }
  173. float mus_char_to_lfloat(const unsigned char *inp)
  174. {
  175. float o;
  176. unsigned char *outp = (unsigned char *)&o;
  177. #if (!MUS_LITTLE_ENDIAN)
  178. outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];
  179. #else
  180. memcpy((void *)outp, (void *)inp, 4);
  181. #endif
  182. return(o);
  183. }
  184. void mus_bshort_to_char(unsigned char *j, short x)
  185. {
  186. unsigned char *ox = (unsigned char *)&x;
  187. #if MUS_LITTLE_ENDIAN
  188. j[0] = ox[1]; j[1] = ox[0];
  189. #else
  190. memcpy((void *)j, (void *)ox, 2); /* I wonder if this is faster */
  191. #endif
  192. }
  193. short mus_char_to_bshort(const unsigned char *inp)
  194. {
  195. short o;
  196. unsigned char *outp = (unsigned char *)&o;
  197. #if MUS_LITTLE_ENDIAN
  198. outp[0] = inp[1]; outp[1] = inp[0];
  199. #else
  200. memcpy((void *)outp, (void *)inp, 2);
  201. #endif
  202. return(o);
  203. }
  204. void mus_lshort_to_char(unsigned char *j, short x)
  205. {
  206. unsigned char *ox = (unsigned char *)&x;
  207. #if (!MUS_LITTLE_ENDIAN)
  208. j[0] = ox[1]; j[1] = ox[0];
  209. #else
  210. memcpy((void *)j, (void *)ox, 2);
  211. #endif
  212. }
  213. short mus_char_to_lshort(const unsigned char *inp)
  214. {
  215. short o;
  216. unsigned char *outp = (unsigned char *)&o;
  217. #if (!MUS_LITTLE_ENDIAN)
  218. outp[0] = inp[1]; outp[1] = inp[0];
  219. #else
  220. memcpy((void *)outp, (void *)inp, 2);
  221. #endif
  222. return(o);
  223. }
  224. #if ((MUS_LITTLE_ENDIAN) && (!HAVE_BYTESWAP_H)) || ((!MUS_LITTLE_ENDIAN) && (HAVE_SUN))
  225. static void mus_ubshort_to_char(unsigned char *j, unsigned short x)
  226. {
  227. unsigned char *ox = (unsigned char *)&x;
  228. #if MUS_LITTLE_ENDIAN
  229. j[0] = ox[1]; j[1] = ox[0];
  230. #else
  231. memcpy((void *)j, (void *)ox, 2);
  232. #endif
  233. }
  234. #endif
  235. unsigned short mus_char_to_ubshort(const unsigned char *inp)
  236. {
  237. unsigned short o;
  238. unsigned char *outp = (unsigned char *)&o;
  239. #if MUS_LITTLE_ENDIAN
  240. outp[0] = inp[1]; outp[1] = inp[0];
  241. #else
  242. memcpy((void *)outp, (void *)inp, 2);
  243. #endif
  244. return(o);
  245. }
  246. #if (!MUS_LITTLE_ENDIAN) && (!HAVE_BYTESWAP_H)
  247. static void mus_ulshort_to_char(unsigned char *j, unsigned short x)
  248. {
  249. unsigned char *ox = (unsigned char *)&x;
  250. #if (!MUS_LITTLE_ENDIAN)
  251. j[0] = ox[1]; j[1] = ox[0];
  252. #else
  253. memcpy((void *)j, (void *)ox, 2);
  254. #endif
  255. }
  256. #endif
  257. unsigned short mus_char_to_ulshort(const unsigned char *inp)
  258. {
  259. unsigned short o;
  260. unsigned char *outp = (unsigned char *)&o;
  261. #if (!MUS_LITTLE_ENDIAN)
  262. outp[0] = inp[1]; outp[1] = inp[0];
  263. #else
  264. memcpy((void *)outp, (void *)inp, 2);
  265. #endif
  266. return(o);
  267. }
  268. void mus_bdouble_to_char(unsigned char *j, double x)
  269. {
  270. unsigned char *ox = (unsigned char *)&x;
  271. #if (MUS_LITTLE_ENDIAN)
  272. j[0] = ox[7]; j[1] = ox[6]; j[2] = ox[5]; j[3] = ox[4]; j[4] = ox[3]; j[5] = ox[2]; j[6] = ox[1]; j[7] = ox[0];
  273. #else
  274. memcpy((void *)j, (void *)ox, 8);
  275. #endif
  276. }
  277. double mus_char_to_ldouble(const unsigned char *inp)
  278. {
  279. double o;
  280. unsigned char *outp = (unsigned char *)&o;
  281. #if (MUS_LITTLE_ENDIAN)
  282. memcpy((void *)outp, (void *)inp, 8);
  283. #else
  284. outp[0] = inp[7]; outp[1] = inp[6]; outp[2] = inp[5]; outp[3] = inp[4]; outp[4] = inp[3]; outp[5] = inp[2]; outp[6] = inp[1]; outp[7] = inp[0];
  285. #endif
  286. return(o);
  287. }
  288. #if (!MUS_LITTLE_ENDIAN)
  289. static void mus_ldouble_to_char(unsigned char *j, double x)
  290. {
  291. unsigned char *ox = (unsigned char *)&x;
  292. #if (MUS_LITTLE_ENDIAN)
  293. memcpy((void *)j, (void *)ox, 8);
  294. #else
  295. j[0] = ox[7]; j[1] = ox[6]; j[2] = ox[5]; j[3] = ox[4]; j[4] = ox[3]; j[5] = ox[2]; j[6] = ox[1]; j[7] = ox[0];
  296. #endif
  297. }
  298. #endif
  299. double mus_char_to_bdouble(const unsigned char *inp)
  300. {
  301. double o;
  302. unsigned char *outp = (unsigned char *)&o;
  303. #if (MUS_LITTLE_ENDIAN)
  304. outp[0] = inp[7]; outp[1] = inp[6]; outp[2] = inp[5]; outp[3] = inp[4]; outp[4] = inp[3]; outp[5] = inp[2]; outp[6] = inp[1]; outp[7] = inp[0];
  305. #else
  306. memcpy((void *)outp, (void *)inp, 8);
  307. #endif
  308. return(o);
  309. }
  310. int mus_char_to_uninterpreted_int(const unsigned char *inp)
  311. {
  312. int o;
  313. unsigned char *outp = (unsigned char *)&o;
  314. memcpy((void *)outp, (void *)inp, 4);
  315. return(o);
  316. }
  317. unsigned int mus_char_to_ubint(const unsigned char *inp)
  318. {
  319. unsigned int o;
  320. unsigned char *outp = (unsigned char *)&o;
  321. #if MUS_LITTLE_ENDIAN
  322. outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];
  323. #else
  324. memcpy((void *)outp, (void *)inp, 4);
  325. #endif
  326. return(o);
  327. }
  328. unsigned int mus_char_to_ulint(const unsigned char *inp)
  329. {
  330. unsigned int o;
  331. unsigned char *outp = (unsigned char *)&o;
  332. #if (!MUS_LITTLE_ENDIAN)
  333. outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];
  334. #else
  335. memcpy((void *)outp, (void *)inp, 4);
  336. #endif
  337. return(o);
  338. }
  339. #if HAVE_BYTESWAP_H
  340. #include <byteswap.h>
  341. #endif
  342. #if MUS_LITTLE_ENDIAN
  343. #if HAVE_BYTESWAP_H
  344. #define big_endian_short(n) ((short)(bswap_16((*((unsigned short *)n)))))
  345. #define big_endian_int(n) ((int)(bswap_32((*((unsigned int *)n)))))
  346. #define big_endian_unsigned_short(n) ((unsigned short)(bswap_16((*((unsigned short *)n)))))
  347. #else
  348. #define big_endian_short(n) (mus_char_to_bshort(n))
  349. #define big_endian_int(n) (mus_char_to_bint(n))
  350. #define big_endian_unsigned_short(n) (mus_char_to_ubshort(n))
  351. #endif
  352. #if __GNUC__ && HAVE_BYTESWAP_H
  353. /* these work, but newer versions of gcc complain about strict aliasing rules
  354. * #define big_endian_float(n) ({unsigned int x; x = bswap_32((*((unsigned int *)n))); (*((float *)&x));})
  355. * #define big_endian_double(n) ({unsigned long long int x; x = bswap_64((*((unsigned long long int *)n))); (*((double *)&x));})
  356. */
  357. typedef struct {union {unsigned int i; float f;} u;} uiflt;
  358. typedef struct {union {unsigned long long int l; double d;} u;} uidbl;
  359. #define big_endian_float(n) ({uiflt x; x.u.i = bswap_32((*((unsigned int *)n))); x.u.f;})
  360. #define big_endian_double(n) ({uidbl x; x.u.l = bswap_64((*((unsigned long long int *)n))); x.u.d;})
  361. #else
  362. #define big_endian_float(n) (mus_char_to_bfloat(n))
  363. #define big_endian_double(n) (mus_char_to_bdouble(n))
  364. #endif
  365. #define little_endian_short(n) (*((short *)n))
  366. #define little_endian_int(n) (*((int *)n))
  367. #define little_endian_float(n) (*((float *)n))
  368. #define little_endian_double(n) (*((double *)n))
  369. #define little_endian_unsigned_short(n) (*((unsigned short *)n))
  370. #if HAVE_BYTESWAP_H
  371. #define set_big_endian_short(n, x) (*((short *)n)) = ((short)(bswap_16(x)))
  372. #define set_big_endian_int(n, x) (*((int *)n)) = ((int)(bswap_32(x)))
  373. #define set_big_endian_unsigned_short(n, x) (*((unsigned short *)n)) = ((unsigned short)(bswap_16(x)))
  374. #else
  375. #define set_big_endian_short(n, x) mus_bshort_to_char(n, x)
  376. #define set_big_endian_int(n, x) mus_bint_to_char(n, x)
  377. #define set_big_endian_unsigned_short(n, x) mus_ubshort_to_char(n, x)
  378. #endif
  379. #define set_big_endian_float(n, x) mus_bfloat_to_char(n, x)
  380. #define set_big_endian_double(n, x) mus_bdouble_to_char(n, x)
  381. #define set_little_endian_short(n, x) (*((short *)n)) = x
  382. #define set_little_endian_int(n, x) (*((int *)n)) = x
  383. #define set_little_endian_float(n, x) (*((float *)n)) = x
  384. #define set_little_endian_double(n, x) (*((double *)n)) = x
  385. #define set_little_endian_unsigned_short(n, x) (*((unsigned short *)n)) = x
  386. #else
  387. #if (!HAVE_SUN)
  388. #define big_endian_short(n) (*((short *)n))
  389. #define big_endian_int(n) (*((int *)n))
  390. #define big_endian_float(n) (*((float *)n))
  391. #define big_endian_double(n) (*((double *)n))
  392. #define big_endian_unsigned_short(n) (*((unsigned short *)n))
  393. #define set_big_endian_short(n, x) (*((short *)n)) = x
  394. #define set_big_endian_int(n, x) (*((int *)n)) = x
  395. #define set_big_endian_float(n, x) (*((float *)n)) = x
  396. #define set_big_endian_double(n, x) (*((double *)n)) = x
  397. #define set_big_endian_unsigned_short(n, x) (*((unsigned short *)n)) = x
  398. #else
  399. #define big_endian_short(n) (mus_char_to_bshort(n))
  400. #define big_endian_int(n) (mus_char_to_bint(n))
  401. #define big_endian_float(n) (mus_char_to_bfloat(n))
  402. #define big_endian_double(n) (mus_char_to_bdouble(n))
  403. #define big_endian_unsigned_short(n) (mus_char_to_ubshort(n))
  404. #define set_big_endian_short(n, x) mus_bshort_to_char(n, x)
  405. #define set_big_endian_int(n, x) mus_bint_to_char(n, x)
  406. #define set_big_endian_float(n, x) mus_bfloat_to_char(n, x)
  407. #define set_big_endian_double(n, x) mus_bdouble_to_char(n, x)
  408. #define set_big_endian_unsigned_short(n, x) mus_ubshort_to_char(n, x)
  409. #endif
  410. #if HAVE_BYTESWAP_H
  411. #define little_endian_short(n) ((short)(bswap_16((*((unsigned short *)n)))))
  412. #define little_endian_int(n) ((int)(bswap_32((*((unsigned int *)n)))))
  413. #define little_endian_unsigned_short(n) ((unsigned short)(bswap_16((*((unsigned short *)n)))))
  414. #else
  415. #define little_endian_short(n) (mus_char_to_lshort(n))
  416. #define little_endian_int(n) (mus_char_to_lint(n))
  417. #define little_endian_unsigned_short(n) (mus_char_to_ulshort(n))
  418. #endif
  419. #if __GNUC__ && HAVE_BYTESWAP_H
  420. #define little_endian_float(n) ({unsigned int x; x = bswap_32((*((unsigned int *)n))); (*((float *)&x));})
  421. #define little_endian_double(n) ({unsigned long long int x; x = bswap_64((*((unsigned long long int *)n))); (*((double *)&x));})
  422. #else
  423. #define little_endian_float(n) (mus_char_to_lfloat(n))
  424. #define little_endian_double(n) (mus_char_to_ldouble(n))
  425. #endif
  426. #if HAVE_BYTESWAP_H
  427. #define set_little_endian_short(n, x) (*((short *)n)) = ((short)(bswap_16(x)))
  428. #define set_little_endian_int(n, x) (*((int *)n)) = ((int)(bswap_32(x)))
  429. #define set_little_endian_unsigned_short(n, x) (*((unsigned short *)n)) = ((unsigned short)(bswap_16(x)))
  430. #else
  431. #define set_little_endian_short(n, x) mus_lshort_to_char(n, x)
  432. #define set_little_endian_int(n, x) mus_lint_to_char(n, x)
  433. #define set_little_endian_unsigned_short(n, x) mus_ulshort_to_char(n, x)
  434. #endif
  435. #define set_little_endian_float(n, x) mus_lfloat_to_char(n, x)
  436. #define set_little_endian_double(n, x) mus_ldouble_to_char(n, x)
  437. #endif
  438. /* ---------------- file descriptors ---------------- */
  439. static bool clipping_default = false;
  440. bool mus_clipping(void) {return(clipping_default);}
  441. bool mus_set_clipping(bool new_value) {clipping_default = new_value; return(new_value);}
  442. typedef struct {
  443. char *name;
  444. mus_sample_t sample_type;
  445. mus_header_t header_type;
  446. int bytes_per_sample, chans;
  447. bool clipping;
  448. mus_long_t data_location;
  449. bool saved;
  450. mus_long_t framples;
  451. mus_float_t **saved_data;
  452. void *next;
  453. } io_fd;
  454. static io_fd *io_fd_free_list = NULL;
  455. static io_fd *io_fd_alloc(void)
  456. {
  457. if (io_fd_free_list)
  458. {
  459. io_fd *p;
  460. p = io_fd_free_list;
  461. io_fd_free_list = (io_fd *)(p->next);
  462. return(p);
  463. }
  464. return((io_fd *)malloc(sizeof(io_fd)));
  465. }
  466. static void io_fd_free(io_fd *p)
  467. {
  468. p->next = (void *)io_fd_free_list;
  469. io_fd_free_list = p;
  470. }
  471. static int io_fd_size = 0;
  472. static io_fd **io_fds = NULL;
  473. #define IO_FD_ALLOC_SIZE 8
  474. int mus_file_open_descriptors(int tfd, const char *name, mus_sample_t samp_type, int size /* datum size */, mus_long_t location, int chans, mus_header_t type)
  475. {
  476. int err = MUS_NO_ERROR;
  477. if (io_fd_size == 0)
  478. {
  479. io_fd_size = tfd + IO_FD_ALLOC_SIZE;
  480. io_fds = (io_fd **)calloc(io_fd_size, sizeof(io_fd *));
  481. }
  482. if (io_fds)
  483. {
  484. if (io_fd_size <= tfd)
  485. {
  486. int i, lim;
  487. lim = io_fd_size;
  488. io_fd_size = tfd + IO_FD_ALLOC_SIZE;
  489. io_fds = (io_fd **)realloc(io_fds, io_fd_size * sizeof(io_fd *));
  490. for (i = lim; i < io_fd_size; i++) io_fds[i] = NULL;
  491. }
  492. if (io_fds[tfd] == NULL)
  493. io_fds[tfd] = io_fd_alloc();
  494. if (io_fds[tfd])
  495. {
  496. io_fd *fd;
  497. fd = io_fds[tfd];
  498. fd->framples = 0;
  499. fd->sample_type = samp_type;
  500. fd->bytes_per_sample = size;
  501. fd->data_location = location;
  502. fd->clipping = clipping_default;
  503. fd->header_type = type;
  504. fd->chans = chans;
  505. fd->saved = false;
  506. fd->saved_data = NULL;
  507. if (name)
  508. {
  509. int len;
  510. len = strlen(name);
  511. fd->name = (char *)malloc((len + 1) * sizeof(char));
  512. strcpy(fd->name, name);
  513. fd->name[len] = 0;
  514. }
  515. else fd->name = NULL;
  516. }
  517. else err = MUS_MEMORY_ALLOCATION_FAILED;
  518. }
  519. else err = MUS_MEMORY_ALLOCATION_FAILED;
  520. return(err);
  521. }
  522. void scan_io_fds_for_saved_data(mus_float_t **data);
  523. void scan_io_fds_for_saved_data(mus_float_t **data)
  524. {
  525. if ((io_fds) &&
  526. (io_fd_size > 0))
  527. {
  528. int i;
  529. for (i = 0; i < io_fd_size; i++)
  530. {
  531. io_fd *fd;
  532. fd = io_fds[i];
  533. if ((fd) &&
  534. (fd->saved) &&
  535. (fd->saved_data == data))
  536. {
  537. fd->saved = false;
  538. fd->saved_data = NULL;
  539. }
  540. }
  541. }
  542. }
  543. bool mus_file_clipping(int tfd)
  544. {
  545. io_fd *fd;
  546. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(false);
  547. fd = io_fds[tfd];
  548. return(fd->clipping);
  549. }
  550. int mus_file_set_clipping(int tfd, bool clipped)
  551. {
  552. io_fd *fd;
  553. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED);
  554. fd = io_fds[tfd];
  555. fd->clipping = clipped;
  556. return(MUS_NO_ERROR);
  557. }
  558. int mus_file_set_header_type(int tfd, mus_header_t type)
  559. {
  560. io_fd *fd;
  561. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED);
  562. fd = io_fds[tfd];
  563. fd->header_type = type;
  564. return(MUS_NO_ERROR);
  565. }
  566. mus_header_t mus_file_header_type(int tfd)
  567. {
  568. io_fd *fd;
  569. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(MUS_UNKNOWN_HEADER);
  570. fd = io_fds[tfd];
  571. return(fd->header_type);
  572. }
  573. char *mus_file_fd_name(int tfd)
  574. {
  575. io_fd *fd;
  576. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(NULL);
  577. fd = io_fds[tfd];
  578. return(fd->name);
  579. }
  580. int mus_file_set_chans(int tfd, int chans)
  581. {
  582. io_fd *fd;
  583. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED);
  584. fd = io_fds[tfd];
  585. fd->chans = chans;
  586. return(MUS_NO_ERROR);
  587. }
  588. void mus_file_save_data(int tfd, mus_long_t framples, mus_float_t **data)
  589. {
  590. io_fd *fd;
  591. fd = io_fds[tfd];
  592. if (!fd->saved)
  593. {
  594. /* fprintf(stderr, "mus_file_save_data %d\n", tfd); */
  595. fd->saved = true;
  596. fd->framples = framples;
  597. fd->saved_data = data;
  598. }
  599. }
  600. /* ---------------- open, creat, close ---------------- */
  601. int mus_file_open_read(const char *arg)
  602. {
  603. int fd;
  604. #if (defined(_MSC_VER) || __CYGWIN__)
  605. fd = OPEN(arg, O_RDONLY | O_BINARY, 0);
  606. #else
  607. fd = OPEN(arg, O_RDONLY, 0);
  608. #endif
  609. return(fd);
  610. }
  611. bool mus_file_probe(const char *arg)
  612. {
  613. #ifndef _MSC_VER
  614. if (!arg) return(false);
  615. return(access(arg, F_OK) == 0);
  616. #else
  617. int fd;
  618. #ifdef O_NONBLOCK
  619. fd = OPEN(arg, O_RDONLY, O_NONBLOCK);
  620. #else
  621. fd = OPEN(arg, O_RDONLY, 0);
  622. #endif
  623. if (fd == -1) return(false);
  624. CLOSE(fd, arg);
  625. return(true);
  626. #endif
  627. }
  628. int mus_file_open_write(const char *arg)
  629. {
  630. int fd;
  631. #if (defined(_MSC_VER) || __CYGWIN__)
  632. if ((fd = OPEN(arg, O_RDWR | O_BINARY, 0)) == -1)
  633. fd = CREAT(arg, S_IREAD | S_IWRITE); /* 0x0100 | 0x0080 */
  634. #else
  635. if ((fd = OPEN(arg, O_RDWR, 0)) == -1)
  636. fd = CREAT(arg, 0666); /* equivalent to the new open(arg, O_RDWR | O_CREAT | O_TRUNC, 0666) */
  637. #endif
  638. else lseek(fd, 0L, SEEK_END);
  639. return(fd);
  640. }
  641. int mus_file_create(const char *arg)
  642. {
  643. #if (defined(_MSC_VER ) || __CYGWIN__)
  644. return(CREAT(arg, S_IREAD | S_IWRITE));
  645. #else
  646. return(CREAT(arg, 0666));
  647. #endif
  648. }
  649. int mus_file_reopen_write(const char *arg)
  650. {
  651. int fd;
  652. #if (defined(_MSC_VER) || __CYGWIN__)
  653. fd = OPEN(arg, O_RDWR | O_BINARY, 0);
  654. #else
  655. fd = OPEN(arg, O_RDWR, 0);
  656. #endif
  657. return(fd);
  658. }
  659. int mus_file_close(int fd)
  660. {
  661. io_fd *fdp;
  662. #if (!USE_SND)
  663. int close_result = 0;
  664. #endif
  665. if ((io_fds == NULL) || (fd >= io_fd_size) || (fd < 0) || (io_fds[fd] == NULL)) return(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED);
  666. fdp = io_fds[fd];
  667. #if USE_SND
  668. CLOSE(fd, fdp->name);
  669. #else
  670. close_result = close(fd);
  671. #endif
  672. if (fdp->name) {free(fdp->name); fdp->name = NULL;}
  673. io_fd_free(fdp);
  674. io_fds[fd] = NULL;
  675. #if (!USE_SND)
  676. if (close_result < 0)
  677. return(MUS_CANT_CLOSE_FILE);
  678. #endif
  679. return(MUS_NO_ERROR);
  680. }
  681. /* ---------------- seek ---------------- */
  682. mus_long_t mus_file_seek_frample(int tfd, mus_long_t frample)
  683. {
  684. io_fd *fd;
  685. if (io_fds == NULL)
  686. return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED, "mus_file_seek_frample: no file descriptors!"));
  687. if (tfd >= io_fd_size)
  688. return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED,
  689. "mus_file_seek_frample: file descriptors not realloc'd? (tfd: %d, io_fd_size: %d)", tfd, io_fd_size));
  690. if ((tfd < 0) ||
  691. (io_fds[tfd] == NULL))
  692. return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED, "mus_file_seek_frample: file descriptor = %d?", tfd));
  693. fd = io_fds[tfd];
  694. if (fd->sample_type == MUS_UNKNOWN_SAMPLE)
  695. return(mus_error(MUS_NOT_A_SOUND_FILE, "mus_file_seek_frample: invalid sample type for %s", fd->name));
  696. return(lseek(tfd, fd->data_location + (fd->chans * frample * fd->bytes_per_sample), SEEK_SET));
  697. }
  698. /* ---------------- mulaw/alaw conversions ----------------
  699. *
  700. * x : input signal with max value 32767
  701. * mu : compression parameter (mu = 255 used for telephony)
  702. * y = (32767/log(1+mu))*log(1+mu*abs(x)/32767)*sign(x); -- this isn't right -- typo?
  703. */
  704. /* from sox g711.c */
  705. #define QUANT_MASK (0xf) /* Quantization field mask. */
  706. #define SEG_SHIFT (4) /* Left shift for segment number. */
  707. static short seg_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
  708. static int search(int val, short *table, int size)
  709. {
  710. int i;
  711. for (i = 0; i < size; i++) {if (val <= *table++) return(i);}
  712. return(size);
  713. }
  714. static unsigned char to_alaw(int pcm_val)
  715. {
  716. int mask, seg;
  717. if (pcm_val >= 0) mask = 0xD5; else {mask = 0x55; pcm_val = -pcm_val - 8;}
  718. seg = search(pcm_val, seg_end, 8);
  719. if (seg >= 8) return(0x7F ^ mask);
  720. else
  721. {
  722. unsigned char aval;
  723. aval = seg << SEG_SHIFT;
  724. if (seg < 2) aval |= (pcm_val >> 4) & QUANT_MASK; else aval |= (pcm_val >> (seg + 3)) & QUANT_MASK;
  725. return(aval ^ mask);
  726. }
  727. }
  728. #define A_(a) mus_short_to_sample(a)
  729. static const mus_float_t mus_alaw[256] = {
  730. A_(-5504), A_(-5248), A_(-6016), A_(-5760), A_(-4480), A_(-4224), A_(-4992), A_(-4736), A_(-7552), A_(-7296),
  731. A_(-8064), A_(-7808), A_(-6528), A_(-6272), A_(-7040), A_(-6784), A_(-2752), A_(-2624), A_(-3008), A_(-2880),
  732. A_(-2240), A_(-2112), A_(-2496), A_(-2368), A_(-3776), A_(-3648), A_(-4032), A_(-3904), A_(-3264), A_(-3136),
  733. A_(-3520), A_(-3392), A_(-22016), A_(-20992), A_(-24064), A_(-23040), A_(-17920), A_(-16896), A_(-19968), A_(-18944),
  734. A_(-30208), A_(-29184), A_(-32256), A_(-31232), A_(-26112), A_(-25088), A_(-28160), A_(-27136), A_(-11008), A_(-10496),
  735. A_(-12032), A_(-11520), A_(-8960), A_(-8448), A_(-9984), A_(-9472), A_(-15104), A_(-14592), A_(-16128), A_(-15616),
  736. A_(-13056), A_(-12544), A_(-14080), A_(-13568), A_(-344), A_(-328), A_(-376), A_(-360), A_(-280), A_(-264), A_(-312),
  737. A_(-296), A_(-472), A_(-456), A_(-504), A_(-488), A_(-408), A_(-392), A_(-440), A_(-424), A_(-88), A_(-72), A_(-120),
  738. A_(-104), A_(-24), A_(-8), A_(-56), A_(-40), A_(-216), A_(-200), A_(-248), A_(-232), A_(-152), A_(-136), A_(-184),
  739. A_(-168), A_(-1376), A_(-1312), A_(-1504), A_(-1440), A_(-1120), A_(-1056), A_(-1248), A_(-1184), A_(-1888), A_(-1824),
  740. A_(-2016), A_(-1952), A_(-1632), A_(-1568), A_(-1760), A_(-1696), A_(-688), A_(-656), A_(-752), A_(-720), A_(-560),
  741. A_(-528), A_(-624), A_(-592), A_(-944), A_(-912), A_(-1008), A_(-976), A_(-816), A_(-784), A_(-880), A_(-848), A_(5504),
  742. A_(5248), A_(6016), A_(5760), A_(4480), A_(4224), A_(4992), A_(4736), A_(7552), A_(7296), A_(8064), A_(7808), A_(6528),
  743. A_(6272), A_(7040), A_(6784), A_(2752), A_(2624), A_(3008), A_(2880), A_(2240), A_(2112), A_(2496), A_(2368), A_(3776),
  744. A_(3648), A_(4032), A_(3904), A_(3264), A_(3136), A_(3520), A_(3392), A_(22016), A_(20992), A_(24064), A_(23040), A_(17920),
  745. A_(16896), A_(19968), A_(18944), A_(30208), A_(29184), A_(32256), A_(31232), A_(26112), A_(25088), A_(28160), A_(27136),
  746. A_(11008), A_(10496), A_(12032), A_(11520), A_(8960), A_(8448), A_(9984), A_(9472), A_(15104), A_(14592), A_(16128),
  747. A_(15616), A_(13056), A_(12544), A_(14080), A_(13568), A_(344), A_(328), A_(376), A_(360), A_(280), A_(264), A_(312),
  748. A_(296), A_(472), A_(456), A_(504), A_(488), A_(408), A_(392), A_(440), A_(424), A_(88), A_(72), A_(120), A_(104),
  749. A_(24), A_(8), A_(56), A_(40), A_(216), A_(200), A_(248), A_(232), A_(152), A_(136), A_(184), A_(168), A_(1376),
  750. A_(1312), A_(1504), A_(1440), A_(1120), A_(1056), A_(1248), A_(1184), A_(1888), A_(1824), A_(2016), A_(1952), A_(1632),
  751. A_(1568), A_(1760), A_(1696), A_(688), A_(656), A_(752), A_(720), A_(560), A_(528), A_(624), A_(592), A_(944), A_(912),
  752. A_(1008), A_(976), A_(816), A_(784), A_(880), A_(848)
  753. };
  754. #define BIAS (0x84) /* Bias for linear code. */
  755. static unsigned char to_mulaw(int pcm_val)
  756. {
  757. int mask;
  758. int seg;
  759. if (pcm_val < 0) {pcm_val = BIAS - pcm_val; mask = 0x7F;} else {pcm_val += BIAS; mask = 0xFF;}
  760. seg = search(pcm_val, seg_end, 8);
  761. if (seg >= 8) return(0x7F ^ mask);
  762. else
  763. {
  764. unsigned char uval;
  765. uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF);
  766. return(uval ^ mask);
  767. }
  768. }
  769. #define MU_(a) mus_short_to_sample(a)
  770. static const mus_float_t mus_mulaw[256] = {
  771. MU_(-32124), MU_(-31100), MU_(-30076), MU_(-29052), MU_(-28028), MU_(-27004), MU_(-25980), MU_(-24956), MU_(-23932), MU_(-22908),
  772. MU_(-21884), MU_(-20860), MU_(-19836), MU_(-18812), MU_(-17788), MU_(-16764), MU_(-15996), MU_(-15484), MU_(-14972), MU_(-14460),
  773. MU_(-13948), MU_(-13436), MU_(-12924), MU_(-12412), MU_(-11900), MU_(-11388), MU_(-10876), MU_(-10364), MU_(-9852), MU_(-9340),
  774. MU_(-8828), MU_(-8316), MU_(-7932), MU_(-7676), MU_(-7420), MU_(-7164), MU_(-6908), MU_(-6652), MU_(-6396), MU_(-6140), MU_(-5884),
  775. MU_(-5628), MU_(-5372), MU_(-5116), MU_(-4860), MU_(-4604), MU_(-4348), MU_(-4092), MU_(-3900), MU_(-3772), MU_(-3644), MU_(-3516),
  776. MU_(-3388), MU_(-3260), MU_(-3132), MU_(-3004), MU_(-2876), MU_(-2748), MU_(-2620), MU_(-2492), MU_(-2364), MU_(-2236), MU_(-2108),
  777. MU_(-1980), MU_(-1884), MU_(-1820), MU_(-1756), MU_(-1692), MU_(-1628), MU_(-1564), MU_(-1500), MU_(-1436), MU_(-1372), MU_(-1308),
  778. MU_(-1244), MU_(-1180), MU_(-1116), MU_(-1052), MU_(-988), MU_(-924), MU_(-876), MU_(-844), MU_(-812), MU_(-780), MU_(-748),
  779. MU_(-716), MU_(-684), MU_(-652), MU_(-620), MU_(-588), MU_(-556), MU_(-524), MU_(-492), MU_(-460), MU_(-428), MU_(-396),
  780. MU_(-372), MU_(-356), MU_(-340), MU_(-324), MU_(-308), MU_(-292), MU_(-276), MU_(-260), MU_(-244), MU_(-228), MU_(-212),
  781. MU_(-196), MU_(-180), MU_(-164), MU_(-148), MU_(-132), MU_(-120), MU_(-112), MU_(-104), MU_(-96), MU_(-88), MU_(-80), MU_(-72),
  782. MU_(-64), MU_(-56), MU_(-48), MU_(-40), MU_(-32), MU_(-24), MU_(-16), MU_(-8), MU_(0), MU_(32124), MU_(31100), MU_(30076),
  783. MU_(29052), MU_(28028), MU_(27004), MU_(25980), MU_(24956), MU_(23932), MU_(22908), MU_(21884), MU_(20860), MU_(19836),
  784. MU_(18812), MU_(17788), MU_(16764), MU_(15996), MU_(15484), MU_(14972), MU_(14460), MU_(13948), MU_(13436), MU_(12924),
  785. MU_(12412), MU_(11900), MU_(11388), MU_(10876), MU_(10364), MU_(9852), MU_(9340), MU_(8828), MU_(8316), MU_(7932), MU_(7676),
  786. MU_(7420), MU_(7164), MU_(6908), MU_(6652), MU_(6396), MU_(6140), MU_(5884), MU_(5628), MU_(5372), MU_(5116), MU_(4860),
  787. MU_(4604), MU_(4348), MU_(4092), MU_(3900), MU_(3772), MU_(3644), MU_(3516), MU_(3388), MU_(3260), MU_(3132), MU_(3004),
  788. MU_(2876), MU_(2748), MU_(2620), MU_(2492), MU_(2364), MU_(2236), MU_(2108), MU_(1980), MU_(1884), MU_(1820), MU_(1756),
  789. MU_(1692), MU_(1628), MU_(1564), MU_(1500), MU_(1436), MU_(1372), MU_(1308), MU_(1244), MU_(1180), MU_(1116), MU_(1052),
  790. MU_(988), MU_(924), MU_(876), MU_(844), MU_(812), MU_(780), MU_(748), MU_(716), MU_(684), MU_(652), MU_(620), MU_(588),
  791. MU_(556), MU_(524), MU_(492), MU_(460), MU_(428), MU_(396), MU_(372), MU_(356), MU_(340), MU_(324), MU_(308), MU_(292),
  792. MU_(276), MU_(260), MU_(244), MU_(228), MU_(212), MU_(196), MU_(180), MU_(164), MU_(148), MU_(132), MU_(120), MU_(112),
  793. MU_(104), MU_(96), MU_(88), MU_(80), MU_(72), MU_(64), MU_(56), MU_(48), MU_(40), MU_(32), MU_(24), MU_(16), MU_(8), MU_(0)
  794. };
  795. #define B_(a) mus_byte_to_sample(a)
  796. static const mus_float_t mus_byte[256] = {
  797. B_(0), B_(1), B_(2), B_(3), B_(4), B_(5), B_(6), B_(7), B_(8), B_(9), B_(10), B_(11), B_(12), B_(13), B_(14), B_(15), B_(16),
  798. B_(17), B_(18), B_(19), B_(20), B_(21), B_(22), B_(23), B_(24), B_(25), B_(26), B_(27), B_(28), B_(29), B_(30), B_(31), B_(32),
  799. B_(33), B_(34), B_(35), B_(36), B_(37), B_(38), B_(39), B_(40), B_(41), B_(42), B_(43), B_(44), B_(45), B_(46), B_(47), B_(48),
  800. B_(49), B_(50), B_(51), B_(52), B_(53), B_(54), B_(55), B_(56), B_(57), B_(58), B_(59), B_(60), B_(61), B_(62), B_(63), B_(64),
  801. B_(65), B_(66), B_(67), B_(68), B_(69), B_(70), B_(71), B_(72), B_(73), B_(74), B_(75), B_(76), B_(77), B_(78), B_(79), B_(80),
  802. B_(81), B_(82), B_(83), B_(84), B_(85), B_(86), B_(87), B_(88), B_(89), B_(90), B_(91), B_(92), B_(93), B_(94), B_(95), B_(96),
  803. B_(97), B_(98), B_(99), B_(100), B_(101), B_(102), B_(103), B_(104), B_(105), B_(106), B_(107), B_(108), B_(109), B_(110), B_(111),
  804. B_(112), B_(113), B_(114), B_(115), B_(116), B_(117), B_(118), B_(119), B_(120), B_(121), B_(122), B_(123), B_(124), B_(125),
  805. B_(126), B_(127), B_(-128), B_(-127), B_(-126), B_(-125), B_(-124), B_(-123), B_(-122), B_(-121), B_(-120), B_(-119), B_(-118),
  806. B_(-117), B_(-116), B_(-115), B_(-114), B_(-113), B_(-112), B_(-111), B_(-110), B_(-109), B_(-108), B_(-107), B_(-106), B_(-105),
  807. B_(-104), B_(-103), B_(-102), B_(-101), B_(-100), B_(-99), B_(-98), B_(-97), B_(-96), B_(-95), B_(-94), B_(-93), B_(-92), B_(-91),
  808. B_(-90), B_(-89), B_(-88), B_(-87), B_(-86), B_(-85), B_(-84), B_(-83), B_(-82), B_(-81), B_(-80), B_(-79), B_(-78), B_(-77),
  809. B_(-76), B_(-75), B_(-74), B_(-73), B_(-72), B_(-71), B_(-70), B_(-69), B_(-68), B_(-67), B_(-66), B_(-65), B_(-64), B_(-63),
  810. B_(-62), B_(-61), B_(-60), B_(-59), B_(-58), B_(-57), B_(-56), B_(-55), B_(-54), B_(-53), B_(-52), B_(-51), B_(-50), B_(-49),
  811. B_(-48), B_(-47), B_(-46), B_(-45), B_(-44), B_(-43), B_(-42), B_(-41), B_(-40), B_(-39), B_(-38), B_(-37), B_(-36), B_(-35),
  812. B_(-34), B_(-33), B_(-32), B_(-31), B_(-30), B_(-29), B_(-28), B_(-27), B_(-26), B_(-25), B_(-24), B_(-23), B_(-22), B_(-21),
  813. B_(-20), B_(-19), B_(-18), B_(-17), B_(-16), B_(-15), B_(-14), B_(-13), B_(-12), B_(-11), B_(-10), B_(-9), B_(-8), B_(-7),
  814. B_(-6), B_(-5), B_(-4), B_(-3), B_(-2), B_(-1)
  815. };
  816. #define UB_(a) mus_byte_to_sample(a)
  817. static const mus_float_t mus_ubyte[256] = {
  818. UB_(-128), UB_(-127), UB_(-126), UB_(-125), UB_(-124), UB_(-123), UB_(-122), UB_(-121), UB_(-120), UB_(-119), UB_(-118), UB_(-117),
  819. UB_(-116), UB_(-115), UB_(-114), UB_(-113), UB_(-112), UB_(-111), UB_(-110), UB_(-109), UB_(-108), UB_(-107), UB_(-106), UB_(-105),
  820. UB_(-104), UB_(-103), UB_(-102), UB_(-101), UB_(-100), UB_(-99), UB_(-98), UB_(-97), UB_(-96), UB_(-95), UB_(-94), UB_(-93), UB_(-92),
  821. UB_(-91), UB_(-90), UB_(-89), UB_(-88), UB_(-87), UB_(-86), UB_(-85), UB_(-84), UB_(-83), UB_(-82), UB_(-81), UB_(-80), UB_(-79),
  822. UB_(-78), UB_(-77), UB_(-76), UB_(-75), UB_(-74), UB_(-73), UB_(-72), UB_(-71), UB_(-70), UB_(-69), UB_(-68), UB_(-67), UB_(-66),
  823. UB_(-65), UB_(-64), UB_(-63), UB_(-62), UB_(-61), UB_(-60), UB_(-59), UB_(-58), UB_(-57), UB_(-56), UB_(-55), UB_(-54), UB_(-53),
  824. UB_(-52), UB_(-51), UB_(-50), UB_(-49), UB_(-48), UB_(-47), UB_(-46), UB_(-45), UB_(-44), UB_(-43), UB_(-42), UB_(-41), UB_(-40),
  825. UB_(-39), UB_(-38), UB_(-37), UB_(-36), UB_(-35), UB_(-34), UB_(-33), UB_(-32), UB_(-31), UB_(-30), UB_(-29), UB_(-28), UB_(-27),
  826. UB_(-26), UB_(-25), UB_(-24), UB_(-23), UB_(-22), UB_(-21), UB_(-20), UB_(-19), UB_(-18), UB_(-17), UB_(-16), UB_(-15), UB_(-14),
  827. UB_(-13), UB_(-12), UB_(-11), UB_(-10), UB_(-9), UB_(-8), UB_(-7), UB_(-6), UB_(-5), UB_(-4), UB_(-3), UB_(-2), UB_(-1), UB_(0),
  828. UB_(1), UB_(2), UB_(3), UB_(4), UB_(5), UB_(6), UB_(7), UB_(8), UB_(9), UB_(10), UB_(11), UB_(12), UB_(13), UB_(14), UB_(15),
  829. UB_(16), UB_(17), UB_(18), UB_(19), UB_(20), UB_(21), UB_(22), UB_(23), UB_(24), UB_(25), UB_(26), UB_(27), UB_(28), UB_(29),
  830. UB_(30), UB_(31), UB_(32), UB_(33), UB_(34), UB_(35), UB_(36), UB_(37), UB_(38), UB_(39), UB_(40), UB_(41), UB_(42), UB_(43),
  831. UB_(44), UB_(45), UB_(46), UB_(47), UB_(48), UB_(49), UB_(50), UB_(51), UB_(52), UB_(53), UB_(54), UB_(55), UB_(56), UB_(57),
  832. UB_(58), UB_(59), UB_(60), UB_(61), UB_(62), UB_(63), UB_(64), UB_(65), UB_(66), UB_(67), UB_(68), UB_(69), UB_(70), UB_(71),
  833. UB_(72), UB_(73), UB_(74), UB_(75), UB_(76), UB_(77), UB_(78), UB_(79), UB_(80), UB_(81), UB_(82), UB_(83), UB_(84), UB_(85),
  834. UB_(86), UB_(87), UB_(88), UB_(89), UB_(90), UB_(91), UB_(92), UB_(93), UB_(94), UB_(95), UB_(96), UB_(97), UB_(98), UB_(99),
  835. UB_(100), UB_(101), UB_(102), UB_(103), UB_(104), UB_(105), UB_(106), UB_(107), UB_(108), UB_(109), UB_(110), UB_(111), UB_(112),
  836. UB_(113), UB_(114), UB_(115), UB_(116), UB_(117), UB_(118), UB_(119), UB_(120), UB_(121), UB_(122), UB_(123), UB_(124), UB_(125),
  837. UB_(126), UB_(127)
  838. };
  839. /* ---------------- read ---------------- */
  840. #define BUFLIM (64 * 1024)
  841. #define UBYTE_ZERO 128
  842. #define USHORT_ZERO 32768
  843. #define MUS_SAMPLE_UNSCALED(n) ((n) / 32768.0)
  844. /* see note in _sndlib.h" values are "unscaled" from the DAC's point of view */
  845. static mus_float_t *swapped_shorts = NULL;
  846. static void initialize_swapped_shorts(void)
  847. {
  848. int i;
  849. swapped_shorts = (mus_float_t *)malloc(65536 * sizeof(mus_float_t));
  850. for (i = 0; i < 65536; i++)
  851. {
  852. signed short x;
  853. x = (signed short)(((i >> 8) & 0xff) | ((i & 0xff) << 8));
  854. swapped_shorts[i] = mus_short_to_sample(x);
  855. }
  856. }
  857. static mus_long_t mus_read_any_1(int tfd, mus_long_t beg, int chans, mus_long_t nints, mus_float_t **bufs, mus_float_t **cm, char *inbuf)
  858. {
  859. /* beg no longer means buffer-relative start point (that is assumed to be 0): changed 18-Dec-13
  860. * beg is now the starting frample number in the file data (first frample = 0)
  861. * so old form
  862. * mus_read_any_1(f, 0, ...)
  863. * is now
  864. * mus_read_any_1(f, frample, ...)
  865. */
  866. mus_sample_t samp_type;
  867. int siz, siz_chans;
  868. mus_long_t bytes, lim, leftover, total_read, k, loc, buflim;
  869. unsigned char *jchar;
  870. static char *ur_charbuf = NULL;
  871. char *charbuf = NULL;
  872. mus_float_t *buffer;
  873. if (nints <= 0) return(0);
  874. if (!inbuf)
  875. {
  876. io_fd *fd;
  877. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL))
  878. return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED, "mus_read: no file descriptors!"));
  879. fd = io_fds[tfd];
  880. if (fd->sample_type == MUS_UNKNOWN_SAMPLE)
  881. return(mus_error(MUS_FILE_CLOSED, "mus_read: invalid sample type for %s", fd->name));
  882. samp_type = fd->sample_type;
  883. siz = fd->bytes_per_sample;
  884. if ((samp_type == MUS_OUT_SAMPLE_TYPE) &&
  885. (chans == 1))
  886. /* (beg == 0)) */
  887. {
  888. ssize_t total;
  889. bytes = nints * siz;
  890. total = read(tfd, (char *)(bufs[0]), bytes);
  891. if (total != bytes)
  892. {
  893. if (total <= 0)
  894. memset((void *)(bufs[0]), 0, bytes);
  895. else
  896. {
  897. int i;
  898. for (i = total / siz; i < nints; i++)
  899. bufs[0][i] = 0.0;
  900. }
  901. }
  902. return(total / siz);
  903. }
  904. if (fd->saved)
  905. {
  906. /* fprintf(stderr, "mus_read_any_1 %d use saved data\n", tfd); */
  907. lim = nints;
  908. if (lim > fd->framples)
  909. lim = fd->framples;
  910. bytes = lim * sizeof(mus_float_t);
  911. if (beg < 0) beg = 0;
  912. if ((chans == 1) &&
  913. ((!cm) || (cm[0])))
  914. {
  915. buffer = (mus_float_t *)(bufs[0]);
  916. if (buffer)
  917. {
  918. memcpy((void *)buffer, (void *)(fd->saved_data[0] + beg), bytes);
  919. if (lim < nints)
  920. memset((void *)(buffer + lim), 0, (nints - lim) * sizeof(mus_float_t));
  921. }
  922. }
  923. else
  924. {
  925. for (k = 0; k < chans; k++)
  926. if ((cm == NULL) || (cm[k]))
  927. {
  928. buffer = (mus_float_t *)(bufs[k]);
  929. if (buffer)
  930. {
  931. memcpy((void *)buffer, (void *)(fd->saved_data[k] + beg), bytes);
  932. if (lim < nints)
  933. memset((void *)(buffer + lim), 0, (nints - lim) * sizeof(mus_float_t));
  934. }
  935. }
  936. }
  937. return(lim);
  938. }
  939. if (ur_charbuf == NULL)
  940. ur_charbuf = (char *)malloc(BUFLIM * sizeof(char));
  941. charbuf = ur_charbuf;
  942. }
  943. else
  944. {
  945. charbuf = inbuf;
  946. siz = mus_bytes_per_sample((mus_sample_t)tfd);
  947. samp_type = (mus_sample_t)tfd;
  948. }
  949. siz_chans = siz * chans;
  950. leftover = (nints * siz_chans);
  951. k = (BUFLIM) % siz_chans;
  952. if (k != 0) /* for example, 3 channel output of 1-byte (mulaw) samples will need a mod 3 buffer */
  953. buflim = (BUFLIM) - k;
  954. else buflim = BUFLIM;
  955. total_read = 0;
  956. loc = 0;
  957. #if MUS_LITTLE_ENDIAN
  958. if ((samp_type == MUS_BSHORT) && (!swapped_shorts))
  959. initialize_swapped_shorts();
  960. #else
  961. if ((samp_type == MUS_LSHORT) && (!swapped_shorts))
  962. initialize_swapped_shorts();
  963. #endif
  964. while (leftover > 0)
  965. {
  966. mus_long_t oldloc, loclim;
  967. mus_float_t *bufnow, *bufend, *bufend4;
  968. bytes = leftover;
  969. if (bytes > buflim)
  970. {
  971. leftover = (bytes - buflim);
  972. bytes = buflim;
  973. }
  974. else leftover = 0;
  975. if (!inbuf)
  976. {
  977. ssize_t total;
  978. total = read(tfd, charbuf, bytes);
  979. if (total <= 0)
  980. {
  981. /* zero out trailing section (some callers don't check the returned value) -- this added 9-May-99 */
  982. if (loc < nints)
  983. for (k = 0; k < chans; k++)
  984. if ((cm == NULL) || (cm[k]))
  985. {
  986. mus_float_t *p;
  987. p = bufs[k];
  988. memset((void *)(p + loc), 0, (nints - loc) * sizeof(mus_float_t));
  989. }
  990. return(total_read);
  991. }
  992. lim = (int) (total / siz_chans); /* this divide must be exact (hence the buflim calc above) */
  993. }
  994. else
  995. {
  996. lim = nints; /* framples in this case */
  997. leftover = 0;
  998. }
  999. total_read += lim;
  1000. oldloc = loc;
  1001. if ((chans == 1) &&
  1002. ((!cm) || (cm[0])))
  1003. {
  1004. buffer = (mus_float_t *)(bufs[0]);
  1005. if (buffer)
  1006. {
  1007. loc = oldloc;
  1008. loclim = loc + lim;
  1009. bufnow = (mus_float_t *)(buffer + loc);
  1010. bufend = (mus_float_t *)(buffer + loclim - 1);
  1011. bufend4 = (mus_float_t *)(bufend - 4);
  1012. jchar = (unsigned char *)charbuf;
  1013. switch (samp_type)
  1014. {
  1015. case MUS_BSHORT:
  1016. #if MUS_LITTLE_ENDIAN
  1017. while (bufnow <= bufend4)
  1018. {
  1019. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1020. jchar += 2;
  1021. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1022. jchar += 2;
  1023. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1024. jchar += 2;
  1025. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1026. jchar += 2;
  1027. }
  1028. for (; bufnow <= bufend; jchar += 2) /* unsigned short here as charbuf loc is slower */
  1029. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))]; /* bswap16 is much slower here because of the subsequent short->double conversion */
  1030. #else
  1031. for (; bufnow <= bufend; jchar += 2)
  1032. (*bufnow++) = mus_short_to_sample(big_endian_short(jchar));
  1033. #endif
  1034. break;
  1035. case MUS_LSHORT:
  1036. #if (!MUS_LITTLE_ENDIAN)
  1037. for (; bufnow <= bufend; jchar += 2)
  1038. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1039. #else
  1040. while (bufnow <= bufend4)
  1041. {
  1042. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1043. jchar += 2;
  1044. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1045. jchar += 2;
  1046. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1047. jchar += 2;
  1048. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1049. jchar += 2;
  1050. }
  1051. for (; bufnow <= bufend; jchar += 2)
  1052. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1053. #endif
  1054. break;
  1055. case MUS_BINT:
  1056. while (bufnow <= bufend4)
  1057. {
  1058. (*bufnow++) = mus_int_to_sample(big_endian_int(jchar));
  1059. jchar += 4;
  1060. (*bufnow++) = mus_int_to_sample(big_endian_int(jchar));
  1061. jchar += 4;
  1062. (*bufnow++) = mus_int_to_sample(big_endian_int(jchar));
  1063. jchar += 4;
  1064. (*bufnow++) = mus_int_to_sample(big_endian_int(jchar));
  1065. jchar += 4;
  1066. }
  1067. for (; bufnow <= bufend; jchar += 4)
  1068. (*bufnow++) = mus_int_to_sample(big_endian_int(jchar));
  1069. break;
  1070. case MUS_LINT:
  1071. while (bufnow <= bufend4)
  1072. {
  1073. (*bufnow++) = mus_int_to_sample(little_endian_int(jchar));
  1074. jchar += 4;
  1075. (*bufnow++) = mus_int_to_sample(little_endian_int(jchar));
  1076. jchar += 4;
  1077. (*bufnow++) = mus_int_to_sample(little_endian_int(jchar));
  1078. jchar += 4;
  1079. (*bufnow++) = mus_int_to_sample(little_endian_int(jchar));
  1080. jchar += 4;
  1081. }
  1082. for (; bufnow <= bufend; jchar += 4)
  1083. (*bufnow++) = mus_int_to_sample(little_endian_int(jchar));
  1084. break;
  1085. case MUS_BINTN:
  1086. for (; bufnow <= bufend; jchar += 4)
  1087. (*bufnow++) = mus_int_to_sample((big_endian_int(jchar) >> 8));
  1088. break;
  1089. case MUS_LINTN:
  1090. for (; bufnow <= bufend; jchar += 4)
  1091. (*bufnow++) = mus_int_to_sample((little_endian_int(jchar) >> 8));
  1092. break;
  1093. case MUS_MULAW:
  1094. while (bufnow <= bufend4)
  1095. {
  1096. (*bufnow++) = mus_mulaw[*jchar++];
  1097. (*bufnow++) = mus_mulaw[*jchar++];
  1098. (*bufnow++) = mus_mulaw[*jchar++];
  1099. (*bufnow++) = mus_mulaw[*jchar++];
  1100. }
  1101. for (; bufnow <= bufend; jchar++)
  1102. (*bufnow++) = mus_mulaw[*jchar];
  1103. break;
  1104. case MUS_ALAW:
  1105. while (bufnow <= bufend4)
  1106. {
  1107. (*bufnow++) = mus_alaw[*jchar++];
  1108. (*bufnow++) = mus_alaw[*jchar++];
  1109. (*bufnow++) = mus_alaw[*jchar++];
  1110. (*bufnow++) = mus_alaw[*jchar++];
  1111. }
  1112. for (; bufnow <= bufend; jchar++)
  1113. (*bufnow++) = mus_alaw[*jchar];
  1114. break;
  1115. case MUS_BYTE:
  1116. while (bufnow <= bufend4)
  1117. {
  1118. (*bufnow++) = mus_byte[(unsigned char)(*jchar++)];
  1119. (*bufnow++) = mus_byte[(unsigned char)(*jchar++)];
  1120. (*bufnow++) = mus_byte[(unsigned char)(*jchar++)];
  1121. (*bufnow++) = mus_byte[(unsigned char)(*jchar++)];
  1122. }
  1123. for (; bufnow <= bufend; jchar++)
  1124. (*bufnow++) = mus_byte[(unsigned char)(*jchar)];
  1125. break;
  1126. case MUS_UBYTE:
  1127. while (bufnow <= bufend4)
  1128. {
  1129. (*bufnow++) = mus_ubyte[(unsigned char)(*jchar++)];
  1130. (*bufnow++) = mus_ubyte[(unsigned char)(*jchar++)];
  1131. (*bufnow++) = mus_ubyte[(unsigned char)(*jchar++)];
  1132. (*bufnow++) = mus_ubyte[(unsigned char)(*jchar++)];
  1133. }
  1134. for (; bufnow <= bufend; jchar++)
  1135. (*bufnow++) = mus_ubyte[(unsigned char)(*jchar)];
  1136. break;
  1137. case MUS_BFLOAT:
  1138. while (bufnow <= bufend4)
  1139. {
  1140. (*bufnow++) = (mus_float_t)(big_endian_float(jchar));
  1141. jchar += 4;
  1142. (*bufnow++) = (mus_float_t)(big_endian_float(jchar));
  1143. jchar += 4;
  1144. (*bufnow++) = (mus_float_t)(big_endian_float(jchar));
  1145. jchar += 4;
  1146. (*bufnow++) = (mus_float_t)(big_endian_float(jchar));
  1147. jchar += 4;
  1148. }
  1149. for (; bufnow <= bufend; jchar += 4)
  1150. (*bufnow++) = (mus_float_t)(big_endian_float(jchar));
  1151. break;
  1152. case MUS_BFLOAT_UNSCALED:
  1153. for (; bufnow <= bufend; jchar += 4)
  1154. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(big_endian_float(jchar)));
  1155. break;
  1156. case MUS_BDOUBLE:
  1157. while (bufnow <= bufend4)
  1158. {
  1159. (*bufnow++) = (mus_float_t)(big_endian_double(jchar));
  1160. jchar += 8;
  1161. (*bufnow++) = (mus_float_t)(big_endian_double(jchar));
  1162. jchar += 8;
  1163. (*bufnow++) = (mus_float_t)(big_endian_double(jchar));
  1164. jchar += 8;
  1165. (*bufnow++) = (mus_float_t)(big_endian_double(jchar));
  1166. jchar += 8;
  1167. }
  1168. for (; bufnow <= bufend; jchar += 8)
  1169. (*bufnow++) = (mus_float_t)(big_endian_double(jchar));
  1170. break;
  1171. case MUS_BDOUBLE_UNSCALED:
  1172. for (; bufnow <= bufend; jchar += 8)
  1173. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(big_endian_double(jchar)));
  1174. break;
  1175. case MUS_LFLOAT:
  1176. while (bufnow <= bufend4)
  1177. {
  1178. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1179. jchar += 4;
  1180. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1181. jchar += 4;
  1182. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1183. jchar += 4;
  1184. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1185. jchar += 4;
  1186. }
  1187. for (; bufnow <= bufend; jchar += 4)
  1188. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1189. break;
  1190. case MUS_LFLOAT_UNSCALED:
  1191. for (; bufnow <= bufend; jchar += 4)
  1192. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(little_endian_float(jchar)));
  1193. break;
  1194. case MUS_LDOUBLE:
  1195. for (; bufnow <= bufend; jchar += 8)
  1196. (*bufnow++) = (mus_float_t)(little_endian_double(jchar));
  1197. break;
  1198. case MUS_LDOUBLE_UNSCALED:
  1199. for (; bufnow <= bufend; jchar += 8)
  1200. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(little_endian_double(jchar)));
  1201. break;
  1202. case MUS_UBSHORT:
  1203. while (bufnow <= bufend4)
  1204. {
  1205. (*bufnow++) = mus_short_to_sample((int)(big_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1206. jchar += 2;
  1207. (*bufnow++) = mus_short_to_sample((int)(big_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1208. jchar += 2;
  1209. (*bufnow++) = mus_short_to_sample((int)(big_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1210. jchar += 2;
  1211. (*bufnow++) = mus_short_to_sample((int)(big_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1212. jchar += 2;
  1213. }
  1214. for (; bufnow <= bufend; jchar += 2)
  1215. (*bufnow++) = mus_short_to_sample((int)(big_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1216. break;
  1217. case MUS_ULSHORT:
  1218. while (bufnow <= bufend4)
  1219. {
  1220. (*bufnow++) = mus_short_to_sample((int)(little_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1221. jchar += 2;
  1222. (*bufnow++) = mus_short_to_sample((int)(little_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1223. jchar += 2;
  1224. (*bufnow++) = mus_short_to_sample((int)(little_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1225. jchar += 2;
  1226. (*bufnow++) = mus_short_to_sample((int)(little_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1227. jchar += 2;
  1228. }
  1229. for (; bufnow <= bufend; jchar += 2)
  1230. (*bufnow++) = mus_short_to_sample((int)(little_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1231. break;
  1232. case MUS_B24INT:
  1233. while (bufnow <= bufend4)
  1234. {
  1235. (*bufnow++) = bint24_to_sample(jchar);
  1236. jchar += 3;
  1237. (*bufnow++) = bint24_to_sample(jchar);
  1238. jchar += 3;
  1239. (*bufnow++) = bint24_to_sample(jchar);
  1240. jchar += 3;
  1241. (*bufnow++) = bint24_to_sample(jchar);
  1242. jchar += 3;
  1243. }
  1244. for (; bufnow <= bufend; jchar += 3)
  1245. (*bufnow++) = bint24_to_sample(jchar);
  1246. break;
  1247. case MUS_L24INT:
  1248. {
  1249. int val;
  1250. val = (jchar[2] << 16) + (jchar[1] << 8) + jchar[0];
  1251. if (val >= (1 << 23)) val -= (1 << 24);
  1252. (*bufnow++) = mus_int24_to_sample(val);
  1253. jchar += 2;
  1254. }
  1255. while (bufnow <= bufend4)
  1256. {
  1257. (*bufnow++) = int24_to_sample(jchar);
  1258. jchar += 3;
  1259. (*bufnow++) = int24_to_sample(jchar);
  1260. jchar += 3;
  1261. (*bufnow++) = int24_to_sample(jchar);
  1262. jchar += 3;
  1263. (*bufnow++) = int24_to_sample(jchar);
  1264. jchar += 3;
  1265. }
  1266. for (; bufnow <= bufend; jchar += 3)
  1267. (*bufnow++) = int24_to_sample(jchar);
  1268. break;
  1269. default: break;
  1270. }
  1271. loc = loclim;
  1272. }
  1273. }
  1274. else
  1275. {
  1276. for (k = 0; k < chans; k++)
  1277. {
  1278. if ((cm == NULL) || (cm[k]))
  1279. {
  1280. buffer = (mus_float_t *)(bufs[k]);
  1281. if (buffer)
  1282. {
  1283. loc = oldloc;
  1284. loclim = loc + lim;
  1285. bufnow = (mus_float_t *)(buffer + loc);
  1286. bufend = (mus_float_t *)(buffer + loclim - 1);
  1287. bufend4 = (mus_float_t *)(bufend - 4);
  1288. jchar = (unsigned char *)charbuf;
  1289. jchar += (k * siz);
  1290. switch (samp_type)
  1291. {
  1292. case MUS_BSHORT:
  1293. #if MUS_LITTLE_ENDIAN
  1294. while (bufnow <= bufend4)
  1295. {
  1296. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1297. jchar += siz_chans;
  1298. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1299. jchar += siz_chans;
  1300. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1301. jchar += siz_chans;
  1302. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1303. jchar += siz_chans;
  1304. }
  1305. for (; bufnow <= bufend; jchar += siz_chans)
  1306. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1307. #else
  1308. for (; bufnow <= bufend; jchar += siz_chans)
  1309. (*bufnow++) = mus_short_to_sample(big_endian_short(jchar));
  1310. #endif
  1311. break;
  1312. case MUS_LSHORT:
  1313. #if (!MUS_LITTLE_ENDIAN)
  1314. for (; bufnow <= bufend; jchar += siz_chans)
  1315. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1316. #else
  1317. while (bufnow <= bufend4)
  1318. {
  1319. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1320. jchar += siz_chans;
  1321. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1322. jchar += siz_chans;
  1323. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1324. jchar += siz_chans;
  1325. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1326. jchar += siz_chans;
  1327. }
  1328. for (; bufnow <= bufend; jchar += siz_chans)
  1329. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1330. #endif
  1331. break;
  1332. case MUS_BINT:
  1333. for (; bufnow <= bufend; jchar += siz_chans)
  1334. (*bufnow++) = mus_int_to_sample(big_endian_int(jchar));
  1335. break;
  1336. case MUS_LINT:
  1337. for (; bufnow <= bufend; jchar += siz_chans)
  1338. (*bufnow++) = mus_int_to_sample(little_endian_int(jchar));
  1339. break;
  1340. case MUS_BINTN:
  1341. for (; bufnow <= bufend; jchar += siz_chans)
  1342. (*bufnow++) = mus_int_to_sample((big_endian_int(jchar) >> 8));
  1343. break;
  1344. case MUS_LINTN:
  1345. for (; bufnow <= bufend; jchar += siz_chans)
  1346. (*bufnow++) = mus_int_to_sample((little_endian_int(jchar) >> 8));
  1347. break;
  1348. case MUS_MULAW:
  1349. for (; bufnow <= bufend; jchar += siz_chans)
  1350. (*bufnow++) = mus_mulaw[*jchar];
  1351. break;
  1352. case MUS_ALAW:
  1353. for (; bufnow <= bufend; jchar += siz_chans)
  1354. (*bufnow++) = mus_alaw[*jchar];
  1355. break;
  1356. case MUS_BYTE:
  1357. for (; bufnow <= bufend; jchar += siz_chans)
  1358. (*bufnow++) = mus_byte[(unsigned char)(*jchar)];
  1359. break;
  1360. case MUS_UBYTE:
  1361. for (; bufnow <= bufend; jchar += siz_chans)
  1362. (*bufnow++) = mus_ubyte[(unsigned char)(*jchar)];
  1363. break;
  1364. case MUS_BFLOAT:
  1365. for (; bufnow <= bufend; jchar += siz_chans)
  1366. (*bufnow++) = (mus_float_t)(big_endian_float(jchar));
  1367. break;
  1368. case MUS_BFLOAT_UNSCALED:
  1369. for (; bufnow <= bufend; jchar += siz_chans)
  1370. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(big_endian_float(jchar)));
  1371. break;
  1372. case MUS_BDOUBLE:
  1373. for (; bufnow <= bufend; jchar += siz_chans)
  1374. (*bufnow++) = (mus_float_t)(big_endian_double(jchar));
  1375. break;
  1376. case MUS_BDOUBLE_UNSCALED:
  1377. for (; bufnow <= bufend; jchar += siz_chans)
  1378. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(big_endian_double(jchar)));
  1379. break;
  1380. case MUS_LFLOAT:
  1381. while (bufnow <= bufend4)
  1382. {
  1383. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1384. jchar += siz_chans;
  1385. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1386. jchar += siz_chans;
  1387. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1388. jchar += siz_chans;
  1389. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1390. jchar += siz_chans;
  1391. }
  1392. for (; bufnow <= bufend; jchar += siz_chans)
  1393. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1394. break;
  1395. case MUS_LFLOAT_UNSCALED:
  1396. for (; bufnow <= bufend; jchar += siz_chans)
  1397. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(little_endian_float(jchar)));
  1398. break;
  1399. case MUS_LDOUBLE:
  1400. while (bufnow <= bufend4)
  1401. {
  1402. (*bufnow++) = (mus_float_t)(little_endian_double(jchar));
  1403. jchar += siz_chans;
  1404. (*bufnow++) = (mus_float_t)(little_endian_double(jchar));
  1405. jchar += siz_chans;
  1406. (*bufnow++) = (mus_float_t)(little_endian_double(jchar));
  1407. jchar += siz_chans;
  1408. (*bufnow++) = (mus_float_t)(little_endian_double(jchar));
  1409. jchar += siz_chans;
  1410. }
  1411. for (; bufnow <= bufend; jchar += siz_chans)
  1412. (*bufnow++) = (mus_float_t)(little_endian_double(jchar));
  1413. break;
  1414. case MUS_LDOUBLE_UNSCALED:
  1415. for (; bufnow <= bufend; jchar += siz_chans)
  1416. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(little_endian_double(jchar)));
  1417. break;
  1418. case MUS_UBSHORT:
  1419. for (; bufnow <= bufend; jchar += siz_chans)
  1420. (*bufnow++) = mus_short_to_sample((int)(big_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1421. break;
  1422. case MUS_ULSHORT:
  1423. for (; bufnow <= bufend; jchar += siz_chans)
  1424. (*bufnow++) = mus_short_to_sample((int)(little_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1425. break;
  1426. case MUS_B24INT:
  1427. for (; bufnow <= bufend; jchar += siz_chans)
  1428. (*bufnow++) = bint24_to_sample(jchar);
  1429. break;
  1430. case MUS_L24INT:
  1431. {
  1432. /*align for little_endian_int as above */
  1433. int val;
  1434. val = (jchar[2] << 16) + (jchar[1] << 8) + jchar[0];
  1435. if (val >= (1 << 23)) val -= (1 << 24);
  1436. (*bufnow++) = mus_int24_to_sample(val);
  1437. jchar += siz_chans - 1;
  1438. }
  1439. for (; bufnow <= bufend; jchar += siz_chans)
  1440. (*bufnow++) = int24_to_sample(jchar);
  1441. break;
  1442. default: break;
  1443. }
  1444. loc = loclim;
  1445. }
  1446. }
  1447. }
  1448. }
  1449. }
  1450. return(total_read);
  1451. }
  1452. mus_long_t mus_file_read_any(int tfd, mus_long_t beg, int chans, mus_long_t nints, mus_float_t **bufs, mus_float_t **cm)
  1453. {
  1454. return(mus_read_any_1(tfd, beg, chans, nints, bufs, cm, NULL));
  1455. }
  1456. mus_long_t mus_file_read_file(int tfd, mus_long_t beg, int chans, mus_long_t nints, mus_float_t **bufs)
  1457. {
  1458. /* not currently used anywhere */
  1459. return(mus_read_any_1(tfd, beg, chans, nints, bufs, NULL, NULL));
  1460. }
  1461. mus_long_t mus_file_read_buffer(int charbuf_sample_type, mus_long_t beg, int chans, mus_long_t nints, mus_float_t **bufs, char *charbuf)
  1462. {
  1463. return(mus_read_any_1(charbuf_sample_type, beg, chans, nints, bufs, NULL, charbuf));
  1464. }
  1465. /* the next two were changed 19-Dec-13 (sndlib.h version 23.1)
  1466. * "end" is now actually "dur" -- the number of samples to read, not the end point in the buffer
  1467. * "beg" is the frample number to start at in the data, not the buffer location
  1468. * so old form
  1469. * mus_file_read(f, 0, 99...)
  1470. * is now
  1471. * mus_file_read(f, frample, 100...)
  1472. */
  1473. mus_long_t mus_file_read(int tfd, mus_long_t beg, mus_long_t num, int chans, mus_float_t **bufs)
  1474. {
  1475. mus_long_t rtn;
  1476. rtn = mus_read_any_1(tfd, beg, chans, num, bufs, NULL, NULL);
  1477. if (rtn == MUS_ERROR) return(MUS_ERROR);
  1478. if (rtn < num)
  1479. {
  1480. mus_long_t k;
  1481. /* this zeroing can be fooled if the file is chunked and has trailing, non-data chunks */
  1482. for (k = 0; k < chans; k++)
  1483. {
  1484. mus_float_t *buffer;
  1485. buffer = bufs[k];
  1486. /* this happens routinely in mus_outa + initial write (reads ahead in effect) */
  1487. /* fprintf(stderr, "clear from %lld for %lld\n", rtn, num-rtn); */
  1488. memset((void *)(buffer + rtn), 0, (num - rtn) * sizeof(mus_float_t));
  1489. }
  1490. }
  1491. return(num);
  1492. }
  1493. mus_long_t mus_file_read_chans(int tfd, mus_long_t beg, mus_long_t num, int chans, mus_float_t **bufs, mus_float_t **cm)
  1494. {
  1495. /* an optimization of mus_file_read -- just reads the desired channels */
  1496. mus_long_t rtn, k;
  1497. rtn = mus_read_any_1(tfd, beg, chans, num, bufs, cm, NULL);
  1498. if (rtn == MUS_ERROR) return(MUS_ERROR);
  1499. if (rtn < num)
  1500. for (k = 0; k < chans; k++)
  1501. if ((cm == NULL) || (cm[k]))
  1502. {
  1503. mus_float_t *buffer;
  1504. buffer = bufs[k];
  1505. memset((void *)(buffer + rtn), 0, (num - rtn) * sizeof(mus_float_t));
  1506. }
  1507. return(num);
  1508. }
  1509. /* ---------------- write ---------------- */
  1510. static int checked_write(int tfd, char *buf, mus_long_t chars)
  1511. {
  1512. long long int bytes;
  1513. bytes = (long long int)write(tfd, buf, chars);
  1514. if (bytes != chars)
  1515. {
  1516. io_fd *fd;
  1517. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL))
  1518. return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED, "mus_write: no file descriptors!"));
  1519. fd = io_fds[tfd];
  1520. if (fd->sample_type == MUS_UNKNOWN_SAMPLE)
  1521. return(mus_error(MUS_FILE_CLOSED,
  1522. "attempt to write closed file %s",
  1523. fd->name));
  1524. else
  1525. return(mus_error(MUS_WRITE_ERROR,
  1526. "mus_write: write error for %s%s%s: only %lld of %lld" " bytes written",
  1527. fd->name, (errno) ? ": " : "", (errno) ? STRERROR(errno) : "",
  1528. bytes, chars));
  1529. }
  1530. return(MUS_NO_ERROR);
  1531. }
  1532. static mus_clip_handler_t *mus_clip_handler = NULL;
  1533. static bool (*clip_checker)(void) = NULL;
  1534. mus_clip_handler_t *mus_clip_set_handler(mus_clip_handler_t *new_clip_handler)
  1535. {
  1536. mus_clip_handler_t *old_handler;
  1537. old_handler = mus_clip_handler;
  1538. mus_clip_handler = new_clip_handler;
  1539. return(old_handler);
  1540. }
  1541. mus_clip_handler_t *mus_clip_set_handler_and_checker(mus_clip_handler_t *new_clip_handler, bool (*checker)(void))
  1542. {
  1543. mus_clip_handler_t *old_handler;
  1544. old_handler = mus_clip_handler;
  1545. mus_clip_handler = new_clip_handler;
  1546. clip_checker = checker;
  1547. return(old_handler);
  1548. }
  1549. static int mus_write_1(int tfd, mus_long_t beg, mus_long_t end, int chans, mus_float_t **bufs, char *inbuf, bool clipped)
  1550. {
  1551. int err, siz, siz_chans, val;
  1552. mus_sample_t sample_type;
  1553. mus_long_t bytes, k, lim, leftover, loc, buflim;
  1554. bool clipping = false;
  1555. unsigned char *jchar;
  1556. static char *ur_charbuf = NULL;
  1557. char *charbuf = NULL;
  1558. mus_float_t *buffer;
  1559. if (chans <= 0) return(MUS_NO_ERROR); /* ?? */
  1560. if (!inbuf)
  1561. {
  1562. io_fd *fd;
  1563. if ((io_fds == NULL) ||
  1564. (tfd >= io_fd_size) ||
  1565. (tfd < 0) ||
  1566. (io_fds[tfd] == NULL))
  1567. return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED, "mus_write: no file descriptors!"));
  1568. fd = io_fds[tfd];
  1569. if (fd->sample_type == MUS_UNKNOWN_SAMPLE)
  1570. return(mus_error(MUS_FILE_CLOSED, "mus_write: invalid sample type for %s", fd->name));
  1571. siz = fd->bytes_per_sample;
  1572. sample_type = fd->sample_type;
  1573. clipping = fd->clipping;
  1574. if ((sample_type == MUS_OUT_SAMPLE_TYPE) &&
  1575. (chans == 1) &&
  1576. (!clipping) &&
  1577. (beg == 0))
  1578. {
  1579. bytes = (end + 1) * siz;
  1580. return(checked_write(tfd, (char *)(bufs[0]), bytes));
  1581. }
  1582. }
  1583. else
  1584. {
  1585. siz = mus_bytes_per_sample((mus_sample_t)tfd);
  1586. sample_type = (mus_sample_t)tfd; /* in this case, tfd is the sample type (see mus_file_write_buffer below) -- this should be changed! */
  1587. clipping = clipped;
  1588. }
  1589. lim = (end - beg + 1);
  1590. siz_chans = siz * chans;
  1591. leftover = lim * siz_chans;
  1592. k = (BUFLIM) % siz_chans;
  1593. if (k != 0)
  1594. buflim = (BUFLIM) - k;
  1595. else buflim = BUFLIM;
  1596. loc = beg;
  1597. if (inbuf)
  1598. charbuf = inbuf;
  1599. while (leftover > 0)
  1600. {
  1601. int oldloc;
  1602. bytes = leftover;
  1603. if (bytes > buflim)
  1604. {
  1605. leftover = (bytes - buflim);
  1606. bytes = buflim;
  1607. }
  1608. else leftover = 0;
  1609. lim = (int)(bytes / siz_chans); /* see note above */
  1610. oldloc = loc;
  1611. for (k = 0; k < chans; k++)
  1612. {
  1613. mus_long_t loclim;
  1614. mus_float_t *bufnow, *bufend, *bufend4;
  1615. if (bufs[k] == NULL) continue;
  1616. loc = oldloc;
  1617. buffer = (mus_float_t *)(bufs[k]);
  1618. if (clipping)
  1619. {
  1620. int clipend;
  1621. mus_float_t sample;
  1622. clipend = oldloc + lim;
  1623. bufnow = (mus_float_t *)(buffer + oldloc);
  1624. bufend = (mus_float_t *)(buffer + clipend - 1);
  1625. bufend4 = (mus_float_t *)(bufend - 4);
  1626. if (clip_checker) clip_checker();
  1627. if (mus_clip_handler)
  1628. {
  1629. for (; bufnow <= bufend; bufnow++)
  1630. {
  1631. sample = (*bufnow);
  1632. if ((sample >= 1.0) || (sample < -1.0)) (*bufnow) = (*mus_clip_handler)(sample);
  1633. }
  1634. }
  1635. else
  1636. {
  1637. while (bufnow <= bufend4)
  1638. {
  1639. sample = (*bufnow);
  1640. if (sample >= 1.0) (*bufnow) = 0.99999; else if (sample < -1.0) (*bufnow) = -1.0;
  1641. bufnow++;
  1642. sample = (*bufnow);
  1643. if (sample >= 1.0) (*bufnow) = 0.99999; else if (sample < -1.0) (*bufnow) = -1.0;
  1644. bufnow++;
  1645. sample = (*bufnow);
  1646. if (sample >= 1.0) (*bufnow) = 0.99999; else if (sample < -1.0) (*bufnow) = -1.0;
  1647. bufnow++;
  1648. sample = (*bufnow);
  1649. if (sample >= 1.0) (*bufnow) = 0.99999; else if (sample < -1.0) (*bufnow) = -1.0;
  1650. bufnow++;
  1651. }
  1652. for (; bufnow <= bufend; bufnow++)
  1653. {
  1654. sample = (*bufnow);
  1655. if (sample >= 1.0) (*bufnow) = 0.99999; else if (sample < -1.0) (*bufnow) = -1.0;
  1656. }
  1657. }
  1658. }
  1659. if ((sample_type == MUS_OUT_SAMPLE_TYPE) &&
  1660. (chans == 1) &&
  1661. (beg == 0) &&
  1662. (!inbuf)) /* "tfd" can be sample type */
  1663. {
  1664. bytes = (end + 1) * siz;
  1665. return(checked_write(tfd, (char *)(bufs[0]), bytes));
  1666. }
  1667. loclim = loc + lim;
  1668. if (!charbuf)
  1669. {
  1670. if (ur_charbuf == NULL)
  1671. ur_charbuf = (char *)malloc(BUFLIM * sizeof(char));
  1672. /*
  1673. ur_charbuf = (char *)calloc(BUFLIM, sizeof(char));
  1674. else memset((void *)ur_charbuf, 0, BUFLIM);
  1675. */
  1676. charbuf = ur_charbuf;
  1677. }
  1678. bufnow = (mus_float_t *)(buffer + loc);
  1679. bufend = (mus_float_t *)(buffer + loclim - 1);
  1680. bufend4 = (mus_float_t *)(bufend - 4);
  1681. jchar = (unsigned char *)charbuf; /* if to_buffer we should add the loop offset here, or never loop */
  1682. jchar += (k * siz);
  1683. switch (sample_type)
  1684. {
  1685. case MUS_BSHORT:
  1686. while (bufnow <= bufend4)
  1687. {
  1688. set_big_endian_short(jchar, mus_sample_to_short(*bufnow++));
  1689. jchar += siz_chans;
  1690. set_big_endian_short(jchar, mus_sample_to_short(*bufnow++));
  1691. jchar += siz_chans;
  1692. set_big_endian_short(jchar, mus_sample_to_short(*bufnow++));
  1693. jchar += siz_chans;
  1694. set_big_endian_short(jchar, mus_sample_to_short(*bufnow++));
  1695. jchar += siz_chans;
  1696. }
  1697. for (; bufnow <= bufend; jchar += siz_chans)
  1698. set_big_endian_short(jchar, mus_sample_to_short(*bufnow++));
  1699. break;
  1700. case MUS_LSHORT:
  1701. for (; bufnow <= bufend; jchar += siz_chans)
  1702. set_little_endian_short(jchar, mus_sample_to_short(*bufnow++));
  1703. break;
  1704. case MUS_BINT:
  1705. for (; bufnow <= bufend; jchar += siz_chans)
  1706. set_big_endian_int(jchar, mus_sample_to_int(*bufnow++));
  1707. break;
  1708. case MUS_LINT:
  1709. for (; bufnow <= bufend; jchar += siz_chans)
  1710. set_little_endian_int(jchar, mus_sample_to_int(*bufnow++));
  1711. break;
  1712. case MUS_BINTN:
  1713. for (; bufnow <= bufend; jchar += siz_chans)
  1714. set_big_endian_int(jchar, mus_sample_to_int(*bufnow++) << 8);
  1715. break;
  1716. case MUS_LINTN:
  1717. for (; bufnow <= bufend; jchar += siz_chans)
  1718. set_little_endian_int(jchar, mus_sample_to_int(*bufnow++) << 8);
  1719. break;
  1720. case MUS_MULAW:
  1721. for (; bufnow <= bufend; jchar += siz_chans)
  1722. (*jchar) = to_mulaw(mus_sample_to_short(*bufnow++));
  1723. break;
  1724. case MUS_ALAW:
  1725. for (; bufnow <= bufend; jchar += siz_chans)
  1726. (*jchar) = to_alaw(mus_sample_to_short(*bufnow++));
  1727. break;
  1728. case MUS_BYTE:
  1729. for (; bufnow <= bufend; jchar += siz_chans)
  1730. (*((signed char *)jchar)) = mus_sample_to_byte(*bufnow++);
  1731. break;
  1732. case MUS_UBYTE:
  1733. for (; bufnow <= bufend; jchar += siz_chans)
  1734. (*jchar) = mus_sample_to_byte(*bufnow++) + UBYTE_ZERO;
  1735. break;
  1736. case MUS_BFLOAT:
  1737. for (; bufnow <= bufend; jchar += siz_chans)
  1738. set_big_endian_float(jchar, *bufnow++);
  1739. break;
  1740. case MUS_LFLOAT:
  1741. while (bufnow <= bufend4)
  1742. {
  1743. set_little_endian_float(jchar, *bufnow++);
  1744. jchar += siz_chans;
  1745. set_little_endian_float(jchar, *bufnow++);
  1746. jchar += siz_chans;
  1747. set_little_endian_float(jchar, *bufnow++);
  1748. jchar += siz_chans;
  1749. set_little_endian_float(jchar, *bufnow++);
  1750. jchar += siz_chans;
  1751. }
  1752. for (; bufnow <= bufend; jchar += siz_chans)
  1753. set_little_endian_float(jchar, *bufnow++);
  1754. break;
  1755. case MUS_BDOUBLE:
  1756. for (; bufnow <= bufend; jchar += siz_chans)
  1757. set_big_endian_double(jchar, *bufnow++);
  1758. break;
  1759. case MUS_LDOUBLE:
  1760. while (bufnow <= bufend4)
  1761. {
  1762. set_little_endian_double(jchar, *bufnow++);
  1763. jchar += siz_chans;
  1764. set_little_endian_double(jchar, *bufnow++);
  1765. jchar += siz_chans;
  1766. set_little_endian_double(jchar, *bufnow++);
  1767. jchar += siz_chans;
  1768. set_little_endian_double(jchar, *bufnow++);
  1769. jchar += siz_chans;
  1770. }
  1771. for (; bufnow <= bufend; jchar += siz_chans)
  1772. set_little_endian_double(jchar, *bufnow++);
  1773. break;
  1774. case MUS_BFLOAT_UNSCALED:
  1775. for (; bufnow <= bufend; jchar += siz_chans)
  1776. set_big_endian_float(jchar, 32768.0 * (*bufnow++));
  1777. break;
  1778. case MUS_LFLOAT_UNSCALED:
  1779. for (; bufnow <= bufend; jchar += siz_chans)
  1780. set_little_endian_float(jchar, 32768.0 * (*bufnow++));
  1781. break;
  1782. case MUS_BDOUBLE_UNSCALED:
  1783. for (; bufnow <= bufend; jchar += siz_chans)
  1784. set_big_endian_double(jchar, 32768.0 * (*bufnow++));
  1785. break;
  1786. case MUS_LDOUBLE_UNSCALED:
  1787. for (; bufnow <= bufend; jchar += siz_chans)
  1788. set_little_endian_double(jchar, 32768.0 * (*bufnow++));
  1789. break;
  1790. case MUS_UBSHORT:
  1791. for (; bufnow <= bufend; jchar += siz_chans)
  1792. set_big_endian_unsigned_short(jchar, (unsigned short)(mus_sample_to_short(*bufnow++) + USHORT_ZERO));
  1793. break;
  1794. case MUS_ULSHORT:
  1795. for (; bufnow <= bufend; jchar += siz_chans)
  1796. set_little_endian_unsigned_short(jchar, (unsigned short)(mus_sample_to_short(*bufnow++) + USHORT_ZERO));
  1797. break;
  1798. case MUS_B24INT:
  1799. {
  1800. int c3;
  1801. mus_long_t bk;
  1802. bk = (k * 3);
  1803. c3 = chans * 3;
  1804. for (; bufnow <= bufend; bk += c3)
  1805. {
  1806. val = mus_sample_to_int24(*bufnow++);
  1807. charbuf[bk] = (val >> 16);
  1808. charbuf[bk + 1] = (val >> 8);
  1809. charbuf[bk + 2] = (val & 0xFF);
  1810. }
  1811. }
  1812. break;
  1813. case MUS_L24INT:
  1814. {
  1815. int c3;
  1816. mus_long_t bk;
  1817. bk = (k * 3);
  1818. c3 = chans * 3;
  1819. for (; bufnow <= bufend; bk += c3)
  1820. {
  1821. val = mus_sample_to_int24(*bufnow++);
  1822. charbuf[bk + 2] = (val >> 16);
  1823. charbuf[bk + 1] = (val >> 8);
  1824. charbuf[bk] = (val & 0xFF);
  1825. }
  1826. }
  1827. break;
  1828. default: break;
  1829. }
  1830. loc = loclim;
  1831. }
  1832. if (!inbuf)
  1833. {
  1834. err = checked_write(tfd, charbuf, bytes);
  1835. if (err == MUS_ERROR)
  1836. return(MUS_ERROR);
  1837. }
  1838. }
  1839. return(MUS_NO_ERROR);
  1840. }
  1841. int mus_file_write(int tfd, mus_long_t beg, mus_long_t end, int chans, mus_float_t **bufs)
  1842. {
  1843. return(mus_write_1(tfd, beg, end, chans, bufs, NULL, false)); /* if inbuf is NULL (as here), clipped is ignored, so the "false" could be anything */
  1844. }
  1845. int mus_file_write_file(int tfd, mus_long_t beg, mus_long_t end, int chans, mus_float_t **bufs)
  1846. {
  1847. return(mus_write_1(tfd, beg, end, chans, bufs, NULL, false));
  1848. }
  1849. int mus_file_write_buffer(int charbuf_sample_type, mus_long_t beg, mus_long_t end, int chans, mus_float_t **bufs, char *charbuf, bool clipped)
  1850. {
  1851. return(mus_write_1(charbuf_sample_type, beg, end, chans, bufs, charbuf, clipped));
  1852. }
  1853. /* for CLM */
  1854. void mus_reset_io_c(void)
  1855. {
  1856. io_fd_size = 0;
  1857. io_fds = NULL;
  1858. clipping_default = false;
  1859. mus_clip_set_handler(NULL);
  1860. }
  1861. #if !(defined(_MSC_VER) && (!(defined(__CYGWIN__))))
  1862. static int sndlib_strlen(const char *str)
  1863. {
  1864. /* strlen(NULL) -> seg fault! */
  1865. if ((str) && (*str)) return(strlen(str));
  1866. return(0);
  1867. }
  1868. #endif
  1869. static char *saved_cwd = NULL;
  1870. char *mus_getcwd(void)
  1871. {
  1872. int i, path_max = 0;
  1873. char *pwd = NULL;
  1874. if (saved_cwd) return(saved_cwd);
  1875. #ifndef _MSC_VER
  1876. path_max = pathconf("/", _PC_PATH_MAX);
  1877. #endif
  1878. if (path_max < 1024)
  1879. {
  1880. #if defined(PATH_MAX)
  1881. path_max = PATH_MAX;
  1882. #endif
  1883. if (path_max < 1024)
  1884. path_max = 1024;
  1885. }
  1886. for (i = path_max;; i *= 2)
  1887. {
  1888. char *res;
  1889. if (pwd) free(pwd);
  1890. pwd = (char *)calloc(i, sizeof(char));
  1891. #if (defined(_MSC_VER) || __CYGWIN__)
  1892. res = _getcwd(pwd, i);
  1893. #else
  1894. res = getcwd(pwd, i);
  1895. #endif
  1896. if (res) break; /* NULL is returned if failure, but what about success? should I check errno=ERANGE? */
  1897. }
  1898. saved_cwd = pwd;
  1899. return(pwd);
  1900. }
  1901. #if HAVE_EXTENSION_LANGUAGE
  1902. #include "sndlib2xen.h"
  1903. /* for g_mus_sound_path */
  1904. #endif
  1905. char *mus_expand_filename(const char *filename)
  1906. {
  1907. /* fill out under-specified library pathnames etc */
  1908. #if defined(_MSC_VER) && (!(defined(__CYGWIN__)))
  1909. return(mus_strdup(filename));
  1910. #else
  1911. char *file_name_buf;
  1912. char *tok, *orig;
  1913. int i, j, len, orig_len;
  1914. /* realpath does not speed this up */
  1915. if ((filename) && (*filename))
  1916. len = strlen(filename);
  1917. else return(NULL);
  1918. if ((len == 1) && (filename[0] == '.'))
  1919. {
  1920. orig = mus_getcwd();
  1921. orig_len = sndlib_strlen(orig);
  1922. file_name_buf = (char *)malloc((orig_len + 4) * sizeof(char));
  1923. strcpy(file_name_buf, orig);
  1924. file_name_buf[orig_len] = '/';
  1925. file_name_buf[orig_len + 1] = '\0';
  1926. /* fprintf(stderr, "%d: %s -> %s\n", __LINE__, filename, file_name_buf); */
  1927. return(file_name_buf);
  1928. }
  1929. tok = (char *)strchr(filename, (int)'/');
  1930. if (!tok)
  1931. {
  1932. orig = mus_getcwd();
  1933. orig_len = sndlib_strlen(orig);
  1934. file_name_buf = (char *)malloc((len + orig_len + 4) * sizeof(char));
  1935. strcpy(file_name_buf, orig);
  1936. file_name_buf[orig_len] = '/';
  1937. strncpy((char *)(file_name_buf + orig_len + 1), filename, len + 1);
  1938. /* fprintf(stderr, "%d: %s -> %s\n", __LINE__, filename, file_name_buf); */
  1939. #if HAVE_EXTENSION_LANGUAGE
  1940. if (!mus_file_probe(file_name_buf))
  1941. {
  1942. Xen p;
  1943. for (p = g_mus_sound_path(); Xen_is_pair(p); p = Xen_cdr(p))
  1944. {
  1945. char *nfile;
  1946. const char *path;
  1947. int path_len;
  1948. path = Xen_string_to_C_string(Xen_car(p));
  1949. path_len = sndlib_strlen(path);
  1950. nfile = (char *)malloc((len + path_len + 4) * sizeof(char));
  1951. strcpy(nfile, path);
  1952. if (nfile[path_len - 1] != '/')
  1953. {
  1954. nfile[path_len] = '/';
  1955. strncpy((char *)(nfile + path_len + 1), filename, len + 1);
  1956. }
  1957. else strncpy((char *)(nfile + path_len), filename, len + 1);
  1958. /* fprintf(stderr, "%d: %s -> %s\n", __LINE__, filename, nfile); */
  1959. if (mus_file_probe(nfile))
  1960. {
  1961. free(file_name_buf);
  1962. return(nfile);
  1963. }
  1964. free(nfile);
  1965. }
  1966. }
  1967. #endif
  1968. return(file_name_buf);
  1969. }
  1970. orig = mus_strdup(filename);
  1971. tok = orig;
  1972. /* get rid of "//" */
  1973. for (i = 0, j = 0; i < len - 1; i++)
  1974. {
  1975. if ((tok[i] == '/') &&
  1976. (tok[i + 1] == '/'))
  1977. j = i + 1;
  1978. }
  1979. if (j > 0)
  1980. {
  1981. for (i = 0; j < len; i++, j++)
  1982. tok[i] = tok[j];
  1983. tok[i] ='\0';
  1984. }
  1985. /* get rid of "~/" at start */
  1986. if (tok[0] != '/')
  1987. {
  1988. char *home = NULL;
  1989. if ((tok[0] == '~') && (home = getenv("HOME")))
  1990. {
  1991. file_name_buf = (char *)malloc((len + sndlib_strlen(home) + 8) * sizeof(char));
  1992. strcpy(file_name_buf, home);
  1993. strcat(file_name_buf, ++tok);
  1994. }
  1995. else
  1996. {
  1997. char *pwd;
  1998. pwd = mus_getcwd();
  1999. file_name_buf = (char *)malloc((len + sndlib_strlen(pwd) + 8) * sizeof(char));
  2000. strcpy(file_name_buf, pwd);
  2001. strcat(file_name_buf, "/");
  2002. if (tok[0])
  2003. strcat(file_name_buf, tok);
  2004. }
  2005. }
  2006. else
  2007. {
  2008. file_name_buf = (char *)malloc((len + 8) * sizeof(char));
  2009. strcpy(file_name_buf, tok);
  2010. }
  2011. /* get rid of "/../" and "/./" also "/." at end */
  2012. {
  2013. int slash_at = -1;
  2014. bool found_one = true;
  2015. while (found_one)
  2016. {
  2017. found_one = false;
  2018. len = strlen(file_name_buf);
  2019. for (i = 0; i < len - 3; i++)
  2020. if (file_name_buf[i] == '/')
  2021. {
  2022. if ((file_name_buf[i + 1] == '.') &&
  2023. (file_name_buf[i + 2] == '.') &&
  2024. (file_name_buf[i + 3] == '/'))
  2025. {
  2026. i += 4;
  2027. for (j = slash_at + 1; i < len; i++, j++)
  2028. file_name_buf[j] = file_name_buf[i];
  2029. file_name_buf[j] = '\0';
  2030. found_one = true;
  2031. break;
  2032. }
  2033. else
  2034. {
  2035. if ((file_name_buf[i + 1] == '.') &&
  2036. (file_name_buf[i + 2] == '/'))
  2037. {
  2038. for (j = i + 3, i = i + 1; j < len; i++, j++)
  2039. file_name_buf[i] = file_name_buf[j];
  2040. file_name_buf[i] = '\0';
  2041. found_one = true;
  2042. }
  2043. else slash_at = i;
  2044. }
  2045. }
  2046. }
  2047. len = strlen(file_name_buf);
  2048. if ((len > 1) &&
  2049. (file_name_buf[len - 1] == '.') &&
  2050. (file_name_buf[len - 2] == '/'))
  2051. file_name_buf[len - 1] = '\0';
  2052. }
  2053. free(orig);
  2054. return(file_name_buf);
  2055. #endif
  2056. }
  2057. char *mus_format(const char *format, ...)
  2058. {
  2059. /* caller should free result */
  2060. #ifndef _MSC_VER
  2061. va_list ap;
  2062. int bytes;
  2063. char *result = NULL;
  2064. va_start(ap, format);
  2065. bytes = vasprintf(&result, format, ap);
  2066. va_end(ap);
  2067. if (bytes == -1)
  2068. return(NULL);
  2069. return(result);
  2070. #else
  2071. #define MUS_FORMAT_BUFFER_SIZE 256
  2072. char *buf = NULL;
  2073. int needed_bytes = 0;
  2074. va_list ap;
  2075. buf = (char *)calloc(MUS_FORMAT_BUFFER_SIZE, sizeof(char));
  2076. va_start(ap, format);
  2077. needed_bytes = vsnprintf(buf, MUS_FORMAT_BUFFER_SIZE, format, ap);
  2078. va_end(ap);
  2079. if (needed_bytes >= MUS_FORMAT_BUFFER_SIZE) /* "=" here because we need room for the trailing 0 */
  2080. {
  2081. free(buf);
  2082. buf = (char *)calloc(needed_bytes + 1, sizeof(char));
  2083. va_start(ap, format);
  2084. vsnprintf(buf, needed_bytes + 1, format, ap);
  2085. va_end(ap);
  2086. }
  2087. return(buf);
  2088. #endif
  2089. }
  2090. mus_float_t mus_fclamp(mus_float_t lo, mus_float_t val, mus_float_t hi)
  2091. {
  2092. if (val > hi)
  2093. return(hi);
  2094. else
  2095. if (val < lo)
  2096. return(lo);
  2097. else return(val);
  2098. }
  2099. int mus_iclamp(int lo, int val, int hi)
  2100. {
  2101. if (val > hi)
  2102. return(hi);
  2103. else
  2104. if (val < lo)
  2105. return(lo);
  2106. else return(val);
  2107. }
  2108. mus_long_t mus_oclamp(mus_long_t lo, mus_long_t val, mus_long_t hi)
  2109. {
  2110. if (val > hi)
  2111. return(hi);
  2112. else
  2113. if (val < lo)
  2114. return(lo);
  2115. else return(val);
  2116. }
  2117. /* raw sample data peak value (without per-sample conversion) */
  2118. /* ---------------- short ---------------- */
  2119. #define SHORT_BYTES 2
  2120. static void min_max_shorts(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2121. {
  2122. short cur_min, cur_max, tmp;
  2123. short *sbuf;
  2124. int i, len, len2;
  2125. sbuf = (short *)data;
  2126. len = bytes / SHORT_BYTES;
  2127. cur_min = sbuf[chan];
  2128. cur_max = cur_min;
  2129. len2 = len - 2 * chans;
  2130. i = chan; /* we read the first sample above, but we're reading by twos below */
  2131. while (i <= len2)
  2132. {
  2133. tmp = sbuf[i];
  2134. if (tmp < cur_min) cur_min = tmp; else if (tmp > cur_max) cur_max = tmp;
  2135. i += chans;
  2136. tmp = sbuf[i];
  2137. if (tmp < cur_min) cur_min = tmp; else if (tmp > cur_max) cur_max = tmp;
  2138. i += chans;
  2139. }
  2140. if (i < len)
  2141. {
  2142. tmp = sbuf[i];
  2143. if (tmp < cur_min) cur_min = tmp; else if (tmp > cur_max) cur_max = tmp;
  2144. }
  2145. (*min_samp) = (mus_float_t)cur_min / (mus_float_t)(1 << 15);
  2146. (*max_samp) = (mus_float_t)cur_max / (mus_float_t)(1 << 15);
  2147. }
  2148. static void min_max_switch_shorts(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2149. {
  2150. short cur_min, cur_max;
  2151. /* frample based */
  2152. unsigned char *samp, *eod, *eod2;
  2153. int bytes_per_frample;
  2154. bytes_per_frample = chans * SHORT_BYTES;
  2155. eod = (unsigned char *)(data + bytes);
  2156. eod2 = (unsigned char *)(eod - 2 * bytes_per_frample);
  2157. #if MUS_LITTLE_ENDIAN
  2158. cur_min = big_endian_short((unsigned char *)(data + (chan * SHORT_BYTES)));
  2159. #else
  2160. cur_min = little_endian_short((unsigned char *)(data + (chan * SHORT_BYTES)));
  2161. #endif
  2162. cur_max = cur_min;
  2163. samp = (unsigned char *)(data + (chan * SHORT_BYTES));
  2164. while (samp <= eod2)
  2165. {
  2166. short val;
  2167. #if MUS_LITTLE_ENDIAN
  2168. val = big_endian_short(samp);
  2169. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2170. samp += bytes_per_frample;
  2171. val = big_endian_short(samp);
  2172. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2173. samp += bytes_per_frample;
  2174. #else
  2175. val = little_endian_short(samp);
  2176. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2177. samp += bytes_per_frample;
  2178. val = little_endian_short(samp);
  2179. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2180. samp += bytes_per_frample;
  2181. #endif
  2182. }
  2183. if (samp < eod)
  2184. {
  2185. short val;
  2186. #if MUS_LITTLE_ENDIAN
  2187. val = big_endian_short(samp);
  2188. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2189. #else
  2190. val = little_endian_short(samp);
  2191. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2192. #endif
  2193. }
  2194. (*min_samp) = (mus_float_t)cur_min / (mus_float_t)(1 << 15);
  2195. (*max_samp) = (mus_float_t)cur_max / (mus_float_t)(1 << 15);
  2196. }
  2197. /* ---------------- unsigned short ---------------- */
  2198. static void min_max_ushorts(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2199. {
  2200. unsigned short cur_min, cur_max;
  2201. unsigned short *sbuf;
  2202. int i, len, len2;
  2203. sbuf = (unsigned short *)data;
  2204. len = bytes / SHORT_BYTES;
  2205. len2 = len - 2 * chans;
  2206. cur_min = sbuf[chan];
  2207. cur_max = cur_min;
  2208. i = chan;
  2209. while (i <= len2)
  2210. {
  2211. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2212. i += chans;
  2213. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2214. i += chans;
  2215. }
  2216. if (i < len)
  2217. {
  2218. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2219. }
  2220. (*min_samp) = (mus_float_t)(cur_min - USHORT_ZERO) / (mus_float_t)(1 << 15);
  2221. (*max_samp) = (mus_float_t)(cur_max - USHORT_ZERO) / (mus_float_t)(1 << 15);
  2222. }
  2223. static void min_max_switch_ushorts(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2224. {
  2225. unsigned short cur_min, cur_max;
  2226. /* frample based */
  2227. unsigned char *samp, *eod, *eod2;
  2228. int bytes_per_frample;
  2229. bytes_per_frample = chans * SHORT_BYTES;
  2230. eod = (unsigned char *)(data + bytes);
  2231. eod2 = (unsigned char *)(eod - 2 * bytes_per_frample);
  2232. #if MUS_LITTLE_ENDIAN
  2233. cur_min = big_endian_unsigned_short((unsigned char *)(data + (chan * SHORT_BYTES)));
  2234. #else
  2235. cur_min = little_endian_unsigned_short((unsigned char *)(data + (chan * SHORT_BYTES)));
  2236. #endif
  2237. cur_max = cur_min;
  2238. samp = (unsigned char *)(data + (chan * SHORT_BYTES));
  2239. while (samp <= eod2)
  2240. {
  2241. unsigned short val;
  2242. #if MUS_LITTLE_ENDIAN
  2243. val = big_endian_unsigned_short(samp);
  2244. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2245. samp += bytes_per_frample;
  2246. val = big_endian_unsigned_short(samp);
  2247. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2248. samp += bytes_per_frample;
  2249. #else
  2250. val = little_endian_unsigned_short(samp);
  2251. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2252. samp += bytes_per_frample;
  2253. val = little_endian_unsigned_short(samp);
  2254. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2255. samp += bytes_per_frample;
  2256. #endif
  2257. }
  2258. if (samp < eod)
  2259. {
  2260. unsigned short val;
  2261. #if MUS_LITTLE_ENDIAN
  2262. val = big_endian_unsigned_short(samp);
  2263. #else
  2264. val = little_endian_unsigned_short(samp);
  2265. #endif
  2266. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2267. }
  2268. (*min_samp) = (mus_float_t)(cur_min - USHORT_ZERO) / (mus_float_t)(1 << 15);
  2269. (*max_samp) = (mus_float_t)(cur_max - USHORT_ZERO) / (mus_float_t)(1 << 15);
  2270. }
  2271. /* ---------------- int ---------------- */
  2272. #define INT_BYTES 4
  2273. static void min_max_ints(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp, bool standard)
  2274. {
  2275. int cur_min, cur_max;
  2276. int *sbuf;
  2277. int i, len, len2;
  2278. sbuf = (int *)data;
  2279. len = bytes / INT_BYTES;
  2280. len2 = len - 2 *chans;
  2281. cur_min = sbuf[chan];
  2282. cur_max = cur_min;
  2283. i = chan;
  2284. while (i <= len2)
  2285. {
  2286. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2287. i += chans;
  2288. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2289. i += chans;
  2290. }
  2291. if (i < len)
  2292. {
  2293. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2294. }
  2295. (*min_samp) = (mus_float_t)cur_min / (mus_float_t)(1 << 23);
  2296. (*max_samp) = (mus_float_t)cur_max / (mus_float_t)(1 << 23);
  2297. if (!standard)
  2298. {
  2299. (*min_samp) = (mus_float_t)(*min_samp) / (mus_float_t)(1 << 8);
  2300. (*max_samp) = (mus_float_t)(*max_samp) / (mus_float_t)(1 << 8);
  2301. }
  2302. }
  2303. /* (with-sound (:sample-type mus-lintn :statistics #t) (fm-violin 0 1 440 .1)) */
  2304. static void min_max_switch_ints(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp, bool standard)
  2305. {
  2306. int cur_min, cur_max;
  2307. /* frample based */
  2308. unsigned char *samp, *eod, *eod2;
  2309. int bytes_per_frample;
  2310. bytes_per_frample = chans * INT_BYTES;
  2311. eod = (unsigned char *)(data + bytes);
  2312. eod2 = (unsigned char *)(eod - 2 * bytes_per_frample);
  2313. #if MUS_LITTLE_ENDIAN
  2314. cur_min = big_endian_int((unsigned char *)(data + (chan * INT_BYTES)));
  2315. #else
  2316. cur_min = little_endian_int((unsigned char *)(data + (chan * INT_BYTES)));
  2317. #endif
  2318. cur_max = cur_min;
  2319. samp = (unsigned char *)(data + (chan * INT_BYTES));
  2320. while (samp <= eod2)
  2321. {
  2322. int val;
  2323. #if MUS_LITTLE_ENDIAN
  2324. val = big_endian_int(samp);
  2325. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2326. samp += bytes_per_frample;
  2327. val = big_endian_int(samp);
  2328. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2329. samp += bytes_per_frample;
  2330. #else
  2331. val = little_endian_int(samp);
  2332. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2333. samp += bytes_per_frample;
  2334. val = little_endian_int(samp);
  2335. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2336. samp += bytes_per_frample;
  2337. #endif
  2338. }
  2339. if (samp < eod)
  2340. {
  2341. int val;
  2342. #if MUS_LITTLE_ENDIAN
  2343. val = big_endian_int(samp);
  2344. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2345. #else
  2346. val = little_endian_int(samp);
  2347. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2348. #endif
  2349. }
  2350. (*min_samp) = (mus_float_t)cur_min / (mus_float_t)(1 << 23);
  2351. (*max_samp) = (mus_float_t)cur_max / (mus_float_t)(1 << 23);
  2352. if (!standard)
  2353. {
  2354. (*min_samp) = (mus_float_t)(*min_samp) / (mus_float_t)(1 << 8);
  2355. (*max_samp) = (mus_float_t)(*max_samp) / (mus_float_t)(1 << 8);
  2356. }
  2357. }
  2358. /* ---------------- float ---------------- */
  2359. #define FLOAT_BYTES 4
  2360. static void min_max_floats(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp, bool unscaled)
  2361. {
  2362. float cur_min, cur_max;
  2363. float *sbuf;
  2364. int i, len, len2;
  2365. sbuf = (float *)data;
  2366. len = bytes / FLOAT_BYTES;
  2367. len2 = len - 2 *chans;
  2368. cur_min = sbuf[chan];
  2369. cur_max = cur_min;
  2370. i = chan;
  2371. while (i < len2)
  2372. {
  2373. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2374. i += chans;
  2375. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2376. i += chans;
  2377. }
  2378. while (i < len)
  2379. {
  2380. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2381. i += chans;
  2382. }
  2383. if (unscaled)
  2384. {
  2385. (*min_samp) = cur_min / 32768.0;
  2386. (*max_samp) = cur_max / 32768.0;
  2387. }
  2388. else
  2389. {
  2390. (*min_samp) = cur_min;
  2391. (*max_samp) = cur_max;
  2392. }
  2393. }
  2394. static void min_max_switch_floats(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp, bool unscaled)
  2395. {
  2396. float cur_min, cur_max;
  2397. /* frample based */
  2398. unsigned char *samp, *eod, *eod2;
  2399. int bytes_per_frample;
  2400. bytes_per_frample = chans * FLOAT_BYTES;
  2401. eod = (unsigned char *)(data + bytes);
  2402. eod2 = (unsigned char *)(eod - 2 * bytes_per_frample);
  2403. #if MUS_LITTLE_ENDIAN
  2404. cur_min = big_endian_float((unsigned char *)(data + (chan * FLOAT_BYTES)));
  2405. #else
  2406. cur_min = little_endian_float((unsigned char *)(data + (chan * FLOAT_BYTES)));
  2407. #endif
  2408. cur_max = cur_min;
  2409. samp = (unsigned char *)(data + (chan * FLOAT_BYTES));
  2410. while (samp <= eod2)
  2411. {
  2412. float val;
  2413. #if MUS_LITTLE_ENDIAN
  2414. val = big_endian_float(samp);
  2415. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2416. samp += bytes_per_frample;
  2417. val = big_endian_float(samp);
  2418. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2419. samp += bytes_per_frample;
  2420. #else
  2421. val = little_endian_float(samp);
  2422. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2423. samp += bytes_per_frample;
  2424. val = little_endian_float(samp);
  2425. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2426. samp += bytes_per_frample;
  2427. #endif
  2428. }
  2429. if (samp < eod)
  2430. {
  2431. float val;
  2432. #if MUS_LITTLE_ENDIAN
  2433. val = big_endian_float(samp);
  2434. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2435. #else
  2436. val = little_endian_float(samp);
  2437. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2438. #endif
  2439. }
  2440. if (unscaled)
  2441. {
  2442. (*min_samp) = cur_min / 32768.0;
  2443. (*max_samp) = cur_max / 32768.0;
  2444. }
  2445. else
  2446. {
  2447. (*min_samp) = cur_min;
  2448. (*max_samp) = cur_max;
  2449. }
  2450. }
  2451. /* ---------------- double ---------------- */
  2452. #define DOUBLE_BYTES 8
  2453. static void min_max_doubles(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp, bool unscaled)
  2454. {
  2455. double cur_min, cur_max;
  2456. double *sbuf;
  2457. int i, len, len2;
  2458. sbuf = (double *)data;
  2459. len = bytes / DOUBLE_BYTES;
  2460. len2 = len - 2 * chans;
  2461. cur_min = sbuf[chan];
  2462. cur_max = cur_min;
  2463. i = chan;
  2464. while (i <= len2)
  2465. {
  2466. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2467. i += chans;
  2468. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2469. i += chans;
  2470. }
  2471. if (i < len)
  2472. {
  2473. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2474. }
  2475. if (unscaled)
  2476. {
  2477. (*min_samp) = cur_min / 32768.0;
  2478. (*max_samp) = cur_max / 32768.0;
  2479. }
  2480. else
  2481. {
  2482. (*min_samp) = cur_min;
  2483. (*max_samp) = cur_max;
  2484. }
  2485. }
  2486. static void min_max_switch_doubles(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp, bool unscaled)
  2487. {
  2488. double cur_min, cur_max;
  2489. /* frample based */
  2490. unsigned char *samp, *eod, *eod2;
  2491. int bytes_per_frample;
  2492. bytes_per_frample = chans * DOUBLE_BYTES;
  2493. eod = (unsigned char *)(data + bytes);
  2494. eod2 = (unsigned char *)(eod - 2 * bytes_per_frample);
  2495. #if MUS_LITTLE_ENDIAN
  2496. cur_min = big_endian_double((unsigned char *)(data + (chan * DOUBLE_BYTES)));
  2497. #else
  2498. cur_min = little_endian_double((unsigned char *)(data + (chan * DOUBLE_BYTES)));
  2499. #endif
  2500. cur_max = cur_min;
  2501. samp = (unsigned char *)(data + (chan * DOUBLE_BYTES));
  2502. while (samp <= eod2)
  2503. {
  2504. double val;
  2505. #if MUS_LITTLE_ENDIAN
  2506. val = big_endian_double(samp);
  2507. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2508. samp += bytes_per_frample;
  2509. val = big_endian_double(samp);
  2510. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2511. samp += bytes_per_frample;
  2512. #else
  2513. val = little_endian_double(samp);
  2514. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2515. samp += bytes_per_frample;
  2516. val = little_endian_double(samp);
  2517. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2518. samp += bytes_per_frample;
  2519. #endif
  2520. }
  2521. if (samp < eod)
  2522. {
  2523. double val;
  2524. #if MUS_LITTLE_ENDIAN
  2525. val = big_endian_double(samp);
  2526. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2527. #else
  2528. val = little_endian_double(samp);
  2529. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2530. #endif
  2531. }
  2532. if (unscaled)
  2533. {
  2534. (*min_samp) = cur_min / 32768.0;
  2535. (*max_samp) = cur_max / 32768.0;
  2536. }
  2537. else
  2538. {
  2539. (*min_samp) = cur_min;
  2540. (*max_samp) = cur_max;
  2541. }
  2542. }
  2543. /* ---------------- 3-byte samples ---------------- */
  2544. #define THREE_BYTES 3
  2545. static int big_three(unsigned char *data, int loc)
  2546. {
  2547. int val;
  2548. val = (data[loc + 0] << 16) + (data[loc + 1] << 8) + data[loc + 2];
  2549. if (val >= (1 << 23)) val -= (1 << 24);
  2550. return(val);
  2551. }
  2552. static int little_three(unsigned char *data, int loc)
  2553. {
  2554. int val;
  2555. val = (data[loc + 2] << 16) + (data[loc + 1] << 8) + data[loc + 0];
  2556. if (val >= (1 << 23)) val -= (1 << 24);
  2557. return(val);
  2558. }
  2559. static void min_max_24s(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp, bool big_endian)
  2560. {
  2561. int cur_min, cur_max;
  2562. int i, k, bytes_per_frample, len, len2, offset;
  2563. bytes_per_frample = chans * THREE_BYTES;
  2564. len = bytes / bytes_per_frample;
  2565. len2 = len - 2 * bytes_per_frample;
  2566. offset = chan * THREE_BYTES;
  2567. k = offset;
  2568. if (big_endian)
  2569. {
  2570. cur_min = big_three(data, k);
  2571. cur_max = cur_min;
  2572. i = 0;
  2573. /* k += bytes_per_frample; */
  2574. while (i <= len2)
  2575. {
  2576. int val;
  2577. val = big_three(data, k);
  2578. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2579. i++;
  2580. k += bytes_per_frample;
  2581. val = big_three(data, k);
  2582. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2583. i++;
  2584. k += bytes_per_frample;
  2585. }
  2586. while (i < len)
  2587. {
  2588. int val;
  2589. val = big_three(data, k);
  2590. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2591. i++;
  2592. k += bytes_per_frample;
  2593. }
  2594. }
  2595. else
  2596. {
  2597. cur_min = little_three(data, k);
  2598. cur_max = cur_min;
  2599. i = 0;
  2600. /* k += bytes_per_frample; */
  2601. while (i <= len2)
  2602. {
  2603. int val;
  2604. val = little_three(data, k);
  2605. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2606. i++;
  2607. k += bytes_per_frample;
  2608. val = little_three(data, k);
  2609. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2610. i++;
  2611. k += bytes_per_frample;
  2612. }
  2613. while (i < len)
  2614. {
  2615. int val;
  2616. val = little_three(data, k);
  2617. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2618. i++;
  2619. k += bytes_per_frample;
  2620. }
  2621. }
  2622. (*min_samp) = (mus_float_t)cur_min / (mus_float_t)(1 << 23);
  2623. (*max_samp) = (mus_float_t)cur_max / (mus_float_t)(1 << 23);
  2624. }
  2625. /* ---------------- mulaw, alaw, byte ---------------- */
  2626. static void min_max_mulaw(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2627. {
  2628. mus_float_t cur_min, cur_max;
  2629. int i, b2;
  2630. cur_min = mus_mulaw[(int)data[chan]];
  2631. cur_max = cur_min;
  2632. i = chan;
  2633. b2 = bytes - 2 * chans;
  2634. while (i <= b2)
  2635. {
  2636. mus_float_t val;
  2637. val = mus_mulaw[(int)data[i]];
  2638. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2639. i += chans;
  2640. val = mus_mulaw[(int)data[i]];
  2641. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2642. i += chans;
  2643. }
  2644. if (i < bytes)
  2645. {
  2646. mus_float_t val;
  2647. val = mus_mulaw[(int)data[i]];
  2648. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2649. }
  2650. (*min_samp) = cur_min;
  2651. (*max_samp) = cur_max;
  2652. }
  2653. static void min_max_alaw(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2654. {
  2655. mus_float_t cur_min, cur_max;
  2656. int i, b2;
  2657. cur_min = mus_alaw[(int)data[chan]];
  2658. cur_max = cur_min;
  2659. i = chan;
  2660. b2 = bytes - 2 * chans;
  2661. while (i <= b2)
  2662. {
  2663. mus_float_t val;
  2664. val = mus_alaw[(int)data[i]];
  2665. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2666. i += chans;
  2667. val = mus_alaw[(int)data[i]];
  2668. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2669. i += chans;
  2670. }
  2671. if (i < bytes)
  2672. {
  2673. mus_float_t val;
  2674. val = mus_alaw[(int)data[i]];
  2675. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2676. }
  2677. (*min_samp) = cur_min;
  2678. (*max_samp) = cur_max;
  2679. }
  2680. static void min_max_bytes(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2681. {
  2682. signed char cur_min, cur_max;
  2683. int i, b2;
  2684. cur_min = (signed char)(data[chan]);
  2685. cur_max = cur_min;
  2686. i = chan;
  2687. b2 = bytes - 2 * chans;
  2688. while (i <= b2)
  2689. {
  2690. signed char val;
  2691. val = (signed char)(data[i]);
  2692. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2693. i += chans;
  2694. val = (signed char)(data[i]);
  2695. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2696. i += chans;
  2697. }
  2698. if (i < bytes)
  2699. {
  2700. signed char val;
  2701. val = (signed char)(data[i]);
  2702. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2703. }
  2704. (*min_samp) = (mus_float_t)cur_min / (mus_float_t)(1 << 7);
  2705. (*max_samp) = (mus_float_t)cur_max / (mus_float_t)(1 << 7);
  2706. }
  2707. static void min_max_ubytes(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2708. {
  2709. unsigned char cur_min, cur_max;
  2710. int i, b2;
  2711. cur_min = data[chan];
  2712. cur_max = cur_min;
  2713. i = chan;
  2714. b2 = bytes - 2 * chans;
  2715. while (i <= b2)
  2716. {
  2717. if (data[i] < cur_min) cur_min = data[i]; else if (data[i] > cur_max) cur_max = data[i];
  2718. i += chans;
  2719. if (data[i] < cur_min) cur_min = data[i]; else if (data[i] > cur_max) cur_max = data[i];
  2720. i += chans;
  2721. }
  2722. if (i < bytes)
  2723. {
  2724. if (data[i] < cur_min) cur_min = data[i]; else if (data[i] > cur_max) cur_max = data[i];
  2725. }
  2726. (*min_samp) = (mus_float_t)(cur_min - UBYTE_ZERO) / (mus_float_t)(1 << 7);
  2727. (*max_samp) = (mus_float_t)(cur_max - UBYTE_ZERO) / (mus_float_t)(1 << 7);
  2728. }
  2729. int mus_samples_bounds(unsigned char *data, int bytes, int chan, int chans, mus_sample_t samp_type, mus_float_t *min_samp, mus_float_t *max_samp)
  2730. {
  2731. switch (samp_type)
  2732. {
  2733. case MUS_MULAW:
  2734. min_max_mulaw(data, bytes, chan, chans, min_samp, max_samp);
  2735. break;
  2736. case MUS_ALAW:
  2737. min_max_alaw(data, bytes, chan, chans, min_samp, max_samp);
  2738. break;
  2739. case MUS_BYTE:
  2740. min_max_bytes(data, bytes, chan, chans, min_samp, max_samp);
  2741. break;
  2742. case MUS_UBYTE:
  2743. min_max_ubytes(data, bytes, chan, chans, min_samp, max_samp);
  2744. break;
  2745. case MUS_L24INT:
  2746. min_max_24s(data, bytes, chan, chans, min_samp, max_samp, false);
  2747. break;
  2748. case MUS_B24INT:
  2749. min_max_24s(data, bytes, chan, chans, min_samp, max_samp, true);
  2750. break;
  2751. #if MUS_LITTLE_ENDIAN
  2752. case MUS_LSHORT:
  2753. min_max_shorts(data, bytes, chan, chans, min_samp, max_samp);
  2754. break;
  2755. case MUS_BSHORT:
  2756. min_max_switch_shorts(data, bytes, chan, chans, min_samp, max_samp);
  2757. break;
  2758. case MUS_ULSHORT:
  2759. min_max_ushorts(data, bytes, chan, chans, min_samp, max_samp);
  2760. break;
  2761. case MUS_UBSHORT:
  2762. min_max_switch_ushorts(data, bytes, chan, chans, min_samp, max_samp);
  2763. break;
  2764. case MUS_LINT:
  2765. case MUS_LINTN:
  2766. min_max_ints(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_LINT);
  2767. break;
  2768. case MUS_BINT:
  2769. case MUS_BINTN:
  2770. min_max_switch_ints(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_BINT);
  2771. break;
  2772. case MUS_LFLOAT:
  2773. case MUS_LFLOAT_UNSCALED:
  2774. min_max_floats(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_LFLOAT_UNSCALED);
  2775. break;
  2776. case MUS_BFLOAT:
  2777. case MUS_BFLOAT_UNSCALED:
  2778. min_max_switch_floats(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_BFLOAT_UNSCALED);
  2779. break;
  2780. case MUS_LDOUBLE:
  2781. case MUS_LDOUBLE_UNSCALED:
  2782. min_max_doubles(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_LDOUBLE_UNSCALED);
  2783. break;
  2784. case MUS_BDOUBLE:
  2785. case MUS_BDOUBLE_UNSCALED:
  2786. min_max_switch_doubles(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_BDOUBLE_UNSCALED);
  2787. break;
  2788. #else /* big endian */
  2789. case MUS_LSHORT:
  2790. min_max_switch_shorts(data, bytes, chan, chans, min_samp, max_samp);
  2791. break;
  2792. case MUS_BSHORT:
  2793. min_max_shorts(data, bytes, chan, chans, min_samp, max_samp);
  2794. break;
  2795. case MUS_ULSHORT:
  2796. min_max_switch_ushorts(data, bytes, chan, chans, min_samp, max_samp);
  2797. break;
  2798. case MUS_UBSHORT:
  2799. min_max_ushorts(data, bytes, chan, chans, min_samp, max_samp);
  2800. break;
  2801. case MUS_LINT:
  2802. case MUS_LINTN:
  2803. min_max_switch_ints(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_LINT);
  2804. break;
  2805. case MUS_BINT:
  2806. case MUS_BINTN:
  2807. min_max_ints(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_BINT);
  2808. break;
  2809. case MUS_LFLOAT:
  2810. case MUS_LFLOAT_UNSCALED:
  2811. min_max_switch_floats(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_LFLOAT_UNSCALED);
  2812. break;
  2813. case MUS_BFLOAT:
  2814. case MUS_BFLOAT_UNSCALED:
  2815. min_max_floats(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_BFLOAT_UNSCALED);
  2816. break;
  2817. case MUS_LDOUBLE:
  2818. case MUS_LDOUBLE_UNSCALED:
  2819. min_max_switch_doubles(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_LDOUBLE_UNSCALED);
  2820. break;
  2821. case MUS_BDOUBLE:
  2822. case MUS_BDOUBLE_UNSCALED:
  2823. min_max_doubles(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_BDOUBLE_UNSCALED);
  2824. break;
  2825. #endif
  2826. default:
  2827. return(MUS_ERROR);
  2828. break;
  2829. }
  2830. return(MUS_NO_ERROR);
  2831. }
  2832. char *mus_strdup(const char *str)
  2833. {
  2834. char *newstr = NULL;
  2835. int len;
  2836. if ((!str) || (!(*str))) return(NULL);
  2837. len = strlen(str);
  2838. newstr = (char *)malloc(len + 1);
  2839. strcpy(newstr, str);
  2840. newstr[len] = '\0';
  2841. return(newstr);
  2842. }
  2843. int mus_strlen(const char *str)
  2844. {
  2845. /* strlen(NULL) -> seg fault! */
  2846. if ((str) && (*str)) return(strlen(str));
  2847. return(0);
  2848. }
  2849. bool mus_strcmp(const char *s1, const char *s2)
  2850. {
  2851. if ((!s1) || (!s2)) return(s1 == s2);
  2852. while (true)
  2853. {
  2854. unsigned char c1, c2;
  2855. c1 = (unsigned char) *s1++;
  2856. c2 = (unsigned char) *s2++;
  2857. if (c1 != c2) return(false);
  2858. if (c1 == '\0') break;
  2859. }
  2860. return(true);
  2861. #if 0
  2862. return((str1 == str2) ||
  2863. ((str1) && (str2) &&
  2864. (strcmp(str1, str2) == 0)));
  2865. #endif
  2866. }
  2867. char *mus_strcat(char *errmsg, const char *str, int *size)
  2868. {
  2869. int new_len, err_size;
  2870. new_len = (mus_strlen(str) + mus_strlen(errmsg));
  2871. err_size = size[0];
  2872. if (new_len >= err_size)
  2873. {
  2874. if ((err_size * 2) > new_len)
  2875. err_size = err_size * 2;
  2876. else err_size = new_len * 2;
  2877. errmsg = (char *)realloc(errmsg, err_size * sizeof(char));
  2878. size[0] = err_size;
  2879. }
  2880. strcat(errmsg, str);
  2881. return(errmsg);
  2882. }