Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from scipy.io import wavfile
- import numpy as np
- import wave_helper as wh
- import copy
- import random
- import math
- MAX_VOLUME = 32767
- PI = math.pi
- class Wave:
- """A class to define wave"""
- def __init__(self, tup):
- self.frame_rate = tup[0]
- self.data = tup[1]
- def reverse_wave(wave):
- """This function returns a reversed wav file."""
- reversed_wave = wave
- wave.data = wave.data[::-1]
- return reversed_wave
- def memutza(sound1, sound2, sound3=None):
- if sound3 is None:
- return [int((sound1[0] + sound2[0]) / 2),
- int((sound1[1] + sound2[1]) / 2)]
- else:
- return [int((sound1[0] + sound2[0] + sound3[0]) / 3),
- int((sound1[1] + sound2[1] + sound3[1]) / 3)]
- def slow_wave(wave):
- """this function returns a slow wave
- writen by Gilad"""
- new_data = []
- for i in range(len(wave.data) - 1):
- new_data.append(wave.data[i])
- new_data.append(memutza(wave.data[i], wave.data[i + 1]))
- slown_wave = copy.deepcopy(wave)
- slown_wave.data = new_data
- return slown_wave
- def lower_volume(wave):
- """this function lowers the vlume of the wave
- writen by Gilad The Great"""
- low_volume = []
- for i in wave.data:
- low_volume.append([int(i[0] / 1.2), int(i[1] / 1.2)])
- new_wave = copy.deepcopy(wave)
- new_wave.data = low_volume
- return new_wave
- def dimming_filter(wave):
- """this function dimes the sound of the wave
- written by Gilad """
- new_data = [memutza(wave.data[0], wave.data[1])]
- for i in range(1, len(wave.data) - 1):
- new_data.append(
- memutza(wave.data[i - 1], wave.data[i], wave.data[i + 1]))
- new_data.append(memutza(wave.data[-1], wave.data[-2]))
- new_wave = Wave((wave.frame_rate, new_data))
- return new_wave
- def write_note(note, time):
- """this function returns the data of a single note for
- the given time
- written by Gilad"""
- sound = []
- time = float(time)
- if note == "Q":
- return [[0, 0]] * time
- if note == 'A':
- freq = 440
- if note == 'B':
- freq = 494
- if note == 'C':
- freq = 523
- if note == 'D':
- freq = 587
- if note == 'E':
- freq = 659
- if note == 'F':
- freq = 698
- if note == 'G':
- freq = 784
- samples_per_cycle = 2000 / freq
- for i in range(int(time * 125)):
- x = MAX_VOLUME * math.sin(2 * PI * i / samples_per_cycle)
- sound.append([int(x), int(x)])
- return sound
- def create_wave(compos):
- """creates a new wave from the given compos
- written by Gilad"""
- data = []
- compos = str(compos)
- list_of_notes = list(compos.split())
- for i in range(0, len(list_of_notes), 2):
- data += write_note(list_of_notes[i], list_of_notes[i + 1])
- return Wave((2000, data))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement