All files / src/renderer/lib/music exportSong.ts

0% Statements 0/42
0% Branches 0/13
0% Functions 0/5
0% Lines 0/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                               
import API from "renderer/lib/api";
import l10n from "shared/lib/lang/l10n";
import {
  ERROR_TIMED_OUT,
  ERROR_AUDIO_ENCODE_FAILED,
} from "shared/lib/music/constants";
import { MusicExportFormat } from "shared/lib/music/types";
import { Song } from "shared/lib/uge/types";
 
const exportMimeTypes: Record<MusicExportFormat, string> = {
  wav: "audio/wav",
  mp3: "audio/mpeg",
  flac: "audio/flac",
};
 
const exportSong = (
  song: Song,
  format: MusicExportFormat,
  loopCount: number,
) => {
  return new Promise<{
    format: MusicExportFormat;
    data: Uint8Array;
  }>((resolve, reject) => {
    const requestId = `request_${Math.random()}`;
 
    const timeout = window.setTimeout(() => {
      unsubscribe();
      reject(new Error(l10n("ERROR_TIMED_OUT")));
    }, 120000);
 
    const unsubscribe = API.events.music.response.subscribe((_, data) => {
      if (data.action === "exported-song" && data.requestId === requestId) {
        window.clearTimeout(timeout);
        unsubscribe();
        resolve({
          format: data.format,
          data: data.data,
        });
      } else Iif (
        data.action === "export-failed" &&
        data.requestId === requestId
      ) {
        window.clearTimeout(timeout);
        unsubscribe();
        reject(new Error(data.message));
      }
    });
 
    API.music.sendToMusicWindow({
      requestId,
      action: "export-song",
      song,
      format,
      loopCount,
    });
  });
};
 
export const downloadExportedSong = async (
  song: Song,
  format: MusicExportFormat,
  loopCount: number,
  filename: string,
) => {
  Iif (!song) {
    return;
  }
  try {
    const exportedData = await exportSong(song, format, loopCount);
    const blob = new Blob([new Uint8Array(exportedData.data)], {
      type: exportMimeTypes[exportedData.format],
    });
    const url = window.URL.createObjectURL(blob);
    const link = document.createElement("a");
    link.href = url;
    link.download = filename;
    document.body.appendChild(link);
    link.click();
    link.remove();
    window.URL.revokeObjectURL(url);
  } catch (error) {
    const message = error instanceof Error ? error.message : String(error);
    let displayMessage = message;
    if (message === ERROR_TIMED_OUT) {
      displayMessage = l10n("ERROR_TIMED_OUT");
    } else Iif (message === ERROR_AUDIO_ENCODE_FAILED) {
      displayMessage = l10n("ERROR_AUDIO_ENCODE_FAILED");
    }
    API.dialog.showError(
      l10n("ERROR_EXPORT_FILE_FAILED", { filetype: format }),
      displayMessage,
    );
  }
};