File size: 8,929 Bytes
b0bfea8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { findRIFFListType, readRIFFChunk } from "../basic_soundfont/riff_chunk.js";
import { readBytesAsString } from "../../utils/byte_functions/string.js";
import {
    SpessaSynthGroupCollapsed,
    SpessaSynthGroupEnd,
    SpessaSynthInfo,
    SpessaSynthWarn
} from "../../utils/loggin.js";
import { consoleColors } from "../../utils/other.js";
import { readLittleEndian, signedInt16 } from "../../utils/byte_functions/little_endian.js";
import { DLSSample } from "./dls_sample.js";

const W_FORMAT_TAG = {
    PCM: 0x01,
    ALAW: 0x6
};

/**
 * @param dataChunk {RiffChunk}
 * @param bytesPerSample {number}
 * @returns {Float32Array}
 */
function readPCM(dataChunk, bytesPerSample)
{
    const maxSampleValue = Math.pow(2, bytesPerSample * 8 - 1); // Max value for the sample
    const maxUnsigned = Math.pow(2, bytesPerSample * 8);
    
    let normalizationFactor;
    let isUnsigned = false;
    
    if (bytesPerSample === 1)
    {
        normalizationFactor = 255; // For 8-bit normalize from 0-255
        isUnsigned = true;
    }
    else
    {
        normalizationFactor = maxSampleValue; // For 16-bit normalize from -32,768 to 32,767
    }
    const sampleLength = dataChunk.size / bytesPerSample;
    const sampleData = new Float32Array(sampleLength);
    for (let i = 0; i < sampleData.length; i++)
    {
        // read
        let sample = readLittleEndian(dataChunk.chunkData, bytesPerSample);
        // turn into signed
        if (isUnsigned)
        {
            // normalize unsigned 8-bit sample
            sampleData[i] = (sample / normalizationFactor) - 0.5;
        }
        else
        {
            // normalize signed 16-bit sample
            if (sample >= maxSampleValue)
            {
                sample -= maxUnsigned;
            }
            sampleData[i] = sample / normalizationFactor;
        }
    }
    return sampleData;
}

/**
 * @param dataChunk {RiffChunk}
 * @param bytesPerSample {number}
 * @returns {Float32Array}
 */
function readALAW(dataChunk, bytesPerSample)
{
    const sampleLength = dataChunk.size / bytesPerSample;
    const sampleData = new Float32Array(sampleLength);
    for (let i = 0; i < sampleData.length; i++)
    {
        // read
        const input = readLittleEndian(dataChunk.chunkData, bytesPerSample);
        
        // https://en.wikipedia.org/wiki/G.711#A-law
        // re-toggle toggled bits
        let sample = input ^ 0x55;
        
        // remove sign bit
        sample &= 0x7F;
        
        // extract exponent
        let exponent = sample >> 4;
        // extract mantissa
        let mantissa = sample & 0xF;
        if (exponent > 0)
        {
            mantissa += 16; // add leading '1', if exponent > 0
        }
        
        mantissa = (mantissa << 4) + 0x8;
        if (exponent > 1)
        {
            mantissa = mantissa << (exponent - 1);
        }
        
        const s16sample = input > 127 ? mantissa : -mantissa;
        
        // convert to float
        sampleData[i] = s16sample / 32678;
    }
    return sampleData;
}

/**
 * @this {DLSSoundFont}
 * @param waveListChunk {RiffChunk}
 */
