File size: 2,936 Bytes
0a01d76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import init, {
  MoshiASRDecoder,
  initThreadPool,
} from "./build/wasm_speech_streaming.js";

async function fetchArrayBuffer(url) {
  const cacheName = "whisper-candle-cache";
  const cache = await caches.open(cacheName);
  const cachedResponse = await cache.match(url);
  if (cachedResponse) {
    const data = await cachedResponse.arrayBuffer();
    return new Uint8Array(data);
  }
  const res = await fetch(url, { cache: "force-cache" });
  cache.put(url, res.clone());
  return new Uint8Array(await res.arrayBuffer());
}

class MoshiASR {
  static decoder = null;

  // Initialize the model
  static async initialize(params) {
    const { weightsURL, tokenizerURL, mimiURL, configURL } = params;

    if (this.decoder) {
      self.postMessage({ status: "model_ready" });
      return;
    }

    try {
      await init();
      const numThreads = navigator.hardwareConcurrency || 4;
      await initThreadPool(numThreads);

      self.postMessage({
        status: "loading",
        message: `Loading Model (~950 MB)`,
      });

      const [weightsArrayU8, tokenizerArrayU8, mimiArrayU8, configArrayU8] =
        await Promise.all([
          fetchArrayBuffer(weightsURL),
          fetchArrayBuffer(tokenizerURL),
          fetchArrayBuffer(mimiURL),
          fetchArrayBuffer(configURL),
        ]);

      this.decoder = new MoshiASRDecoder(
        weightsArrayU8,
        tokenizerArrayU8,
        mimiArrayU8,
        configArrayU8
      );

      self.postMessage({ status: "model_ready" });
    } catch (error) {
      self.postMessage({ error: error.message });
    }
  }

  static startStream() {
    if (this.decoder) {
      this.decoder.start_streaming();
    }
  }

  static stopStream() {
    if (this.decoder) {
      this.decoder.stop_streaming();
    }
  }

  static processAudio(audioData) {
    if (this.decoder) {
      this.decoder.process_audio_chunk(audioData, (word) => {
        self.postMessage({
          status: "streaming",
          word: word,
        });
      });
      self.postMessage({
        status: "chunk_processed",
      });
    }
  }
}

self.addEventListener("message", async (event) => {
  const { command } = event.data;

  try {
    switch (command) {
      case "initialize":
        const { weightsURL, modelID, tokenizerURL, mimiURL, configURL } =
          event.data;
        await MoshiASR.initialize({
          weightsURL,
          modelID,
          tokenizerURL,
          mimiURL,
          configURL,
        });
        break;

      case "start_stream":
        MoshiASR.startStream();
        break;

      case "stop_stream":
        MoshiASR.stopStream();
        break;

      case "process_audio":
        const { audioData } = event.data;
        MoshiASR.processAudio(audioData);
        break;

      default:
        self.postMessage({ error: "Unknown command: " + command });
    }
  } catch (e) {
    self.postMessage({ error: e.message });
  }
});