allfeat-midds 0.1.5

Substrate compatible MIDDS main crate.
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// This file is part of Allfeat.

// Copyright (C) 2022-2025 Allfeat.
// SPDX-License-Identifier: GPL-3.0-or-later

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

extern crate alloc;

#[cfg(feature = "runtime-benchmarks")]
use alloc::{format, string::ToString};

use frame_support::sp_runtime::RuntimeDebug;
use parity_scale_codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use scale_info::TypeInfo;

#[cfg(feature = "js")]
use wasm_bindgen::prelude::*;

use crate::{
    shared::{
        conversion::{Validatable, ValidationError},
        Key,
    },
    track::types::{
        Isrc, TrackBeatsPerMinute, TrackContributors, TrackDuration, TrackGenres,
        TrackMasteringPlace, TrackMixingPlace, TrackPerformers, TrackProducers, TrackRecordYear,
        TrackRecordingPlace, TrackTitle, TrackTitleAliases, TrackVersion,
    },
    Midds, MiddsId,
};

/// A Track represents a specific recorded performance or production
/// of a musical work. It links metadata such as contributors,
/// recording details, and identification codes.
#[derive(
    Clone,
    Eq,
    PartialEq,
    Encode,
    Decode,
    TypeInfo,
    DecodeWithMemTracking,
    MaxEncodedLen,
    RuntimeDebug,
)]
#[cfg_attr(feature = "js", wasm_bindgen(inspectable))]
pub struct Track {
    /// ISRC (International Standard Recording Code) that uniquely identifies this recording.
    #[cfg_attr(feature = "js", wasm_bindgen(getter_with_clone))]
    pub isrc: Isrc,

    /// The linked musical work this track is based on (must refer to a registered MIDDS).
    pub musical_work: MiddsId,

    /// Main artist MIDDS identifier (typically the primary performer).
    pub artist: MiddsId,

    /// List of producer MIDDS identifiers who participated in the production.
    #[cfg_attr(feature = "js", wasm_bindgen(getter_with_clone))]
    pub producers: TrackProducers,

    /// List of performer MIDDS identifiers who contributed to the performance.
    #[cfg_attr(feature = "js", wasm_bindgen(getter_with_clone))]
    pub performers: TrackPerformers,

    /// Additional contributors (e.g., sound engineers, featured artists).
    #[cfg_attr(feature = "js", wasm_bindgen(getter_with_clone))]
    pub contributors: TrackContributors,

    /// Main title of the track.
    #[cfg_attr(feature = "js", wasm_bindgen(getter_with_clone))]
    pub title: TrackTitle,

    /// Optional list of alternative titles for the track.
    #[cfg_attr(feature = "js", wasm_bindgen(getter_with_clone))]
    pub title_aliases: TrackTitleAliases,

    /// Year the track was recorded (4-digit Gregorian year).
    pub recording_year: Option<TrackRecordYear>,

    /// Music genres attributed to this recording.
    #[cfg_attr(feature = "js", wasm_bindgen(getter_with_clone))]
    pub genres: TrackGenres,

    /// Version or type of the track (e.g., Remix, Acoustic, Live).
    pub version: Option<TrackVersion>,

    /// Duration of the track in seconds.
    pub duration: Option<TrackDuration>,

    /// Beats per minute (BPM), representing the tempo of the track.
    pub bpm: Option<TrackBeatsPerMinute>,

    /// Musical key (e.g., C, G#, etc.) the track is in.
    pub key: Option<Key>,

    /// Free-text field indicating where the recording took place.
    #[cfg_attr(feature = "js", wasm_bindgen(getter_with_clone))]
    pub recording_place: Option<TrackRecordingPlace>,

    /// Free-text field indicating where the mixing of the track occurred.
    #[cfg_attr(feature = "js", wasm_bindgen(getter_with_clone))]
    pub mixing_place: Option<TrackMixingPlace>,

    /// Free-text field indicating where the mastering of the track occurred.
    #[cfg_attr(feature = "js", wasm_bindgen(getter_with_clone))]
    pub mastering_place: Option<TrackMasteringPlace>,
}

// Implements the `Midds` trait, marking this struct as a MIDDS object.
impl Midds for Track {
    const NAME: &'static str = "Track";

    #[cfg(feature = "runtime-benchmarks")]
    type BenchmarkHelper = TrackBenchmarkHelper;
}

/// Validation implementation for Track
impl Validatable for Track {
    type Error = ValidationError;

    fn validate(&self) -> Result<(), Self::Error> {
        // Validate ISRC format - the macro-generated type already handles validation
        if self.isrc.is_empty() {
            return Err(ValidationError::invalid_format(
                "ISRC",
                "ISRC cannot be empty",
            ));
        }

        // Validate title is not empty
        if self.title.is_empty() {
            return Err(ValidationError::invalid_format(
                "Title",
                "Title cannot be empty",
            ));
        }

        // Validate recording year if present
        if let Some(year) = self.recording_year {
            if !(1000..=9999).contains(&year) {
                return Err(ValidationError::invalid_format(
                    "RecordingYear",
                    "Year must be 4 digits",
                ));
            }
        }

        // Validate duration if present
        if let Some(duration) = self.duration {
            if duration == 0 {
                return Err(ValidationError::invalid_format(
                    "Duration",
                    "Duration must be greater than 0",
                ));
            }
        }

        // Validate BPM if present
        if let Some(bpm) = self.bpm {
            if bpm == 0 || bpm > 300 {
                return Err(ValidationError::invalid_format(
                    "BPM",
                    "BPM must be between 1 and 300",
                ));
            }
        }

        // Validate MIDDS ID references are not zero
        if self.musical_work == 0 {
            return Err(ValidationError::invalid_format(
                "MusicalWork",
                "Musical work ID cannot be 0",
            ));
        }
        if self.artist == 0 {
            return Err(ValidationError::invalid_format(
                "Artist",
                "Artist ID cannot be 0",
            ));
        }

        // Validate all producer IDs are not zero
        for &producer_id in &self.producers {
            if producer_id == 0 {
                return Err(ValidationError::invalid_format(
                    "Producers",
                    "Producer ID cannot be 0",
                ));
            }
        }

        // Validate all performer IDs are not zero
        for &performer_id in &self.performers {
            if performer_id == 0 {
                return Err(ValidationError::invalid_format(
                    "Performers",
                    "Performer ID cannot be 0",
                ));
            }
        }

        // Validate all contributor IDs are not zero
        for &contributor_id in &self.contributors {
            if contributor_id == 0 {
                return Err(ValidationError::invalid_format(
                    "Contributors",
                    "Contributor ID cannot be 0",
                ));
            }
        }

        // Validate title aliases are not empty
        for alias in &self.title_aliases {
            if alias.is_empty() {
                return Err(ValidationError::invalid_format(
                    "TitleAliases",
                    "Title alias cannot be empty",
                ));
            }
        }

        Ok(())
    }
}

#[cfg(feature = "runtime-benchmarks")]
pub struct TrackBenchmarkHelper;

#[cfg(feature = "runtime-benchmarks")]
impl crate::benchmarking::BenchmarkHelperT<Track> for TrackBenchmarkHelper {
    fn min_size() -> Track {
        use crate::track::types::*;
        use core::str::FromStr;

        Track {
            isrc: Isrc::from_str("US1234567890").unwrap(),
            musical_work: 1,
            artist: 1,
            producers: TrackProducers::new(),
            performers: TrackPerformers::new(),
            contributors: TrackContributors::new(),
            title: TrackTitle::from_str("T").unwrap(),
            title_aliases: TrackTitleAliases::new(),
            recording_year: None,
            genres: TrackGenres::new(),
            version: None,
            duration: None,
            bpm: None,
            key: None,
            recording_place: None,
            mixing_place: None,
            mastering_place: None,
        }
    }

    fn max_size() -> Track {
        use crate::track::types::*;
        use core::str::FromStr;

        // Helper to create maximum length strings
        let max_isrc = "US".to_string() + &"A".repeat(10); // 12 chars max
        let max_title = "T".repeat(256); // 256 chars max
        let max_place = "P".repeat(256); // 256 chars max

        // Helper to create maximum collections
        let mut producers = TrackProducers::new();
        // Fill to maximum capacity (64 producers)
        for i in 0..64 {
            if producers.push(u64::MAX - i as u64).is_err() {
                break;
            }
        }

        let mut performers = TrackPerformers::new();
        // Fill to maximum capacity (256 performers)
        for i in 0..256 {
            if performers.push(u64::MAX - i as u64).is_err() {
                break;
            }
        }

        let mut contributors = TrackContributors::new();
        // Fill to maximum capacity (256 contributors)
        for i in 0..256 {
            if contributors.push(u64::MAX - i as u64).is_err() {
                break;
            }
        }

        let mut title_aliases = TrackTitleAliases::new();
        // Fill to maximum capacity (16 aliases)
        for i in 0..16 {
            let alias_title = TrackTitle::from_str(&format!("Alias{i}")).unwrap();
            if title_aliases.push(alias_title).is_err() {
                break;
            }
        }

        let mut genres = TrackGenres::new();
        // Fill to maximum capacity (5 genres)
        for _ in 0..5 {
            if genres
                .push(allfeat_music_genres::GenreId::Electropop)
                .is_err()
            {
                break;
            }
        }

        Track {
            isrc: Isrc::from_str(&max_isrc).unwrap(),
            musical_work: u64::MAX,
            artist: u64::MAX,
            producers,
            performers,
            contributors,
            title: TrackTitle::from_str(&max_title).unwrap(),
            title_aliases,
            recording_year: Some(2024),
            genres,
            version: Some(TrackVersion::Edit),
            duration: Some(u16::MAX),
            bpm: Some(u16::MAX),
            key: Some(crate::shared::Key::Cs),
            recording_place: Some(TrackRecordingPlace::from_str(&max_place).unwrap()),
            mixing_place: Some(TrackMixingPlace::from_str(&max_place).unwrap()),
            mastering_place: Some(TrackMasteringPlace::from_str(&max_place).unwrap()),
        }
    }

    fn variable_size(complexity: f32) -> Track {
        use crate::benchmarking::utils::*;
        use crate::track::types::*;
        use core::str::FromStr;

        // Calculate dynamic lengths based on complexity
        let title_len =
            target_length_for_complexity::<frame_support::traits::ConstU32<256>>(complexity);
        let isrc_len = (7.0 + complexity * 5.0) as usize; // 7-12 characters

        // Create complexity-based ISRC
        let isrc_base = format!(
            "US{:0width$}",
            1234567890u64,
            width = isrc_len.saturating_sub(2).min(10)
        );
        let isrc_content = if isrc_base.len() > 12 {
            isrc_base[..12].to_string()
        } else {
            isrc_base
        };

        // Create complexity-based title
        let title_content = if complexity > 0.8 {
            format!(
                "Complex Track Title {}",
                "X".repeat(title_len.saturating_sub(20))
            )
        } else if complexity > 0.5 {
            format!("Track {}", "T".repeat(title_len.saturating_sub(7)))
        } else {
            "T".repeat(title_len.max(1))
        };

        // Create producers based on complexity
        let mut producers = TrackProducers::new();
        let producer_count = (complexity * 64.0) as usize;
        for i in 0..producer_count {
            if producers.push(i as u64 + 1).is_err() {
                break;
            }
        }

        // Create performers based on complexity
        let mut performers = TrackPerformers::new();
        let performer_count = (complexity * 256.0) as usize;
        for i in 0..performer_count {
            if performers.push(i as u64 + 1000).is_err() {
                break;
            }
        }

        // Create contributors based on complexity
        let mut contributors = TrackContributors::new();
        let contributor_count = (complexity * 128.0) as usize; // Half of max for variety
        for i in 0..contributor_count {
            if contributors.push(i as u64 + 2000).is_err() {
                break;
            }
        }

        // Create title aliases based on complexity
        let mut title_aliases = TrackTitleAliases::new();
        if complexity > 0.4 {
            let alias_count = (complexity * 16.0) as usize;
            for i in 0..alias_count {
                let alias_content = format!("Alias{i}");
                if let Ok(alias) = TrackTitle::from_str(&alias_content) {
                    if title_aliases.push(alias).is_err() {
                        break;
                    }
                }
            }
        }

        // Create genres based on complexity
        let mut genres = TrackGenres::new();
        if complexity > 0.3 {
            let genre_count = (complexity * 5.0) as usize;
            for _ in 0..genre_count {
                if genres
                    .push(allfeat_music_genres::GenreId::Electropop)
                    .is_err()
                {
                    break;
                }
            }
        }

        // Create place names based on complexity
        let recording_place = if complexity > 0.7 {
            let place_len = ((complexity * 100.0) as usize + 1).min(256);
            Some(TrackRecordingPlace::from_str(&"R".repeat(place_len)).ok())
        } else {
            None
        };

        let mixing_place = if complexity > 0.8 {
            let place_len = ((complexity * 100.0) as usize + 1).min(256);
            Some(TrackMixingPlace::from_str(&"M".repeat(place_len)).ok())
        } else {
            None
        };

        let mastering_place = if complexity > 0.9 {
            let place_len = ((complexity * 100.0) as usize + 1).min(256);
            Some(TrackMasteringPlace::from_str(&"S".repeat(place_len)).ok())
        } else {
            None
        };

        // Build the final Track
        Track {
            isrc: Isrc::from_str(&isrc_content)
                .unwrap_or_else(|_| Isrc::from_str("US1234567890").unwrap()),
            musical_work: (complexity * 1000000.0) as u64 + 1,
            artist: (complexity * 1000000.0) as u64 + 1,
            producers,
            performers,
            contributors,
            title: TrackTitle::from_str(&title_content)
                .unwrap_or_else(|_| TrackTitle::from_str("T").unwrap()),
            title_aliases,
            recording_year: if complexity > 0.2 {
                Some((1900.0 + complexity * 124.0) as u16)
            } else {
                None
            },
            genres,
            version: if complexity > 0.4 {
                Some(TrackVersion::Original)
            } else {
                None
            },
            duration: if complexity > 0.3 {
                Some((complexity * 600.0) as u16)
            } else {
                None
            },
            bpm: if complexity > 0.5 {
                Some((complexity * 200.0 + 60.0) as u16)
            } else {
                None
            },
            key: if complexity > 0.6 {
                Some(crate::shared::Key::C)
            } else {
                None
            },
            recording_place: recording_place.unwrap_or(None),
            mixing_place: mixing_place.unwrap_or(None),
            mastering_place: mastering_place.unwrap_or(None),
        }
    }
}