export function readDLSSamples(waveListChunk)
{
    SpessaSynthGroupCollapsed(
        "%cLoading Wave samples...",
        consoleColors.recognized
    );
    let sampleID = 0;
    while (waveListChunk.chunkData.currentIndex < waveListChunk.chunkData.length)
    {
        const waveChunk = readRIFFChunk(waveListChunk.chunkData);
        this.verifyHeader(waveChunk, "LIST");
        this.verifyText(readBytesAsString(waveChunk.chunkData, 4), "wave");
        
        /**
         * @type {RiffChunk[]}
         */
        const waveChunks = [];
        while (waveChunk.chunkData.currentIndex < waveChunk.chunkData.length)
        {
            waveChunks.push(readRIFFChunk(waveChunk.chunkData));
        }
        
        const fmtChunk = waveChunks.find(c => c.header === "fmt ");
        if (!fmtChunk)
        {
            throw new Error("No fmt chunk in the wave file!");
        }
        // https://github.com/tpn/winsdk-10/blob/9b69fd26ac0c7d0b83d378dba01080e93349c2ed/Include/10.0.14393.0/shared/mmreg.h#L2108
        const waveFormat = readLittleEndian(fmtChunk.chunkData, 2);
        const channelsAmount = readLittleEndian(fmtChunk.chunkData, 2);
        if (channelsAmount !== 1)
        {
            throw new Error(`Only mono samples are supported. Fmt reports ${channelsAmount} channels`);
        }
        const sampleRate = readLittleEndian(fmtChunk.chunkData, 4);
        // skip avg bytes
        readLittleEndian(fmtChunk.chunkData, 4);
        // blockAlign
        readLittleEndian(fmtChunk.chunkData, 2);
        // it's bits per sample because one channel
        const wBitsPerSample = readLittleEndian(fmtChunk.chunkData, 2);
        const bytesPerSample = wBitsPerSample / 8;
        
        // read the data
        let failed = false;
        const dataChunk = waveChunks.find(c => c.header === "data");
        if (!dataChunk)
        {
            this.parsingError("No data chunk in the WAVE chunk!");
        }
        let sampleData;
        switch (waveFormat)
        {
            default:
                failed = true;
                sampleData = new Float32Array(dataChunk.size / bytesPerSample);
                break;
            
            case W_FORMAT_TAG.PCM:
                sampleData = readPCM(dataChunk, bytesPerSample);
                break;
            
            case W_FORMAT_TAG.ALAW:
                sampleData = readALAW(dataChunk, bytesPerSample);
                break;
            
        }
        
        // read sample name
        const waveInfo = findRIFFListType(waveChunks, "INFO");
        let sampleName = `Unnamed ${sampleID}`;
        if (waveInfo)
        {
            let infoChunk = readRIFFChunk(waveInfo.chunkData);
            while (infoChunk.header !== "INAM" && waveInfo.chunkData.currentIndex < waveInfo.chunkData.length)
            {
                infoChunk = readRIFFChunk(waveInfo.chunkData);
            }
            if (infoChunk.header === "INAM")
            {
                sampleName = readBytesAsString(infoChunk.chunkData, infoChunk.size).trim();
            }
        }
        
        // correct defaults
        let sampleKey = 60;
        let samplePitch = 0;
        let sampleLoopStart = 0;
        let sampleLoopEnd = sampleData.length - 1;
        let sampleDbAttenuation = 0;
        
        // read wsmp
        const wsmpChunk = waveChunks.find(c => c.header === "wsmp");
        if (wsmpChunk)
        {
            // skip cbsize
            readLittleEndian(wsmpChunk.chunkData, 4);
            sampleKey = readLittleEndian(wsmpChunk.chunkData, 2);
            // section 1.14.2: Each relative pitch unit represents 1/65536 cents.
            // but that doesn't seem true for this one: it's just cents.
            samplePitch = signedInt16(
                wsmpChunk.chunkData[wsmpChunk.chunkData.currentIndex++],
                wsmpChunk.chunkData[wsmpChunk.chunkData.currentIndex++]
            );
            
            // pitch correction: convert hundreds to the root key
            const samplePitchSemitones = Math.trunc(samplePitch / 100);
            sampleKey += samplePitchSemitones;
            samplePitch -= samplePitchSemitones * 100;
            
            
            // gain is applied it manually here (literally multiplying the samples)
            const gainCorrection = readLittleEndian(wsmpChunk.chunkData, 4);
            // convert to signed and turn into decibels
            sampleDbAttenuation = (gainCorrection | 0) / -655360;
            // no idea about ful options
            readLittleEndian(wsmpChunk.chunkData, 4);
            const loopsAmount = readLittleEndian(wsmpChunk.chunkData, 4);
            if (loopsAmount === 1)
            {
                // skip size and type
                readLittleEndian(wsmpChunk.chunkData, 8);
                sampleLoopStart = readLittleEndian(wsmpChunk.chunkData, 4);
                const loopSize = readLittleEndian(wsmpChunk.chunkData, 4);
                sampleLoopEnd = sampleLoopStart + loopSize;
            }
        }
        else
        {
            SpessaSynthWarn("No wsmp chunk in wave... using sane defaults.");
        }
        
        if (failed)
        {
            console.error(`Failed to load '${sampleName}': Unsupported format: (${waveFormat})`);
        }
        
        this.samples.push(new DLSSample(
            sampleName,
            sampleRate,
            sampleKey,
            samplePitch,
            sampleLoopStart,
            sampleLoopEnd,
            sampleData,
            sampleDbAttenuation
        ));
        
        
        sampleID++;
        SpessaSynthInfo(
            `%cLoaded sample %c${sampleName}`,
            consoleColors.info,
            consoleColors.recognized
        );
    }
    SpessaSynthGroupEnd();
}