Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # music_generator_plus.py zzz
- from struct import pack
- import math
- import wave
- import random
- import sys
- import array
- import time
- dir='C://py//audio//'
- RATE=44100
- def save_as_mp3(data, params):
- f = wave.open(dir + str(time.time()/10-66334200).split('.')[0][-8:] + '.mp3', 'w') # .mp3 or .wav
- f.setparams(params)
- f.writeframes(data.tobytes())
- f.close()
- def generate_random_music(bpm=95, freq=100, duration=60*3, sampleRate=RATE, volume=100):
- def set_wave():
- wave_params = { 'samples': numSamples, 'channels': numChan, 'rate': sampleRate, 'bits': 16 }
- wave_path = dir + 'sound.' + str(time.time()/10-66334200).split('.')[0][-8:] + '.wav'
- wave = wave.open(wave_path, 'wb')
- save_as_mp3(data, wave_params)
- wave.setparams(wave_params)
- wave.writeframes(pack('<BBB', 1, bpm, numSamples))
- wave.writeframes(pack('<BBB', 1, len(patt), len(patt)))
- wave.writeframes(pack('<BBB', 1, len(patt2), len(patt2)))
- wave.writeframes(pack('<BBB', 1, len(patt), len(patt)))
- wave.writeframes(pack('<BBB', 1, len(patt2), len(patt2)))
- wave.writeframes(pack('<BBB', 1, 0, volume))
- wave.writeframes(pack('<B', 42))
- wave.writeframes(pack('<BB', 64, 0, freq/freq*255.0))
- wave.writeframes(pack('<BB', 32, 0, 0))
- wave.writeframes(pack('<BB', 0, 0, 0))
- def tune():
- if freq % 2:
- patt = [0.5, 0.5, 0.5, 0.5]
- patt2 = [0.5, 0.5, 0.5, 0.5]
- # add bass
- if freq % 4:
- patt = [0.5, 0.0, 0.5, 0.0]
- patt2 = [0.5, 0.0, 0.5, 0.0]
- # add piano
- if freq % 3:
- patt = [0.0, 0.0, 0.0, 0.5]
- patt2 = [0.0, 0.0, 0.0, 0.5]
- # add drums
- if freq % 8:
- patt = [0.5, 0.5, 0.0, 0.0]
- patt2 = [0.5, 0.5, 0.0, 0.0]
- # add guitar
- if freq % 6:
- patt = [0.5, 0.0, 0.0, 0.5]
- patt2 = [0.5, 0.0, 0.0, 0.5]
- else:
- patt = [0.0, 0.0, 0.0, 0.0]
- patt2 = [0.0, 0.0, 0.0, 0.0]
- return pratt, pratt2
- def create_pattern(sample, pratt):
- pratt.append(sample)
- if sample % 4:
- pratt.append(0.0)
- if sample % 12:
- pratt.append(0.0)
- if sample % 3:
- pratt.append(0.0)
- if sample % 3 * 2:
- pratt.append(0.0)
- pratt.append(sample)
- return pratt
- bpm = int(bpm)
- duration = int(duration)
- freq = int(freq)
- sampleRate = int(sampleRate)
- data = array.array('h')
- numChan = 2 # of channels (1: mono, 2: stereo)
- dataSize = 2
- numSamplesPerCyc = int(sampleRate / freq)
- numSamples = int(sampleRate * duration / 2)
- # bass in stereo set randomly to 0.5 to 1.5 octaves above middle C
- # piano in stereo set randomly to 1.5 to 3 octaves above middle C
- # drums in stereo set randomly to 3 to 6 octaves above middle C
- # guitar in stereo set randomly to 6 to 12 octaves above middle C
- pratt = array.array('h')
- pratt2 = array.array('h')
- for prattI in range(numSamples):
- for prattII in range(numSamples):
- if bpm < 60:
- sample = int(((RATE / 100.0 / freq - 1) / sampleRate * duration) / 2)
- else:
- sample = int((RATE / 100.0 / 60 * 2 * (RATE / 100.0 / freq - 1) / sampleRate) / 2 * duration)
- patt = create_pattern(sample, pratt)
- patt2 = create_pattern(sample, pratt2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement