Spaces:
Sleeping
Sleeping
Update pages/ai_buddy.py
Browse files- pages/ai_buddy.py +270 -270
pages/ai_buddy.py
CHANGED
@@ -61,119 +61,119 @@ def get_ai_response(user_input, buddy_config, therapy_technique=None):
|
|
61 |
response = chat.invoke(messages).content
|
62 |
return response
|
63 |
|
64 |
-
|
65 |
-
while not stop_event.is_set():
|
66 |
-
playsound(sound_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
def play_sound_for_duration(sound_file, duration):
|
69 |
-
start_time = time.time()
|
70 |
-
while time.time() - start_time < duration:
|
71 |
-
playsound(sound_file, block=False)
|
72 |
-
time.sleep(0.1) # Short sleep to prevent excessive CPU usage
|
73 |
-
# Ensure the sound stops after the duration
|
74 |
-
pygame.mixer.quit()
|
75 |
-
|
76 |
-
def get_sound_files(directory):
|
77 |
-
return [f for f in os.listdir(directory) if f.endswith('.mp3')]
|
78 |
-
|
79 |
-
def get_sound_file_path(sound_name, sound_dir):
|
80 |
-
# Convert the sound name to a filename
|
81 |
-
filename = f"{sound_name.lower().replace(' ', '_')}.mp3"
|
82 |
-
return os.path.join(sound_dir, filename)
|
83 |
-
|
84 |
-
SOUND_OPTIONS = [
|
85 |
-
"Gentle Rain", "Ocean Waves", "Forest Ambience", "Soft Wind Chimes",
|
86 |
-
"Tibetan Singing Bowls", "Humming Song", "Crackling Fireplace",
|
87 |
-
"Birdsong", "White Noise", "Zen River", "Heartbeat", "Deep Space",
|
88 |
-
"Whale Songs", "Bamboo Flute", "Thunderstorm", "Cat Purring",
|
89 |
-
"Campfire", "Windchimes", "Waterfall", "Beach Waves", "Cicadas",
|
90 |
-
"Coffee Shop Ambience", "Grandfather Clock", "Rainstorm on Tent",
|
91 |
-
"Tropical Birds", "Subway Train", "Washing Machine", "Fan White Noise",
|
92 |
-
"Tibetan Bells", "Wind in Trees", "Meditation Bowl", "Meditation Bowl2", "Birds Singing Rainy Day"
|
93 |
-
]
|
94 |
-
|
95 |
-
def show_meditation_timer():
|
96 |
-
st.subheader("🧘♀️ Enhanced Meditation Timer")
|
97 |
-
|
98 |
-
sound_dir = os.path.join(os.path.dirname(__file__), "..", "sounds")
|
99 |
-
|
100 |
-
col1, col2 = st.columns(2)
|
101 |
-
|
102 |
-
with col1:
|
103 |
-
duration = st.slider("Select duration (minutes)", 1, 60, 5)
|
104 |
-
background_sound = st.selectbox("Background Sound", SOUND_OPTIONS)
|
105 |
-
|
106 |
-
with col2:
|
107 |
-
interval_options = ["None", "Every 5 minutes", "Every 10 minutes"]
|
108 |
-
interval_reminder = st.selectbox("Interval Reminders", interval_options)
|
109 |
-
end_sound = st.selectbox("End of Session Sound", SOUND_OPTIONS)
|
110 |
-
|
111 |
-
if st.button("Start Meditation", key="start_meditation"):
|
112 |
-
progress_bar = st.progress(0)
|
113 |
-
status_text = st.empty()
|
114 |
-
|
115 |
-
# Initialize pygame mixer
|
116 |
-
pygame.mixer.init()
|
117 |
-
|
118 |
-
# Load background sound
|
119 |
-
background_sound_file = get_sound_file_path(background_sound, sound_dir)
|
120 |
-
if not os.path.exists(background_sound_file):
|
121 |
-
st.error(f"Background sound file not found: {background_sound_file}")
|
122 |
-
return
|
123 |
-
|
124 |
-
# Load end of session sound
|
125 |
-
end_sound_file = get_sound_file_path(end_sound, sound_dir)
|
126 |
-
if not os.path.exists(end_sound_file):
|
127 |
-
st.error(f"End sound file not found: {end_sound_file}")
|
128 |
-
return
|
129 |
-
|
130 |
-
# Play background sound on loop
|
131 |
-
pygame.mixer.music.load(background_sound_file)
|
132 |
-
pygame.mixer.music.play(-1) # -1 means loop indefinitely
|
133 |
-
|
134 |
-
start_time = time.time()
|
135 |
-
end_time = start_time + (duration * 60)
|
136 |
-
|
137 |
-
try:
|
138 |
-
while time.time() < end_time:
|
139 |
-
elapsed_time = time.time() - start_time
|
140 |
-
progress = elapsed_time / (duration * 60)
|
141 |
-
progress_bar.progress(progress)
|
142 |
-
|
143 |
-
remaining_time = end_time - time.time()
|
144 |
-
mins, secs = divmod(int(remaining_time), 60)
|
145 |
-
status_text.text(f"Time remaining: {mins:02d}:{secs:02d}")
|
146 |
-
|
147 |
-
if interval_reminder != "None":
|
148 |
-
interval = 5 if interval_reminder == "Every 5 minutes" else 10
|
149 |
-
if int(elapsed_time) > 0 and int(elapsed_time) % (interval * 60) == 0:
|
150 |
-
st.toast(f"{interval} minutes passed", icon="⏰")
|
151 |
-
|
152 |
-
# Check if 10 seconds remaining
|
153 |
-
if remaining_time <= 10 and remaining_time > 9:
|
154 |
-
pygame.mixer.music.stop() # Stop background sound
|
155 |
-
pygame.mixer.Sound(end_sound_file).play() # Play end sound
|
156 |
-
|
157 |
-
if remaining_time <= 0:
|
158 |
-
break
|
159 |
-
|
160 |
-
time.sleep(0.1) # Update more frequently for smoother countdown
|
161 |
-
finally:
|
162 |
-
# Stop all sounds
|
163 |
-
pygame.mixer.quit()
|
164 |
-
|
165 |
-
# Ensure the progress bar is full and time remaining shows 00:00
|
166 |
-
progress_bar.progress(1.0)
|
167 |
-
status_text.text("Time remaining: 00:00")
|
168 |
-
|
169 |
-
st.success("Meditation complete!")
|
170 |
-
st.balloons()
|
171 |
-
|
172 |
-
if 'achievements' not in st.session_state:
|
173 |
-
st.session_state.achievements = set()
|
174 |
-
st.session_state.achievements.add("Zen Master")
|
175 |
-
st.success("Achievement Unlocked: Zen Master 🧘♀️")
|
176 |
-
'''
|
177 |
def show_personalized_recommendations():
|
178 |
st.subheader("🎯 Personalized Recommendations")
|
179 |
|
@@ -243,164 +243,164 @@ def show_personalized_recommendations():
|
|
243 |
for rec in additional_recs:
|
244 |
st.markdown(f"- {rec}")
|
245 |
|
246 |
-
|
247 |
-
t = np.linspace(0, duration_seconds, int(sample_rate * duration_seconds), False)
|
248 |
-
left_channel = np.sin(2 * np.pi * freq1 * t)
|
249 |
-
right_channel = np.sin(2 * np.pi * freq2 * t)
|
250 |
-
stereo_audio = np.vstack((left_channel, right_channel)).T
|
251 |
-
return (stereo_audio * 32767).astype(np.int16)
|
252 |
-
|
253 |
-
def get_binary_file_downloader_html(bin_file, file_label='File'):
|
254 |
-
b64 = base64.b64encode(bin_file).decode()
|
255 |
-
return f'<a href="data:application/octet-stream;base64,{b64}" download="{file_label}.wav" class="download-link">Download {file_label}</a>'
|
256 |
-
|
257 |
-
def show_binaural_beats():
|
258 |
-
st.subheader("🎵 Binaural Beats Generator")
|
259 |
-
|
260 |
-
st.markdown("""
|
261 |
-
<style>
|
262 |
-
.stButton>button {
|
263 |
-
background-color: #4CAF50;
|
264 |
-
color: white;
|
265 |
-
font-weight: bold;
|
266 |
-
}
|
267 |
-
.download-link {
|
268 |
-
background-color: #008CBA;
|
269 |
-
color: white;
|
270 |
-
padding: 10px 15px;
|
271 |
-
text-align: center;
|
272 |
-
text-decoration: none;
|
273 |
-
display: inline-block;
|
274 |
-
font-size: 16px;
|
275 |
-
margin: 4px 2px;
|
276 |
-
cursor: pointer;
|
277 |
-
border-radius: 4px;
|
278 |
-
}
|
279 |
-
.stop-button {
|
280 |
-
background-color: #f44336;
|
281 |
-
color: white;
|
282 |
-
font-weight: bold;
|
283 |
-
}
|
284 |
-
</style>
|
285 |
-
""", unsafe_allow_html=True)
|
286 |
-
|
287 |
-
st.write("Binaural beats are created when two slightly different frequencies are played in each ear, potentially influencing brainwave activity.")
|
288 |
-
|
289 |
-
preset_beats = {
|
290 |
-
"Deep Relaxation (Delta)": {"base": 100, "beat": 2},
|
291 |
-
"Meditation (Theta)": {"base": 150, "beat": 6},
|
292 |
-
"Relaxation (Alpha)": {"base": 200, "beat": 10},
|
293 |
-
"Light Focus (Low Beta)": {"base": 250, "beat": 14},
|
294 |
-
"High Focus (Mid Beta)": {"base": 300, "beat": 20},
|
295 |
-
"Alertness (High Beta)": {"base": 350, "beat": 30},
|
296 |
-
"Gamma Consciousness": {"base": 400, "beat": 40},
|
297 |
-
"Lucid Dreaming": {"base": 180, "beat": 3},
|
298 |
-
"Memory Enhancement": {"base": 270, "beat": 12},
|
299 |
-
"Creativity Boost": {"base": 220, "beat": 8},
|
300 |
-
"Pain Relief": {"base": 130, "beat": 4},
|
301 |
-
"Mood Elevation": {"base": 315, "beat": 18}
|
302 |
-
}
|
303 |
-
|
304 |
-
col1, col2 = st.columns(2)
|
305 |
-
|
306 |
-
with col1:
|
307 |
-
beat_type = st.selectbox("Choose a preset or custom:", ["Custom"] + list(preset_beats.keys()))
|
308 |
-
|
309 |
-
with col2:
|
310 |
-
duration = st.slider("Duration (minutes):", 1, 60, 15)
|
311 |
-
|
312 |
-
if beat_type == "Custom":
|
313 |
-
col3, col4 = st.columns(2)
|
314 |
-
with col3:
|
315 |
-
base_freq = st.slider("Base Frequency (Hz):", 100, 500, 200)
|
316 |
-
with col4:
|
317 |
-
beat_freq = st.slider("Desired Beat Frequency (Hz):", 1, 40, 10)
|
318 |
-
else:
|
319 |
-
base_freq = preset_beats[beat_type]["base"]
|
320 |
-
beat_freq = preset_beats[beat_type]["beat"]
|
321 |
-
st.info(f"Base Frequency: {base_freq} Hz, Beat Frequency: {beat_freq} Hz")
|
322 |
-
|
323 |
-
if 'audio_playing' not in st.session_state:
|
324 |
-
st.session_state.audio_playing = False
|
325 |
-
|
326 |
-
if 'start_time' not in st.session_state:
|
327 |
-
st.session_state.start_time = None
|
328 |
-
|
329 |
-
if 'end_time' not in st.session_state:
|
330 |
-
st.session_state.end_time = None
|
331 |
-
|
332 |
-
# Create persistent placeholders for UI elements
|
333 |
-
progress_bar = st.empty()
|
334 |
-
status_text = st.empty()
|
335 |
-
stop_button = st.empty()
|
336 |
-
|
337 |
-
generate_button = st.button("Generate and Play Binaural Beat")
|
338 |
-
|
339 |
-
if generate_button:
|
340 |
-
try:
|
341 |
-
# Stop any currently playing audio
|
342 |
-
if st.session_state.audio_playing:
|
343 |
-
pygame.mixer.music.stop()
|
344 |
-
st.session_state.audio_playing = False
|
345 |
-
|
346 |
-
audio_data = generate_binaural_beat(base_freq, base_freq + beat_freq, duration * 60)
|
347 |
-
|
348 |
-
# Save the generated audio to a temporary file
|
349 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_file:
|
350 |
-
temp_filename = temp_file.name
|
351 |
-
wavfile.write(temp_filename, 44100, audio_data)
|
352 |
-
|
353 |
-
# Initialize pygame mixer
|
354 |
-
pygame.mixer.init(frequency=44100, size=-16, channels=2)
|
355 |
-
|
356 |
-
# Load and play the audio
|
357 |
-
pygame.mixer.music.load(temp_filename)
|
358 |
-
pygame.mixer.music.play()
|
359 |
-
st.session_state.audio_playing = True
|
360 |
-
|
361 |
-
st.session_state.start_time = time.time()
|
362 |
-
st.session_state.end_time = st.session_state.start_time + (duration * 60)
|
363 |
-
|
364 |
-
except Exception as e:
|
365 |
-
st.error(f"An error occurred: {str(e)}")
|
366 |
-
st.info("Please try again or contact support if the issue persists.")
|
367 |
-
|
368 |
-
if st.session_state.audio_playing:
|
369 |
-
stop_button_active = stop_button.button("Stop Binaural Beat", key="stop_binaural", type="primary")
|
370 |
-
current_time = time.time()
|
371 |
-
|
372 |
-
if stop_button_active:
|
373 |
-
pygame.mixer.music.stop()
|
374 |
-
st.session_state.audio_playing = False
|
375 |
-
st.session_state.start_time = None
|
376 |
-
st.session_state.end_time = None
|
377 |
-
|
378 |
-
elif current_time < st.session_state.end_time:
|
379 |
-
elapsed_time = current_time - st.session_state.start_time
|
380 |
-
progress = elapsed_time / (st.session_state.end_time - st.session_state.start_time)
|
381 |
-
progress_bar.progress(progress)
|
382 |
-
|
383 |
-
remaining_time = st.session_state.end_time - current_time
|
384 |
-
mins, secs = divmod(int(remaining_time), 60)
|
385 |
-
status_text.text(f"Time remaining: {mins:02d}:{secs:02d}")
|
386 |
-
else:
|
387 |
-
pygame.mixer.music.stop()
|
388 |
-
st.session_state.audio_playing = False
|
389 |
-
st.session_state.start_time = None
|
390 |
-
st.session_state.end_time = None
|
391 |
-
progress_bar.empty()
|
392 |
-
status_text.text("Binaural beat session complete!")
|
393 |
-
|
394 |
-
# Offer download of the generated audio
|
395 |
-
if not st.session_state.audio_playing and 'audio_data' in locals():
|
396 |
-
with io.BytesIO() as buffer:
|
397 |
-
wavfile.write(buffer, 44100, audio_data)
|
398 |
-
st.markdown(get_binary_file_downloader_html(buffer.getvalue(), f"binaural_beat_{base_freq}_{beat_freq}Hz"), unsafe_allow_html=True)
|
399 |
-
|
400 |
-
# Ensure the app updates every second
|
401 |
-
if st.session_state.audio_playing:
|
402 |
-
time.sleep(1)
|
403 |
-
st.experimental_rerun()
|
404 |
|
405 |
def main():
|
406 |
st.set_page_config(page_title="S.H.E.R.L.O.C.K. AI Buddy", page_icon="🕵️", layout="wide")
|
|
|
61 |
response = chat.invoke(messages).content
|
62 |
return response
|
63 |
|
64 |
+
#def play_sound_loop(sound_file, stop_event):
|
65 |
+
# while not stop_event.is_set():
|
66 |
+
# playsound(sound_file)
|
67 |
+
#
|
68 |
+
#def play_sound_for_duration(sound_file, duration):
|
69 |
+
# start_time = time.time()
|
70 |
+
# while time.time() - start_time < duration:
|
71 |
+
# playsound(sound_file, block=False)
|
72 |
+
# time.sleep(0.1) # Short sleep to prevent excessive CPU usage
|
73 |
+
# # Ensure the sound stops after the duration
|
74 |
+
# pygame.mixer.quit()
|
75 |
+
#
|
76 |
+
#def get_sound_files(directory):
|
77 |
+
# return [f for f in os.listdir(directory) if f.endswith('.mp3')]
|
78 |
+
#
|
79 |
+
#def get_sound_file_path(sound_name, sound_dir):
|
80 |
+
# # Convert the sound name to a filename
|
81 |
+
# filename = f"{sound_name.lower().replace(' ', '_')}.mp3"
|
82 |
+
# return os.path.join(sound_dir, filename)
|
83 |
+
#
|
84 |
+
#SOUND_OPTIONS = [
|
85 |
+
# "Gentle Rain", "Ocean Waves", "Forest Ambience", "Soft Wind Chimes",
|
86 |
+
# "Tibetan Singing Bowls", "Humming Song", "Crackling Fireplace",
|
87 |
+
# "Birdsong", "White Noise", "Zen River", "Heartbeat", "Deep Space",
|
88 |
+
# "Whale Songs", "Bamboo Flute", "Thunderstorm", "Cat Purring",
|
89 |
+
# "Campfire", "Windchimes", "Waterfall", "Beach Waves", "Cicadas",
|
90 |
+
# "Coffee Shop Ambience", "Grandfather Clock", "Rainstorm on Tent",
|
91 |
+
# "Tropical Birds", "Subway Train", "Washing Machine", "Fan White Noise",
|
92 |
+
# "Tibetan Bells", "Wind in Trees", "Meditation Bowl", "Meditation Bowl2", "Birds Singing Rainy Day"
|
93 |
+
#]
|
94 |
+
#
|
95 |
+
#def show_meditation_timer():
|
96 |
+
# st.subheader("🧘♀️ Enhanced Meditation Timer")
|
97 |
+
#
|
98 |
+
# sound_dir = os.path.join(os.path.dirname(__file__), "..", "sounds")
|
99 |
+
#
|
100 |
+
# col1, col2 = st.columns(2)
|
101 |
+
#
|
102 |
+
# with col1:
|
103 |
+
# duration = st.slider("Select duration (minutes)", 1, 60, 5)
|
104 |
+
# background_sound = st.selectbox("Background Sound", SOUND_OPTIONS)
|
105 |
+
#
|
106 |
+
# with col2:
|
107 |
+
# interval_options = ["None", "Every 5 minutes", "Every 10 minutes"]
|
108 |
+
# interval_reminder = st.selectbox("Interval Reminders", interval_options)
|
109 |
+
# end_sound = st.selectbox("End of Session Sound", SOUND_OPTIONS)
|
110 |
+
#
|
111 |
+
# if st.button("Start Meditation", key="start_meditation"):
|
112 |
+
# progress_bar = st.progress(0)
|
113 |
+
# status_text = st.empty()
|
114 |
+
#
|
115 |
+
# # Initialize pygame mixer
|
116 |
+
# pygame.mixer.init()
|
117 |
+
#
|
118 |
+
# # Load background sound
|
119 |
+
# background_sound_file = get_sound_file_path(background_sound, sound_dir)
|
120 |
+
# if not os.path.exists(background_sound_file):
|
121 |
+
# st.error(f"Background sound file not found: {background_sound_file}")
|
122 |
+
# return
|
123 |
+
#
|
124 |
+
# # Load end of session sound
|
125 |
+
# end_sound_file = get_sound_file_path(end_sound, sound_dir)
|
126 |
+
# if not os.path.exists(end_sound_file):
|
127 |
+
# st.error(f"End sound file not found: {end_sound_file}")
|
128 |
+
# return
|
129 |
+
#
|
130 |
+
# # Play background sound on loop
|
131 |
+
# pygame.mixer.music.load(background_sound_file)
|
132 |
+
# pygame.mixer.music.play(-1) # -1 means loop indefinitely
|
133 |
+
#
|
134 |
+
# start_time = time.time()
|
135 |
+
# end_time = start_time + (duration * 60)
|
136 |
+
#
|
137 |
+
# try:
|
138 |
+
# while time.time() < end_time:
|
139 |
+
# elapsed_time = time.time() - start_time
|
140 |
+
# progress = elapsed_time / (duration * 60)
|
141 |
+
# progress_bar.progress(progress)
|
142 |
+
#
|
143 |
+
# remaining_time = end_time - time.time()
|
144 |
+
# mins, secs = divmod(int(remaining_time), 60)
|
145 |
+
# status_text.text(f"Time remaining: {mins:02d}:{secs:02d}")
|
146 |
+
#
|
147 |
+
# if interval_reminder != "None":
|
148 |
+
# interval = 5 if interval_reminder == "Every 5 minutes" else 10
|
149 |
+
# if int(elapsed_time) > 0 and int(elapsed_time) % (interval * 60) == 0:
|
150 |
+
# st.toast(f"{interval} minutes passed", icon="⏰")
|
151 |
+
#
|
152 |
+
# # Check if 10 seconds remaining
|
153 |
+
# if remaining_time <= 10 and remaining_time > 9:
|
154 |
+
# pygame.mixer.music.stop() # Stop background sound
|
155 |
+
# pygame.mixer.Sound(end_sound_file).play() # Play end sound
|
156 |
+
#
|
157 |
+
# if remaining_time <= 0:
|
158 |
+
# break
|
159 |
+
#
|
160 |
+
# time.sleep(0.1) # Update more frequently for smoother countdown
|
161 |
+
# finally:
|
162 |
+
# # Stop all sounds
|
163 |
+
# pygame.mixer.quit()
|
164 |
+
#
|
165 |
+
# # Ensure the progress bar is full and time remaining shows 00:00
|
166 |
+
# progress_bar.progress(1.0)
|
167 |
+
# status_text.text("Time remaining: 00:00")
|
168 |
+
#
|
169 |
+
# st.success("Meditation complete!")
|
170 |
+
# st.balloons()
|
171 |
+
#
|
172 |
+
# if 'achievements' not in st.session_state:
|
173 |
+
# st.session_state.achievements = set()
|
174 |
+
# st.session_state.achievements.add("Zen Master")
|
175 |
+
# st.success("Achievement Unlocked: Zen Master 🧘♀️")
|
176 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
def show_personalized_recommendations():
|
178 |
st.subheader("🎯 Personalized Recommendations")
|
179 |
|
|
|
243 |
for rec in additional_recs:
|
244 |
st.markdown(f"- {rec}")
|
245 |
|
246 |
+
#def generate_binaural_beat(freq1, freq2, duration_seconds, sample_rate=44100):
|
247 |
+
# t = np.linspace(0, duration_seconds, int(sample_rate * duration_seconds), False)
|
248 |
+
# left_channel = np.sin(2 * np.pi * freq1 * t)
|
249 |
+
# right_channel = np.sin(2 * np.pi * freq2 * t)
|
250 |
+
# stereo_audio = np.vstack((left_channel, right_channel)).T
|
251 |
+
# return (stereo_audio * 32767).astype(np.int16)
|
252 |
+
#
|
253 |
+
#def get_binary_file_downloader_html(bin_file, file_label='File'):
|
254 |
+
# b64 = base64.b64encode(bin_file).decode()
|
255 |
+
# return f'<a href="data:application/octet-stream;base64,{b64}" download="{file_label}.wav" class="download-link">Download {file_label}</a>'
|
256 |
+
#
|
257 |
+
#def show_binaural_beats():
|
258 |
+
# st.subheader("🎵 Binaural Beats Generator")
|
259 |
+
#
|
260 |
+
# st.markdown("""
|
261 |
+
# <style>
|
262 |
+
# .stButton>button {
|
263 |
+
# background-color: #4CAF50;
|
264 |
+
# color: white;
|
265 |
+
# font-weight: bold;
|
266 |
+
# }
|
267 |
+
# .download-link {
|
268 |
+
# background-color: #008CBA;
|
269 |
+
# color: white;
|
270 |
+
# padding: 10px 15px;
|
271 |
+
# text-align: center;
|
272 |
+
# text-decoration: none;
|
273 |
+
# display: inline-block;
|
274 |
+
# font-size: 16px;
|
275 |
+
# margin: 4px 2px;
|
276 |
+
# cursor: pointer;
|
277 |
+
# border-radius: 4px;
|
278 |
+
# }
|
279 |
+
# .stop-button {
|
280 |
+
# background-color: #f44336;
|
281 |
+
# color: white;
|
282 |
+
# font-weight: bold;
|
283 |
+
# }
|
284 |
+
# </style>
|
285 |
+
# """, unsafe_allow_html=True)
|
286 |
+
#
|
287 |
+
# st.write("Binaural beats are created when two slightly different frequencies are played in each ear, potentially influencing brainwave activity.")
|
288 |
+
#
|
289 |
+
# preset_beats = {
|
290 |
+
# "Deep Relaxation (Delta)": {"base": 100, "beat": 2},
|
291 |
+
# "Meditation (Theta)": {"base": 150, "beat": 6},
|
292 |
+
# "Relaxation (Alpha)": {"base": 200, "beat": 10},
|
293 |
+
# "Light Focus (Low Beta)": {"base": 250, "beat": 14},
|
294 |
+
# "High Focus (Mid Beta)": {"base": 300, "beat": 20},
|
295 |
+
# "Alertness (High Beta)": {"base": 350, "beat": 30},
|
296 |
+
# "Gamma Consciousness": {"base": 400, "beat": 40},
|
297 |
+
# "Lucid Dreaming": {"base": 180, "beat": 3},
|
298 |
+
# "Memory Enhancement": {"base": 270, "beat": 12},
|
299 |
+
# "Creativity Boost": {"base": 220, "beat": 8},
|
300 |
+
# "Pain Relief": {"base": 130, "beat": 4},
|
301 |
+
# "Mood Elevation": {"base": 315, "beat": 18}
|
302 |
+
# }
|
303 |
+
#
|
304 |
+
# col1, col2 = st.columns(2)
|
305 |
+
#
|
306 |
+
# with col1:
|
307 |
+
# beat_type = st.selectbox("Choose a preset or custom:", ["Custom"] + list(preset_beats.keys()))
|
308 |
+
#
|
309 |
+
# with col2:
|
310 |
+
# duration = st.slider("Duration (minutes):", 1, 60, 15)
|
311 |
+
#
|
312 |
+
# if beat_type == "Custom":
|
313 |
+
# col3, col4 = st.columns(2)
|
314 |
+
# with col3:
|
315 |
+
# base_freq = st.slider("Base Frequency (Hz):", 100, 500, 200)
|
316 |
+
# with col4:
|
317 |
+
# beat_freq = st.slider("Desired Beat Frequency (Hz):", 1, 40, 10)
|
318 |
+
# else:
|
319 |
+
# base_freq = preset_beats[beat_type]["base"]
|
320 |
+
# beat_freq = preset_beats[beat_type]["beat"]
|
321 |
+
# st.info(f"Base Frequency: {base_freq} Hz, Beat Frequency: {beat_freq} Hz")
|
322 |
+
#
|
323 |
+
# if 'audio_playing' not in st.session_state:
|
324 |
+
# st.session_state.audio_playing = False
|
325 |
+
#
|
326 |
+
# if 'start_time' not in st.session_state:
|
327 |
+
# st.session_state.start_time = None
|
328 |
+
#
|
329 |
+
# if 'end_time' not in st.session_state:
|
330 |
+
# st.session_state.end_time = None
|
331 |
+
#
|
332 |
+
# # Create persistent placeholders for UI elements
|
333 |
+
# progress_bar = st.empty()
|
334 |
+
# status_text = st.empty()
|
335 |
+
# stop_button = st.empty()
|
336 |
+
#
|
337 |
+
# generate_button = st.button("Generate and Play Binaural Beat")
|
338 |
+
#
|
339 |
+
# if generate_button:
|
340 |
+
# try:
|
341 |
+
# # Stop any currently playing audio
|
342 |
+
# if st.session_state.audio_playing:
|
343 |
+
# pygame.mixer.music.stop()
|
344 |
+
# st.session_state.audio_playing = False
|
345 |
+
#
|
346 |
+
# audio_data = generate_binaural_beat(base_freq, base_freq + beat_freq, duration * 60)
|
347 |
+
#
|
348 |
+
# # Save the generated audio to a temporary file
|
349 |
+
# with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_file:
|
350 |
+
# temp_filename = temp_file.name
|
351 |
+
# wavfile.write(temp_filename, 44100, audio_data)
|
352 |
+
#
|
353 |
+
# # Initialize pygame mixer
|
354 |
+
# pygame.mixer.init(frequency=44100, size=-16, channels=2)
|
355 |
+
#
|
356 |
+
# # Load and play the audio
|
357 |
+
# pygame.mixer.music.load(temp_filename)
|
358 |
+
# pygame.mixer.music.play()
|
359 |
+
# st.session_state.audio_playing = True
|
360 |
+
#
|
361 |
+
# st.session_state.start_time = time.time()
|
362 |
+
# st.session_state.end_time = st.session_state.start_time + (duration * 60)
|
363 |
+
#
|
364 |
+
# except Exception as e:
|
365 |
+
# st.error(f"An error occurred: {str(e)}")
|
366 |
+
# st.info("Please try again or contact support if the issue persists.")
|
367 |
+
#
|
368 |
+
# if st.session_state.audio_playing:
|
369 |
+
# stop_button_active = stop_button.button("Stop Binaural Beat", key="stop_binaural", type="primary")
|
370 |
+
# current_time = time.time()
|
371 |
+
#
|
372 |
+
# if stop_button_active:
|
373 |
+
# pygame.mixer.music.stop()
|
374 |
+
# st.session_state.audio_playing = False
|
375 |
+
# st.session_state.start_time = None
|
376 |
+
# st.session_state.end_time = None
|
377 |
+
#
|
378 |
+
# elif current_time < st.session_state.end_time:
|
379 |
+
# elapsed_time = current_time - st.session_state.start_time
|
380 |
+
# progress = elapsed_time / (st.session_state.end_time - st.session_state.start_time)
|
381 |
+
# progress_bar.progress(progress)
|
382 |
+
#
|
383 |
+
# remaining_time = st.session_state.end_time - current_time
|
384 |
+
# mins, secs = divmod(int(remaining_time), 60)
|
385 |
+
# status_text.text(f"Time remaining: {mins:02d}:{secs:02d}")
|
386 |
+
# else:
|
387 |
+
# pygame.mixer.music.stop()
|
388 |
+
# st.session_state.audio_playing = False
|
389 |
+
# st.session_state.start_time = None
|
390 |
+
# st.session_state.end_time = None
|
391 |
+
# progress_bar.empty()
|
392 |
+
# status_text.text("Binaural beat session complete!")
|
393 |
+
#
|
394 |
+
# # Offer download of the generated audio
|
395 |
+
# if not st.session_state.audio_playing and 'audio_data' in locals():
|
396 |
+
# with io.BytesIO() as buffer:
|
397 |
+
# wavfile.write(buffer, 44100, audio_data)
|
398 |
+
# st.markdown(get_binary_file_downloader_html(buffer.getvalue(), f"binaural_beat_{base_freq}_{beat_freq}Hz"), unsafe_allow_html=True)
|
399 |
+
#
|
400 |
+
# # Ensure the app updates every second
|
401 |
+
# if st.session_state.audio_playing:
|
402 |
+
# time.sleep(1)
|
403 |
+
# st.experimental_rerun()
|
404 |
|
405 |
def main():
|
406 |
st.set_page_config(page_title="S.H.E.R.L.O.C.K. AI Buddy", page_icon="🕵️", layout="wide")
|