Advertisement
here2share

# music_generator_plus.py

Sep 17th, 2022
1,097
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. # music_generator_plus.py zzz
  2.  
  3. from struct import pack
  4. import math
  5. import wave
  6. import random
  7. import sys
  8. import array
  9. import time
  10.  
  11. dir='C://py//audio//'
  12.  
  13. RATE=44100
  14.  
  15. def save_as_mp3(data, params):
  16.     f = wave.open(dir + str(time.time()/10-66334200).split('.')[0][-8:] + '.mp3', 'w') # .mp3 or .wav
  17.     f.setparams(params)
  18.     f.writeframes(data.tobytes())
  19.     f.close()
  20.  
  21. def generate_random_music(bpm=95, freq=100, duration=60*3, sampleRate=RATE, volume=100):
  22.     def set_wave():
  23.         wave_params = { 'samples': numSamples, 'channels': numChan, 'rate': sampleRate, 'bits': 16 }
  24.         wave_path = dir + 'sound.' + str(time.time()/10-66334200).split('.')[0][-8:] + '.wav'
  25.         wave = wave.open(wave_path, 'wb')
  26.         save_as_mp3(data, wave_params)
  27.         wave.setparams(wave_params)
  28.         wave.writeframes(pack('<BBB', 1, bpm, numSamples))
  29.         wave.writeframes(pack('<BBB', 1, len(patt), len(patt)))
  30.         wave.writeframes(pack('<BBB', 1, len(patt2), len(patt2)))
  31.         wave.writeframes(pack('<BBB', 1, len(patt), len(patt)))
  32.         wave.writeframes(pack('<BBB', 1, len(patt2), len(patt2)))
  33.         wave.writeframes(pack('<BBB', 1, 0, volume))
  34.         wave.writeframes(pack('<B', 42))
  35.         wave.writeframes(pack('<BB', 64, 0, freq/freq*255.0))
  36.         wave.writeframes(pack('<BB', 32, 0, 0))
  37.         wave.writeframes(pack('<BB', 0, 0, 0))
  38.     def tune():
  39.         if freq % 2:
  40.             patt = [0.5, 0.5, 0.5, 0.5]
  41.             patt2 = [0.5, 0.5, 0.5, 0.5]
  42.         # add bass
  43.         if freq % 4:
  44.             patt = [0.5, 0.0, 0.5, 0.0]
  45.             patt2 = [0.5, 0.0, 0.5, 0.0]
  46.         # add piano
  47.         if freq % 3:
  48.             patt = [0.0, 0.0, 0.0, 0.5]
  49.             patt2 = [0.0, 0.0, 0.0, 0.5]
  50.         # add drums
  51.         if freq % 8:
  52.             patt = [0.5, 0.5, 0.0, 0.0]
  53.             patt2 = [0.5, 0.5, 0.0, 0.0]
  54.         # add guitar
  55.         if freq % 6:
  56.             patt = [0.5, 0.0, 0.0, 0.5]
  57.             patt2 = [0.5, 0.0, 0.0, 0.5]
  58.         else:
  59.             patt = [0.0, 0.0, 0.0, 0.0]
  60.             patt2 = [0.0, 0.0, 0.0, 0.0]
  61.         return pratt, pratt2
  62.     def create_pattern(sample, pratt):
  63.         pratt.append(sample)
  64.         if sample % 4:
  65.             pratt.append(0.0)
  66.         if sample % 12:
  67.             pratt.append(0.0)
  68.         if sample % 3:
  69.             pratt.append(0.0)
  70.         if sample % 3 * 2:
  71.             pratt.append(0.0)
  72.         pratt.append(sample)
  73.         return pratt
  74.     bpm = int(bpm)
  75.     duration = int(duration)
  76.     freq = int(freq)
  77.     sampleRate = int(sampleRate)
  78.     data = array.array('h')
  79.     numChan = 2 # of channels (1: mono, 2: stereo)
  80.     dataSize = 2
  81.     numSamplesPerCyc = int(sampleRate / freq)
  82.     numSamples = int(sampleRate * duration / 2)
  83.     # bass in stereo set randomly to 0.5 to 1.5 octaves above middle C
  84.     # piano in stereo set randomly to 1.5 to 3 octaves above middle C
  85.     # drums in stereo set randomly to 3 to 6 octaves above middle C
  86.     # guitar in stereo set randomly to 6 to 12 octaves above middle C
  87.     pratt = array.array('h')
  88.     pratt2 = array.array('h')
  89.     for prattI in range(numSamples):
  90.         for prattII in range(numSamples):
  91.             if bpm < 60:
  92.                 sample = int(((RATE / 100.0 / freq - 1) / sampleRate * duration) / 2)
  93.             else:
  94.                 sample = int((RATE / 100.0 / 60 * 2 * (RATE / 100.0 / freq - 1) / sampleRate) / 2 * duration)
  95.             patt = create_pattern(sample, pratt)
  96.             patt2 = create_pattern(sample, pratt2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement