Skip to content

Commit cc096ea

Browse files
authored
Merge pull request fac29b#60 from fac29b/audio-caching
Audio caching
2 parents e2be098 + 7570873 commit cc096ea

5 files changed

Lines changed: 79 additions & 8 deletions

File tree

public/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ <h1 aria-label="app text" class="headline">
126126

127127
<div class="test">
128128
<div class="recording">
129+
<source class="audio_source" type="audio/mpeg">
129130
<i class="fa-solid fa-microphone" name="microphone"></i>
130131
<i class="fa-solid fa-pause" name="pause"></i>
131132
<i class="fa-solid fa-stop" name="stop"></i>
@@ -134,6 +135,23 @@ <h1 aria-label="app text" class="headline">
134135
<input type="number" name="speed" id="speed" min="0.25" max="2" step="0.25" value="1">
135136
</div>
136137
</div>
138+
<!-- <div class="audio-container">
139+
<audio class="recording" controls>
140+
<source class="audio_source" src="" type="audio/mpeg">
141+
Your browser does not support the audio element.
142+
</audio>
143+
<div class="controls">
144+
<i class="fa-solid fa-microphone" name="microphone"></i>
145+
<i class="fa-solid fa-pause" name="pause"></i>
146+
<i class="fa-solid fa-stop" name="stop"></i>
147+
<div class="speed-wrapper">
148+
<label for="speed">Speed</label>
149+
<input type="number" name="speed" id="speed" min="0.25" max="2" step="0.25" value="1">
150+
</div>
151+
</div>
152+
</div> -->
153+
154+
137155
<div id="loading-container">
138156
<div id="loading-indicator"></div>
139157
<h3 id="loading-text">Creating audio and image...</h3>

public/index.js

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,17 @@ import {
5252
emailUserRecipeSection,
5353
} from "./js_utilities/query_selector.js";
5454

55-
let audioElement;
55+
56+
let audioElement = new Audio();
57+
5658
let emailObject
5759

60+
const audio_source = document.querySelector(".audio_source");
61+
62+
console.log(audio_source)
63+
64+
65+
5866

5967

