ptcow 0.1.0

Library for editing and playback of PxTone (.ptcop) music
Documentation
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use crate::{
    Bps, ChNum, SampleRate, VoiceData,
    herd::Tag,
    io::write_varint,
    point::EnvPt,
    pulse_oscillator::OsciPt,
    result::{ProjectReadError, ProjectWriteError, ReadResult, WriteResult},
    voice::{EnvelopeSrc, Voice, VoiceFlags},
    voice_data::{noise::NoiseData, pcm::PcmData, wave::WaveData},
};

#[cfg(feature = "oggv")]
mod oggv;

#[derive(Default, bytemuck::AnyBitPattern, Clone, Copy)]
#[repr(C)]
struct IoPcm {
    x3x_unit_no: u16,
    basic_key: u16,
    voice_flags: VoiceFlags,
    ch: u8,
    bps: u16,
    sps: SampleRate,
    tuning: f32,
    data_size: u32,
}

#[derive(Default, bytemuck::AnyBitPattern, bytemuck::NoUninit, Clone, Copy)]
#[repr(C)]
struct IoPtn {
    x3x_unit_no: u16,
    basic_key: u16,
    voice_flags: VoiceFlags,
    tuning: f32,
    rrr: i32,
}

/// I/O
impl Voice {
    pub(crate) fn read_mate_pcm(&mut self, rd: &mut crate::io::Reader) -> ReadResult {
        let _size = rd.next::<u32>()?;

        let pcm = rd.next::<IoPcm>()?;

        let bps = match pcm.bps {
            8 => Bps::B8,
            16 => Bps::B16,
            _ => return Err(ProjectReadError::FmtUnknown),
        };

        let chnum = match pcm.ch {
            1 => ChNum::Mono,
            2 => ChNum::Stereo,
            _ => return Err(ProjectReadError::FmtUnknown),
        };
        let mut pcm_data = PcmData::new();
        pcm_data.create(
            chnum,
            pcm.sps.into(),
            bps,
            pcm.data_size / u32::from(bps as u16 / 8 * u16::from(pcm.ch)),
        );
        let smp_buf = pcm_data.sample_mut();
        rd.fill_slice(smp_buf)?;
        self.allocate::<false>();
        let vu = &mut self.units[0];
        vu.data = VoiceData::Pcm(pcm_data);
        vu.flags = pcm.voice_flags;
        vu.basic_key = i32::from(pcm.basic_key);
        vu.tuning = pcm.tuning;

        Ok(())
    }

    pub(crate) fn write_mate_pcm(&self, out: &mut Vec<u8>, data: &PcmData) {
        out.extend_from_slice(Tag::MatePCM.to_code());
        #[expect(clippy::cast_possible_truncation)]
        let io_size: u32 = size_of::<IoPcm>() as u32 + data.smp.len() as u32;
        out.extend_from_slice(&io_size.to_le_bytes());
        let vu = &self.units[0];
        let io_pcm = IoPcm {
            x3x_unit_no: 0,
            basic_key: vu.basic_key.try_into().unwrap(),
            voice_flags: vu.flags,
            ch: data.ch as _,
            bps: data.bps as _,
            // TODO: Normally this assumption shouldn't be violated, but ogg voices
            // can have higher sps than what can fit into 16 bits.
            //
            // The fix for that is to not load ogg voices as pcm voices, but to properly load them as
            // ogg voices, and serialize them as such, rather than as pcm.
            sps: data.sps.try_into().unwrap(),
            tuning: vu.tuning,
            data_size: data.smp.len().try_into().unwrap(),
        };
        let mut io_pcm_byte_buf: Vec<u8> = vec![0; size_of::<IoPcm>()];
        // Safety: YOLO
        unsafe {
            std::ptr::copy(
                (&raw const io_pcm).cast(),
                io_pcm_byte_buf.as_mut_ptr(),
                size_of::<IoPcm>(),
            );
        }
        out.extend_from_slice(&io_pcm_byte_buf);
        out.extend_from_slice(&data.smp);
    }

    pub(crate) fn read_mate_ptn(&mut self, rd: &mut crate::io::Reader) -> ReadResult {
        let size = rd.next::<u32>()?;
        let ptn = rd.next::<IoPtn>()?;

        if ptn.rrr > 1 || ptn.rrr < 0 {
            return Err(ProjectReadError::FmtUnknown);
        }

        let mut noise_data = NoiseData::new();
        noise_data.read(rd)?;
        self.allocate::<false>();
        let vu = &mut self.units[0];
        noise_data.io_size = size;
        vu.data = VoiceData::Noise(noise_data);
        vu.flags = ptn.voice_flags;
        vu.basic_key = i32::from(ptn.basic_key);
        vu.tuning = ptn.tuning;

        Ok(())
    }

    pub(crate) fn write_mate_ptn(&self, out: &mut Vec<u8>, data: &NoiseData) {
        out.extend_from_slice(Tag::MatePTN.to_code());
        out.extend_from_slice(&data.io_size.to_le_bytes());
        let vu = &self.units[0];
        let ptn = IoPtn {
            x3x_unit_no: 0,
            basic_key: vu.basic_key.try_into().unwrap(),
            voice_flags: vu.flags,
            tuning: vu.tuning,
            rrr: 1,
        };
        out.extend_from_slice(bytemuck::bytes_of(&ptn));
        data.write(out);
    }

