Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. from scipy.io import wavfile
  2. import numpy as np
  3. import wave_helper as wh
  4. import copy
  5. import random
  6. import math
  7.  
  8. MAX_VOLUME = 32767
  9. PI = math.pi
  10.  
  11.  
  12. class Wave:
  13.     """A class to define wave"""
  14.  
  15.     def __init__(self, tup):
  16.         self.frame_rate = tup[0]
  17.         self.data = tup[1]
  18.  
  19.  
  20. def reverse_wave(wave):
  21.     """This function returns a reversed wav file."""
  22.     reversed_wave = wave
  23.     wave.data = wave.data[::-1]
  24.     return reversed_wave
  25.  
  26.  
  27. def memutza(sound1, sound2, sound3=None):
  28.     if sound3 is None:
  29.         return [int((sound1[0] + sound2[0]) / 2),
  30.                 int((sound1[1] + sound2[1]) / 2)]
  31.     else:
  32.         return [int((sound1[0] + sound2[0] + sound3[0]) / 3),
  33.                 int((sound1[1] + sound2[1] + sound3[1]) / 3)]
  34.  
  35.  
  36. def slow_wave(wave):
  37.     """this function returns a slow wave
  38.    writen by Gilad"""
  39.     new_data = []
  40.     for i in range(len(wave.data) - 1):
  41.         new_data.append(wave.data[i])
  42.         new_data.append(memutza(wave.data[i], wave.data[i + 1]))
  43.     slown_wave = copy.deepcopy(wave)
  44.     slown_wave.data = new_data
  45.     return slown_wave
  46.  
  47.  
  48. def lower_volume(wave):
  49.     """this function lowers the vlume of the wave
  50.    writen by Gilad The Great"""
  51.     low_volume = []
  52.     for i in wave.data:
  53.         low_volume.append([int(i[0] / 1.2), int(i[1] / 1.2)])
  54.     new_wave = copy.deepcopy(wave)
  55.     new_wave.data = low_volume
  56.     return new_wave
  57.  
  58.  
  59. def dimming_filter(wave):
  60.     """this function dimes the sound of the wave
  61.     written by Gilad """
  62.     new_data = [memutza(wave.data[0], wave.data[1])]
  63.     for i in range(1, len(wave.data) - 1):
  64.         new_data.append(
  65.             memutza(wave.data[i - 1], wave.data[i], wave.data[i + 1]))
  66.     new_data.append(memutza(wave.data[-1], wave.data[-2]))
  67.     new_wave = Wave((wave.frame_rate, new_data))
  68.     return new_wave
  69.  
  70.  
  71. def write_note(note, time):
  72.     """this function returns the data of a single note for
  73.    the given time
  74.    written by Gilad"""
  75.     sound = []
  76.     time = float(time)
  77.     if note == "Q":
  78.         return [[0, 0]] * time
  79.     if note == 'A':
  80.         freq = 440
  81.     if note == 'B':
  82.         freq = 494
  83.     if note == 'C':
  84.         freq = 523
  85.     if note == 'D':
  86.         freq = 587
  87.     if note == 'E':
  88.         freq = 659
  89.     if note == 'F':
  90.         freq = 698
  91.     if note == 'G':
  92.         freq = 784
  93.     samples_per_cycle = 2000 / freq
  94.     for i in range(int(time * 125)):
  95.         x = MAX_VOLUME * math.sin(2 * PI * i / samples_per_cycle)
  96.         sound.append([int(x), int(x)])
  97.     return sound
  98.  
  99.  
  100. def create_wave(compos):
  101.     """creates a new wave from the given compos
  102.    written by Gilad"""
  103.     data = []
  104.     compos = str(compos)
  105.     list_of_notes = list(compos.split())
  106.     for i in range(0, len(list_of_notes), 2):
  107.         data += write_note(list_of_notes[i], list_of_notes[i + 1])
  108.     return Wave((2000, data))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement