zarox commited on
Commit
3e0f2ab
·
1 Parent(s): b386c65
Files changed (1) hide show
  1. app.py +100 -117
app.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import secrets
3
  import threading
4
  import gradio as gr
 
5
  from AudioFusion import Fusion
6
 
7
  from telethon.sync import TelegramClient, events, Button
@@ -136,130 +137,112 @@ with gr.Blocks(title="Audio Fusion") as iface:
136
 
137
  client = TelegramClient('session_name', API_ID, API_HASH)
138
 
 
 
139
 
140
- # Define the available commands
141
- commands = {
142
- '/start': 'Welcome to Audio Bot! Send me an audio file to get started.',
143
- '/help': 'List all available commands.',
144
- '/trim': 'Trim the audio. Usage: /trim start_time end_time',
145
- '/volume': 'Adjust volume. Usage: /volume level',
146
- '/reverse': 'Reverse the audio.',
147
- '/reverb': 'Apply reverb effect to the audio. Usage: /reverb roomSize damping width wetLevel dryLevel',
148
- '/speedup': 'Increase the speed of the audio.',
149
- '/slowdown': 'Decrease the speed of the audio.',
150
- '/effect8D': 'Apply 8D effect to the audio. Usage: /effect8D panBoundary jumpPercentage timeLtoR volumeMultiplier',
151
- '/save': 'Save the modified audio. Usage: /save outputFileName',
152
- }
153
-
154
- @client.on(events.NewMessage(pattern='/start'))
155
  async def start_handler(event):
156
- await event.respond(commands['/start'])
157
-
158
- @client.on(events.NewMessage(pattern='/help'))
159
- async def help_handler(event):
160
- help_text = '\n'.join(f'{cmd}: {desc}' for cmd, desc in commands.items())
161
- await event.respond(help_text)
162
-
163
- @client.on(events.NewMessage(incoming=True))
164
- async def audio_handler(event):
165
- reply = await event.get_reply_message()
166
- if event.message.media and event.message.media.document.mime_type.startswith('audio') and event.is_private:
167
- msg = await event.reply("`Downloading...`")
168
- # Download the audio file
169
- file_path = await client.download_media(event.message, f"downloads/{event.sender.id}_audio.mp3")
170
- return await msg.edit("`Downloaded successfully...`")
171
- elif event.is_group and event.text.startswith("/edit") and reply:
172
- if reply.media and reply.media.document.mime_type.startswith('audio'):
173
- msg = await event.reply("`Downloading...`")
174
- file_path = await reply.download_media(f"downloads/{event.sender.id}_audio.mp3")
175
- # try:
176
- # # Load the audio file using your AudioFusion class
177
- # audio = Fusion.loadSound(file_path)
178
- # except Fusion.InvalidMusicFileError as e:
179
- # await event.respond(f'Error: {e}')
180
- # return os.remove(file_path)
181
- return await msg.edit("`Downloaded successfully...`")
182
- else:
183
- return await event.respond("`Please send an audio file...`")
184
- else:
185
- if event.is_private: return await event.respond("`Please send an audio file...`")
186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
 
188
- @client.on(events.NewMessage(incoming=True, pattern="/(edit|trim|volume|reverse|reverb|speedup|slowdown|effect8d|save|delete)"))
189
- async def audio_editor(event):
190
- file_path = f"downloads/{event.sender.id}_audio.mp3"
191
- if not os.path.exists(file_path): return
192
  try:
193
- # Load the audio file using your AudioFusion class
194
- audio = Fusion.loadSound(os.path.abspath(file_path))
195
- except Fusion.InvalidMusicFileError as e:
196
- await event.reply(f'Error: {e}')
197
- return os.remove(file_path)
 
 
 
 
 
 
 
 
 
 
 
198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
 
