Update README.md
Browse files
README.md
CHANGED
@@ -1,40 +1,45 @@
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import matplotlib.pyplot as plt
|
2 |
+
import numpy as np
|
3 |
+
from scipy.io.wavfile import write
|
4 |
+
|
5 |
+
# Load the image
|
6 |
+
image_path = 'hugging_face.jpg'
|
7 |
+
image = plt.imread(image_path)
|
8 |
+
|
9 |
+
# Convert the image to grayscale
|
10 |
+
grayscale_image = np.mean(image, axis=2)
|
11 |
+
|
12 |
+
# Define the musical parameters
|
13 |
+
sample_rate = 44100 # Audio sample rate (Hz)
|
14 |
+
duration = 0.1 # Duration of each note (seconds)
|
15 |
+
|
16 |
+
# Define the mapping from pixel values to musical notes
|
17 |
+
min_note = 40 # MIDI note number for the lowest pixel value
|
18 |
+
max_note = 80 # MIDI note number for the highest pixel value
|
19 |
+
|
20 |
+
# Rescale the pixel values to the range [min_note, max_note]
|
21 |
+
scaled_image = (grayscale_image - np.min(grayscale_image))
|
22 |
+
scaled_image *= (max_note - min_note) / np.max(scaled_image)
|
23 |
+
scaled_image += min_note
|
24 |
+
|
25 |
+
# Generate the audio signal
|
26 |
+
total_duration = int(duration * sample_rate * grayscale_image.shape[1])
|
27 |
+
t = np.linspace(0, total_duration / sample_rate, total_duration, endpoint=False)
|
28 |
+
audio_signal = np.zeros(total_duration)
|
29 |
+
for i, column in enumerate(scaled_image.T):
|
30 |
+
start = int(i * duration * sample_rate)
|
31 |
+
end = int((i + 1) * duration * sample_rate)
|
32 |
+
audio_signal[start:end] = np.sin(2 * np.pi * column * t[start:end])
|
33 |
+
|
34 |
+
# Normalize the audio signal
|
35 |
+
audio_signal /= np.max(np.abs(audio_signal))
|
36 |
+
audio_signal *= 32767 # Scale the signal to the range of a 16-bit integer
|
37 |
+
|
38 |
+
# Convert the audio signal to 16-bit integer format
|
39 |
+
audio_signal = audio_signal.astype(np.int16)
|
40 |
+
|
41 |
+
# Save the audio signal to a WAV file
|
42 |
+
output_file = 'hugging_face.wav'
|
43 |
+
write(output_file, sample_rate, audio_signal)
|
44 |
+
|
45 |
+
print(f"Audio file '{output_file}' generated successfully!")
|