    pub(crate) fn read_mate_ptv(&mut self, rd: &mut crate::io::Reader) -> ReadResult {
        let _size: u32 = rd.next()?;
        let _ptv: IoPtv = rd.next()?;

        self.ptv_read(rd)?;
        Ok(())
    }
    pub(crate) fn write_mate_ptv(&self, out: &mut Vec<u8>) -> WriteResult {
        out.extend_from_slice(Tag::MatePTV.to_code());
        let io_ptv = IoPtv {
            x3x_unit_no: 0,
            rrr: 0,
            x3x_tuning: 0.0,
            size: 0,
        };
        let size: u32 = 0;
        let idx_before_written = out.len();
        out.extend_from_slice(&size.to_le_bytes());
        out.extend_from_slice(bytemuck::bytes_of(&io_ptv));
        self.ptv_write(out)?;
        let idx_after_written = out.len();
        #[expect(clippy::cast_possible_truncation)]
        let written_size = (idx_after_written - (idx_before_written + 4)) as u32;
        out[idx_before_written..idx_before_written + 4]
            .copy_from_slice(&written_size.to_le_bytes());
        let io_ptv_size_offset = idx_before_written + 12;
        let io_ptv_written_size = written_size - 12;
        out[io_ptv_size_offset..io_ptv_size_offset + 4]
            .copy_from_slice(&io_ptv_written_size.to_le_bytes());
        Ok(())
    }
    #[expect(clippy::inconsistent_digit_grouping)]
    fn ptv_read(&mut self, rd: &mut crate::io::Reader) -> ReadResult {
        if &rd.next::<[u8; 8]>()? != b"PTVOICE-" {
            return Err(ProjectReadError::InvalidTag);
        }
        let ver: u32 = rd.next()?;

        if ver > 2006_01_11 {
            return Err(ProjectReadError::FmtNewer);
        }
        let _total: i32 = rd.next()?;
        let _x3x_basic = rd.next_varint()?;
        let work1 = rd.next_varint()?;
        let work2 = rd.next_varint()?;
        if work1 != 0 || work2 != 0 {
            return Err(ProjectReadError::FmtUnknown);
        }
        let num = rd.next_varint()?;
        match num {
            1 => self.allocate::<false>(),
            2 => self.allocate::<true>(),
            _ => return Err(ProjectReadError::FmtUnknown),
        }
        for vu in &mut self.units {
            vu.basic_key = rd.next_varint()?.cast_signed();
            vu.volume = rd.next_varint()?.cast_signed().try_into().unwrap();
            vu.pan = rd.next_varint()?.cast_signed().try_into().unwrap();
            let work1 = rd.next_varint()?;
            vu.tuning = f32::from_bits(work1);
            vu.flags = VoiceFlags::from_bits_retain(rd.next_varint()?);
            let data_flags = rd.next_varint()?;
            if data_flags & PTV_DATAFLAG_WAVE != 0 {
                let mut wave_data = WaveData::Coord {
                    points: Vec::new(),
                    resolution: 0,
                };
                read_wave(rd, &mut wave_data)?;
                vu.data = VoiceData::Wave(wave_data);
            }
            if data_flags & PTV_DATAFLAG_ENVELOPE != 0 {
                read_envelope(rd, &mut vu.envelope)?;
            }
        }
        Ok(())
    }
    #[expect(clippy::inconsistent_digit_grouping)]
    fn ptv_write(&self, out: &mut Vec<u8>) -> WriteResult {
        out.extend_from_slice(b"PTVOICE-");
        let ver: u32 = 2006_01_11;
        out.extend_from_slice(&ver.to_le_bytes());
        let total_offset = out.len();
        let total: i32 = 0;
        out.extend_from_slice(&total.to_le_bytes());
        let x3x_basic: u32 = 0;
        write_varint(x3x_basic, out);
        let work1: u32 = 0;
        let work2: u32 = 0;
        write_varint(work1, out);
        write_varint(work2, out);
        #[expect(clippy::cast_possible_truncation)]
        let ch_num: u32 = self.units.len() as u32;
        write_varint(ch_num, out);
        for vu in &self.units {
            write_varint(vu.basic_key.cast_unsigned(), out);
            write_varint(vu.volume.cast_unsigned().into(), out);
            write_varint(vu.pan.cast_unsigned().into(), out);
            write_varint(vu.tuning.to_bits(), out);
            write_varint(vu.flags.bits(), out);
            let mut data_flags = PTV_DATAFLAG_WAVE;
            if !vu.envelope.points.is_empty() {
                data_flags |= PTV_DATAFLAG_ENVELOPE;
            }
            write_varint(data_flags, out);
            let VoiceData::Wave(wave_data) = &vu.data else {
                unreachable!()
            };
            write_wave(wave_data, out)?;
            if !vu.envelope.points.is_empty() {
                write_envelope(&vu.envelope, out);
            }
        }
        let current_offset = out.len();
        #[expect(clippy::cast_possible_truncation)]
        let amount_written = (current_offset - (total_offset + 4)) as u32;
        out[total_offset..total_offset + 4].copy_from_slice(&amount_written.to_le_bytes());
        Ok(())
    }
    pub(crate) fn read_ogg(&mut self, rd: &mut crate::io::Reader<'_>) -> ReadResult {
        let _size: u32 = rd.next()?;
        #[cfg_attr(not(feature = "oggv"), expect(unused_variables))]
        let io_oggv: IoOggv = rd.next()?;
        self.allocate::<false>();
        let _ch: i32 = rd.next()?;
        let _sps2: i32 = rd.next()?;
        let _smp_num: i32 = rd.next()?;
        let size: u32 = rd.next()?;
        if size == 0 {
            return Ok(());
        }
        #[cfg(feature = "oggv")]
        {
            oggv::read(rd, &io_oggv, size as usize, &mut self.units[0])
        }
        #[cfg(not(feature = "oggv"))]
        {
            Err(ProjectReadError::OggvSupportDisabled)
        }
    }
}

#[derive(Clone, Copy, bytemuck::AnyBitPattern)]
#[repr(C)]
struct IoOggv {
    xxx: u16,
    basic_key: u16,
    voice_flags: VoiceFlags,
    tuning: f32,
}

fn read_wave(rd: &mut crate::io::Reader, wave_data: &mut WaveData) -> ReadResult {
    let kind = rd.next_varint()?;
    *wave_data = match kind {
        0 => {
            let num = rd.next_varint()?;
            let reso = rd.next_varint()?;
            let mut points = vec![OsciPt::ZERO; num as usize];
            for pt in &mut points {
                pt.x = u16::from(rd.next::<u8>()?);
                pt.y = i16::from(rd.next::<i8>()?);
            }
            WaveData::Coord {
                resolution: reso.try_into().unwrap(),
                points,
            }
        }
        1 => {
            let num = rd.next_varint()?;
            let mut points = vec![OsciPt::ZERO; num as usize];

            for pt in &mut points {
                let x = rd.next_varint()?;
                pt.x = x.try_into().map_err(|_| ProjectReadError::OvertonePointOutOfRange(x))?;
                let y = rd.next_varint()?;
                pt.y = i16::try_from(y.cast_signed())
                    .map_err(|_| ProjectReadError::OvertonePointOutOfRange(y))?;
            }
            WaveData::Overtone { points }
        }
        _ => panic!("Invalid/unsupported type: {kind}"),
    };

    Ok(())
}

fn write_wave(wave_data: &WaveData, out: &mut Vec<u8>) -> WriteResult {
    match wave_data {
        WaveData::Coord {
            resolution,
            points: coordinates,
        } => {
            write_varint(0, out);
            #[expect(clippy::cast_possible_truncation)]
            let num_pts: u32 = coordinates.len() as u32;
            write_varint(num_pts, out);
            write_varint(u32::from(*resolution), out);
            for pt in coordinates {
                // We use a 16 bit point type here because that's what oscillator
                // expects, but the PxTone format only saves 8 bits.
                let x = pt.x.try_into().map_err(|_| ProjectWriteError::CoordWavePointOutOfRange)?;
                let y: i8 =
                    pt.y.try_into().map_err(|_| ProjectWriteError::CoordWavePointOutOfRange)?;
                out.push(x);
                out.push(y.cast_unsigned());
            }
        }
        WaveData::Overtone {
            points: coordinates,
        } => {
            write_varint(1, out);
            #[expect(clippy::cast_possible_truncation)]
            let num_pts: u32 = coordinates.len() as u32;
            write_varint(num_pts, out);
            for pt in coordinates {
                write_varint(pt.x.into(), out);
                write_varint(i32::from(pt.y).cast_unsigned(), out);
            }
        }
    }
    Ok(())
}

fn read_envelope(rd: &mut crate::io::Reader, envelope: &mut EnvelopeSrc) -> ReadResult {
    envelope.seconds_per_point = rd.next_varint()?;
    let envelope_head = rd.next_varint()? as usize;
    let body_num = rd.next_varint()? as usize;
    if body_num != 0 {
        return Err(ProjectReadError::FmtUnknown);
    }
    let tail = rd.next_varint()? as usize;
    if tail != 1 {
        return Err(ProjectReadError::FmtUnknown);
    }
    let num = envelope_head + body_num + tail;
    envelope.points = vec![EnvPt::ZERO; num];
    for pt in &mut envelope.points {
        pt.x = rd.next_varint()?.try_into().unwrap();
        pt.y = rd.next_varint()?.try_into().unwrap();
    }
    Ok(())
}

fn write_envelope(envelope: &EnvelopeSrc, out: &mut Vec<u8>) {
    write_varint(envelope.seconds_per_point, out);
    let envelope_head = envelope.points.len().saturating_sub(1);
    #[expect(clippy::cast_possible_truncation)]
    write_varint(envelope_head as u32, out);
    let tail = 1;
    #[expect(clippy::cast_possible_truncation)]
    let body_num = envelope.points.len() as u32 - (envelope_head as u32 + tail);
    assert_eq!(body_num, 0);
    write_varint(body_num, out);
    write_varint(tail, out);
    for pt in &envelope.points {
        write_varint(pt.x.into(), out);
        write_varint(pt.y.into(), out);
    }
}

#[derive(Clone, Copy, bytemuck::AnyBitPattern, bytemuck::NoUninit)]
#[repr(C)]
struct IoPtv {
    x3x_unit_no: u16,
    rrr: u16,
    x3x_tuning: f32,
    size: i32,
}

const PTV_DATAFLAG_WAVE: u32 = 1;
const PTV_DATAFLAG_ENVELOPE: u32 = 2;