200
- # Process the user's command
201
- if event.raw_text.startswith('/trim'):
202
- # Parse parameters and apply the trim effect
203
- # Example usage: /trim 5000 10000
204
- parameters = event.raw_text.split()[1:]
205
- start_time, end_time = map(int, parameters)
206
- audio = audio[start_time:end_time]
207
- await event.reply("`Following effect added to your audio file...`")
208
-
209
- elif event.raw_text.startswith('/volume'):
210
- # Parse parameters and adjust volume
211
- # Example usage: /volume 3
212
- parameters = event.raw_text.split()[1:]
213
- volume_level = int(parameters[0])
214
- audio = audio + volume_level # Adjust volume using Pydub's volume adjustment
215
- await event.reply("`Following effect added to your audio file...`")
216
-
217
- elif event.raw_text.startswith('/reverse'):
218
- # Reverse the audio
219
- audio = audio.reverse()
220
- await event.reply("`Following effect added to your audio file...`")
221
-
222
- elif event.raw_text.startswith('/speedup'):
223
- # Increase the speed of the audio
224
- audio = Fusion.effectSlowed(audio, speedMultiplier=0.8)
225
- await event.reply("`Following effect added to your audio file...`")
226
-
227
- elif event.raw_text.startswith('/slowdown'):
228
- # Decrease the speed of the audio
229
- audio = Fusion.effectSlowed(audio, speedMultiplier=1.2)
230
- await event.reply("`Following effect added to your audio file...`")
231
-
232
- elif event.raw_text.startswith('/effect8d'):
233
- # Parse parameters and apply the effect
234
- # Example usage: /effect8D 100 5 10000 6
235
- parameters = event.raw_text.split()[1:]
236
- audio = Fusion.effect8D(audio, *map(int, parameters))
237
- await event.reply("`Following effect added to your audio file...`")
238
-
239
- elif event.raw_text.startswith('/reverb'):
240
- # Parse parameters and apply the effect
241
- # Example usage: /effectReverb 0.8 1 0.5 0.3 0.8
242
- parameters = event.raw_text.split()[1:]
243
- audio = Fusion.effectReverb(audio, *map(float, parameters))
244
- await event.reply("`Following effect added to your audio file...`")
245
-
246
- elif event.raw_text.startswith('/save'):
247
- # Parse parameters and save the modified audio
248
- msg = await event.reply("`Uploading...`")
249
- # Example usage: /save outputFileName
250
- parameters = event.raw_text.split()[1:]
251
- output_file_name = parameters[0]
252
- output_file_path = Fusion.saveSound(audio, output_file_name)
253
-
254
- # Send the modified audio file to the user
255
- await client.send_file(event.chat_id, output_file_path, caption=f'Modified audio: {output_file_name}.mp3')
256
- await msg.delete()
257
- # Clean up the temporary files
258
- os.remove(output_file_path)
259
-
260
- elif event.raw_text.startswith('/delete'):
261
- os.remove(file_path)
262
- msg = await event.reply("`Removed saved audio...`")
263
 
264
  async def initiation():
265
  await client.send_message(-1001662130485, "**Hugging is Running.**", buttons=[(Button.url("Execal", "https://t.me/execal"),)],)
 
2
  import secrets
3
  import threading
4
  import gradio as gr
5
+ from io import BytesIO
6
  from AudioFusion import Fusion
7
 
8
  from telethon.sync import TelegramClient, events, Button
 
137
 
138
  client = TelegramClient('session_name', API_ID, API_HASH)
139
 
140
+ # Define the states for user interaction
141
+ states = {}
142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  async def start_handler(event):
144
+ await event.reply("Welcome to AudioFusion Bot! Send me an audio file, and I'll apply effects for you.")
145
+
146
+ buttons = [
147
+ [Button.inline('Slowed', b'slowed'), Button.inline('8D', b'8d')],
148
+ [Button.inline('Reverb', b'reverb'), Button.inline('Reverse', b'reverse')],
149
+ [Button.inline('Volume', b'volume'), Button.inline('Speedup', b'speedup')],
150
+ [Button.inline('Preview', b'preview')],
151
+ [Button.inline('Send', b'send')],
152
+ ]
153
+
154
+ async def buttons_handler(event):
155
+ user_id = event.sender_id
156
+
157
+ # Download the audio file and store it in the user's state
158
+ reply_message = await event.get_reply_message()
159
+ if not reply_message or not reply_message.file:
160
+ await event.reply("Please reply to an audio file.")
161
+ return
162
+
163
+ audio_file = BytesIO()
164
+ await event.client.download_media(reply_message, audio_file)
165
+ audio_file.seek(0)
 
 
 
 
 
 
 
 
166
 
167
+ # Store the audio file in the user's state
168
+ states[user_id] = audio_file
169
+
170
+ await client.send_file(event.chat_id, file="image.jpg", caption="Preview the current modification:", buttons=buttons)
171
+
172
+
173
+ async def audio_effect_handler(event):
174
+ user_id = event.sender_id
175
+ if user_id not in states or not states[user_id]:
176
+ await event.answer("`No audio file found. Please use /buttons command to upload an audio file.`")
177
+ return
178
+
179
+ # Retrieve the audio file from the user's state
180
+ audio_file = states[user_id]
181
 
182
+ query = event.pattern_match.group(1).decode("UTF-8")
 
 
 
183
  try:
184
+ sound = Fusion.loadSound(audio_file)
185
+ if query == 'slowed':
186
+ modified_sound = Fusion.effectSlowed(sound, 0.82)
187
+ elif query == 'speedup':
188
+ modified_sound = Fusion.effectSlowed(sound, 1.2)
189
+ elif query == '8d':
190
+ modified_sound = Fusion.effect8D(sound)
191
+ elif query == 'reverb':
192
+ modified_sound = Fusion.effectReverb(sound)
193
+ elif query == 'reverse':
194
+ modified_sound = sound.reverse()
195
+
196
+ # Update the user's state with the modified sound
197
+ states[user_id] = modified_sound
198
+
199
+ await event.answer"Effect applied. Click 'Send' to receive the modified audio file.")
200
 
201
+ except Fusion.InvalidMusicFileError as e:
202
+ await event.reply(str(e))
203
+ except Exception as e:
204
+ await event.reply(f"An error occurred: {str(e)}")
205
+
206
+
207
+ async def preview_handler(event):
208
+ user_id = event.sender_id
209
+ if user_id in states and states[user_id]:
210
+ # Send the current modification for preview
211
+ output_file_name = f"{user_id}_preview"
212
+ output_file = Fusion.saveSound(states[user_id], output_file_name)
213
+ await event.edit("`Uploading...`", buttons=buttons)
214
+ # Edit the message and send the audio file in the edited message
215
+ await event.edit(file=output_file, text="`Preview the current modification:`", buttons=buttons)
216
 
217
+ # Clean up - remove the saved preview audio file
218
+ os.remove(output_file)
219
+ else:
220
+ await event.answer("`No modified audio file found. Please apply an effect first.`")
221
+
222
+
223
+ async def send_handler(event):
224
+ user_id = event.sender_id
225
+ if user_id in states and states[user_id]:
226
+ # Send the modified sound file
227
+ output_file_name = f"{user_id}_modified_audio"
228
+ output_file = Fusion.saveSound(states[user_id], output_file_name)
229
+ await event.reply(file=output_file)
230
+
231
+ # Clean up - remove the user's state and the saved audio file
232
+ del states[user_id]
233
+ os.remove(output_file)
234
+ await event.delete()
235
+ else:
236
+ await event.answer("No modified audio file found. Please apply an effect first.")
237
+
238
+
239
+
240
+ # Register command and button handlers
241
+ client.on(events.NewMessage(pattern='/start')(start_handler))
242
+ client.on(events.NewMessage(pattern='/buttons')(buttons_handler))
243
+ client.on(events.CallbackQuery(pattern=b'(slowed|8d|reverb|reverse|trim|volume|speedup)')(audio_effect_handler))
244
+ client.on(events.CallbackQuery(pattern=b'preview')(preview_handler))
245
+ client.on(events.CallbackQuery(pattern=b'send')(send_handler))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
 
247
  async def initiation():
248
  await client.send_message(-1001662130485, "**Hugging is Running.**", buttons=[(Button.url("Execal", "https://t.me/execal"),)],)