-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.py
132 lines (113 loc) · 4.53 KB
/
sound.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from pydub.generators import Sine
import logging
import os
import tempfile
import winsound
from math import log10
def play_celebratory_melody():
try:
logging.debug("Attempting to play melody.")
# Create a celebratory melody with a sequence of notes
durations = [250, 250, 300, 200, 250, 300, 450] # Durations in milliseconds
notes = [
Sine(523), # C5
Sine(587), # D5
Sine(659), # E5
Sine(784), # G5
Sine(880), # A5
Sine(988), # B5
Sine(1046), # C6
]
# Initial and final volumes as a percentage
initial_volume = 0.1
final_volume = 0.5
# Calculate the volume increase per note
volume_step = (final_volume - initial_volume) / (len(notes) - 1)
segments = []
for i, note in enumerate(notes):
volume = initial_volume + i * volume_step
segment = note.to_audio_segment(duration=durations[i]).apply_gain(
20 * log10(volume)
)
segments.append(segment)
melody = sum(segments)
# Save the generated melody to a temporary WAV file
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
melody.export(tmpfile.name, format="wav")
# Play the WAV file
winsound.PlaySound(tmpfile.name, winsound.SND_FILENAME)
logging.debug("Melody finished playing.")
# Clean up the temporary file
os.remove(tmpfile.name)
except Exception as e:
logging.error("Error occurred while attempting to play melody: %s", e)
def play_rest_end_melody():
try:
logging.debug("Attempting to play rest end melody.")
# Create an inspiring melody with a sequence of notes
durations = [400, 400, 400, 600, 400, 400, 400] # Durations in milliseconds
notes = [
Sine(261), # C4
Sine(329), # E4
Sine(392), # G4
Sine(523), # C5
Sine(659), # E5
Sine(784), # G5
Sine(1046), # C6
]
# Initial and final volumes as a percentage
initial_volume = 0.2
final_volume = 0.6
# Calculate the volume increase per note
volume_step = (final_volume - initial_volume) / (len(notes) - 1)
segments = []
for i, note in enumerate(notes):
volume = initial_volume + i * volume_step
segment = note.to_audio_segment(duration=durations[i]).apply_gain(
20 * log10(volume)
)
segments.append(segment)
melody = sum(segments)
# Save the generated melody to a temporary WAV file
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
melody.export(tmpfile.name, format="wav")
# Play the WAV file
winsound.PlaySound(tmpfile.name, winsound.SND_FILENAME)
logging.debug("Melody finished playing.")
# Clean up the temporary file
os.remove(tmpfile.name)
except Exception as e:
logging.error("Error occurred while attempting to play rest end melody: %s", e)
def play_bell_sound():
try:
logging.debug("Attempting to play bell sound.")
# Create a bell sound with a sequence of notes
durations = [500, 500, 500] # Durations in milliseconds
notes = [
Sine(659), # E5
Sine(784), # G5
Sine(988), # B5
]
# Initial and final volumes as a percentage
initial_volume = 0.3
final_volume = 0.7
# Calculate the volume increase per note
volume_step = (final_volume - initial_volume) / (len(notes) - 1)
segments = []
for i, note in enumerate(notes):
volume = initial_volume + i * volume_step
segment = note.to_audio_segment(duration=durations[i]).apply_gain(
20 * log10(volume)
)
segments.append(segment)
bell_sound = sum(segments)
# Save the generated bell sound to a temporary WAV file
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
bell_sound.export(tmpfile.name, format="wav")
# Play the WAV file
winsound.PlaySound(tmpfile.name, winsound.SND_FILENAME)
logging.debug("Bell sound finished playing.")
# Clean up the temporary file
os.remove(tmpfile.name)
except Exception as e:
logging.error("Error occurred while attempting to play bell sound: %s", e)