Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tones.py
- import math
- import wave
- import struct
- audio = []
- sample_rate = 44100.0
- dir = 'C://py//audio//'
- def append_silence(duration_milliseconds=500):
- """
- Adding silence is easy - we add zeros to the end of our array
- """
- num_samples = duration_milliseconds * (sample_rate / 1000.0)
- for x in range(int(num_samples)):
- audio.append(0.0)
- def append_sinewave(
- freq=440.0,
- duration_milliseconds=500,
- volume=1.0):
- num_samples = duration_milliseconds * (sample_rate / 1000.0)
- for x in range(int(num_samples)):
- audio.append(volume * math.sin(2 * math.pi * freq * ( x / sample_rate )))
- def save_wav(file_name):
- # open up a mp3 file
- mp3_file=wave.open(dir+file_name+".mp3","w")
- # wav params
- nchannels = 1
- sampwidth = 2
- # 44100 is the industry standard sample rate - CD quality.
- nframes = len(audio)
- comptype = "NONE"
- compname = "not compressed"
- mp3_file.setparams((nchannels, sampwidth, sample_rate, nframes, comptype, compname))
- for sample in audio:
- mp3_file.writeframes(struct.pack('h', int( sample * 32767.0 )))
- mp3_file.close()
- def noteToFreq(note):
- a = 440 # common value frequency is 440Hz
- return (a / 32.) * (2 ** ((note - 9.) / 12))
- audio = []
- for _ in range(8):
- append_sinewave(freq=noteToFreq(80),duration_milliseconds=30)
- append_sinewave(freq=noteToFreq(85),duration_milliseconds=60)
- append_sinewave(freq=noteToFreq(90),duration_milliseconds=30)
- append_silence(duration_milliseconds=150)
- save_wav("testing")
- notes = [45, 47, 48, 50, 52, 53, 55, 57, 59, 60, 62, 64, 65, 67, 69]
- for i in range(len(notes)):
- audio = []
- append_sinewave(freq=noteToFreq(notes[i]+12),duration_milliseconds=200)
- save_wav("note"+str(i+1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement