Last active
March 29, 2023 03:40
-
-
Save AndrewBarfield/159254b37d1d982f7f8b6bbce7154ffe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Binaural Beats Generator | |
# | |
# Streamlit applet to generate binaural beats. | |
# User can set Duration (seconds), Carrier Frequency (Hz), and Beat Frequency (Hz). | |
# Generated WAV file can be downloaded from link below graphs. | |
# | |
import streamlit as st | |
import numpy as np | |
import scipy.io.wavfile | |
from scipy.signal import windows | |
from io import BytesIO | |
import matplotlib.pyplot as plt | |
import base64 | |
def generate_binaural_beats(duration, carrier_frequency, beat_frequency, sample_rate, amplitude): | |
time = np.linspace(0, duration, duration * sample_rate) | |
left_wave = amplitude * \ | |
np.sin(2 * np.pi * (carrier_frequency - beat_frequency / 2) * time) | |
right_wave = amplitude * \ | |
np.sin(2 * np.pi * (carrier_frequency + beat_frequency / 2) * time) | |
return left_wave, right_wave | |
def save_wav_file(sample_rate, left_wave, right_wave, file_name): | |
stereo_data = np.array([left_wave, right_wave]).T | |
scipy.io.wavfile.write(file_name, sample_rate, | |
stereo_data.astype(np.float32)) | |
st.title("Binaural Beats Generator") | |
duration = st.slider("Duration (seconds)", min_value=1, | |
max_value=60, value=10, step=1) | |
carrier_frequency = st.slider( | |
"Carrier Frequency (Hz)", min_value=20, max_value=1000, value=440, step=1) | |
beat_frequency = st.slider("Beat Frequency (Hz)", | |
min_value=1, max_value=50, value=10, step=1) | |
if st.button("Generate Binaural Beats"): | |
sample_rate = 44100 | |
amplitude = 0.5 | |
left_wave, right_wave = generate_binaural_beats( | |
duration, carrier_frequency, beat_frequency, sample_rate, amplitude) | |
save_wav_file(sample_rate, left_wave, right_wave, "binaural_beats.wav") | |
st.success("Binaural beats generated successfully!") | |
# Plot the first second of the left and right waves | |
time = np.linspace(0, 1, sample_rate) | |
fig, ax = plt.subplots(2, 1, figsize=(10, 6), sharex=True) | |
ax[0].plot(time, left_wave[:sample_rate], label="Left Channel") | |
ax[0].legend() | |
ax[0].grid() | |
ax[1].plot(time, right_wave[:sample_rate], | |
label="Right Channel", color="r") | |
ax[1].legend() | |
ax[1].grid() | |
ax[1].set_xlabel("Time (s)") | |
st.pyplot(fig) | |
# Create a download link for the WAV file | |
with open("binaural_beats.wav", "rb") as f: | |
wav_file = f.read() | |
b64_file = base64.b64encode(wav_file).decode("utf-8") | |
href = f'<a href="data:file/wav;base64,{b64_file}" download="binaural_beats.wav">Download Binaural Beats WAV File</a>' | |
st.markdown(href, unsafe_allow_html=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment