timgremore commited on
Commit
6b2e0a9
·
1 Parent(s): 2e56172

fix: Start AudioContext in response to user action

Browse files

Browsers don't permit initializing an AudioContext unless it is in response to a user action.

assets/js/app.js CHANGED
@@ -61,25 +61,6 @@ Hooks.AudioRecorder = {
61
  async mounted() {
62
  let audioBuffer = [];
63
 
64
- audioContext = new AudioContext({ sampleRate: 16_000 });
65
-
66
- await audioContext.audioWorklet.addModule("/assets/pcm-processor.js");
67
-
68
- pcmNode = new AudioWorkletNode(audioContext, "pcm-processor", {
69
- processorOptions: { chunkSize: 16_000 },
70
- });
71
-
72
- pcmNode.port.onmessage = (event) => {
73
- audioBuffer.push(event.data);
74
-
75
- if (audioBuffer.length >= 160_000) {
76
- const data = audioBuffer.splice(0, 160_000);
77
- const res = this.pushEvent("audio_chunk", { data });
78
-
79
- if (!res) audioContext.suspend();
80
- }
81
- };
82
-
83
  this.handleEvent("start_audio_recording", async () => {
84
  console.log("starting audio recording");
85
 
@@ -123,8 +104,9 @@ Hooks.AudioRecorder = {
123
 
124
  this.handleEvent("stop_audio_recording", async () => {
125
  console.log("Stopping audio recording");
126
- audioContext.suspend();
127
  this.pushEvent("audio_chunk", { data: audioBuffer });
 
128
  });
129
  },
130
 
 
61
  async mounted() {
62
  let audioBuffer = [];
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  this.handleEvent("start_audio_recording", async () => {
65
  console.log("starting audio recording");
66
 
 
104
 
105
  this.handleEvent("stop_audio_recording", async () => {
106
  console.log("Stopping audio recording");
107
+ if (audioContext.state == "running") audioContext.suspend();
108
  this.pushEvent("audio_chunk", { data: audioBuffer });
109
+ audioBuffer = [];
110
  });
111
  },
112
 
lib/medicode_web/components/sidebar_component.ex CHANGED
@@ -139,13 +139,14 @@ defmodule MedicodeWeb.Components.SidebarComponent do
139
  status: :recording
140
  })
141
 
142
- transcriptions = list_transcriptions(socket.assigns.current_user)
143
-
144
  Medicode.TranscriptionSupervisor.start_transcription(new_transcription)
145
 
 
 
146
  socket
147
  |> assign(:status, :streaming_audio)
148
  |> assign(:transcriptions, transcriptions)
 
149
  |> push_patch(to: ~p"/transcriptions/#{new_transcription.id}")
150
  end
151
 
 
139
  status: :recording
140
  })
141
 
 
 
142
  Medicode.TranscriptionSupervisor.start_transcription(new_transcription)
143
 
144
+ transcriptions = list_transcriptions(socket.assigns.current_user)
145
+
146
  socket
147
  |> assign(:status, :streaming_audio)
148
  |> assign(:transcriptions, transcriptions)
149
+ |> push_event("start_audio_recording", %{})
150
  |> push_patch(to: ~p"/transcriptions/#{new_transcription.id}")
151
  end
152
 
lib/medicode_web/live/transcriptions_live/show.ex CHANGED
@@ -55,7 +55,8 @@ defmodule MedicodeWeb.TranscriptionsLive.Show do
55
  |> stream(:chunk_ids, transcription_chunk_ids, reset: true)
56
 
57
  if transcription.status == :recording do
58
- push_event(socket, "start_audio_recording", %{})
 
59
  else
60
  socket
61
  end
 
55
  |> stream(:chunk_ids, transcription_chunk_ids, reset: true)
56
 
57
  if transcription.status == :recording do
58
+ socket
59
+ # push_event(socket, "start_audio_recording", %{})
60
  else
61
  socket
62
  end