File size: 2,594 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
import { combineArrays, IndexedByteArray } from "../../../utils/indexed_array.js";
import { writeDword, writeWord } from "../../../utils/byte_functions/little_endian.js";
import { writeRIFFOddSize } from "../riff_chunk.js";
import { writeWavesample } from "./wsmp.js";
import { getStringBytesZero } from "../../../utils/byte_functions/string.js";
import { SpessaSynthInfo } from "../../../utils/loggin.js";
import { consoleColors } from "../../../utils/other.js";

/**
 * @param sample {BasicSample}
 * @returns {IndexedByteArray}
 */
export function writeDLSSample(sample)
{
    const fmtData = new IndexedByteArray(18);
    writeWord(fmtData, 1); // wFormatTag
    writeWord(fmtData, 1); // wChannels
    writeDword(fmtData, sample.sampleRate);
    writeDword(fmtData, sample.sampleRate * 2); // 16-bit samples
    writeWord(fmtData, 2); // wBlockAlign
    writeWord(fmtData, 16); // wBitsPerSample
    const fmt = writeRIFFOddSize(
        "fmt ",
        fmtData
    );
    let loop = 1;
    if (sample.sampleLoopStartIndex + Math.abs(sample.getAudioData().length - sample.sampleLoopEndIndex) < 2)
    {
        loop = 0;
    }
    const wsmp = writeWavesample(
        sample,
        sample.samplePitch,
        sample.samplePitchCorrection,
        0,
        sample.sampleLoopStartIndex,
        sample.sampleLoopEndIndex,
        loop
    );
    const audio = sample.getAudioData();
    let data;
    // if sample is compressed, getRawData cannot be used
    if (sample.isCompressed)
    {
        const data16 = new Int16Array(audio.length);
        
        for (let i = 0; i < audio.length; i++)
        {
            // 32,767, as 32,768 may cause overflow (because vorbis can go above 1 sometimes)
            data16[i] = audio[i] * 32767;
        }
        
        
        data = writeRIFFOddSize(
            "data",
            new IndexedByteArray(data16.buffer)
        );
    }
    else
    {
        data = writeRIFFOddSize(
            "data",
            sample.getRawData()
        );
    }
    
    const inam = writeRIFFOddSize(
        "INAM",
        getStringBytesZero(sample.sampleName)
    );
    const info = writeRIFFOddSize(
        "INFO",
        inam,
        false,
        true
    );
    SpessaSynthInfo(
        `%cSaved %c${sample.sampleName}%c succesfully!`,
        consoleColors.recognized,
        consoleColors.value,
        consoleColors.recognized
    );
    return writeRIFFOddSize(
        "wave",
        combineArrays([
            fmt,
            wsmp,
            data,
            info
        ]),
        false,
        true
    );
}