sys.path.append('../..')
import numpy as np

from tuning import pitch_to_freq
from synthesis import sine, generate_and_save

def complex_tone(partials):
    # partial = ((freq0, amp0), (freq1, amp1), ...)
    return lambda t: np.sum(sine(t, freq=freq, amplitude=amp) for (freq, amp) in partials)

def dissonance(base, difference):
    '''Returns the timbre composed of two sines of unit amplitude.'''
    return ((base, 1), (base + difference, 1))

def uniform_random(size, a, b):
    return a + (b - a) * np.random.random(size)

if __name__ == '__main__':
    numpy.random.seed(42)
    
    log_diff_range = (-1, 4)
    
    sample_count = 100
    log_diffs = uniform_random(sample_count, *log_diff_range)
    
    base = 440
    for log_diff in log_diffs:
        diff = 10 ** log_diff
        generate_and_save(complex_tone(dissonance(base, diff)),
            filename="%s_%s.wav" % (base, diff), duration=3)
Ejemplo n.º 2
0
from scipy.signal import chirp

from analysis import split_to_blocks
from synthesis import generate_and_save
from reassignment import compute_spectra

block_size = 2048
fs = 44100
f1, f2 = 440, 880
duration = block_size / fs
times, x = generate_and_save(lambda t: chirp(t, f1, duration, f2),
                             duration=duration,
                             fade_ends=False)

x_blocks = split_to_blocks(x, block_size, block_size)
X, X_cross_time, X_cross_freq, X_inst_freqs, X_group_delays = compute_spectra(
    x_blocks[0], w)

idx = (X_inst_freqs >= f1 / fs) & (X_inst_freqs <= f2 / fs)
plt.scatter(X_group_delays[idx],
            X_inst_freqs[idx],
            alpha=0.5,
            s=abs(X)[idx],
            c=abs(X)[idx])
from scipy.signal import chirp

from analysis import split_to_blocks
from synthesis import generate_and_save
from reassignment import compute_spectra

block_size = 2048
fs = 44100
f1, f2 = 440, 880
duration = block_size / fs
times, x = generate_and_save(lambda t: chirp(t, f1, duration, f2),
    duration=duration, fade_ends=False)

x_blocks = split_to_blocks(x, block_size, block_size)
X, X_cross_time, X_cross_freq, X_inst_freqs, X_group_delays = compute_spectra(x_blocks[0], w)


idx = (X_inst_freqs >= f1 / fs) & (X_inst_freqs <= f2 / fs)
plt.scatter(X_group_delays[idx], X_inst_freqs[idx], alpha=0.5, s=abs(X)[idx], c=abs(X)[idx])
def generate_and_play(func, filename='test.wav', **kwargs):
    t, samples = generate_and_save(func, filename, **kwargs)
    play(filename)
    return t, samples
def complex_tone(partials):
    # partial = ((freq0, amp0), (freq1, amp1), ...)
    return lambda t: np.sum(
        sine(t, freq=freq, amplitude=amp) for (freq, amp) in partials)


def dissonance(base, difference):
    '''Returns the timbre composed of two sines of unit amplitude.'''
    return ((base, 1), (base + difference, 1))


def uniform_random(size, a, b):
    return a + (b - a) * np.random.random(size)


if __name__ == '__main__':
    numpy.random.seed(42)

    log_diff_range = (-1, 4)

    sample_count = 100
    log_diffs = uniform_random(sample_count, *log_diff_range)

    base = 440
    for log_diff in log_diffs:
        diff = 10**log_diff
        generate_and_save(complex_tone(dissonance(base, diff)),
                          filename="%s_%s.wav" % (base, diff),
                          duration=3)