File size: 2,726 Bytes
b0798af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { IAudioMetadata } from 'music-metadata';
import type { Track } from './models';
import type { Store } from './store';

export function saveMetadata(store: Store, track: Track) {
  const { artwork, artwork_mime, ...rest } = track;
  if (artwork)
    store.put(
      'artwork',
      { id: track.id, data: artwork, mime: artwork_mime },
      track.owner_id || '',
    );
  const saved = {
    ...rest,
    has_artwork: !!artwork || !!track.has_artwork,
    metadata_updated_at: new Date().toISOString(),
  };
  store.put('tracks', saved, track.owner_id || '');
  return saved;
}

// Keep lyrics and binary artwork out of list responses. Never invent absent tags.
export function audioMetadata(metadata: IAudioMetadata): Partial<Track> {
  const c = metadata.common,
    f = metadata.format;
  const picture = c.picture?.find(
    (p) =>
      ['image/jpeg', 'image/png', 'image/webp'].includes(p.format) &&
      p.data.length <= 4 * 1024 * 1024,
  );
  const lyrics = (c.lyrics || [])
    .slice(0, 8)
    .map((l) => ({
      language: l.language,
      text: (l.text || '').slice(0, 100000),
      lines: (l.syncText || []).slice(0, 5000).map((line) => ({
        text: line.text.slice(0, 2000),
        ...(l.timeStampFormat === 2 && Number.isFinite(line.timestamp)
          ? { timestamp: line.timestamp }
          : {}),
      })),
    }))
    .filter((l) => l.text || l.lines.length);
  return {
    ...(c.title ? { title: c.title } : {}),
    ...(c.artists?.length || c.artist
      ? {
          artist_name: (c.artists?.length ? c.artists : [c.artist!]).join(', '),
        }
      : {}),
    ...(c.album ? { album_title: c.album } : {}),
    ...(c.genre?.length ? { genre: c.genre.join(', ') } : {}),
    ...(f.duration ? { duration_ms: Math.round(f.duration * 1000) } : {}),
    artwork: picture ? Buffer.from(picture.data).toString('base64') : undefined,
    artwork_mime: picture?.format,
    lyrics,
    metadata_version: 2,
    metadata_error: undefined,
    details: {
      artists: c.artists || (c.artist ? [c.artist] : []),
      album_artist: c.albumartist,
      year: c.year,
      date: c.date,
      original_date: c.originaldate,
      composers: c.composer,
      track_number: c.track.no,
      track_total: c.track.of,
      disc_number: c.disk.no,
      disc_total: c.disk.of,
      genres: c.genre,
      label: c.label,
      copyright: c.copyright,
      isrc: c.isrc,
      bpm: c.bpm,
      encoded_by: c.encodedby,
      codec: f.codec,
      container: f.container,
      bitrate: f.bitrate,
      sample_rate: f.sampleRate,
      bits_per_sample: f.bitsPerSample,
      channels: f.numberOfChannels,
      lossless: f.lossless,
      tag_formats: f.tagTypes,
    },
  };
}