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Filter, Compressor, Limiter, Reverb, Gain
|
| 6 |
+
from pedalboard.io import AudioFile
|
| 7 |
+
import numpy as np
|
| 8 |
+
from scipy.signal import butter, lfilter
|
| 9 |
+
|
| 10 |
+
class AudioProcessor:
|
| 11 |
+
def __init__(self, target_file, reference_file):
|
| 12 |
+
self.target_file = target_file
|
| 13 |
+
self.reference_file = reference_file
|
| 14 |
+
self.mastered_24bit = 'my_song_master_24bit_.wav'
|
| 15 |
+
|
| 16 |
+
def mix_and_generate(self):
|
| 17 |
+
# Let's keep only warning outputs here, muting everything else
|
| 18 |
+
mg.log(warning_handler=print)
|
| 19 |
+
|
| 20 |
+
# Perform the mixing and generating to 24bit
|
| 21 |
+
mg.process(
|
| 22 |
+
target=self.target_file,
|
| 23 |
+
reference=self.reference_file,
|
| 24 |
+
results=[
|
| 25 |
+
mg.pcm16("my_song_master_16bit_.wav"),
|
| 26 |
+
mg.pcm24(self.mastered_24bit),
|
| 27 |
+
],
|
| 28 |
+
preview_target=mg.pcm16("preview_my_song.flac"),
|
| 29 |
+
preview_result=mg.pcm16("preview_my_song_master.flac"),
|
| 30 |
+
)
|
| 31 |
+
print("Mixing and generating to 24bit completed.")
|
| 32 |
+
|
| 33 |
+
def stereo_widen(self, audio, width=1.2):
|
| 34 |
+
left_channel = audio[0::2] * width
|
| 35 |
+
right_channel = audio[1::2] * width
|
| 36 |
+
widened_audio = np.empty_like(audio)
|
| 37 |
+
widened_audio[0::2] = left_channel
|
| 38 |
+
widened_audio[1::2] = right_channel
|
| 39 |
+
return widened_audio
|
| 40 |
+
|
| 41 |
+
def reduce_piano_volume(self, audio, sample_rate, freq_low=200, freq_high=2000, reduction_db=-18):
|
| 42 |
+
nyquist = 0.5 * sample_rate
|
| 43 |
+
low = freq_low / nyquist
|
| 44 |
+
high = freq_high / nyquist
|
| 45 |
+
b, a = butter(1, [low, high], btype='band')
|
| 46 |
+
filtered_audio = lfilter(b, a, audio)
|
| 47 |
+
gain_reduction = 10 ** (reduction_db / 20)
|
| 48 |
+
reduced_audio = audio - (filtered_audio * gain_reduction)
|
| 49 |
+
return reduced_audio
|
| 50 |
+
|
| 51 |
+
def determine_parameters(self, audio):
|
| 52 |
+
avg_volume = np.mean(np.abs(audio))
|
| 53 |
+
highpass_cutoff = 100 + (avg_volume * 50)
|
| 54 |
+
compressor_threshold = -20 + (avg_volume * 10)
|
| 55 |
+
reverb_room_size = min(max(avg_volume, 0.1), 0.5)
|
| 56 |
+
return highpass_cutoff, compressor_threshold, reverb_room_size
|
| 57 |
+
|
| 58 |
+
def master_audio(self):
|
| 59 |
+
# Load the mixed and generated 24bit audio file
|
| 60 |
+
print(f"Loading 24bit audio file: {self.mastered_24bit}")
|
| 61 |
+
with AudioFile(self.mastered_24bit) as f:
|
| 62 |
+
audio = f.read(f.frames)
|
| 63 |
+
sample_rate = f.samplerate
|
| 64 |
+
|
| 65 |
+
# Automatically determine parameters
|
| 66 |
+
highpass_cutoff, compressor_threshold, reverb_room_size = self.determine_parameters(audio)
|
| 67 |
+
print(f"Determined Parameters - Highpass Cutoff: {highpass_cutoff}, Compressor Threshold: {compressor_threshold}, Reverb Room Size: {reverb_room_size}")
|
| 68 |
+
|
| 69 |
+
# Create a pedalboard with the desired effects
|
| 70 |
+
board = Pedalboard([
|
| 71 |
+
HighpassFilter(cutoff_frequency_hz=highpass_cutoff),
|
| 72 |
+
Compressor(threshold_db=compressor_threshold, ratio=4),
|
| 73 |
+
Limiter(threshold_db=-0.1),
|
| 74 |
+
Reverb(room_size=reverb_room_size, wet_level=0.2),
|
| 75 |
+
Gain(gain_db=3),
|
| 76 |
+
])
|
| 77 |
+
|
| 78 |
+
# Process the audio with the pedalboard
|
| 79 |
+
processed_audio = board(audio, sample_rate)
|
| 80 |
+
|
| 81 |
+
# Apply custom stereo widening
|
| 82 |
+
processed_audio = self.stereo_widen(processed_audio)
|
| 83 |
+
|
| 84 |
+
# Reduce piano volume
|
| 85 |
+
processed_audio = self.reduce_piano_volume(processed_audio, sample_rate)
|
| 86 |
+
|
| 87 |
+
# Save the processed audio to a new file
|
| 88 |
+
output_file = 'final_mastered_audio.wav'
|
| 89 |
+
print(f"Saving final mastered audio to: {output_file}")
|
| 90 |
+
with AudioFile(output_file, 'w', sample_rate, processed_audio.shape[0]) as f:
|
| 91 |
+
f.write(processed_audio)
|
| 92 |
+
|
| 93 |
+
print("Audio mastering completed.")
|
| 94 |
+
return output_file
|
| 95 |
+
|
| 96 |
+
# Streamlit app
|
| 97 |
+
st.title('Moonarch Music Analysis: Mixing & Mastering')
|
| 98 |
+
st.header("Experience the Future of Sound")
|
| 99 |
+
st.write("""
|
| 100 |
+
Everything you need to create and release your music, including samples, plugins, unlimited distribution, and the world's best AI mastering engine.
|
| 101 |
+
""")
|
| 102 |
+
|
| 103 |
+
# Display the Start button
|
| 104 |
+
if st.button("Start using Moonarch"):
|
| 105 |
+
st.write("Welcome to Moonarch! Let's start creating amazing music.")
|
| 106 |
+
|
| 107 |
+
# Add upload option
|
| 108 |
+
uploaded_file = st.file_uploader("Upload a song", type=["mp3", "wav"])
|
| 109 |
+
|
| 110 |
+
if uploaded_file is not None:
|
| 111 |
+
st.write("File uploaded successfully.")
|
| 112 |
+
|
| 113 |
+
# Placeholder for progress bar
|
| 114 |
+
progress_bar = st.progress(0)
|
| 115 |
+
|
| 116 |
+
# Save uploaded file to a temporary location
|
| 117 |
+
audio_file_path = os.path.join(os.getcwd(), f'{uploaded_file.name}')
|
| 118 |
+
with open(audio_file_path, 'wb') as f:
|
| 119 |
+
f.write(uploaded_file.getbuffer())
|
| 120 |
+
|
| 121 |
+
# Simulate file processing
|
| 122 |
+
for percent_complete in range(100):
|
| 123 |
+
time.sleep(0.01)
|
| 124 |
+
progress_bar.progress(percent_complete + 1)
|
| 125 |
+
|
| 126 |
+
st.write("File processing complete.")
|
| 127 |
+
|
| 128 |
+
# Reference file for mixing
|
| 129 |
+
reference_file = 'audio_example.mp3'
|
| 130 |
+
|
| 131 |
+
# Initialize the processor
|
| 132 |
+
processor = AudioProcessor(target_file=audio_file_path, reference_file=reference_file)
|
| 133 |
+
|
| 134 |
+
if st.button('Master Audio'):
|
| 135 |
+
with st.spinner('Mixing, generating, and mastering audio...'):
|
| 136 |
+
processor.mix_and_generate()
|
| 137 |
+
final_output = processor.master_audio()
|
| 138 |
+
|
| 139 |
+
st.success('Audio mastering complete!')
|
| 140 |
+
st.audio(final_output)
|
| 141 |
+
|
| 142 |
+
with open(final_output, 'rb') as f:
|
| 143 |
+
st.download_button(
|
| 144 |
+
label="Download Mastered Audio",
|
| 145 |
+
data=f,
|
| 146 |
+
file_name='final_mastered_audio.wav',
|
| 147 |
+
mime='audio/wav'
|
| 148 |
+
)
|