Dlsbank.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212
  1. /*
  2. * DLSBank.cpp
  3. * -----------
  4. * Purpose: Sound bank loading.
  5. * Notes : Supported sound bank types: DLS (including embedded DLS in MSS & RMI), SF2, SF3 / SF4 (modified SF2 with compressed samples)
  6. * Authors: Olivier Lapicque
  7. * OpenMPT Devs
  8. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  9. */
  10. #include "stdafx.h"
  11. #include "Sndfile.h"
  12. #ifdef MODPLUG_TRACKER
  13. #include "../mptrack/Mptrack.h"
  14. #include "../common/mptFileIO.h"
  15. #endif
  16. #include "Dlsbank.h"
  17. #include "Loaders.h"
  18. #include "SampleCopy.h"
  19. #include "../common/mptStringBuffer.h"
  20. #include "../common/FileReader.h"
  21. #include "openmpt/base/Endian.hpp"
  22. #include "SampleIO.h"
  23. #include "mpt/io/base.hpp"
  24. #include "mpt/io/io.hpp"
  25. #include "mpt/io/io_stdstream.hpp"
  26. OPENMPT_NAMESPACE_BEGIN
  27. #ifdef MODPLUG_TRACKER
  28. #ifdef MPT_ALL_LOGGING
  29. #define DLSBANK_LOG
  30. #define DLSINSTR_LOG
  31. #endif
  32. #define F_RGN_OPTION_SELFNONEXCLUSIVE 0x0001
  33. // Region Flags
  34. enum RegionFlags
  35. {
  36. DLSREGION_KEYGROUPMASK = 0x0F,
  37. DLSREGION_OVERRIDEWSMP = 0x10,
  38. DLSREGION_PINGPONGLOOP = 0x20,
  39. DLSREGION_SAMPLELOOP = 0x40,
  40. DLSREGION_SELFNONEXCLUSIVE = 0x80,
  41. DLSREGION_SUSTAINLOOP = 0x100,
  42. };
  43. ///////////////////////////////////////////////////////////////////////////
  44. // Articulation connection graph definitions
  45. enum ConnectionSource : uint16
  46. {
  47. // Generic Sources
  48. CONN_SRC_NONE = 0x0000,
  49. CONN_SRC_LFO = 0x0001,
  50. CONN_SRC_KEYONVELOCITY = 0x0002,
  51. CONN_SRC_KEYNUMBER = 0x0003,
  52. CONN_SRC_EG1 = 0x0004,
  53. CONN_SRC_EG2 = 0x0005,
  54. CONN_SRC_PITCHWHEEL = 0x0006,
  55. CONN_SRC_POLYPRESSURE = 0x0007,
  56. CONN_SRC_CHANNELPRESSURE = 0x0008,
  57. CONN_SRC_VIBRATO = 0x0009,
  58. // Midi Controllers 0-127
  59. CONN_SRC_CC1 = 0x0081,
  60. CONN_SRC_CC7 = 0x0087,
  61. CONN_SRC_CC10 = 0x008a,
  62. CONN_SRC_CC11 = 0x008b,
  63. CONN_SRC_CC91 = 0x00db,
  64. CONN_SRC_CC93 = 0x00dd,
  65. CONN_SRC_RPN0 = 0x0100,
  66. CONN_SRC_RPN1 = 0x0101,
  67. CONN_SRC_RPN2 = 0x0102,
  68. };
  69. enum ConnectionDestination : uint16
  70. {
  71. // Generic Destinations
  72. CONN_DST_NONE = 0x0000,
  73. CONN_DST_ATTENUATION = 0x0001,
  74. CONN_DST_RESERVED = 0x0002,
  75. CONN_DST_PITCH = 0x0003,
  76. CONN_DST_PAN = 0x0004,
  77. // LFO Destinations
  78. CONN_DST_LFO_FREQUENCY = 0x0104,
  79. CONN_DST_LFO_STARTDELAY = 0x0105,
  80. CONN_DST_KEYNUMBER = 0x0005,
  81. // EG1 Destinations
  82. CONN_DST_EG1_ATTACKTIME = 0x0206,
  83. CONN_DST_EG1_DECAYTIME = 0x0207,
  84. CONN_DST_EG1_RESERVED = 0x0208,
  85. CONN_DST_EG1_RELEASETIME = 0x0209,
  86. CONN_DST_EG1_SUSTAINLEVEL = 0x020a,
  87. CONN_DST_EG1_DELAYTIME = 0x020b,
  88. CONN_DST_EG1_HOLDTIME = 0x020c,
  89. CONN_DST_EG1_SHUTDOWNTIME = 0x020d,
  90. // EG2 Destinations
  91. CONN_DST_EG2_ATTACKTIME = 0x030a,
  92. CONN_DST_EG2_DECAYTIME = 0x030b,
  93. CONN_DST_EG2_RESERVED = 0x030c,
  94. CONN_DST_EG2_RELEASETIME = 0x030d,
  95. CONN_DST_EG2_SUSTAINLEVEL = 0x030e,
  96. CONN_DST_EG2_DELAYTIME = 0x030f,
  97. CONN_DST_EG2_HOLDTIME = 0x0310,
  98. CONN_TRN_NONE = 0x0000,
  99. CONN_TRN_CONCAVE = 0x0001,
  100. };
  101. //////////////////////////////////////////////////////////
  102. // Supported DLS1 Articulations
  103. // [4-bit transform][12-bit dest][8-bit control][8-bit source] = 32-bit ID
  104. constexpr uint32 DLSArt(uint8 src, uint8 ctl, uint16 dst)
  105. {
  106. return (dst << 16u) | (ctl << 8u) | src;
  107. }
  108. enum DLSArt : uint32
  109. {
  110. // Vibrato / Tremolo
  111. ART_LFO_FREQUENCY = DLSArt(CONN_SRC_NONE, CONN_SRC_NONE, CONN_DST_LFO_FREQUENCY),
  112. ART_LFO_STARTDELAY = DLSArt(CONN_SRC_NONE, CONN_SRC_NONE, CONN_DST_LFO_STARTDELAY),
  113. ART_LFO_ATTENUATION = DLSArt(CONN_SRC_LFO, CONN_SRC_NONE, CONN_DST_ATTENUATION),
  114. ART_LFO_PITCH = DLSArt(CONN_SRC_LFO, CONN_SRC_NONE, CONN_DST_PITCH),
  115. ART_LFO_MODWTOATTN = DLSArt(CONN_SRC_LFO, CONN_SRC_CC1, CONN_DST_ATTENUATION),
  116. ART_LFO_MODWTOPITCH = DLSArt(CONN_SRC_LFO, CONN_SRC_CC1, CONN_DST_PITCH),
  117. // Volume Envelope
  118. ART_VOL_EG_ATTACKTIME = DLSArt(CONN_SRC_NONE, CONN_SRC_NONE, CONN_DST_EG1_ATTACKTIME),
  119. ART_VOL_EG_DECAYTIME = DLSArt(CONN_SRC_NONE, CONN_SRC_NONE, CONN_DST_EG1_DECAYTIME),
  120. ART_VOL_EG_SUSTAINLEVEL = DLSArt(CONN_SRC_NONE, CONN_SRC_NONE, CONN_DST_EG1_SUSTAINLEVEL),
  121. ART_VOL_EG_RELEASETIME = DLSArt(CONN_SRC_NONE, CONN_SRC_NONE, CONN_DST_EG1_RELEASETIME),
  122. ART_VOL_EG_DELAYTIME = DLSArt(CONN_SRC_NONE, CONN_SRC_NONE, CONN_DST_EG1_DELAYTIME),
  123. ART_VOL_EG_HOLDTIME = DLSArt(CONN_SRC_NONE, CONN_SRC_NONE, CONN_DST_EG1_HOLDTIME),
  124. ART_VOL_EG_SHUTDOWNTIME = DLSArt(CONN_SRC_NONE, CONN_SRC_NONE, CONN_DST_EG1_SHUTDOWNTIME),
  125. ART_VOL_EG_VELTOATTACK = DLSArt(CONN_SRC_KEYONVELOCITY, CONN_SRC_NONE, CONN_DST_EG1_ATTACKTIME),
  126. ART_VOL_EG_KEYTODECAY = DLSArt(CONN_SRC_KEYNUMBER, CONN_SRC_NONE, CONN_DST_EG1_DECAYTIME),
  127. // Pitch Envelope
  128. ART_PITCH_EG_ATTACKTIME = DLSArt(CONN_SRC_NONE, CONN_SRC_NONE, CONN_DST_EG2_ATTACKTIME),
  129. ART_PITCH_EG_DECAYTIME = DLSArt(CONN_SRC_NONE, CONN_SRC_NONE, CONN_DST_EG2_DECAYTIME),
  130. ART_PITCH_EG_SUSTAINLEVEL = DLSArt(CONN_SRC_NONE, CONN_SRC_NONE, CONN_DST_EG2_SUSTAINLEVEL),
  131. ART_PITCH_EG_RELEASETIME = DLSArt(CONN_SRC_NONE, CONN_SRC_NONE, CONN_DST_EG2_RELEASETIME),
  132. ART_PITCH_EG_VELTOATTACK = DLSArt(CONN_SRC_KEYONVELOCITY, CONN_SRC_NONE, CONN_DST_EG2_ATTACKTIME),
  133. ART_PITCH_EG_KEYTODECAY = DLSArt(CONN_SRC_KEYNUMBER, CONN_SRC_NONE, CONN_DST_EG2_DECAYTIME),
  134. // Default Pan
  135. ART_DEFAULTPAN = DLSArt(CONN_SRC_NONE, CONN_SRC_NONE, CONN_DST_PAN),
  136. };
  137. //////////////////////////////////////////////////////////
  138. // DLS IFF Chunk IDs
  139. enum IFFChunkID : uint32
  140. {
  141. // Standard IFF chunks IDs
  142. IFFID_FORM = MagicLE("FORM"),
  143. IFFID_RIFF = MagicLE("RIFF"),
  144. IFFID_LIST = MagicLE("LIST"),
  145. IFFID_INFO = MagicLE("INFO"),
  146. // IFF Info fields
  147. IFFID_ICOP = MagicLE("ICOP"),
  148. IFFID_INAM = MagicLE("INAM"),
  149. IFFID_ICMT = MagicLE("ICMT"),
  150. IFFID_IENG = MagicLE("IENG"),
  151. IFFID_ISFT = MagicLE("ISFT"),
  152. IFFID_ISBJ = MagicLE("ISBJ"),
  153. // Wave IFF chunks IDs
  154. IFFID_wave = MagicLE("wave"),
  155. IFFID_wsmp = MagicLE("wsmp"),
  156. IFFID_XDLS = MagicLE("XDLS"),
  157. IFFID_DLS = MagicLE("DLS "),
  158. IFFID_MLS = MagicLE("MLS "),
  159. IFFID_RMID = MagicLE("RMID"),
  160. IFFID_colh = MagicLE("colh"),
  161. IFFID_ins = MagicLE("ins "),
  162. IFFID_insh = MagicLE("insh"),
  163. IFFID_ptbl = MagicLE("ptbl"),
  164. IFFID_wvpl = MagicLE("wvpl"),
  165. IFFID_rgn = MagicLE("rgn "),
  166. IFFID_rgn2 = MagicLE("rgn2"),
  167. IFFID_rgnh = MagicLE("rgnh"),
  168. IFFID_wlnk = MagicLE("wlnk"),
  169. IFFID_art1 = MagicLE("art1"),
  170. IFFID_art2 = MagicLE("art2"),
  171. };
  172. //////////////////////////////////////////////////////////
  173. // DLS Structures definitions
  174. struct IFFCHUNK
  175. {
  176. uint32le id;
  177. uint32le len;
  178. };
  179. MPT_BINARY_STRUCT(IFFCHUNK, 8)
  180. struct RIFFChunkID
  181. {
  182. uint32le id_RIFF;
  183. uint32le riff_len;
  184. uint32le id_DLS;
  185. };
  186. MPT_BINARY_STRUCT(RIFFChunkID, 12)
  187. struct LISTChunk
  188. {
  189. uint32le id;
  190. uint32le len;
  191. uint32le listid;
  192. };
  193. MPT_BINARY_STRUCT(LISTChunk, 12)
  194. struct DLSRgnRange
  195. {
  196. uint16le usLow;
  197. uint16le usHigh;
  198. };
  199. MPT_BINARY_STRUCT(DLSRgnRange, 4)
  200. struct VERSChunk
  201. {
  202. uint32le id;
  203. uint32le len;
  204. uint16le version[4];
  205. };
  206. MPT_BINARY_STRUCT(VERSChunk, 16)
  207. struct PTBLChunk
  208. {
  209. uint32le cbSize;
  210. uint32le cCues;
  211. };
  212. MPT_BINARY_STRUCT(PTBLChunk, 8)
  213. struct INSHChunk
  214. {
  215. uint32le cRegions;
  216. uint32le ulBank;
  217. uint32le ulInstrument;
  218. };
  219. MPT_BINARY_STRUCT(INSHChunk, 12)
  220. struct RGNHChunk
  221. {
  222. DLSRgnRange RangeKey;
  223. DLSRgnRange RangeVelocity;
  224. uint16le fusOptions;
  225. uint16le usKeyGroup;
  226. };
  227. MPT_BINARY_STRUCT(RGNHChunk, 12)
  228. struct WLNKChunk
  229. {
  230. uint16le fusOptions;
  231. uint16le usPhaseGroup;
  232. uint32le ulChannel;
  233. uint32le ulTableIndex;
  234. };
  235. MPT_BINARY_STRUCT(WLNKChunk, 12)
  236. struct ART1Chunk
  237. {
  238. uint32le cbSize;
  239. uint32le cConnectionBlocks;
  240. };
  241. MPT_BINARY_STRUCT(ART1Chunk, 8)
  242. struct ConnectionBlock
  243. {
  244. uint16le usSource;
  245. uint16le usControl;
  246. uint16le usDestination;
  247. uint16le usTransform;
  248. int32le lScale;
  249. };
  250. MPT_BINARY_STRUCT(ConnectionBlock, 12)
  251. struct WSMPChunk
  252. {
  253. uint32le cbSize;
  254. uint16le usUnityNote;
  255. int16le sFineTune;
  256. int32le lAttenuation;
  257. uint32le fulOptions;
  258. uint32le cSampleLoops;
  259. };
  260. MPT_BINARY_STRUCT(WSMPChunk, 20)
  261. struct WSMPSampleLoop
  262. {
  263. uint32le cbSize;
  264. uint32le ulLoopType;
  265. uint32le ulLoopStart;
  266. uint32le ulLoopLength;
  267. };
  268. MPT_BINARY_STRUCT(WSMPSampleLoop, 16)
  269. /////////////////////////////////////////////////////////////////////
  270. // SF2 IFF Chunk IDs
  271. enum SF2ChunkID : uint32
  272. {
  273. IFFID_ifil = MagicLE("ifil"),
  274. IFFID_sfbk = MagicLE("sfbk"),
  275. IFFID_sfpk = MagicLE("sfpk"), // SF2Pack compressed soundfont
  276. IFFID_sdta = MagicLE("sdta"),
  277. IFFID_pdta = MagicLE("pdta"),
  278. IFFID_phdr = MagicLE("phdr"),
  279. IFFID_pbag = MagicLE("pbag"),
  280. IFFID_pgen = MagicLE("pgen"),
  281. IFFID_inst = MagicLE("inst"),
  282. IFFID_ibag = MagicLE("ibag"),
  283. IFFID_igen = MagicLE("igen"),
  284. IFFID_shdr = MagicLE("shdr"),
  285. };
  286. ///////////////////////////////////////////
  287. // SF2 Generators IDs
  288. enum SF2Generators : uint16
  289. {
  290. SF2_GEN_START_LOOP_FINE = 2,
  291. SF2_GEN_END_LOOP_FINE = 3,
  292. SF2_GEN_MODENVTOFILTERFC = 11,
  293. SF2_GEN_PAN = 17,
  294. SF2_GEN_DECAYMODENV = 28,
  295. SF2_GEN_ATTACKVOLENV = 34,
  296. SF2_GEN_HOLDVOLENV = 34,
  297. SF2_GEN_DECAYVOLENV = 36,
  298. SF2_GEN_SUSTAINVOLENV = 37,
  299. SF2_GEN_RELEASEVOLENV = 38,
  300. SF2_GEN_INSTRUMENT = 41,
  301. SF2_GEN_KEYRANGE = 43,
  302. SF2_GEN_START_LOOP_COARSE = 45,
  303. SF2_GEN_ATTENUATION = 48,
  304. SF2_GEN_END_LOOP_COARSE = 50,
  305. SF2_GEN_COARSETUNE = 51,
  306. SF2_GEN_FINETUNE = 52,
  307. SF2_GEN_SAMPLEID = 53,
  308. SF2_GEN_SAMPLEMODES = 54,
  309. SF2_GEN_SCALE_TUNING = 56,
  310. SF2_GEN_KEYGROUP = 57,
  311. SF2_GEN_UNITYNOTE = 58,
  312. };
  313. /////////////////////////////////////////////////////////////////////
  314. // SF2 Structures Definitions
  315. struct SFPresetHeader
  316. {
  317. char achPresetName[20];
  318. uint16le wPreset;
  319. uint16le wBank;
  320. uint16le wPresetBagNdx;
  321. uint32le dwLibrary;
  322. uint32le dwGenre;
  323. uint32le dwMorphology;
  324. };
  325. MPT_BINARY_STRUCT(SFPresetHeader, 38)
  326. struct SFPresetBag
  327. {
  328. uint16le wGenNdx;
  329. uint16le wModNdx;
  330. };
  331. MPT_BINARY_STRUCT(SFPresetBag, 4)
  332. struct SFGenList
  333. {
  334. uint16le sfGenOper;
  335. uint16le genAmount;
  336. };
  337. MPT_BINARY_STRUCT(SFGenList, 4)
  338. struct SFInst
  339. {
  340. char achInstName[20];
  341. uint16le wInstBagNdx;
  342. };
  343. MPT_BINARY_STRUCT(SFInst, 22)
  344. struct SFInstBag
  345. {
  346. uint16le wGenNdx;
  347. uint16le wModNdx;
  348. };
  349. MPT_BINARY_STRUCT(SFInstBag, 4)
  350. struct SFInstGenList
  351. {
  352. uint16le sfGenOper;
  353. uint16le genAmount;
  354. };
  355. MPT_BINARY_STRUCT(SFInstGenList, 4)
  356. struct SFSample
  357. {
  358. char achSampleName[20];
  359. uint32le dwStart;
  360. uint32le dwEnd;
  361. uint32le dwStartloop;
  362. uint32le dwEndloop;
  363. uint32le dwSampleRate;
  364. uint8le byOriginalPitch;
  365. int8le chPitchCorrection;
  366. uint16le wSampleLink;
  367. uint16le sfSampleType;
  368. };
  369. MPT_BINARY_STRUCT(SFSample, 46)
  370. // End of structures definitions
  371. /////////////////////////////////////////////////////////////////////
  372. struct SF2LoaderInfo
  373. {
  374. FileReader presetBags;
  375. FileReader presetGens;
  376. FileReader insts;
  377. FileReader instBags;
  378. FileReader instGens;
  379. };
  380. /////////////////////////////////////////////////////////////////////
  381. // Unit conversion
  382. static uint8 DLSSustainLevelToLinear(int32 sustain)
  383. {
  384. // 0.1% units
  385. if(sustain >= 0)
  386. {
  387. int32 l = sustain / (1000 * 512);
  388. if(l >= 0 && l <= 128)
  389. return static_cast<uint8>(l);
  390. }
  391. return 128;
  392. }
  393. static uint8 SF2SustainLevelToLinear(int32 sustain)
  394. {
  395. // 0.1% units
  396. int32 l = 128 * (1000 - Clamp(sustain, 0, 1000)) / 1000;
  397. return static_cast<uint8>(l);
  398. }
  399. int32 CDLSBank::DLS32BitTimeCentsToMilliseconds(int32 lTimeCents)
  400. {
  401. // tc = log2(time[secs]) * 1200*65536
  402. // time[secs] = 2^(tc/(1200*65536))
  403. if ((uint32)lTimeCents == 0x80000000) return 0;
  404. double fmsecs = 1000.0 * std::pow(2.0, ((double)lTimeCents)/(1200.0*65536.0));
  405. if (fmsecs < -32767) return -32767;
  406. if (fmsecs > 32767) return 32767;
  407. return (int32)fmsecs;
  408. }
  409. // 0dB = 0x10000
  410. int32 CDLSBank::DLS32BitRelativeGainToLinear(int32 lCentibels)
  411. {
  412. // v = 10^(cb/(200*65536)) * V
  413. return (int32)(65536.0 * std::pow(10.0, ((double)lCentibels)/(200*65536.0)) );
  414. }
  415. int32 CDLSBank::DLS32BitRelativeLinearToGain(int32 lGain)
  416. {
  417. // cb = log10(v/V) * 200 * 65536
  418. if (lGain <= 0) return -960 * 65536;
  419. return (int32)(200 * 65536.0 * std::log10(((double)lGain) / 65536.0));
  420. }
  421. int32 CDLSBank::DLSMidiVolumeToLinear(uint32 nMidiVolume)
  422. {
  423. return (nMidiVolume * nMidiVolume << 16) / (127*127);
  424. }
  425. /////////////////////////////////////////////////////////////////////
  426. // Implementation
  427. CDLSBank::CDLSBank()
  428. {
  429. m_nMaxWaveLink = 0;
  430. m_nType = SOUNDBANK_TYPE_INVALID;
  431. }
  432. bool CDLSBank::IsDLSBank(const mpt::PathString &filename)
  433. {
  434. RIFFChunkID riff;
  435. if(filename.empty()) return false;
  436. mpt::ifstream f(filename, std::ios::binary);
  437. if(!f)
  438. {
  439. return false;
  440. }
  441. MemsetZero(riff);
  442. mpt::IO::Read(f, riff);
  443. // Check for embedded DLS sections
  444. if(riff.id_RIFF == IFFID_FORM)
  445. {
  446. // Miles Sound System
  447. do
  448. {
  449. uint32 len = mpt::bit_cast<uint32be>(riff.riff_len);
  450. if (len <= 4) break;
  451. if (riff.id_DLS == IFFID_XDLS)
  452. {
  453. mpt::IO::Read(f, riff);
  454. break;
  455. }
  456. if((len % 2u) != 0)
  457. len++;
  458. if (!mpt::IO::SeekRelative(f, len-4)) break;
  459. } while (mpt::IO::Read(f, riff));
  460. } else if(riff.id_RIFF == IFFID_RIFF && riff.id_DLS == IFFID_RMID)
  461. {
  462. for (;;)
  463. {
  464. if(!mpt::IO::Read(f, riff))
  465. break;
  466. if (riff.id_DLS == IFFID_DLS)
  467. break; // found it
  468. int len = riff.riff_len;
  469. if((len % 2u) != 0)
  470. len++;
  471. if ((len <= 4) || !mpt::IO::SeekRelative(f, len-4)) break;
  472. }
  473. }
  474. return ((riff.id_RIFF == IFFID_RIFF)
  475. && ((riff.id_DLS == IFFID_DLS) || (riff.id_DLS == IFFID_MLS) || (riff.id_DLS == IFFID_sfbk))
  476. && (riff.riff_len >= 256));
  477. }
  478. ///////////////////////////////////////////////////////////////
  479. // Find an instrument based on the given parameters
  480. const DLSINSTRUMENT *CDLSBank::FindInstrument(bool isDrum, uint32 bank, uint32 program, uint32 key, uint32 *pInsNo) const
  481. {
  482. // This helps finding the "more correct" instrument if we search for an instrument in any bank, and the higher-bank instruments appear first in the file
  483. // Fixes issues when loading GeneralUser GS into OpenMPT's MIDI library.
  484. std::vector<std::reference_wrapper<const DLSINSTRUMENT>> sortedInstr{m_Instruments.begin(), m_Instruments.end()};
  485. if(bank >= 0x4000 || program >= 0x80)
  486. {
  487. std::sort(sortedInstr.begin(), sortedInstr.end(), [](const DLSINSTRUMENT &l, const DLSINSTRUMENT &r)
  488. { return std::tie(l.ulBank, l.ulInstrument) < std::tie(r.ulBank, r.ulInstrument); });
  489. }
  490. for(const DLSINSTRUMENT &dlsIns : sortedInstr)
  491. {
  492. uint32 insbank = ((dlsIns.ulBank & 0x7F00) >> 1) | (dlsIns.ulBank & 0x7F);
  493. if((bank >= 0x4000) || (insbank == bank))
  494. {
  495. if(isDrum && (dlsIns.ulBank & F_INSTRUMENT_DRUMS))
  496. {
  497. if((program >= 0x80) || (program == (dlsIns.ulInstrument & 0x7F)))
  498. {
  499. for(const auto &region : dlsIns.Regions)
  500. {
  501. if(region.IsDummy())
  502. continue;
  503. if((!key || key >= 0x80)
  504. || (key >= region.uKeyMin && key <= region.uKeyMax))
  505. {
  506. if(pInsNo)
  507. *pInsNo = static_cast<uint32>(std::distance(m_Instruments.data(), &dlsIns));
  508. // cppcheck false-positive
  509. // cppcheck-suppress returnDanglingLifetime
  510. return &dlsIns;
  511. }
  512. }
  513. }
  514. } else if(!isDrum && !(dlsIns.ulBank & F_INSTRUMENT_DRUMS))
  515. {
  516. if((program >= 0x80) || (program == (dlsIns.ulInstrument & 0x7F)))
  517. {
  518. if(pInsNo)
  519. *pInsNo = static_cast<uint32>(std::distance(m_Instruments.data(), &dlsIns));
  520. // cppcheck false-positive
  521. // cppcheck-suppress returnDanglingLifetime
  522. return &dlsIns;
  523. }
  524. }
  525. }
  526. }
  527. return nullptr;
  528. }
  529. bool CDLSBank::FindAndExtract(CSoundFile &sndFile, const INSTRUMENTINDEX ins, const bool isDrum) const
  530. {
  531. ModInstrument *pIns = sndFile.Instruments[ins];
  532. if(pIns == nullptr)
  533. return false;
  534. uint32 dlsIns = 0, drumRgn = 0;
  535. const uint32 program = (pIns->nMidiProgram != 0) ? pIns->nMidiProgram - 1 : 0;
  536. const uint32 key = isDrum ? (pIns->nMidiDrumKey & 0x7F) : 0xFF;
  537. if(FindInstrument(isDrum, (pIns->wMidiBank - 1) & 0x3FFF, program, key, &dlsIns)
  538. || FindInstrument(isDrum, (pIns->wMidiBank - 1) & 0x3F80, program, key, &dlsIns)
  539. || FindInstrument(isDrum, 0xFFFF, isDrum ? 0xFF : program, key, &dlsIns))
  540. {
  541. if(key < 0x80) drumRgn = GetRegionFromKey(dlsIns, key);
  542. if(ExtractInstrument(sndFile, ins, dlsIns, drumRgn))
  543. {
  544. pIns = sndFile.Instruments[ins]; // Reset pointer because ExtractInstrument may delete the previous value.
  545. if((key >= 24) && (key < 24 + std::size(szMidiPercussionNames)))
  546. {
  547. pIns->name = szMidiPercussionNames[key - 24];
  548. }
  549. return true;
  550. }
  551. }
  552. return false;
  553. }
  554. ///////////////////////////////////////////////////////////////
  555. // Update DLS instrument definition from an IFF chunk
  556. bool CDLSBank::UpdateInstrumentDefinition(DLSINSTRUMENT *pDlsIns, FileReader chunk)
  557. {
  558. IFFCHUNK header;
  559. chunk.ReadStruct(header);
  560. if(!header.len || !chunk.CanRead(header.len))
  561. return false;
  562. if(header.id == IFFID_LIST)
  563. {
  564. uint32 listid = chunk.ReadUint32LE();
  565. while(chunk.CanRead(sizeof(IFFCHUNK)))
  566. {
  567. IFFCHUNK subHeader;
  568. chunk.ReadStruct(subHeader);
  569. chunk.SkipBack(sizeof(IFFCHUNK));
  570. FileReader subData = chunk.ReadChunk(subHeader.len + sizeof(IFFCHUNK));
  571. if(subHeader.len & 1)
  572. {
  573. chunk.Skip(1);
  574. }
  575. UpdateInstrumentDefinition(pDlsIns, subData);
  576. }
  577. switch(listid)
  578. {
  579. case IFFID_rgn: // Level 1 region
  580. case IFFID_rgn2: // Level 2 region
  581. pDlsIns->Regions.push_back({});
  582. break;
  583. }
  584. } else
  585. {
  586. switch(header.id)
  587. {
  588. case IFFID_insh:
  589. {
  590. INSHChunk insh;
  591. chunk.ReadStruct(insh);
  592. pDlsIns->ulBank = insh.ulBank;
  593. pDlsIns->ulInstrument = insh.ulInstrument;
  594. //Log("%3d regions, bank 0x%04X instrument %3d\n", insh.cRegions, pDlsIns->ulBank, pDlsIns->ulInstrument);
  595. break;
  596. }
  597. case IFFID_rgnh:
  598. if(!pDlsIns->Regions.empty())
  599. {
  600. RGNHChunk rgnh;
  601. chunk.ReadStruct(rgnh);
  602. DLSREGION &region = pDlsIns->Regions.back();
  603. region.uKeyMin = (uint8)rgnh.RangeKey.usLow;
  604. region.uKeyMax = (uint8)rgnh.RangeKey.usHigh;
  605. region.fuOptions = (uint8)(rgnh.usKeyGroup & DLSREGION_KEYGROUPMASK);
  606. if(rgnh.fusOptions & F_RGN_OPTION_SELFNONEXCLUSIVE)
  607. region.fuOptions |= DLSREGION_SELFNONEXCLUSIVE;
  608. //Log(" Region %d: fusOptions=0x%02X usKeyGroup=0x%04X ", pDlsIns->nRegions, rgnh.fusOptions, rgnh.usKeyGroup);
  609. //Log("KeyRange[%3d,%3d] ", rgnh.RangeKey.usLow, rgnh.RangeKey.usHigh);
  610. }
  611. break;
  612. case IFFID_wlnk:
  613. if (!pDlsIns->Regions.empty())
  614. {
  615. WLNKChunk wlnk;
  616. chunk.ReadStruct(wlnk);
  617. DLSREGION &region = pDlsIns->Regions.back();
  618. region.nWaveLink = (uint16)wlnk.ulTableIndex;
  619. if((region.nWaveLink < Util::MaxValueOfType(region.nWaveLink)) && (region.nWaveLink >= m_nMaxWaveLink))
  620. m_nMaxWaveLink = region.nWaveLink + 1;
  621. //Log(" WaveLink %d: fusOptions=0x%02X usPhaseGroup=0x%04X ", pDlsIns->nRegions, wlnk.fusOptions, wlnk.usPhaseGroup);
  622. //Log("ulChannel=%d ulTableIndex=%4d\n", wlnk.ulChannel, wlnk.ulTableIndex);
  623. }
  624. break;
  625. case IFFID_wsmp:
  626. if(!pDlsIns->Regions.empty())
  627. {
  628. DLSREGION &region = pDlsIns->Regions.back();
  629. WSMPChunk wsmp;
  630. chunk.ReadStruct(wsmp);
  631. region.fuOptions |= DLSREGION_OVERRIDEWSMP;
  632. region.uUnityNote = (uint8)wsmp.usUnityNote;
  633. region.sFineTune = wsmp.sFineTune;
  634. int32 lVolume = DLS32BitRelativeGainToLinear(wsmp.lAttenuation) / 256;
  635. if (lVolume > 256) lVolume = 256;
  636. if (lVolume < 4) lVolume = 4;
  637. region.usVolume = (uint16)lVolume;
  638. //Log(" WaveSample %d: usUnityNote=%2d sFineTune=%3d ", pDlsEnv->nRegions, p->usUnityNote, p->sFineTune);
  639. //Log("fulOptions=0x%04X loops=%d\n", p->fulOptions, p->cSampleLoops);
  640. if((wsmp.cSampleLoops) && (wsmp.cbSize + sizeof(WSMPSampleLoop) <= header.len))
  641. {
  642. WSMPSampleLoop loop;
  643. chunk.Seek(sizeof(IFFCHUNK) + wsmp.cbSize);
  644. chunk.ReadStruct(loop);
  645. //Log("looptype=%2d loopstart=%5d loopend=%5d\n", ploop->ulLoopType, ploop->ulLoopStart, ploop->ulLoopLength);
  646. if(loop.ulLoopLength > 3)
  647. {
  648. region.fuOptions |= DLSREGION_SAMPLELOOP;
  649. //if(loop.ulLoopType) region.fuOptions |= DLSREGION_PINGPONGLOOP;
  650. region.ulLoopStart = loop.ulLoopStart;
  651. region.ulLoopEnd = loop.ulLoopStart + loop.ulLoopLength;
  652. }
  653. }
  654. }
  655. break;
  656. case IFFID_art1:
  657. case IFFID_art2:
  658. {
  659. ART1Chunk art1;
  660. chunk.ReadStruct(art1);
  661. if(!(pDlsIns->ulBank & F_INSTRUMENT_DRUMS))
  662. {
  663. pDlsIns->nMelodicEnv = static_cast<uint32>(m_Envelopes.size() + 1);
  664. }
  665. if(art1.cbSize + art1.cConnectionBlocks * sizeof(ConnectionBlock) > header.len)
  666. break;
  667. DLSENVELOPE dlsEnv;
  668. MemsetZero(dlsEnv);
  669. dlsEnv.nDefPan = 128;
  670. dlsEnv.nVolSustainLevel = 128;
  671. //Log(" art1 (%3d bytes): cbSize=%d cConnectionBlocks=%d\n", p->len, p->cbSize, p->cConnectionBlocks);
  672. chunk.Seek(sizeof(IFFCHUNK) + art1.cbSize);
  673. for (uint32 iblk = 0; iblk < art1.cConnectionBlocks; iblk++)
  674. {
  675. ConnectionBlock blk;
  676. chunk.ReadStruct(blk);
  677. // [4-bit transform][12-bit dest][8-bit control][8-bit source] = 32-bit ID
  678. uint32 dwArticulation = blk.usTransform;
  679. dwArticulation = (dwArticulation << 12) | (blk.usDestination & 0x0FFF);
  680. dwArticulation = (dwArticulation << 8) | (blk.usControl & 0x00FF);
  681. dwArticulation = (dwArticulation << 8) | (blk.usSource & 0x00FF);
  682. switch(dwArticulation)
  683. {
  684. case ART_DEFAULTPAN:
  685. {
  686. int32 pan = 128 + blk.lScale / (65536000/128);
  687. dlsEnv.nDefPan = mpt::saturate_cast<uint8>(pan);
  688. }
  689. break;
  690. case ART_VOL_EG_ATTACKTIME:
  691. // 32-bit time cents units. range = [0s, 20s]
  692. dlsEnv.wVolAttack = 0;
  693. if(blk.lScale > -0x40000000)
  694. {
  695. int32 l = blk.lScale - 78743200; // maximum velocity
  696. if (l > 0) l = 0;
  697. int32 attacktime = DLS32BitTimeCentsToMilliseconds(l);
  698. if (attacktime < 0) attacktime = 0;
  699. if (attacktime > 20000) attacktime = 20000;
  700. if (attacktime >= 20) dlsEnv.wVolAttack = (uint16)(attacktime / 20);
  701. //Log("%3d: Envelope Attack Time set to %d (%d time cents)\n", (uint32)(dlsEnv.ulInstrument & 0x7F)|((dlsEnv.ulBank >> 16) & 0x8000), attacktime, pblk->lScale);
  702. }
  703. break;
  704. case ART_VOL_EG_DECAYTIME:
  705. // 32-bit time cents units. range = [0s, 20s]
  706. dlsEnv.wVolDecay = 0;
  707. if(blk.lScale > -0x40000000)
  708. {
  709. int32 decaytime = DLS32BitTimeCentsToMilliseconds(blk.lScale);
  710. if (decaytime > 20000) decaytime = 20000;
  711. if (decaytime >= 20) dlsEnv.wVolDecay = (uint16)(decaytime / 20);
  712. //Log("%3d: Envelope Decay Time set to %d (%d time cents)\n", (uint32)(dlsEnv.ulInstrument & 0x7F)|((dlsEnv.ulBank >> 16) & 0x8000), decaytime, pblk->lScale);
  713. }
  714. break;
  715. case ART_VOL_EG_RELEASETIME:
  716. // 32-bit time cents units. range = [0s, 20s]
  717. dlsEnv.wVolRelease = 0;
  718. if(blk.lScale > -0x40000000)
  719. {
  720. int32 releasetime = DLS32BitTimeCentsToMilliseconds(blk.lScale);
  721. if (releasetime > 20000) releasetime = 20000;
  722. if (releasetime >= 20) dlsEnv.wVolRelease = (uint16)(releasetime / 20);
  723. //Log("%3d: Envelope Release Time set to %d (%d time cents)\n", (uint32)(dlsEnv.ulInstrument & 0x7F)|((dlsEnv.ulBank >> 16) & 0x8000), dlsEnv.wVolRelease, pblk->lScale);
  724. }
  725. break;
  726. case ART_VOL_EG_SUSTAINLEVEL:
  727. // 0.1% units
  728. if(blk.lScale >= 0)
  729. {
  730. dlsEnv.nVolSustainLevel = DLSSustainLevelToLinear(blk.lScale);
  731. }
  732. break;
  733. //default:
  734. // Log(" Articulation = 0x%08X value=%d\n", dwArticulation, blk.lScale);
  735. }
  736. }
  737. m_Envelopes.push_back(dlsEnv);
  738. }
  739. break;
  740. case IFFID_INAM:
  741. chunk.ReadString<mpt::String::spacePadded>(pDlsIns->szName, header.len);
  742. break;
  743. #if 0
  744. default:
  745. {
  746. char sid[5];
  747. memcpy(sid, &header.id, 4);
  748. sid[4] = 0;
  749. Log(" \"%s\": %d bytes\n", (uint32)sid, header.len.get());
  750. }
  751. #endif
  752. }
  753. }
  754. return true;
  755. }
  756. ///////////////////////////////////////////////////////////////
  757. // Converts SF2 chunks to DLS
  758. bool CDLSBank::UpdateSF2PresetData(SF2LoaderInfo &sf2info, const IFFCHUNK &header, FileReader &chunk)
  759. {
  760. if (!chunk.IsValid()) return false;
  761. switch(header.id)
  762. {
  763. case IFFID_phdr:
  764. if(m_Instruments.empty())
  765. {
  766. uint32 numIns = static_cast<uint32>(chunk.GetLength() / sizeof(SFPresetHeader));
  767. if(numIns <= 1)
  768. break;
  769. // The terminal sfPresetHeader record should never be accessed, and exists only to provide a terminal wPresetBagNdx with which to determine the number of zones in the last preset.
  770. numIns--;
  771. m_Instruments.resize(numIns);
  772. #ifdef DLSBANK_LOG
  773. MPT_LOG_GLOBAL(LogDebug, "DLSBank", MPT_UFORMAT("phdr: {} instruments")(m_Instruments.size()));
  774. #endif
  775. SFPresetHeader psfh;
  776. chunk.ReadStruct(psfh);
  777. for(auto &dlsIns : m_Instruments)
  778. {
  779. mpt::String::WriteAutoBuf(dlsIns.szName) = mpt::String::ReadAutoBuf(psfh.achPresetName);
  780. dlsIns.ulInstrument = psfh.wPreset & 0x7F;
  781. dlsIns.ulBank = (psfh.wBank >= 128) ? F_INSTRUMENT_DRUMS : (psfh.wBank << 8);
  782. dlsIns.wPresetBagNdx = psfh.wPresetBagNdx;
  783. dlsIns.wPresetBagNum = 1;
  784. chunk.ReadStruct(psfh);
  785. if (psfh.wPresetBagNdx > dlsIns.wPresetBagNdx) dlsIns.wPresetBagNum = static_cast<uint16>(psfh.wPresetBagNdx - dlsIns.wPresetBagNdx);
  786. }
  787. }
  788. break;
  789. case IFFID_pbag:
  790. if(!m_Instruments.empty() && chunk.CanRead(sizeof(SFPresetBag)))
  791. {
  792. sf2info.presetBags = chunk.GetChunk(chunk.BytesLeft());
  793. }
  794. #ifdef DLSINSTR_LOG
  795. else MPT_LOG_GLOBAL(LogDebug, "DLSINSTR", U_("pbag: no instruments!"));
  796. #endif
  797. break;
  798. case IFFID_pgen:
  799. if(!m_Instruments.empty() && chunk.CanRead(sizeof(SFGenList)))
  800. {
  801. sf2info.presetGens = chunk.GetChunk(chunk.BytesLeft());
  802. }
  803. #ifdef DLSINSTR_LOG
  804. else MPT_LOG_GLOBAL(LogDebug, "DLSINSTR", U_("pgen: no instruments!"));
  805. #endif
  806. break;
  807. case IFFID_inst:
  808. if(!m_Instruments.empty() && chunk.CanRead(sizeof(SFInst)))
  809. {
  810. sf2info.insts = chunk.GetChunk(chunk.BytesLeft());
  811. }
  812. break;
  813. case IFFID_ibag:
  814. if(!m_Instruments.empty() && chunk.CanRead(sizeof(SFInstBag)))
  815. {
  816. sf2info.instBags = chunk.GetChunk(chunk.BytesLeft());
  817. }
  818. break;
  819. case IFFID_igen:
  820. if(!m_Instruments.empty() && chunk.CanRead(sizeof(SFInstGenList)))
  821. {
  822. sf2info.instGens = chunk.GetChunk(chunk.BytesLeft());
  823. }
  824. break;
  825. case IFFID_shdr:
  826. if (m_SamplesEx.empty())
  827. {
  828. uint32 numSmp = static_cast<uint32>(chunk.GetLength() / sizeof(SFSample));
  829. if (numSmp < 1) break;
  830. m_SamplesEx.resize(numSmp);
  831. m_WaveForms.resize(numSmp);
  832. #ifdef DLSINSTR_LOG
  833. MPT_LOG_GLOBAL(LogDebug, "DLSINSTR", MPT_UFORMAT("shdr: {} samples")(m_SamplesEx.size()));
  834. #endif
  835. for (uint32 i = 0; i < numSmp; i++)
  836. {
  837. SFSample p;
  838. chunk.ReadStruct(p);
  839. DLSSAMPLEEX &dlsSmp = m_SamplesEx[i];
  840. mpt::String::WriteAutoBuf(dlsSmp.szName) = mpt::String::ReadAutoBuf(p.achSampleName);
  841. dlsSmp.dwLen = 0;
  842. dlsSmp.dwSampleRate = p.dwSampleRate;
  843. dlsSmp.byOriginalPitch = p.byOriginalPitch;
  844. dlsSmp.chPitchCorrection = static_cast<int8>(Util::muldivr(p.chPitchCorrection, 128, 100));
  845. // cognitone's sf2convert tool doesn't set the correct sample flags (0x01 / 0x02 instead of 0x10/ 0x20).
  846. // For SF3, we ignore this and go by https://github.com/FluidSynth/fluidsynth/wiki/SoundFont3Format instead
  847. // As cognitone's tool is the only tool writing SF4 files, we always assume compressed samples with SF4 files if bits 0/1 are set.
  848. uint16 sampleType = p.sfSampleType;
  849. if(m_sf2version >= 0x4'0000 && m_sf2version <= 0x4'FFFF && (sampleType & 0x03))
  850. sampleType = (sampleType & 0xFFFC) | 0x10;
  851. dlsSmp.compressed = (sampleType & 0x10);
  852. if(((sampleType & 0x7FCF) <= 4) && (p.dwEnd >= p.dwStart + 4))
  853. {
  854. m_WaveForms[i] = p.dwStart;
  855. dlsSmp.dwLen = (p.dwEnd - p.dwStart);
  856. if(!dlsSmp.compressed)
  857. {
  858. m_WaveForms[i] *= 2;
  859. dlsSmp.dwLen *= 2;
  860. if((p.dwEndloop > p.dwStartloop + 7) && (p.dwStartloop >= p.dwStart))
  861. {
  862. dlsSmp.dwStartloop = p.dwStartloop - p.dwStart;
  863. dlsSmp.dwEndloop = p.dwEndloop - p.dwStart;
  864. }
  865. } else
  866. {
  867. if(p.dwEndloop > p.dwStartloop + 7)
  868. {
  869. dlsSmp.dwStartloop = p.dwStartloop;
  870. dlsSmp.dwEndloop = p.dwEndloop;
  871. }
  872. }
  873. }
  874. }
  875. }
  876. break;
  877. #ifdef DLSINSTR_LOG
  878. default:
  879. {
  880. char sdbg[5];
  881. memcpy(sdbg, &header.id, 4);
  882. sdbg[4] = 0;
  883. MPT_LOG_GLOBAL(LogDebug, "DLSINSTR", MPT_UFORMAT("Unsupported SF2 chunk: {} ({} bytes)")(mpt::ToUnicode(mpt::Charset::ASCII, mpt::String::ReadAutoBuf(sdbg)), header.len.get()));
  884. }
  885. #endif
  886. }
  887. return true;
  888. }
  889. static int16 SF2TimeToDLS(int16 amount)
  890. {
  891. int32 time = CDLSBank::DLS32BitTimeCentsToMilliseconds(static_cast<int32>(amount) << 16);
  892. return static_cast<int16>(Clamp(time, 20, 20000) / 20);
  893. }
  894. // Convert all instruments to the DLS format
  895. bool CDLSBank::ConvertSF2ToDLS(SF2LoaderInfo &sf2info)
  896. {
  897. if (m_Instruments.empty() || m_SamplesEx.empty())
  898. return false;
  899. const uint32 numInsts = static_cast<uint32>(sf2info.insts.GetLength() / sizeof(SFInst));
  900. const uint32 numInstBags = static_cast<uint32>(sf2info.instBags.GetLength() / sizeof(SFInstBag));
  901. std::vector<std::pair<uint16, uint16>> instruments; // instrument, key range
  902. std::vector<SFGenList> generators;
  903. std::vector<SFInstGenList> instrGenerators;
  904. for(auto &dlsIns : m_Instruments)
  905. {
  906. instruments.clear();
  907. DLSENVELOPE dlsEnv;
  908. int32 instrAttenuation = 0;
  909. int16 instrFinetune = 0;
  910. // Default Envelope Values
  911. dlsEnv.wVolAttack = 0;
  912. dlsEnv.wVolDecay = 0;
  913. dlsEnv.wVolRelease = 0;
  914. dlsEnv.nVolSustainLevel = 128;
  915. dlsEnv.nDefPan = 128;
  916. // Load Preset Bags
  917. sf2info.presetBags.Seek(dlsIns.wPresetBagNdx * sizeof(SFPresetBag));
  918. for(uint32 ipbagcnt = 0; ipbagcnt < dlsIns.wPresetBagNum; ipbagcnt++)
  919. {
  920. // Load generators for each preset bag
  921. SFPresetBag bag[2];
  922. if(!sf2info.presetBags.ReadArray(bag))
  923. break;
  924. sf2info.presetBags.SkipBack(sizeof(SFPresetBag));
  925. sf2info.presetGens.Seek(bag[0].wGenNdx * sizeof(SFGenList));
  926. uint16 keyRange = 0xFFFF;
  927. if(!sf2info.presetGens.ReadVector(generators, bag[1].wGenNdx - bag[0].wGenNdx))
  928. continue;
  929. for(const auto &gen : generators)
  930. {
  931. const int16 value = static_cast<int16>(gen.genAmount);
  932. switch(gen.sfGenOper)
  933. {
  934. case SF2_GEN_ATTACKVOLENV:
  935. dlsEnv.wVolAttack = SF2TimeToDLS(gen.genAmount);
  936. break;
  937. case SF2_GEN_DECAYVOLENV:
  938. dlsEnv.wVolDecay = SF2TimeToDLS(gen.genAmount);
  939. break;
  940. case SF2_GEN_SUSTAINVOLENV:
  941. // 0.1% units
  942. if(gen.genAmount >= 0)
  943. {
  944. dlsEnv.nVolSustainLevel = SF2SustainLevelToLinear(gen.genAmount);
  945. }
  946. break;
  947. case SF2_GEN_RELEASEVOLENV:
  948. dlsEnv.wVolRelease = SF2TimeToDLS(gen.genAmount);
  949. break;
  950. case SF2_GEN_INSTRUMENT:
  951. if(const auto instr = std::make_pair(gen.genAmount.get(), keyRange); !mpt::contains(instruments, instr))
  952. instruments.push_back(instr);
  953. keyRange = 0xFFFF;
  954. break;
  955. case SF2_GEN_KEYRANGE:
  956. keyRange = gen.genAmount;
  957. break;
  958. case SF2_GEN_ATTENUATION:
  959. instrAttenuation = -value;
  960. break;
  961. case SF2_GEN_COARSETUNE:
  962. instrFinetune += value * 128;
  963. break;
  964. case SF2_GEN_FINETUNE:
  965. instrFinetune += static_cast<int16>(Util::muldiv(static_cast<int8>(value), 128, 100));
  966. break;
  967. #if 0
  968. default:
  969. Log("Ins %3d: bag %3d gen %3d: ", nIns, ipbagndx, ipgenndx);
  970. Log("genoper=%d amount=0x%04X ", gen.sfGenOper, gen.genAmount);
  971. Log((pSmp->ulBank & F_INSTRUMENT_DRUMS) ? "(drum)\n" : "\n");
  972. #endif
  973. }
  974. }
  975. }
  976. // Envelope
  977. if (!(dlsIns.ulBank & F_INSTRUMENT_DRUMS))
  978. {
  979. m_Envelopes.push_back(dlsEnv);
  980. dlsIns.nMelodicEnv = static_cast<uint32>(m_Envelopes.size());
  981. }
  982. // Load Instrument Bags
  983. dlsIns.Regions.clear();
  984. for(const auto & [nInstrNdx, keyRange] : instruments)
  985. {
  986. if(nInstrNdx >= numInsts)
  987. continue;
  988. sf2info.insts.Seek(nInstrNdx * sizeof(SFInst));
  989. SFInst insts[2];
  990. sf2info.insts.ReadArray(insts);
  991. const uint32 numRegions = insts[1].wInstBagNdx - insts[0].wInstBagNdx;
  992. dlsIns.Regions.reserve(dlsIns.Regions.size() + numRegions);
  993. //Log("\nIns %3d, %2d regions:\n", nIns, pSmp->nRegions);
  994. DLSREGION globalZone{};
  995. globalZone.uUnityNote = 0xFF; // 0xFF means undefined -> use sample root note
  996. globalZone.tuning = 100;
  997. globalZone.sFineTune = instrFinetune;
  998. globalZone.nWaveLink = Util::MaxValueOfType(globalZone.nWaveLink);
  999. if(keyRange != 0xFFFF)
  1000. {
  1001. globalZone.uKeyMin = static_cast<uint8>(keyRange & 0xFF);
  1002. globalZone.uKeyMax = static_cast<uint8>(keyRange >> 8);
  1003. if(globalZone.uKeyMin > globalZone.uKeyMax)
  1004. std::swap(globalZone.uKeyMin, globalZone.uKeyMax);
  1005. } else
  1006. {
  1007. globalZone.uKeyMin = 0;
  1008. globalZone.uKeyMax = 127;
  1009. }
  1010. for(uint32 nRgn = 0; nRgn < numRegions; nRgn++)
  1011. {
  1012. uint32 ibagcnt = insts[0].wInstBagNdx + nRgn;
  1013. if(ibagcnt >= numInstBags)
  1014. break;
  1015. // Create a new envelope for drums
  1016. DLSENVELOPE *pDlsEnv = &dlsEnv;
  1017. if(!(dlsIns.ulBank & F_INSTRUMENT_DRUMS) && dlsIns.nMelodicEnv > 0 && dlsIns.nMelodicEnv <= m_Envelopes.size())
  1018. {
  1019. pDlsEnv = &m_Envelopes[dlsIns.nMelodicEnv - 1];
  1020. }
  1021. DLSREGION rgn = globalZone;
  1022. // Region Default Values
  1023. int32 regionAttn = 0;
  1024. // Load Generators
  1025. sf2info.instBags.Seek(ibagcnt * sizeof(SFInstBag));
  1026. SFInstBag bags[2];
  1027. sf2info.instBags.ReadArray(bags);
  1028. sf2info.instGens.Seek(bags[0].wGenNdx * sizeof(SFInstGenList));
  1029. uint16 lastOp = SF2_GEN_SAMPLEID;
  1030. int32 loopStart = 0, loopEnd = 0;
  1031. if(!sf2info.instGens.ReadVector(instrGenerators, bags[1].wGenNdx - bags[0].wGenNdx))
  1032. break;
  1033. for(const auto &gen : instrGenerators)
  1034. {
  1035. uint16 value = gen.genAmount;
  1036. lastOp = gen.sfGenOper;
  1037. switch(gen.sfGenOper)
  1038. {
  1039. case SF2_GEN_KEYRANGE:
  1040. {
  1041. uint8 keyMin = static_cast<uint8>(value & 0xFF);
  1042. uint8 keyMax = static_cast<uint8>(value >> 8);
  1043. if(keyMin > keyMax)
  1044. std::swap(keyMin, keyMax);
  1045. rgn.uKeyMin = std::max(rgn.uKeyMin, keyMin);
  1046. rgn.uKeyMax = std::min(rgn.uKeyMax, keyMax);
  1047. // There was no overlap between instrument region and preset region - skip it
  1048. if(rgn.uKeyMin > rgn.uKeyMax)
  1049. rgn.uKeyMin = rgn.uKeyMax = 0xFF;
  1050. }
  1051. break;
  1052. case SF2_GEN_UNITYNOTE:
  1053. if (value < 128) rgn.uUnityNote = static_cast<uint8>(value);
  1054. break;
  1055. case SF2_GEN_ATTACKVOLENV:
  1056. pDlsEnv->wVolAttack = SF2TimeToDLS(gen.genAmount);
  1057. break;
  1058. case SF2_GEN_DECAYVOLENV:
  1059. pDlsEnv->wVolDecay = SF2TimeToDLS(gen.genAmount);
  1060. break;
  1061. case SF2_GEN_SUSTAINVOLENV:
  1062. // 0.1% units
  1063. if(gen.genAmount >= 0)
  1064. {
  1065. pDlsEnv->nVolSustainLevel = SF2SustainLevelToLinear(gen.genAmount);
  1066. }
  1067. break;
  1068. case SF2_GEN_RELEASEVOLENV:
  1069. pDlsEnv->wVolRelease = SF2TimeToDLS(gen.genAmount);
  1070. break;
  1071. case SF2_GEN_PAN:
  1072. {
  1073. int32 pan = static_cast<int16>(value);
  1074. pan = std::clamp(Util::muldivr(pan + 500, 256, 1000), 0, 256);
  1075. rgn.panning = static_cast<int16>(pan);
  1076. pDlsEnv->nDefPan = mpt::saturate_cast<uint8>(pan);
  1077. }
  1078. break;
  1079. case SF2_GEN_ATTENUATION:
  1080. regionAttn = -static_cast<int16>(value);
  1081. break;
  1082. case SF2_GEN_SAMPLEID:
  1083. if (value < m_SamplesEx.size())
  1084. {
  1085. rgn.nWaveLink = value;
  1086. rgn.ulLoopStart = mpt::saturate_cast<uint32>(m_SamplesEx[value].dwStartloop + loopStart);
  1087. rgn.ulLoopEnd = mpt::saturate_cast<uint32>(m_SamplesEx[value].dwEndloop + loopEnd);
  1088. }
  1089. break;
  1090. case SF2_GEN_SAMPLEMODES:
  1091. value &= 3;
  1092. rgn.fuOptions &= uint16(~(DLSREGION_SAMPLELOOP|DLSREGION_PINGPONGLOOP|DLSREGION_SUSTAINLOOP));
  1093. if(value == 1)
  1094. rgn.fuOptions |= DLSREGION_SAMPLELOOP;
  1095. else if(value == 2)
  1096. rgn.fuOptions |= DLSREGION_SAMPLELOOP | DLSREGION_PINGPONGLOOP;
  1097. else if(value == 3)
  1098. rgn.fuOptions |= DLSREGION_SAMPLELOOP | DLSREGION_SUSTAINLOOP;
  1099. rgn.fuOptions |= DLSREGION_OVERRIDEWSMP;
  1100. break;
  1101. case SF2_GEN_KEYGROUP:
  1102. rgn.fuOptions |= (value & DLSREGION_KEYGROUPMASK);
  1103. break;
  1104. case SF2_GEN_COARSETUNE:
  1105. rgn.sFineTune += static_cast<int16>(value) * 128;
  1106. break;
  1107. case SF2_GEN_FINETUNE:
  1108. rgn.sFineTune += static_cast<int16>(Util::muldiv(static_cast<int8>(value), 128, 100));
  1109. break;
  1110. case SF2_GEN_SCALE_TUNING:
  1111. rgn.tuning = mpt::saturate_cast<uint8>(value);
  1112. break;
  1113. case SF2_GEN_START_LOOP_FINE:
  1114. loopStart += static_cast<int16>(value);
  1115. break;
  1116. case SF2_GEN_END_LOOP_FINE:
  1117. loopEnd += static_cast<int16>(value);
  1118. break;
  1119. case SF2_GEN_START_LOOP_COARSE:
  1120. loopStart += static_cast<int16>(value) * 32768;
  1121. break;
  1122. case SF2_GEN_END_LOOP_COARSE:
  1123. loopEnd += static_cast<int16>(value) * 32768;
  1124. break;
  1125. //default:
  1126. // Log(" gen=%d value=%04X\n", pgen->sfGenOper, pgen->genAmount);
  1127. }
  1128. }
  1129. int32 linearVol = DLS32BitRelativeGainToLinear(((instrAttenuation + regionAttn) * 65536) / 10) / 256;
  1130. Limit(linearVol, 16, 256);
  1131. rgn.usVolume = static_cast<uint16>(linearVol);
  1132. if(lastOp != SF2_GEN_SAMPLEID && nRgn == 0)
  1133. globalZone = rgn;
  1134. else if(!rgn.IsDummy())
  1135. dlsIns.Regions.push_back(rgn);
  1136. //Log("\n");
  1137. }
  1138. }
  1139. }
  1140. return true;
  1141. }
  1142. ///////////////////////////////////////////////////////////////
  1143. // Open: opens a DLS bank
  1144. bool CDLSBank::Open(const mpt::PathString &filename)
  1145. {
  1146. if(filename.empty()) return false;
  1147. m_szFileName = filename;
  1148. InputFile f(filename, SettingCacheCompleteFileBeforeLoading());
  1149. if(!f.IsValid()) return false;
  1150. return Open(GetFileReader(f));
  1151. }
  1152. bool CDLSBank::Open(FileReader file)
  1153. {
  1154. uint32 nInsDef;
  1155. if(file.GetOptionalFileName())
  1156. m_szFileName = file.GetOptionalFileName().value();
  1157. file.Rewind();
  1158. size_t dwMemLength = file.GetLength();
  1159. size_t dwMemPos = 0;
  1160. if(!file.CanRead(256))
  1161. {
  1162. return false;
  1163. }
  1164. RIFFChunkID riff;
  1165. file.ReadStruct(riff);
  1166. // Check DLS sections embedded in RMI midi files
  1167. if(riff.id_RIFF == IFFID_RIFF && riff.id_DLS == IFFID_RMID)
  1168. {
  1169. while(file.ReadStruct(riff))
  1170. {
  1171. if(riff.id_RIFF == IFFID_RIFF && riff.id_DLS == IFFID_DLS)
  1172. {
  1173. file.SkipBack(sizeof(riff));
  1174. break;
  1175. }
  1176. uint32 len = riff.riff_len;
  1177. if((len % 2u) != 0)
  1178. len++;
  1179. file.SkipBack(4);
  1180. file.Skip(len);
  1181. }
  1182. }
  1183. // Check XDLS sections embedded in big endian IFF files (Miles Sound System)
  1184. if (riff.id_RIFF == IFFID_FORM)
  1185. {
  1186. do
  1187. {
  1188. if(riff.id_DLS == IFFID_XDLS)
  1189. {
  1190. file.ReadStruct(riff);
  1191. break;
  1192. }
  1193. uint32 len = mpt::bit_cast<uint32be>(riff.riff_len);
  1194. if((len % 2u) != 0)
  1195. len++;
  1196. file.SkipBack(4);
  1197. file.Skip(len);
  1198. } while(file.ReadStruct(riff));
  1199. }
  1200. if (riff.id_RIFF != IFFID_RIFF
  1201. || (riff.id_DLS != IFFID_DLS && riff.id_DLS != IFFID_MLS && riff.id_DLS != IFFID_sfbk)
  1202. || !file.CanRead(riff.riff_len - 4))
  1203. {
  1204. #ifdef DLSBANK_LOG
  1205. MPT_LOG_GLOBAL(LogDebug, "DLSBANK", U_("Invalid DLS bank!"));
  1206. #endif
  1207. return false;
  1208. }
  1209. SF2LoaderInfo sf2info;
  1210. m_nType = (riff.id_DLS == IFFID_sfbk) ? SOUNDBANK_TYPE_SF2 : SOUNDBANK_TYPE_DLS;
  1211. m_dwWavePoolOffset = 0;
  1212. m_sf2version = 0;
  1213. m_Instruments.clear();
  1214. m_WaveForms.clear();
  1215. m_Envelopes.clear();
  1216. nInsDef = 0;
  1217. if (dwMemLength > 8 + riff.riff_len + dwMemPos) dwMemLength = 8 + riff.riff_len + dwMemPos;
  1218. bool applyPaddingToSampleChunk = true;
  1219. while(file.CanRead(sizeof(IFFCHUNK)))
  1220. {
  1221. IFFCHUNK chunkHeader;
  1222. file.ReadStruct(chunkHeader);
  1223. dwMemPos = file.GetPosition();
  1224. FileReader chunk = file.ReadChunk(chunkHeader.len);
  1225. bool applyPadding = (chunkHeader.len % 2u) != 0;
  1226. if(!chunk.LengthIsAtLeast(chunkHeader.len))
  1227. break;
  1228. switch(chunkHeader.id)
  1229. {
  1230. // DLS 1.0: Instruments Collection Header
  1231. case IFFID_colh:
  1232. #ifdef DLSBANK_LOG
  1233. MPT_LOG_GLOBAL(LogDebug, "DLSBANK", MPT_UFORMAT("colh ({} bytes)")(chunkHeader.len.get()));
  1234. #endif
  1235. if (m_Instruments.empty())
  1236. {
  1237. m_Instruments.resize(chunk.ReadUint32LE());
  1238. #ifdef DLSBANK_LOG
  1239. MPT_LOG_GLOBAL(LogDebug, "DLSBANK", MPT_UFORMAT(" {} instruments")(m_Instruments.size()));
  1240. #endif
  1241. }
  1242. break;
  1243. // DLS 1.0: Instruments Pointers Table
  1244. case IFFID_ptbl:
  1245. #ifdef DLSBANK_LOG
  1246. MPT_LOG_GLOBAL(LogDebug, "DLSBANK", MPT_UFORMAT("ptbl ({} bytes)")(chunkHeader.len.get()));
  1247. #endif
  1248. if (m_WaveForms.empty())
  1249. {
  1250. PTBLChunk ptbl;
  1251. chunk.ReadStruct(ptbl);
  1252. chunk.Skip(ptbl.cbSize - 8);
  1253. uint32 cues = std::min(ptbl.cCues.get(), mpt::saturate_cast<uint32>(chunk.BytesLeft() / sizeof(uint32)));
  1254. m_WaveForms.reserve(cues);
  1255. for(uint32 i = 0; i < cues; i++)
  1256. {
  1257. m_WaveForms.push_back(chunk.ReadUint32LE());
  1258. }
  1259. #ifdef DLSBANK_LOG
  1260. MPT_LOG_GLOBAL(LogDebug, "DLSBANK", MPT_UFORMAT(" {} waveforms")(m_WaveForms.size()));
  1261. #endif
  1262. }
  1263. break;
  1264. // DLS 1.0: LIST section
  1265. case IFFID_LIST:
  1266. #ifdef DLSBANK_LOG
  1267. MPT_LOG_GLOBAL(LogDebug, "DLSBANK", U_("LIST"));
  1268. #endif
  1269. {
  1270. uint32 listid = chunk.ReadUint32LE();
  1271. if (((listid == IFFID_wvpl) && (m_nType & SOUNDBANK_TYPE_DLS))
  1272. || ((listid == IFFID_sdta) && (m_nType & SOUNDBANK_TYPE_SF2)))
  1273. {
  1274. m_dwWavePoolOffset = dwMemPos + 4;
  1275. #ifdef DLSBANK_LOG
  1276. MPT_LOG_GLOBAL(LogDebug, "DLSBANK", MPT_UFORMAT("Wave Pool offset: {}")(m_dwWavePoolOffset));
  1277. #endif
  1278. if(!applyPaddingToSampleChunk)
  1279. applyPadding = false;
  1280. break;
  1281. }
  1282. while (chunk.CanRead(12))
  1283. {
  1284. IFFCHUNK listHeader;
  1285. chunk.ReadStruct(listHeader);
  1286. if(!chunk.CanRead(listHeader.len))
  1287. break;
  1288. FileReader subData = chunk.GetChunkAt(chunk.GetPosition() - sizeof(IFFCHUNK), listHeader.len + 8);
  1289. FileReader listChunk = chunk.ReadChunk(listHeader.len);
  1290. if(listHeader.len % 2u)
  1291. chunk.Skip(1);
  1292. // DLS Instrument Headers
  1293. if (listHeader.id == IFFID_LIST && (m_nType & SOUNDBANK_TYPE_DLS))
  1294. {
  1295. uint32 subID = listChunk.ReadUint32LE();
  1296. if ((subID == IFFID_ins) && (nInsDef < m_Instruments.size()))
  1297. {
  1298. DLSINSTRUMENT &dlsIns = m_Instruments[nInsDef];
  1299. //Log("Instrument %d:\n", nInsDef);
  1300. dlsIns.Regions.push_back({});
  1301. UpdateInstrumentDefinition(&dlsIns, subData);
  1302. nInsDef++;
  1303. }
  1304. } else
  1305. // DLS/SF2 Bank Information
  1306. if (listid == IFFID_INFO && listHeader.len)
  1307. {
  1308. switch(listHeader.id)
  1309. {
  1310. case IFFID_ifil:
  1311. m_sf2version = listChunk.ReadUint16LE() << 16;
  1312. m_sf2version |= listChunk.ReadUint16LE();
  1313. if(m_sf2version >= 0x3'0000 && m_sf2version <= 0x4'FFFF)
  1314. {
  1315. // "SF3" / "SF4" with compressed samples. The padding of the sample chunk is now optional (probably because it was simply forgotten to be added)
  1316. applyPaddingToSampleChunk = false;
  1317. }
  1318. listChunk.Skip(2);
  1319. break;
  1320. case IFFID_INAM:
  1321. listChunk.ReadString<mpt::String::maybeNullTerminated>(m_BankInfo.szBankName, listChunk.BytesLeft());
  1322. break;
  1323. case IFFID_IENG:
  1324. listChunk.ReadString<mpt::String::maybeNullTerminated>(m_BankInfo.szEngineer, listChunk.BytesLeft());
  1325. break;
  1326. case IFFID_ICOP:
  1327. listChunk.ReadString<mpt::String::maybeNullTerminated>(m_BankInfo.szCopyRight, listChunk.BytesLeft());
  1328. break;
  1329. case IFFID_ICMT:
  1330. listChunk.ReadString<mpt::String::maybeNullTerminated>(m_BankInfo.szComments, listChunk.BytesLeft());
  1331. break;
  1332. case IFFID_ISFT:
  1333. listChunk.ReadString<mpt::String::maybeNullTerminated>(m_BankInfo.szSoftware, listChunk.BytesLeft());
  1334. break;
  1335. case IFFID_ISBJ:
  1336. listChunk.ReadString<mpt::String::maybeNullTerminated>(m_BankInfo.szDescription, listChunk.BytesLeft());
  1337. break;
  1338. }
  1339. } else
  1340. if ((listid == IFFID_pdta) && (m_nType & SOUNDBANK_TYPE_SF2))
  1341. {
  1342. UpdateSF2PresetData(sf2info, listHeader, listChunk);
  1343. }
  1344. }
  1345. }
  1346. break;
  1347. #ifdef DLSBANK_LOG
  1348. default:
  1349. {
  1350. char sdbg[5];
  1351. memcpy(sdbg, &chunkHeader.id, 4);
  1352. sdbg[4] = 0;
  1353. MPT_LOG_GLOBAL(LogDebug, "DLSBANK", MPT_UFORMAT("Unsupported chunk: {} ({} bytes)")(mpt::ToUnicode(mpt::Charset::ASCII, mpt::String::ReadAutoBuf(sdbg)), chunkHeader.len.get()));
  1354. }
  1355. break;
  1356. #endif
  1357. }
  1358. if(applyPadding)
  1359. file.Skip(1);
  1360. }
  1361. // Build the ptbl is not present in file
  1362. if ((m_WaveForms.empty()) && (m_dwWavePoolOffset) && (m_nType & SOUNDBANK_TYPE_DLS) && (m_nMaxWaveLink > 0))
  1363. {
  1364. #ifdef DLSBANK_LOG
  1365. MPT_LOG_GLOBAL(LogDebug, "DLSBANK", MPT_UFORMAT("ptbl not present: building table ({} wavelinks)...")(m_nMaxWaveLink));
  1366. #endif
  1367. m_WaveForms.reserve(m_nMaxWaveLink);
  1368. file.Seek(m_dwWavePoolOffset);
  1369. while(m_WaveForms.size() < m_nMaxWaveLink && file.CanRead(sizeof(IFFCHUNK)))
  1370. {
  1371. IFFCHUNK chunk;
  1372. file.ReadStruct(chunk);
  1373. if (chunk.id == IFFID_LIST)
  1374. m_WaveForms.push_back(file.GetPosition() - m_dwWavePoolOffset - sizeof(IFFCHUNK));
  1375. file.Skip(chunk.len);
  1376. }
  1377. #ifdef DLSBANK_LOG
  1378. MPT_LOG_GLOBAL(LogDebug, "DLSBANK", MPT_UFORMAT("Found {} waveforms")(m_WaveForms.size()));
  1379. #endif
  1380. }
  1381. // Convert the SF2 data to DLS
  1382. if ((m_nType & SOUNDBANK_TYPE_SF2) && !m_SamplesEx.empty() && !m_Instruments.empty())
  1383. {
  1384. ConvertSF2ToDLS(sf2info);
  1385. }
  1386. #ifdef DLSBANK_LOG
  1387. MPT_LOG_GLOBAL(LogDebug, "DLSBANK", U_("DLS bank closed"));
  1388. #endif
  1389. return true;
  1390. }
  1391. ////////////////////////////////////////////////////////////////////////////////////////
  1392. // Extracts the Waveforms from a DLS/SF2 bank
  1393. uint32 CDLSBank::GetRegionFromKey(uint32 nIns, uint32 nKey) const
  1394. {
  1395. if(nIns >= m_Instruments.size())
  1396. return 0;
  1397. const DLSINSTRUMENT &dlsIns = m_Instruments[nIns];
  1398. for(uint32 rgn = 0; rgn < static_cast<uint32>(dlsIns.Regions.size()); rgn++)
  1399. {
  1400. const auto &region = dlsIns.Regions[rgn];
  1401. if(nKey < region.uKeyMin || nKey > region.uKeyMax)
  1402. continue;
  1403. if(region.nWaveLink == Util::MaxValueOfType(region.nWaveLink))
  1404. continue;
  1405. return rgn;
  1406. }
  1407. return 0;
  1408. }
  1409. bool CDLSBank::ExtractWaveForm(uint32 nIns, uint32 nRgn, std::vector<uint8> &waveData, uint32 &length) const
  1410. {
  1411. waveData.clear();
  1412. length = 0;
  1413. if (nIns >= m_Instruments.size() || !m_dwWavePoolOffset)
  1414. {
  1415. #ifdef DLSBANK_LOG
  1416. MPT_LOG_GLOBAL(LogDebug, "DLSBANK", MPT_UFORMAT("ExtractWaveForm({}) failed: m_Instruments.size()={} m_dwWavePoolOffset={} m_WaveForms.size()={}")(nIns, m_Instruments.size(), m_dwWavePoolOffset, m_WaveForms.size()));
  1417. #endif
  1418. return false;
  1419. }
  1420. const DLSINSTRUMENT &dlsIns = m_Instruments[nIns];
  1421. if(nRgn >= dlsIns.Regions.size())
  1422. {
  1423. #ifdef DLSBANK_LOG
  1424. MPT_LOG_GLOBAL(LogDebug, "DLSBANK", MPT_UFORMAT("invalid waveform region: nIns={} nRgn={} pSmp->nRegions={}")(nIns, nRgn, dlsIns.Regions.size()));
  1425. #endif
  1426. return false;
  1427. }
  1428. uint32 nWaveLink = dlsIns.Regions[nRgn].nWaveLink;
  1429. if(nWaveLink >= m_WaveForms.size())
  1430. {
  1431. #ifdef DLSBANK_LOG
  1432. MPT_LOG_GLOBAL(LogDebug, "DLSBANK", MPT_UFORMAT("Invalid wavelink id: nWaveLink={} nWaveForms={}")(nWaveLink, m_WaveForms.size()));
  1433. #endif
  1434. return false;
  1435. }
  1436. mpt::ifstream f(m_szFileName, std::ios::binary);
  1437. if(!f)
  1438. {
  1439. return false;
  1440. }
  1441. mpt::IO::Offset sampleOffset = mpt::saturate_cast<mpt::IO::Offset>(m_WaveForms[nWaveLink] + m_dwWavePoolOffset);
  1442. if(mpt::IO::SeekAbsolute(f, sampleOffset))
  1443. {
  1444. if (m_nType & SOUNDBANK_TYPE_SF2)
  1445. {
  1446. if (m_SamplesEx[nWaveLink].dwLen)
  1447. {
  1448. if (mpt::IO::SeekRelative(f, 8))
  1449. {
  1450. length = m_SamplesEx[nWaveLink].dwLen;
  1451. try
  1452. {
  1453. waveData.assign(length + 8, 0);
  1454. mpt::IO::ReadRaw(f, waveData.data(), length);
  1455. } catch(mpt::out_of_memory e)
  1456. {
  1457. mpt::delete_out_of_memory(e);
  1458. }
  1459. }
  1460. }
  1461. } else
  1462. {
  1463. LISTChunk chunk;
  1464. if(mpt::IO::Read(f, chunk))
  1465. {
  1466. if((chunk.id == IFFID_LIST) && (chunk.listid == IFFID_wave) && (chunk.len > 4))
  1467. {
  1468. length = chunk.len + 8;
  1469. try
  1470. {
  1471. waveData.assign(chunk.len + sizeof(IFFCHUNK), 0);
  1472. memcpy(waveData.data(), &chunk, sizeof(chunk));
  1473. mpt::IO::ReadRaw(f, &waveData[sizeof(chunk)], length - sizeof(chunk));
  1474. } catch(mpt::out_of_memory e)
  1475. {
  1476. mpt::delete_out_of_memory(e);
  1477. }
  1478. }
  1479. }
  1480. }
  1481. }
  1482. return !waveData.empty();
  1483. }
  1484. bool CDLSBank::ExtractSample(CSoundFile &sndFile, SAMPLEINDEX nSample, uint32 nIns, uint32 nRgn, int transpose) const
  1485. {
  1486. std::vector<uint8> pWaveForm;
  1487. uint32 dwLen = 0;
  1488. bool ok, hasWaveform;
  1489. if(nIns >= m_Instruments.size())
  1490. return false;
  1491. const DLSINSTRUMENT &dlsIns = m_Instruments[nIns];
  1492. if(nRgn >= dlsIns.Regions.size())
  1493. return false;
  1494. if(!ExtractWaveForm(nIns, nRgn, pWaveForm, dwLen))
  1495. return false;
  1496. if(dwLen < 16)
  1497. return false;
  1498. ok = false;
  1499. FileReader wsmpChunk;
  1500. if (m_nType & SOUNDBANK_TYPE_SF2)
  1501. {
  1502. sndFile.DestroySample(nSample);
  1503. uint32 nWaveLink = dlsIns.Regions[nRgn].nWaveLink;
  1504. ModSample &sample = sndFile.GetSample(nSample);
  1505. if (sndFile.m_nSamples < nSample) sndFile.m_nSamples = nSample;
  1506. if (nWaveLink < m_SamplesEx.size())
  1507. {
  1508. const DLSSAMPLEEX &p = m_SamplesEx[nWaveLink];
  1509. #ifdef DLSINSTR_LOG
  1510. MPT_LOG_GLOBAL(LogDebug, "DLSINSTR", MPT_UFORMAT(" SF2 WaveLink #{}: {}Hz")(nWaveLink, p.dwSampleRate));
  1511. #endif
  1512. sample.Initialize();
  1513. FileReader chunk{mpt::as_span(pWaveForm.data(), dwLen)};
  1514. if(!p.compressed || !sndFile.ReadSampleFromFile(nSample, chunk, false, false))
  1515. {
  1516. sample.nLength = dwLen / 2;
  1517. SampleIO(
  1518. SampleIO::_16bit,
  1519. SampleIO::mono,
  1520. SampleIO::littleEndian,
  1521. SampleIO::signedPCM)
  1522. .ReadSample(sample, chunk);
  1523. }
  1524. sample.nLoopStart = dlsIns.Regions[nRgn].ulLoopStart;
  1525. sample.nLoopEnd = dlsIns.Regions[nRgn].ulLoopEnd;
  1526. sample.nC5Speed = p.dwSampleRate;
  1527. sample.RelativeTone = p.byOriginalPitch;
  1528. sample.nFineTune = p.chPitchCorrection;
  1529. if(p.szName[0])
  1530. sndFile.m_szNames[nSample] = mpt::String::ReadAutoBuf(p.szName);
  1531. else if(dlsIns.szName[0])
  1532. sndFile.m_szNames[nSample] = mpt::String::ReadAutoBuf(dlsIns.szName);
  1533. }
  1534. hasWaveform = sample.HasSampleData();
  1535. } else
  1536. {
  1537. FileReader file(mpt::as_span(pWaveForm.data(), dwLen));
  1538. hasWaveform = sndFile.ReadWAVSample(nSample, file, false, &wsmpChunk);
  1539. if(dlsIns.szName[0])
  1540. sndFile.m_szNames[nSample] = mpt::String::ReadAutoBuf(dlsIns.szName);
  1541. }
  1542. if (hasWaveform)
  1543. {
  1544. ModSample &sample = sndFile.GetSample(nSample);
  1545. const DLSREGION &rgn = dlsIns.Regions[nRgn];
  1546. sample.uFlags.reset(CHN_LOOP | CHN_PINGPONGLOOP | CHN_SUSTAINLOOP | CHN_PINGPONGSUSTAIN);
  1547. if (rgn.fuOptions & DLSREGION_SAMPLELOOP) sample.uFlags.set(CHN_LOOP);
  1548. if (rgn.fuOptions & DLSREGION_SUSTAINLOOP) sample.uFlags.set(CHN_SUSTAINLOOP);
  1549. if (rgn.fuOptions & DLSREGION_PINGPONGLOOP) sample.uFlags.set(CHN_PINGPONGLOOP);
  1550. if (sample.uFlags[CHN_LOOP | CHN_SUSTAINLOOP])
  1551. {
  1552. if (rgn.ulLoopEnd > rgn.ulLoopStart)
  1553. {
  1554. if (sample.uFlags[CHN_SUSTAINLOOP])
  1555. {
  1556. sample.nSustainStart = rgn.ulLoopStart;
  1557. sample.nSustainEnd = rgn.ulLoopEnd;
  1558. } else
  1559. {
  1560. sample.nLoopStart = rgn.ulLoopStart;
  1561. sample.nLoopEnd = rgn.ulLoopEnd;
  1562. }
  1563. } else
  1564. {
  1565. sample.uFlags.reset(CHN_LOOP|CHN_SUSTAINLOOP);
  1566. }
  1567. }
  1568. // WSMP chunk
  1569. {
  1570. uint32 usUnityNote = rgn.uUnityNote;
  1571. int sFineTune = rgn.sFineTune;
  1572. int lVolume = rgn.usVolume;
  1573. WSMPChunk wsmp;
  1574. if(!(rgn.fuOptions & DLSREGION_OVERRIDEWSMP) && wsmpChunk.IsValid() && wsmpChunk.Skip(sizeof(IFFCHUNK)) && wsmpChunk.ReadStructPartial(wsmp))
  1575. {
  1576. usUnityNote = wsmp.usUnityNote;
  1577. sFineTune = wsmp.sFineTune;
  1578. lVolume = DLS32BitRelativeGainToLinear(wsmp.lAttenuation) / 256;
  1579. if(wsmp.cSampleLoops)
  1580. {
  1581. WSMPSampleLoop loop;
  1582. wsmpChunk.Seek(sizeof(IFFCHUNK) + wsmp.cbSize);
  1583. wsmpChunk.ReadStruct(loop);
  1584. if(loop.ulLoopLength > 3)
  1585. {
  1586. sample.uFlags.set(CHN_LOOP);
  1587. //if (loop.ulLoopType) sample.uFlags |= CHN_PINGPONGLOOP;
  1588. sample.nLoopStart = loop.ulLoopStart;
  1589. sample.nLoopEnd = loop.ulLoopStart + loop.ulLoopLength;
  1590. }
  1591. }
  1592. } else if (m_nType & SOUNDBANK_TYPE_SF2)
  1593. {
  1594. usUnityNote = (usUnityNote < 0x80) ? usUnityNote : sample.RelativeTone;
  1595. sFineTune += sample.nFineTune;
  1596. }
  1597. #ifdef DLSINSTR_LOG
  1598. MPT_LOG_GLOBAL(LogDebug, "DLSINSTR", MPT_UFORMAT("WSMP: usUnityNote={}.{}, {}Hz (transp={})")(usUnityNote, sFineTune, sample.nC5Speed, transpose));
  1599. #endif
  1600. if (usUnityNote > 0x7F) usUnityNote = 60;
  1601. int steps = (60 + transpose - usUnityNote) * 128 + sFineTune;
  1602. sample.Transpose(steps * (1.0 / (12.0 * 128.0)));
  1603. sample.RelativeTone = 0;
  1604. Limit(lVolume, 16, 256);
  1605. sample.nGlobalVol = (uint8)(lVolume / 4); // 0-64
  1606. }
  1607. sample.nPan = GetPanning(nIns, nRgn);
  1608. sample.Convert(MOD_TYPE_IT, sndFile.GetType());
  1609. sample.PrecomputeLoops(sndFile, false);
  1610. ok = true;
  1611. }
  1612. return ok;
  1613. }
  1614. static uint16 ScaleEnvelope(uint32 time, float tempoScale)
  1615. {
  1616. return std::max(mpt::saturate_round<uint16>(time * tempoScale), uint16(1));
  1617. }
  1618. bool CDLSBank::ExtractInstrument(CSoundFile &sndFile, INSTRUMENTINDEX nInstr, uint32 nIns, uint32 nDrumRgn) const
  1619. {
  1620. uint32 minRegion, maxRegion, nEnv;
  1621. if (nIns >= m_Instruments.size())
  1622. return false;
  1623. const DLSINSTRUMENT &dlsIns = m_Instruments[nIns];
  1624. const bool isDrum = (dlsIns.ulBank & F_INSTRUMENT_DRUMS) && nDrumRgn != uint32_max;
  1625. if(isDrum)
  1626. {
  1627. if(nDrumRgn >= dlsIns.Regions.size())
  1628. return false;
  1629. minRegion = nDrumRgn;
  1630. maxRegion = nDrumRgn + 1;
  1631. nEnv = dlsIns.Regions[nDrumRgn].uPercEnv;
  1632. } else
  1633. {
  1634. if(dlsIns.Regions.empty())
  1635. return false;
  1636. minRegion = 0;
  1637. maxRegion = static_cast<uint32>(dlsIns.Regions.size());
  1638. nEnv = dlsIns.nMelodicEnv;
  1639. }
  1640. #ifdef DLSINSTR_LOG
  1641. MPT_LOG_GLOBAL(LogDebug, "DLSINSTR", MPT_UFORMAT("DLS Instrument #{}: {}")(nIns, mpt::ToUnicode(mpt::Charset::ASCII, mpt::String::ReadAutoBuf(dlsIns.szName))));
  1642. MPT_LOG_GLOBAL(LogDebug, "DLSINSTR", MPT_UFORMAT(" Bank=0x{} Instrument=0x{}")(mpt::ufmt::HEX0<4>(dlsIns.ulBank), mpt::ufmt::HEX0<4>(dlsIns.ulInstrument)));
  1643. MPT_LOG_GLOBAL(LogDebug, "DLSINSTR", MPT_UFORMAT(" {} regions, nMelodicEnv={}")(dlsIns.Regions.size(), dlsIns.nMelodicEnv));
  1644. for (uint32 iDbg=0; iDbg<dlsIns.Regions.size(); iDbg++)
  1645. {
  1646. const DLSREGION *prgn = &dlsIns.Regions[iDbg];
  1647. MPT_LOG_GLOBAL(LogDebug, "DLSINSTR", MPT_UFORMAT(" Region {}:")(iDbg));
  1648. MPT_LOG_GLOBAL(LogDebug, "DLSINSTR", MPT_UFORMAT(" WaveLink = {} (loop [{}, {}])")(prgn->nWaveLink, prgn->ulLoopStart, prgn->ulLoopEnd));
  1649. MPT_LOG_GLOBAL(LogDebug, "DLSINSTR", MPT_UFORMAT(" Key Range: [{}, {}]")(prgn->uKeyMin, prgn->uKeyMax));
  1650. MPT_LOG_GLOBAL(LogDebug, "DLSINSTR", MPT_UFORMAT(" fuOptions = 0x{}")(mpt::ufmt::HEX0<4>(prgn->fuOptions)));
  1651. MPT_LOG_GLOBAL(LogDebug, "DLSINSTR", MPT_UFORMAT(" usVolume = {}, Unity Note = {}")(prgn->usVolume, prgn->uUnityNote));
  1652. }
  1653. #endif
  1654. ModInstrument *pIns = new (std::nothrow) ModInstrument();
  1655. if(pIns == nullptr)
  1656. {
  1657. return false;
  1658. }
  1659. if(sndFile.Instruments[nInstr])
  1660. {
  1661. sndFile.DestroyInstrument(nInstr, deleteAssociatedSamples);
  1662. }
  1663. // Initializes Instrument
  1664. if(isDrum)
  1665. {
  1666. uint32 key = dlsIns.Regions[nDrumRgn].uKeyMin;
  1667. if((key >= 24) && (key <= 84))
  1668. {
  1669. std::string s = szMidiPercussionNames[key-24];
  1670. if(!mpt::String::ReadAutoBuf(dlsIns.szName).empty())
  1671. {
  1672. s += MPT_AFORMAT(" ({})")(mpt::trim_right<std::string>(mpt::String::ReadAutoBuf(dlsIns.szName)));
  1673. }
  1674. pIns->name = s;
  1675. } else
  1676. {
  1677. pIns->name = mpt::String::ReadAutoBuf(dlsIns.szName);
  1678. }
  1679. } else
  1680. {
  1681. pIns->name = mpt::String::ReadAutoBuf(dlsIns.szName);
  1682. }
  1683. int transpose = 0;
  1684. if(isDrum)
  1685. {
  1686. for(uint32 iNoteMap = 0; iNoteMap < NOTE_MAX; iNoteMap++)
  1687. {
  1688. if(sndFile.GetType() & (MOD_TYPE_IT | MOD_TYPE_MID | MOD_TYPE_MPT))
  1689. {
  1690. // Format has instrument note mapping
  1691. if(dlsIns.Regions[nDrumRgn].tuning == 0)
  1692. pIns->NoteMap[iNoteMap] = NOTE_MIDDLEC;
  1693. else if (iNoteMap < dlsIns.Regions[nDrumRgn].uKeyMin)
  1694. pIns->NoteMap[iNoteMap] = (uint8)(dlsIns.Regions[nDrumRgn].uKeyMin + NOTE_MIN);
  1695. else if(iNoteMap > dlsIns.Regions[nDrumRgn].uKeyMax)
  1696. pIns->NoteMap[iNoteMap] = (uint8)(dlsIns.Regions[nDrumRgn].uKeyMax + NOTE_MIN);
  1697. } else
  1698. {
  1699. if(iNoteMap == dlsIns.Regions[nDrumRgn].uKeyMin)
  1700. {
  1701. transpose = (dlsIns.Regions[nDrumRgn].uKeyMin + (dlsIns.Regions[nDrumRgn].uKeyMax - dlsIns.Regions[nDrumRgn].uKeyMin) / 2) - 60;
  1702. }
  1703. }
  1704. }
  1705. }
  1706. pIns->nFadeOut = 1024;
  1707. pIns->nMidiProgram = (uint8)(dlsIns.ulInstrument & 0x7F) + 1;
  1708. pIns->nMidiChannel = (uint8)(isDrum ? 10 : 0);
  1709. pIns->wMidiBank = (uint16)(((dlsIns.ulBank & 0x7F00) >> 1) | (dlsIns.ulBank & 0x7F));
  1710. pIns->nNNA = NewNoteAction::NoteOff;
  1711. pIns->nDCT = DuplicateCheckType::Note;
  1712. pIns->nDNA = DuplicateNoteAction::NoteFade;
  1713. sndFile.Instruments[nInstr] = pIns;
  1714. uint32 nLoadedSmp = 0;
  1715. SAMPLEINDEX nextSample = 0;
  1716. // Extract Samples
  1717. std::vector<SAMPLEINDEX> RgnToSmp(dlsIns.Regions.size());
  1718. std::set<uint16> extractedSamples;
  1719. for(uint32 nRgn = minRegion; nRgn < maxRegion; nRgn++)
  1720. {
  1721. bool duplicateRegion = false;
  1722. SAMPLEINDEX nSmp = 0;
  1723. const DLSREGION &rgn = dlsIns.Regions[nRgn];
  1724. if(rgn.IsDummy())
  1725. continue;
  1726. // Elimitate Duplicate Regions
  1727. uint32 dupRegion;
  1728. for(dupRegion = minRegion; dupRegion < nRgn; dupRegion++)
  1729. {
  1730. const DLSREGION &rgn2 = dlsIns.Regions[dupRegion];
  1731. if(RgnToSmp[dupRegion] == 0 || rgn2.IsDummy())
  1732. continue;
  1733. // No need to extract the same sample data twice
  1734. const bool sameSample = (rgn2.nWaveLink == rgn.nWaveLink) && (rgn2.ulLoopEnd == rgn.ulLoopEnd) && (rgn2.ulLoopStart == rgn.ulLoopStart) && extractedSamples.count(rgn.nWaveLink);
  1735. // Candidate for stereo sample creation
  1736. const bool sameKeyRange = (rgn2.uKeyMin == rgn.uKeyMin) && (rgn2.uKeyMax == rgn.uKeyMax);
  1737. if(sameSample || sameKeyRange)
  1738. {
  1739. duplicateRegion = true;
  1740. if(!sameKeyRange)
  1741. nSmp = RgnToSmp[dupRegion];
  1742. break;
  1743. }
  1744. }
  1745. // Create a new sample
  1746. if (!duplicateRegion)
  1747. {
  1748. uint32 nmaxsmp = (m_nType & MOD_TYPE_XM) ? 16 : (NOTE_MAX - NOTE_MIN + 1);
  1749. if (nLoadedSmp >= nmaxsmp)
  1750. {
  1751. nSmp = RgnToSmp[nRgn - 1];
  1752. } else
  1753. {
  1754. nextSample = sndFile.GetNextFreeSample(nInstr, nextSample + 1);
  1755. if (nextSample == SAMPLEINDEX_INVALID) break;
  1756. if (nextSample > sndFile.GetNumSamples()) sndFile.m_nSamples = nextSample;
  1757. nSmp = nextSample;
  1758. nLoadedSmp++;
  1759. }
  1760. }
  1761. RgnToSmp[nRgn] = nSmp;
  1762. // Map all notes to the right sample
  1763. if(nSmp)
  1764. {
  1765. for(uint8 key = 0; key < NOTE_MAX; key++)
  1766. {
  1767. if(isDrum || (key >= rgn.uKeyMin && key <= rgn.uKeyMax))
  1768. {
  1769. pIns->Keyboard[key] = nSmp;
  1770. }
  1771. }
  1772. // Load the sample
  1773. if(!duplicateRegion || !sndFile.GetSample(nSmp).HasSampleData())
  1774. {
  1775. ExtractSample(sndFile, nSmp, nIns, nRgn, transpose);
  1776. extractedSamples.insert(rgn.nWaveLink);
  1777. }
  1778. } else if(duplicateRegion && sndFile.GetSample(RgnToSmp[dupRegion]).GetNumChannels() == 1)
  1779. {
  1780. // Try to combine stereo samples
  1781. const uint16 pan1 = GetPanning(nIns, nRgn), pan2 = GetPanning(nIns, dupRegion);
  1782. if((pan1 < 16 && pan2 >= 240) || (pan2 < 16 && pan1 >= 240))
  1783. {
  1784. ModSample &sample = sndFile.GetSample(RgnToSmp[dupRegion]);
  1785. ModSample sampleCopy = sample;
  1786. sampleCopy.pData.pSample = nullptr;
  1787. sampleCopy.uFlags.set(CHN_16BIT | CHN_STEREO);
  1788. if(!sampleCopy.AllocateSample())
  1789. continue;
  1790. const uint8 offsetOrig = (pan1 < pan2) ? 1 : 0;
  1791. const uint8 offsetNew = (pan1 < pan2) ? 0 : 1;
  1792. std::vector<uint8> pWaveForm;
  1793. uint32 dwLen = 0;
  1794. if(!ExtractWaveForm(nIns, nRgn, pWaveForm, dwLen))
  1795. continue;
  1796. extractedSamples.insert(rgn.nWaveLink);
  1797. // First copy over original channel
  1798. auto pDest = sampleCopy.sample16() + offsetOrig;
  1799. if(sample.uFlags[CHN_16BIT])
  1800. CopySample<SC::ConversionChain<SC::Convert<int16, int16>, SC::DecodeIdentity<int16>>>(pDest, sample.nLength, 2, sample.sample16(), sample.GetSampleSizeInBytes(), 1);
  1801. else
  1802. CopySample<SC::ConversionChain<SC::Convert<int16, int8>, SC::DecodeIdentity<int8>>>(pDest, sample.nLength, 2, sample.sample8(), sample.GetSampleSizeInBytes(), 1);
  1803. sample.FreeSample();
  1804. // Now read the other channel
  1805. if(m_SamplesEx[m_Instruments[nIns].Regions[nRgn].nWaveLink].compressed)
  1806. {
  1807. FileReader file{mpt::as_span(pWaveForm)};
  1808. if(sndFile.ReadSampleFromFile(nSmp, file, false, false))
  1809. {
  1810. pDest = sampleCopy.sample16() + offsetNew;
  1811. const SmpLength copyLength = std::min(sample.nLength, sampleCopy.nLength);
  1812. if(sample.uFlags[CHN_16BIT])
  1813. CopySample<SC::ConversionChain<SC::Convert<int16, int16>, SC::DecodeIdentity<int16>>>(pDest, copyLength, 2, sample.sample16(), sample.GetSampleSizeInBytes(), sample.GetNumChannels());
  1814. else
  1815. CopySample<SC::ConversionChain<SC::Convert<int16, int8>, SC::DecodeIdentity<int8>>>(pDest, copyLength, 2, sample.sample8(), sample.GetSampleSizeInBytes(), sample.GetNumChannels());
  1816. }
  1817. } else
  1818. {
  1819. SmpLength len = std::min(dwLen / 2u, sampleCopy.nLength);
  1820. const int16 *src = reinterpret_cast<int16 *>(pWaveForm.data());
  1821. int16 *dst = sampleCopy.sample16() + offsetNew;
  1822. CopySample<SC::ConversionChain<SC::Convert<int16, int16>, SC::DecodeIdentity<int16>>>(dst, len, 2, src, pWaveForm.size(), 1);
  1823. }
  1824. sample.FreeSample();
  1825. sample = sampleCopy;
  1826. }
  1827. }
  1828. }
  1829. float tempoScale = 1.0f;
  1830. if(sndFile.m_nTempoMode == TempoMode::Modern)
  1831. {
  1832. uint32 ticksPerBeat = sndFile.m_nDefaultRowsPerBeat * sndFile.m_nDefaultSpeed;
  1833. if(ticksPerBeat != 0)
  1834. tempoScale = ticksPerBeat / 24.0f;
  1835. }
  1836. // Initializes Envelope
  1837. if ((nEnv) && (nEnv <= m_Envelopes.size()))
  1838. {
  1839. const DLSENVELOPE &part = m_Envelopes[nEnv - 1];
  1840. // Volume Envelope
  1841. if ((part.wVolAttack) || (part.wVolDecay < 20*50) || (part.nVolSustainLevel) || (part.wVolRelease < 20*50))
  1842. {
  1843. pIns->VolEnv.dwFlags.set(ENV_ENABLED);
  1844. // Delay section
  1845. // -> DLS level 2
  1846. // Attack section
  1847. pIns->VolEnv.clear();
  1848. if (part.wVolAttack)
  1849. {
  1850. pIns->VolEnv.push_back(0, (uint8)(ENVELOPE_MAX / (part.wVolAttack / 2 + 2) + 8)); // /-----
  1851. pIns->VolEnv.push_back(ScaleEnvelope(part.wVolAttack, tempoScale), ENVELOPE_MAX); // |
  1852. } else
  1853. {
  1854. pIns->VolEnv.push_back(0, ENVELOPE_MAX);
  1855. }
  1856. // Hold section
  1857. // -> DLS Level 2
  1858. // Sustain Level
  1859. if (part.nVolSustainLevel > 0)
  1860. {
  1861. if (part.nVolSustainLevel < 128)
  1862. {
  1863. uint16 lStartTime = pIns->VolEnv.back().tick;
  1864. int32 lSusLevel = - DLS32BitRelativeLinearToGain(part.nVolSustainLevel << 9) / 65536;
  1865. int32 lDecayTime = 1;
  1866. if (lSusLevel > 0)
  1867. {
  1868. lDecayTime = (lSusLevel * (int32)part.wVolDecay) / 960;
  1869. for (uint32 i=0; i<7; i++)
  1870. {
  1871. int32 lFactor = 128 - (1 << i);
  1872. if (lFactor <= part.nVolSustainLevel) break;
  1873. int32 lev = - DLS32BitRelativeLinearToGain(lFactor << 9) / 65536;
  1874. if (lev > 0)
  1875. {
  1876. int32 ltime = (lev * (int32)part.wVolDecay) / 960;
  1877. if ((ltime > 1) && (ltime < lDecayTime))
  1878. {
  1879. uint16 tick = lStartTime + ScaleEnvelope(ltime, tempoScale);
  1880. if(tick > pIns->VolEnv.back().tick)
  1881. {
  1882. pIns->VolEnv.push_back(tick, (uint8)(lFactor / 2));
  1883. }
  1884. }
  1885. }
  1886. }
  1887. }
  1888. uint16 decayEnd = lStartTime + ScaleEnvelope(lDecayTime, tempoScale);
  1889. if (decayEnd > pIns->VolEnv.back().tick)
  1890. {
  1891. pIns->VolEnv.push_back(decayEnd, (uint8)((part.nVolSustainLevel+1) / 2));
  1892. }
  1893. }
  1894. pIns->VolEnv.dwFlags.set(ENV_SUSTAIN);
  1895. } else
  1896. {
  1897. pIns->VolEnv.dwFlags.set(ENV_SUSTAIN);
  1898. pIns->VolEnv.push_back(pIns->VolEnv.back().tick + 1u, pIns->VolEnv.back().value);
  1899. }
  1900. pIns->VolEnv.nSustainStart = pIns->VolEnv.nSustainEnd = (uint8)(pIns->VolEnv.size() - 1);
  1901. // Release section
  1902. if ((part.wVolRelease) && (pIns->VolEnv.back().value > 1))
  1903. {
  1904. int32 lReleaseTime = part.wVolRelease;
  1905. uint16 lStartTime = pIns->VolEnv.back().tick;
  1906. int32 lStartFactor = pIns->VolEnv.back().value;
  1907. int32 lSusLevel = - DLS32BitRelativeLinearToGain(lStartFactor << 10) / 65536;
  1908. int32 lDecayEndTime = (lReleaseTime * lSusLevel) / 960;
  1909. lReleaseTime -= lDecayEndTime;
  1910. if(pIns->VolEnv.nSustainEnd > 0)
  1911. pIns->VolEnv.nReleaseNode = pIns->VolEnv.nSustainEnd;
  1912. for (uint32 i=0; i<5; i++)
  1913. {
  1914. int32 lFactor = 1 + ((lStartFactor * 3) >> (i+2));
  1915. if ((lFactor <= 1) || (lFactor >= lStartFactor)) continue;
  1916. int32 lev = - DLS32BitRelativeLinearToGain(lFactor << 10) / 65536;
  1917. if (lev > 0)
  1918. {
  1919. int32 ltime = (((int32)part.wVolRelease * lev) / 960) - lDecayEndTime;
  1920. if ((ltime > 1) && (ltime < lReleaseTime))
  1921. {
  1922. uint16 tick = lStartTime + ScaleEnvelope(ltime, tempoScale);
  1923. if(tick > pIns->VolEnv.back().tick)
  1924. {
  1925. pIns->VolEnv.push_back(tick, (uint8)lFactor);
  1926. }
  1927. }
  1928. }
  1929. }
  1930. if (lReleaseTime < 1) lReleaseTime = 1;
  1931. auto releaseTicks = ScaleEnvelope(lReleaseTime, tempoScale);
  1932. pIns->VolEnv.push_back(lStartTime + releaseTicks, ENVELOPE_MIN);
  1933. if(releaseTicks > 0)
  1934. {
  1935. pIns->nFadeOut = 32768 / releaseTicks;
  1936. }
  1937. } else
  1938. {
  1939. pIns->VolEnv.push_back(pIns->VolEnv.back().tick + 1u, ENVELOPE_MIN);
  1940. }
  1941. }
  1942. }
  1943. if(isDrum)
  1944. {
  1945. // Create a default envelope for drums
  1946. pIns->VolEnv.dwFlags.reset(ENV_SUSTAIN);
  1947. if(!pIns->VolEnv.dwFlags[ENV_ENABLED])
  1948. {
  1949. pIns->VolEnv.dwFlags.set(ENV_ENABLED);
  1950. pIns->VolEnv.resize(4);
  1951. pIns->VolEnv[0] = EnvelopeNode(0, ENVELOPE_MAX);
  1952. pIns->VolEnv[1] = EnvelopeNode(ScaleEnvelope(5, tempoScale), ENVELOPE_MAX);
  1953. pIns->VolEnv[2] = EnvelopeNode(pIns->VolEnv[1].tick * 2u, ENVELOPE_MID);
  1954. pIns->VolEnv[3] = EnvelopeNode(pIns->VolEnv[2].tick * 2u, ENVELOPE_MIN); // 1 second max. for drums
  1955. }
  1956. }
  1957. pIns->Sanitize(MOD_TYPE_MPT);
  1958. pIns->Convert(MOD_TYPE_MPT, sndFile.GetType());
  1959. return true;
  1960. }
  1961. const char *CDLSBank::GetRegionName(uint32 nIns, uint32 nRgn) const
  1962. {
  1963. if(nIns >= m_Instruments.size())
  1964. return nullptr;
  1965. const DLSINSTRUMENT &dlsIns = m_Instruments[nIns];
  1966. if(nRgn >= dlsIns.Regions.size())
  1967. return nullptr;
  1968. if (m_nType & SOUNDBANK_TYPE_SF2)
  1969. {
  1970. uint32 nWaveLink = dlsIns.Regions[nRgn].nWaveLink;
  1971. if (nWaveLink < m_SamplesEx.size())
  1972. {
  1973. return m_SamplesEx[nWaveLink].szName;
  1974. }
  1975. }
  1976. return nullptr;
  1977. }
  1978. uint16 CDLSBank::GetPanning(uint32 ins, uint32 region) const
  1979. {
  1980. const DLSINSTRUMENT &dlsIns = m_Instruments[ins];
  1981. if(region >= std::size(dlsIns.Regions))
  1982. return 128;
  1983. const DLSREGION &rgn = dlsIns.Regions[region];
  1984. if(rgn.panning >= 0)
  1985. return static_cast<uint16>(rgn.panning);
  1986. if(dlsIns.ulBank & F_INSTRUMENT_DRUMS)
  1987. {
  1988. if(rgn.uPercEnv > 0 && rgn.uPercEnv <= m_Envelopes.size())
  1989. {
  1990. return m_Envelopes[rgn.uPercEnv - 1].nDefPan;
  1991. }
  1992. } else
  1993. {
  1994. if(dlsIns.nMelodicEnv > 0 && dlsIns.nMelodicEnv <= m_Envelopes.size())
  1995. {
  1996. return m_Envelopes[dlsIns.nMelodicEnv - 1].nDefPan;
  1997. }
  1998. }
  1999. return 128;
  2000. }
  2001. #else // !MODPLUG_TRACKER
  2002. MPT_MSVC_WORKAROUND_LNK4221(Dlsbank)
  2003. #endif // MODPLUG_TRACKER
  2004. OPENMPT_NAMESPACE_END