Manasa1 commited on
Commit
c4494a9
·
verified ·
1 Parent(s): 0d07348

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -6
app.py CHANGED
@@ -1,6 +1,19 @@
1
- import random
2
  from pydub import AudioSegment
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  def generate_kids_music(theme):
5
  # Create a simple melody based on themes
6
  notes = {
@@ -10,19 +23,30 @@ def generate_kids_music(theme):
10
  }
11
 
12
  selected_notes = notes.get(theme, ["C4", "E4", "G4"]) # Default notes if theme not found
13
- music = AudioSegment.silent(duration=1000) # Start with 1 second of silence
14
 
 
 
 
 
 
 
 
 
 
 
 
15
  for note in selected_notes:
16
- # Generate a simple sine wave for each note
17
- frequency = 261.63 if note == "C4" else 329.63 if note == "E4" else 392.00 if note == "G4" else 523.25
18
- sine_wave = AudioSegment.sine(frequency=frequency, duration=500) # 0.5 second for each note
19
- music += sine_wave
20
 
21
  # Export the generated music
22
  output_file = f"{theme}_kids_music.wav"
23
  music.export(output_file, format="wav")
24
  return output_file
25
 
 
26
  from transformers import pipeline
27
 
28
  # Load the GPT-2 model for text generation
 
1
+ import numpy as np
2
  from pydub import AudioSegment
3
 
4
+ def sine_wave(frequency, duration_ms):
5
+ """Generate a sine wave tone at a given frequency and duration."""
6
+ sample_rate = 44100 # Sample rate in Hz
7
+ t = np.linspace(0, duration_ms / 1000, int(sample_rate * (duration_ms / 1000)), False)
8
+ wave = 0.5 * np.sin(2 * np.pi * frequency * t) # Sine wave formula
9
+ audio_segment = AudioSegment(
10
+ wave.tobytes(),
11
+ frame_rate=sample_rate,
12
+ sample_width=2, # 16-bit audio
13
+ channels=1 # Mono audio
14
+ )
15
+ return audio_segment
16
+
17
  def generate_kids_music(theme):
18
  # Create a simple melody based on themes
19
  notes = {
 
23
  }
24
 
25
  selected_notes = notes.get(theme, ["C4", "E4", "G4"]) # Default notes if theme not found
26
+ music = AudioSegment.silent(duration=0) # Start with silence
27
 
28
+ # Define frequencies for notes
29
+ frequencies = {
30
+ "C4": 261.63,
31
+ "D4": 293.66,
32
+ "E4": 329.63,
33
+ "F4": 349.23,
34
+ "G4": 392.00,
35
+ "A4": 440.00,
36
+ "C5": 523.25
37
+ }
38
+
39
  for note in selected_notes:
40
+ frequency = frequencies[note]
41
+ sine = sine_wave(frequency, duration_ms=500) # 0.5 second for each note
42
+ music += sine
 
43
 
44
  # Export the generated music
45
  output_file = f"{theme}_kids_music.wav"
46
  music.export(output_file, format="wav")
47
  return output_file
48
 
49
+
50
  from transformers import pipeline
51
 
52
  # Load the GPT-2 model for text generation