Kvikontent commited on
Commit
1dc43b9
·
1 Parent(s): f7cd782

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -47
app.py CHANGED
@@ -1,63 +1,36 @@
1
  import streamlit as st
2
  import numpy as np
3
  import soundfile as sf
 
4
 
5
- def text_to_melody(text, sample_rate=22050, duration=5):
6
- notes_map = {
7
- 'a': 220, 'b': 246.94, 'c': 261.63, 'd': 293.66, 'e': 329.63, 'f': 349.23, 'g': 392, 'h': 440,
8
- 'i': 493.88, 'j': 523.25, 'k': 587.33, 'l': 659.25, 'm': 698.46, 'n': 783.99, 'o': 880,
9
- 'p': 987.77, 'q': 1046.5, 'r': 1174.66, 's': 1318.51, 't': 1396.91, 'u': 1567.98, 'v': 1760,
10
- 'w': 1975.53, 'x': 2093, 'y': 2349.32, 'z': 2637, ' ': 0,
11
- 'а': 277.18, 'б': 311.13, 'в': 329.63, 'г': 369.99, 'д': 415.30, 'е': 440, 'ё': 466.16, 'ж': 493.88,
12
- 'з': 554.37, 'и': 622.25, 'й': 659.25, 'к': 739.99, 'л': 830.61, 'м': 932.33, 'н': 987.77, 'о': 1046.5,
13
- 'п': 1108.73, 'р': 1244.51, 'с': 1318.51, 'т': 1479.98, 'у': 1661.22, 'ф': 1864.66, 'х': 2217.46,
14
- 'ц': 2489.02, 'ч': 2793.83, 'ш': 2959.96, 'щ': 3322.44, 'ъ': 3729.31, 'ы': 4186.01, 'ь': 4698.63,
15
- 'э': 4978.03, 'ю': 5587.65, 'я': 5919.91
16
- # Add more characters as needed
17
- }
18
 
19
- # Russian word melodies
20
- word_melodies = {
21
- 'christmas': [329.63, 392, 493.88, 587.33, 659.25, 783.99, 880, 987.77],
22
- 'celebration': [440, 493.88, 587.33, 659.25, 783.99, 880, 987.77, 1046.5],
23
- 'holiday': [659.25, 698.46, 783.99, 659.25, 698.46, 783.99, 659.25, 329.63, 659.25, 697],
24
- 'joyful': [392, 440, 392, 440, 392, 440, 392, 330, 392],
25
- 'party': [783.99, 880, 783.99, 880, 783.99, 698.46, 659.25, 587.33, 659.25, 698.46, 783.99, 987.77],
26
- 'stars': [261.63, 293.66, 349.23, 392, 440, 493.88, 523.25, 587.33],
27
- 'дружба': [277.18, 329.63, 369.99, 415.30, 493.88, 622.25],
28
- 'солнце': [440, 493.88, 554.37, 659.25, 739.99, 830.61],
29
- 'путешествие': [277.18, 311.13, 349.23, 392, 440, 493.88, 554.37, 622.25, 739.99, 830.61, 932.33, 1046.5]
30
- # Add more words with associated melodies as needed
31
- }
32
-
33
- text = text.lower()
34
- prompt = text
35
-
36
- audio_data = []
37
  for char in prompt:
38
  if char in notes_map:
39
  freq = notes_map[char]
40
- duration = 1 # You can adjust the duration as needed
41
  t = np.linspace(0, duration, int(44100 * duration), False)
42
  note = np.sin(freq * t * 2 * np.pi)
43
  audio_data = np.append(audio_data, note)
44
 
45
- t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
46
- audio = sum(np.sin(2 * np.pi * note * t) for note in audio_data)
47
- audio /= np.max(np.abs(audio))
48
 
49
- return audio, sample_rate
 
 
50
 
51
- def main():
52
- st.title('Text to Melody Streamlit App')
53
- text_input = st.text_area("Enter text for melody generation", value="дружба")
54
 
55
- if st.button("Generate Melody from Text"):
56
- st.write("Generating melody based on the provided text prompt...")
57
- audio_data, sample_rate = text_to_melody(text_input)
58
- with st.empty():
59
- sf.write('output_melody.wav', audio_data, sample_rate, format='wav')
60
- st.audio('output_melody.wav', format='audio/wav')
61
 
62
- if __name__ == '__main__':
63
- main()
 
1
  import streamlit as st
2
  import numpy as np
3
  import soundfile as sf
4
+ import io
5
 
6
+ notes_map = {
7
+ 'a': 220, 'b': 246.94, 'c': 261.3, 'd': 293.66, 'e': 329.63, 'f': 349.23, 'g': 392, 'h': 440,
8
+ 'i': 493.88, 'j': 523.25, 'k': 587.33, 'l': 659.25, 'm': 698.46, 'n': 783.99, 'o': 880,
9
+ 'p': 987.77, 'q': 1046.5, 'r': 1147.66, 's': 1318, 't': 1396, 'u': 1567.98, 'v': 1760.66,
10
+ 'w': 1975.7, 'x': 2095, 'y': 2349.32, 'z': 2607, '!': 5078, ' ': 0
11
+ }
 
 
 
 
 
 
 
12
 
13
+ def generate_music(prompt, duration=1.0):
14
+ audio_data = np.array([])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  for char in prompt:
16
  if char in notes_map:
17
  freq = notes_map[char]
 
18
  t = np.linspace(0, duration, int(44100 * duration), False)
19
  note = np.sin(freq * t * 2 * np.pi)
20
  audio_data = np.append(audio_data, note)
21
 
22
+ return audio_data
 
 
23
 
24
+ st.sidebar.title("Music Generator")
25
+ st.sidebar.write("Music generator based on your input")
26
+ prompt = st.sidebar.text_input("Enter prompt...")
27
 
28
+ if st.sidebar.button("Generate"):
29
+ audio = generate_music(prompt)
 
30
 
31
+ bytes_io = io.BytesIO()
32
+ sf.write(bytes_io, audio, 44100, format ='WAV')
33
+ bytes_io.seek(0)
 
 
 
34
 
35
+ st.audio(bytes_io, format="audio/wav")
36
+ st.write("Audio generation finished successfully")