File size: 2,631 Bytes
811126d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { exec } from "child_process";
import { promisify } from "util";

const execAsync = promisify(exec);

async function addMilitaryRadioEffect(
  inputFile: string,
  outputFile: string
): Promise<void> {
  try {
    // Obtenir le taux d'échantillonnage du fichier d'entrée
    const { stdout: sampleRate } = await execAsync(`soxi -r "${inputFile}"`);
    const rate = sampleRate.trim();

    // Création du bruit blanc temporaire avec le même taux d'échantillonnage
    const noiseCmd = `sox -r ${rate} -n noise.wav synth 10 whitenoise vol 0.15`;
    await execAsync(noiseCmd);

    // Création des grésillements temporaires (bande typique ondes courtes)
    const crackleCmd = `sox -r ${rate} -n crackle.wav synth 10 whitenoise band -n 300 2700 vol 0.1`;
    await execAsync(crackleCmd);

    // Traitement initial de la voix avec filtres ondes courtes (2K40J3E)
    const voiceCmd = `sox "${inputFile}" voice_mod.wav \
      gain -n -1 \
      sinc -n 4096 -b 48 300 \
      sinc -n 4096 -b 48 -2700 \
      compand 0.05,0.2 6:-70,-60,-20 -8 -90 0.1 \
      overdrive 20 \
      gain -n -2`;
    await execAsync(voiceCmd);

    // Mélange des effets
    const mixCommand = `sox -m voice_mod.wav noise.wav crackle.wav temp1.wav`;
    await execAsync(mixCommand);

    // Application des effets finaux
    const effectCommand = `sox temp1.wav "${outputFile}" \
      gain -n -2 \
      sinc -n 4096 -b 48 300 \
      sinc -n 4096 -b 48 -2700 \
      compand 0.02,0.05 -6:-70,-60,-40,-30,-20 -8 -90 0.1 \
      overdrive 25 \
      treble -15 1.5k \
      bass -12 400 \
      echo 0.8 0.5 30 0.6 \
      echo 0.6 0.3 15 0.4 \
      tremolo 0.8 60 \
      contrast 80`;
    await execAsync(effectCommand);

    // Nettoyage des fichiers temporaires
    await execAsync("rm noise.wav crackle.wav temp1.wav voice_mod.wav");

    console.log("Effet radio ondes courtes (2K40J3E) appliqué avec succès!");
  } catch (error) {
    console.error("Erreur lors de l'application de l'effet:", error);
    throw error;
  }
}

// Installation de sox si nécessaire
async function checkAndInstallSox(): Promise<void> {
  try {
    await execAsync("which sox");
    console.log("Sox est déjà installé");
  } catch {
    console.log("Installation de Sox...");
    await execAsync(
      "sudo apt-get update && sudo apt-get install -y sox libsox-fmt-mp3"
    );
    console.log("Sox a été installé avec succès");
  }
}

// Utilisation
(async () => {
  try {
    await checkAndInstallSox();
    await addMilitaryRadioEffect("output.mp3", "output-radio.mp3");
  } catch (error) {
    console.error("Erreur:", error);
  }
})();