File size: 2,196 Bytes
343eed9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b554211
343eed9
 
 
b554211
 
343eed9
 
 
 
b554211
 
343eed9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export class VoiceActor {
  private static readonly SUPPORTED_VOICES = ['jean', 'thierry', 'sylvie', 'antoine', 'fr-CA-JeanNeural', 'fr-FR-VivienneNeural'];

  private constructor(private readonly _value: string) {}

  static create(value: string): VoiceActor {
    if (!this.SUPPORTED_VOICES.includes(value)) {
      throw new Error(`Voice actor not supported: ${value}`);
    }
    return new VoiceActor(value);
  }

  get value(): string {
    return this._value;
  }
}

class SpeechRate {
  private constructor(private readonly _value: string) {}

  static create(value: string): SpeechRate {
    // Validate format like "+10%", "-20%", "0%"
    const rateRegex = /^[+-]?\d+%$/;
    if (!rateRegex.test(value)) {
      throw new Error('Invalid Speech Rate format (expected [+-]N%)');
    }
    return new SpeechRate(value);
  }

  get value(): string {
    return this._value;
  }
}

export class NarrationContent {
  private static readonly EMOTION_REGEX = /\[(CRI|MURMURE|DEMON|COLERE|PEUR)\]/g;

  private constructor(private readonly _value: string) {}

  static create(value: string): NarrationContent {
    return new NarrationContent(value || '');
  }

  get value(): string {
    return this._value;
  }

  get hasEmotions(): boolean {
    return /\[(CRI|MURMURE|DEMON|COLERE|PEUR)\]/.test(this._value);
  }

  get emotions(): string[] {
    const regex = /\[(CRI|MURMURE|DEMON|COLERE|PEUR)\]/g;
    const matches = this._value.match(regex);
    return matches ? Array.from(new Set(matches)) : [];
  }

  get plainText(): string {
    const regex = /\[(CRI|MURMURE|DEMON|COLERE|PEUR)\]/g;
    return this._value.replace(regex, '').replace(/\s+/g, ' ').trim();
  }
}

export interface NarrationConfigProps {
  voice: string;
  rate: string;
}

export class NarrationConfig {
  private constructor(
    public readonly voice: VoiceActor,
    public readonly rate: SpeechRate
  ) {}

  static create(props: NarrationConfigProps): NarrationConfig {
    return new NarrationConfig(
      VoiceActor.create(props.voice),
      SpeechRate.create(props.rate)
    );
  }

  static createDefault(): NarrationConfig {
    return this.create({
      voice: 'jean',
      rate: '0%'
    });
  }
}