-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudioExtras.py
More file actions
80 lines (56 loc) · 2.03 KB
/
audioExtras.py
File metadata and controls
80 lines (56 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 18 12:15:45 2015
@author: davidramsay
"""
from __future__ import print_function
import math
import numpy as np
import sys
import struct
# temporary fixes for python3 compatibility
if sys.version_info[0] < 3:
import cStringIO
from itertools import izip_longest
elif sys.version_info[0] >= 3:
from io import StringIO as cStringIO
from itertools import zip_longest as izip_longest
"""
helper functions for audio interactions.
octaveSpacing(f0, octave)- returns f1 & f2, spaced octave apart and centered around f0
normalize(floatVals) - normalize a float array to [-1, 1]
floatsToWavBinary(array,chunk) - convert floats to an array of int16 chunks (size chunk) to be played
int16toFloat(array) - convert array of int16s to floats
disablePrint() - supresses all screen output
enablePrint() - re-enables screen output
grouper(iterable, n, fillvalue) - return an array of iterable in chunks of size n, and
fills leftover space in last chunk with fillvalue (default None)
"""
def octaveSpacing(f0, octave):
#returns f1,f2 spaced octave and centered at f0 logarithmically
f1 = f0 / (math.sqrt(10 ** (octave*np.log10(2))))
f2 = f0 * (math.sqrt(10 ** (octave*np.log10(2))))
#ratio of two freqs = 10 ** (octave diff * np.log10(2))
#center freq = math.sqrt(freq1*freq2)
return f1, f2
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return izip_longest(*args, fillvalue=fillvalue)
def normalize(floatVals):
floatVals = floatVals/max(abs(floatVals))
return floatVals
def floatsToWavBinary(array,chunk = 1024):
max_amplitude = 32767.0
array = array*max_amplitude
array = array.astype(int)
binArray = []
for x in grouper(array, chunk, 0):
binArray.append(struct.pack("%sh" % len(x), *x))
return binArray
def int16ToFloat(array):
return array/32767.0
def disablePrint():
sys.stdout = cStringIO.StringIO()
def enablePrint():
sys.stdout.flush()
sys.stdout = sys.__stdout__