6068
wantToTakeAPicture.addEventListener("click", () => {
@@ -169,6 +177,33 @@ recipeButtons.forEach((button) => {
169177
console.log(userRecipe);
170178

171179
const CACHE_NAME = 'image-cache-v1';
180+
const CACHE_NAME_AUDIO = 'image-cache-v2'
181+
182+
183+
// Function to cache the audio (without fetching the audio)
184+
async function cacheAudio(audio) {
185+
const cache = await caches.open(CACHE_NAME_AUDIO);
186+
const response = new Response(JSON.stringify({audio, timestamp: Date.now()}));
187+
await cache.put("last-generated-audio", response);
188+
}
189+
190+
// Funnction to get the cached audio data
191+
async function getCachedAudio() {
192+
const cache = await caches.open(CACHE_NAME_AUDIO);
193+
const response = await cache.match("last-generated-audio");
194+
if (response) {
195+
const data = await response.json();
196+
const now = Date.now();
197+
if (now - data.timestamp < 24 * 60 * 60 * 1000) {
198+
return data.url;
199+
} else {
200+
await cache.delete("last-generated-audio")
201+
}
202+
}
203+
return null
204+
}
205+
206+
172207

173208
// Function to cache the image URL (without fetching the image)
174209
async function cacheImageUrl(url) {
@@ -218,15 +253,21 @@ recipeButtons.forEach((button) => {
218253
}
219254

220255
if (data.audio) {
256+
221257
console.log(data.audio);
222-
loadingText.innerHTML = "Hang in there creating the audio and image...";
258+
259+
const audio_data = createAudio(data.audio);
260+
console.log(`line 261: ${audio_data}`)
261+
// Cache the url object
262+
await cacheAudio(audio_data)
263+
223264
displayElementsFlex([recording]);
224265
displayElements([sendRecipeToUserInboxBtn, userWantAnotherRecipe]);
225266

226267
const speechBtns = Array.from(document.querySelectorAll(".fa-solid"));
227268
const speedBtn = document.querySelector("#speed");
228269

229-
audioElement = createAudio(data.audio);
270+
audioElement.src = createAudio(data.audio);
230271

231272
audioElement.stop = function () {
232273
this.pause();
@@ -235,7 +276,7 @@ recipeButtons.forEach((button) => {
235276

236277
userWantAnotherRecipe.addEventListener("click", () => {
237278
displayElements([headline, allergies, ...recipeButtons, mainElement]);
238-
removeElements([userText, emailSection]);
279+
removeElements([userText, emailSection, recording]);
239280
emptyTheElement(gptResponseElement);
240281
resetCheckedStateToFalse(dietaryRequirements);
241282
userText.value = "";
@@ -269,10 +310,20 @@ recipeButtons.forEach((button) => {
269310
await cacheImageUrl(imageUrl);
270311
backgroundImg.addEventListener("load", () => {
271312
backgroundImg.src = imageUrl;
313+
loadingText.textContent = "Hang in there creating the audio...";
314+
272315
})
273316
}
274317
};
275318

319+
// Before setting up the eventSource, check for a cached audio
320+
async function loadCachedAudio() {
321+
const cachedAudio = await getCachedAudio();
322+
if (cachedAudio) {
323+
audioElement.src = cachedAudio;
324+
}
325+
}
326+
276327
// Before setting up the eventSource, check for a cached image URL
277328
async function loadCachedImage() {
278329
const cachedImageUrl = await getCachedImageUrl();
@@ -283,6 +334,7 @@ async function loadCachedImage() {
283334

284335
// Call this function before setting up the eventSource
285336
loadCachedImage();
337+
loadCachedAudio();
286338
});
287339
});
288340

public/js_utilities/functions_and_variables.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ function createAudio(data) {
143143
}
144144

145145
const audioBlob = new Blob([audioData], { type: "audio/mpeg" });
146-
const audioElement = new Audio();
147-
audioElement.src = URL.createObjectURL(audioBlob);
148-
return audioElement;
146+
return URL.createObjectURL(audioBlob)
147+
// const audioElement = new Audio();
148+
// audioElement.src = URL.createObjectURL(audioBlob);
149+
// return audioElement;
149150

150151
}
151152

public/url_folder.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
https://oaidalleapiprodscus.blob.core.windows.net/private/org-nYbqgo3O0LNnYYAoKAmApBfx/user-58Je7efi0iy880e2UYCdYpBm/img-7QfOfKjbs44oxr2AcVgWr2eb.png?st=2024-07-24T14%3A17%3A08Z&se=2024-07-24T16%3A17%3A08Z&sp=r&sv=2023-11-03&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-07-23T22%3A52%3A27Z&ske=2024-07-24T22%3A52%3A27Z&sks=b&skv=2023-11-03&sig=uhjzjezOQlgWwMf6NMQFq40rv0IOlE2BFDshIM0IiX8%3D
1+
https://oaidalleapiprodscus.blob.core.windows.net/private/org-nYbqgo3O0LNnYYAoKAmApBfx/user-58Je7efi0iy880e2UYCdYpBm/img-wXGzIn0FQALmWRTlYIzucreH.png?st=2024-07-26T15%3A35%3A54Z&se=2024-07-26T17%3A35%3A54Z&sp=r&sv=2023-11-03&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-07-25T23%3A14%3A25Z&ske=2024-07-26T23%3A14%3A25Z&sks=b&skv=2023-11-03&sig=hY%2B55WDAQiPx4ga97zVQEZZHXcM3DVFJAX2ZLgGNcvA%3D

speech.mp3

935 KB
Binary file not shown.

0 commit comments

Comments
 (0)