Advertisement
here2share

# random_music_generator.py

Dec 16th, 2022 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. # random_music_generator.py
  2.  
  3. import struct
  4. import wave
  5. import os
  6. import math
  7. import random
  8.  
  9. # Constants for the audio settings
  10. BITRATE = 90    # in bps
  11. CHANNELS = 2    # stereo
  12. FRAMERATE = 44100   # in "Hz" :
  13. SAMPLESIZE = 2  # in bytes
  14. MAX_VOLUME = 32767  # max value for 16-bit audio
  15. DURATION = 240  # in seconds (4 minutes)
  16. PADDING = 8 # number of digits for padding in file names
  17.  
  18. # Save the audio data to an MP3 file
  19. def save_as_mp3():
  20.     for i, track in enumerate(audio_data):
  21.         # Generate a file name with padding
  22.         file_name = 'track_{:0{padding}}.mp3'.format(i, padding=PADDING)
  23.         # Create a new wave file
  24.         wave_file = wave.open(file_name, 'w')
  25.         # Set the audio settings for the wave file
  26.         wave_file.setnchannels(CHANNELS)
  27.         wave_file.setsampwidth(SAMPLESIZE)
  28.         wave_file.setframerate(FRAMERATE)
  29.         wave_file.setcomptype('MP3', 'MP3')
  30.         wave_file.setparams((CHANNELS, SAMPLESIZE, FRAMERATE, 0, 'NONE', 'not compressed'))
  31.         # Write the audio data to the wave file
  32.         wave_file.writeframes(track)
  33.         # Close the wave file
  34.         wave_file.close()
  35.  
  36. def generate_music_track():
  37.     # Generate the left and right channels for the music track
  38.     left_channel = []
  39.     right_channel = []
  40.     for i in range(DURATION * FRAMERATE):
  41.         # Generate a random volume for the left channel
  42.         left_volume = random.randint(0, MAX_VOLUME)
  43.         # Generate a random frequency for the left channel
  44.         left_frequency = random.uniform(200, 400)
  45.         # Generate a sine wave for the left channel
  46.         left_sample = int(left_volume * math.sin(2 * math.pi * i * left_frequency / FRAMERATE))
  47.         # Generate a random volume for the right channel
  48.         right_volume = random.randint(0, MAX_VOLUME)
  49.         # Generate a random frequency for the right channel
  50.         right_frequency = random.uniform(200, 400)
  51.         # Generate a sine wave for the right channel
  52.         right_sample = int(right_volume * math.sin(2 * math.pi * i * right_frequency / FRAMERATE))
  53.         # Add the samples to the left and right channels
  54.         left_channel.append(struct.pack('h', left_sample))
  55.         right_channel.append(struct.pack('h', right_sample))
  56.     # Combine the left and right channels into a single audio track
  57.     audio_track = b''.join([left + right for left, right in zip(left_channel, right_channel)])
  58.     return audio_track
  59.  
  60. drum_beat = []
  61. def generate_drum_beat():
  62.     del drum_beat[:]
  63.     for i in range(DURATION * FRAMERATE):
  64.         # Generate a random volume for the drum beat
  65.         volume = random.randint(0, MAX_VOLUME)
  66.         # Generate a random frequency for the drum beat
  67.         frequency = random.uniform(200, 400)
  68.         # Generate a sine wave for the drum beat
  69.         sample = int(volume * math.sin(2 * math.pi * i * frequency / FRAMERATE))
  70.         # Add the sample to the drum beat
  71.         drum_beat.append(struct.pack('h', sample))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement