Ejemplo n.º 1
0
    # compute interpolated frame
    new_grain.norm = frac * t_norms[0] + (1. - frac) * t_norms[1]
    new_grain.phas = phas_acc
    #print t, step, new_grain.norm
    #print t, step, phas_acc

    # psola
    samples = p.rdo(new_grain)
    if t > warmup: # skip the first few frames to warm up phase vocoder
        # write to sink
        sink_out(samples, hop_s)

    # calculate phase advance
    dphas = t_phases[1] - t_phases[0] - phi_advance
    # unwrap angle to [-pi; pi]
    dphas = unwrap2pi(dphas)
    # cumulate phase, to be used for next frame
    phas_acc += phi_advance + dphas

for t in range(warmup + 1): # purge the last frames from the phase vocoder
    new_grain.norm[:] = 0
    new_grain.phas[:] = 0
    samples = p.rdo(new_grain)
    sink_out(samples, read if t == warmup else hop_s)

# just to make sure
#sink_out.close()

format_out = "read {:d} blocks from {:s} at {:d}Hz and rate {:f}, wrote {:d} blocks to {:s}"
print (format_out.format(block_read, source_filename, samplerate, rate,
    len(steps), output_filename))
Ejemplo n.º 2
0
 def test_unwrap2pi_takes_array_of_float32(self):
     a = arange(-10, 10).astype("float32")
     b = unwrap2pi(a)
     #print zip(a, b)
     assert ( b > -pi ).all()
     assert ( b <= pi ).all()
Ejemplo n.º 3
0
def Stretch(inputfilename, outputfilename, stretchfactor, sample_rate=0):

    win_s = 1024
    hop_s = win_s // 8  # 87.5 % overlap

    warmup = win_s // hop_s - 1

    source_filename = inputfilename
    output_filename = outputfilename
    rate = float(stretchfactor)

    samplerate = sample_rate
    source_in = source(source_filename, samplerate, hop_s)
    samplerate = source_in.samplerate
    p = pvoc(win_s, hop_s)

    # allocate memory to store norms and phases
    n_blocks = source_in.duration // hop_s + 1
    # adding an empty frame at end of spectrogram
    norms = np.zeros((n_blocks + 1, win_s // 2 + 1), dtype=float_type)
    phases = np.zeros((n_blocks + 1, win_s // 2 + 1), dtype=float_type)

    block_read = 0
    while True:
        # read from source
        samples, read = source_in()
        # compute fftgrain
        spec = p(samples)
        # store current grain
        norms[block_read] = spec.norm
        phases[block_read] = spec.phas
        # until end of file
        if read < hop_s: break
        # increment block counter
        block_read += 1

        # just to make sure
        #source_in.close()

    sink_out = sink(output_filename, samplerate)

    # interpolated time steps (j = alpha * i)
    steps = np.arange(0, n_blocks, rate, dtype=float_type)
    # initial phase
    phas_acc = phases[0]
    # excepted phase advance in each bin
    phi_advance = np.linspace(0, np.pi * hop_s,
                              win_s / 2 + 1).astype(float_type)

    new_grain = cvec(win_s)

    for (t, step) in enumerate(steps):

        frac = 1. - np.mod(step, 1.0)
        # get pair of frames
        t_norms = norms[int(step):int(step + 2)]
        t_phases = phases[int(step):int(step + 2)]

        # compute interpolated frame
        new_grain.norm = frac * t_norms[0] + (1. - frac) * t_norms[1]
        new_grain.phas = phas_acc
        #print t, step, new_grain.norm
        #print t, step, phas_acc

        # psola
        samples = p.rdo(new_grain)
        if t > warmup:  # skip the first few frames to warm up phase vocoder
            # write to sink
            sink_out(samples, hop_s)

        # calculate phase advance
        dphas = t_phases[1] - t_phases[0] - phi_advance
        # unwrap angle to [-pi; pi]
        dphas = unwrap2pi(dphas)
        # cumulate phase, to be used for next frame
        phas_acc += phi_advance + dphas

    for t in range(warmup + 1):  # purge the last frames from the phase vocoder
        new_grain.norm[:] = 0
        new_grain.phas[:] = 0
        samples = p.rdo(new_grain)
        sink_out(samples, read if t == warmup else hop_s)

    # just to make sure
    #sink_out.close()

    format_out = "read {:d} blocks from {:s} at {:d}Hz and rate {:f}, wrote {:d} blocks to {:s}"
    print(
        format_out.format(block_read, source_filename, samplerate, rate,
                          len(steps), output_filename))
Ejemplo n.º 4
0
 def test_unwrap2pi_fails_on_list(self):
     with self.assertRaises((TypeError, NotImplementedError)):
         unwrap2pi(["23.","24.",25.])
Ejemplo n.º 5
0
 def test_unwrap2pi_takes_fvec(self):
     a = fvec(10)
     b = unwrap2pi(a)
     #print zip(a, b)
     assert ( b > -pi ).all()
     assert ( b <= pi ).all()
Ejemplo n.º 6
0
 def test_unwrap2pi_takes_array_of_float32(self):
     a = arange(-10, 10).astype("float32")
     b = unwrap2pi(a)
     #print zip(a, b)
     assert (b > -pi).all()
     assert (b <= pi).all()
Ejemplo n.º 7
0
 def test_unwrap2pi(self):
     unwrap2pi(int(23))
     unwrap2pi(float(23.))
     unwrap2pi(int(23.))
     unwrap2pi(arange(10))
     unwrap2pi(arange(10).astype("int"))
     unwrap2pi(arange(10).astype("float"))
     unwrap2pi(arange(10).astype("float32"))
     unwrap2pi([1,3,5])
     unwrap2pi([23.,24.,25.])
     a = fvec(10)
     a[:] = 4.
     unwrap2pi(a)
     a = pi/100. * arange(-600,600).astype("float")
     unwrap2pi(a)
Ejemplo n.º 8
0
    def test_unwrap2pi(self):
        unwrap2pi(int(23))
        unwrap2pi(float(23.))
        unwrap2pi(long(23.))
        unwrap2pi(arange(10))
        unwrap2pi(arange(10).astype("int"))
        unwrap2pi(arange(10).astype("float"))
        unwrap2pi(arange(10).astype("float32"))
        unwrap2pi([1, 3, 5])
        unwrap2pi([23., 24., 25.])
        a = fvec(10)
        a[:] = 4.
        unwrap2pi(a)
        a = pi / 100. * arange(-600, 600).astype("float")
        b = unwrap2pi(a)
        #print zip(a, b)

        try:
            print unwrap2pi(["23.", "24.", 25.])
        except Exception, e:
            pass
Ejemplo n.º 9
0
 def test_unwrap2pi_takes_fvec(self):
     a = fvec(10)
     b = unwrap2pi(a)
     #print zip(a, b)
     assert (b > -pi).all()
     assert (b <= pi).all()
Ejemplo n.º 10
0
        frac = 1. - np.mod(interp_read, 1.0)

        # compute interpolated frame
        new_grain.norm = frac * old_grain.norm + (1. - frac) * cur_grain.norm
        new_grain.phas = phas_acc

        # psola
        samples = p.rdo(new_grain)
        if interp_read > warmup: # skip the first frames to warm up phase vocoder
            # write to sink
            sink_out(samples, hop_s)

        # calculate phase advance
        dphas = cur_grain.phas - old_grain.phas - phi_advance
        # unwrap angle to [-pi; pi]
        dphas = unwrap2pi(dphas)
        # cumulate phase, to be used for next frame
        phas_acc += phi_advance + dphas

        # prepare for next interp block
        interp_block += 1
        interp_read = interp_block * rate
        if interp_read >= block_read:
            break

    # copy cur_grain to old_grain
    old_grain.norm = np.copy(cur_grain.norm)
    old_grain.phas = np.copy(cur_grain.phas)

    block_read += 1
    if read < hop_s: break
Ejemplo n.º 11
0
    def alter_track_tempo(source_filename,
                          output_filename,
                          rate,
                          sample_rate=0):
        win_s = 512
        hop_s = win_s // 8  # 87.5 % overlap

        warm_up = win_s // hop_s - 1
        source_in = source(source_filename, sample_rate, hop_s)
        sample_rate = source_in.samplerate
        p = pvoc(win_s, hop_s)

        sink_out = sink(output_filename, sample_rate)

        # excepted phase advance in each bin
        phi_advance = np.linspace(0, np.pi * hop_s,
                                  win_s / 2 + 1).astype(float_type)

        old_grain = cvec(win_s)
        new_grain = cvec(win_s)

        block_read = 0
        interp_read = 0
        interp_block = 0

        while True:
            samples, read = source_in()
            cur_grain = p(samples)

            if block_read == 1:
                phas_acc = old_grain.phas

            # print "block_read", block_read
            while True and (block_read > 0):
                if interp_read >= block_read:
                    break
                # print "`--- interp_block:", interp_block,
                # print 'at orig_block', interp_read, '<- from', block_read - 1, block_read,
                # print 'old_grain', old_grain, 'cur_grain', cur_grain
                # time to compute interp grain
                frac = 1. - np.mod(interp_read, 1.0)

                # compute interpolated frame
                new_grain.norm = frac * old_grain.norm + (
                    1. - frac) * cur_grain.norm
                new_grain.phas = phas_acc

                # psola
                samples = p.rdo(new_grain)
                if interp_read > warm_up:  # skip the first frames to warm up phase vocoder
                    # write to sink
                    sink_out(samples, hop_s)

                # calculate phase advance
                dphas = cur_grain.phas - old_grain.phas - phi_advance
                # unwrap angle to [-pi; pi]
                dphas = unwrap2pi(dphas)
                # cumulate phase, to be used for next frame
                phas_acc += phi_advance + dphas

                # prepare for next interp block
                interp_block += 1
                interp_read = interp_block * rate
                if interp_read >= block_read:
                    break

            # copy cur_grain to old_grain
            old_grain.norm = np.copy(cur_grain.norm)
            old_grain.phas = np.copy(cur_grain.phas)

            # until end of file
            if read < hop_s: break
            # increment block counter
            block_read += 1

        for t in range(warm_up +
                       2):  # purge the last frames from the phase vocoder
            new_grain.norm[:] = 0
            new_grain.phas[:] = 0
            samples = p.rdo(new_grain)
            sink_out(samples, read if t == warm_up + 1 else hop_s)

        # just to make sure
        source_in.close()
        sink_out.close()

        format_out = "read {:d} blocks from {:s} at {:d}Hz and rate {:f}, wrote {:d} blocks to {:s}"
        logging.info(
            format_out.format(block_read, source_filename, sample_rate, rate,
                              interp_block, output_filename))
Ejemplo n.º 12
0
    def test_unwrap2pi(self):
        unwrap2pi(int(23))
        unwrap2pi(float(23.))
        unwrap2pi(int(23.))
        unwrap2pi(arange(10))
        unwrap2pi(arange(10).astype("int"))
        unwrap2pi(arange(10).astype("float"))
        unwrap2pi(arange(10).astype("float32"))
        unwrap2pi([1,3,5])
        unwrap2pi([23.,24.,25.])
        a = fvec(10)
        a[:] = 4.
        unwrap2pi(a)
        a = pi/100. * arange(-600,600).astype("float")
        b = unwrap2pi (a)
        #print zip(a, b)

        try:
            print(unwrap2pi(["23.","24.",25.]))
        except Exception as e:
            pass
Ejemplo n.º 13
0
    def _stretch_sound(samples, win_s, hop_s, n):
        warmup = win_s // hop_s - 1

        p = pvoc(win_s, hop_s)

        # allocate memory to store norms and phases
        n_blocks = len(samples) // hop_s + 1
        # adding an empty frame at end of spectrogram
        norms = np.zeros((n_blocks + 1, win_s // 2 + 1), dtype=float_type)
        phases = np.zeros((n_blocks + 1, win_s // 2 + 1), dtype=float_type)

        block_read = 0
        steps_max = len(samples) - (len(samples) % hop_s)

        for i in range(0, steps_max, hop_s):
            # pitchin = pitch_o(samples[i:i + self._hop_size])
            # read from source
            samples_proc = np.asarray(samples[i:i + hop_s]).astype(float_type)
            # compute fftgrain
            spec = p(samples_proc)
            # store current grain
            norms[block_read] = spec.norm
            phases[block_read] = spec.phas
            # increment block counter
            block_read += 1

        # just to make sure
        # source_in.close()

        sink_out = np.ndarray([], dtype=float_type)

        # interpolated time steps (j = alpha * i)
        steps = np.arange(0, n_blocks, n, dtype=float_type)
        # initial phase
        phas_acc = phases[0]
        # excepted phase advance in each bin
        phi_advance = np.linspace(0, np.pi * hop_s,
                                  win_s / 2 + 1).astype(float_type)

        new_grain = cvec(win_s)

        for (t, step) in enumerate(steps):

            frac = 1. - np.mod(step, 1.0)
            # get pair of frames
            t_norms = norms[int(step):int(step + 2)]
            t_phases = phases[int(step):int(step + 2)]

            # compute interpolated frame
            new_grain.norm = frac * t_norms[0] + (1. - frac) * t_norms[1]
            new_grain.phas = phas_acc
            # print t, step, new_grain.norm
            # print t, step, phas_acc

            # psola
            samples_proc = p.rdo(new_grain)
            if t > warmup:  # skip the first few frames to warm up phase vocoder
                # write to sink
                sink_out = np.append(sink_out, samples_proc)

            # calculate phase advance
            dphas = t_phases[1] - t_phases[0] - phi_advance
            # unwrap angle to [-pi; pi]
            dphas = unwrap2pi(dphas)
            # cumulate phase, to be used for next frame
            phas_acc += phi_advance + dphas

        for t in range(warmup +
                       1):  # purge the last frames from the phase vocoder
            new_grain.norm[:] = 0
            new_grain.phas[:] = 0
            samples_proc = p.rdo(new_grain)
            sink_out = np.append(sink_out, samples_proc)

        return sink_out
Ejemplo n.º 14
0
def timestretch(rate, samplerate, duration, win_s, hop_s, n_blocks, norms,
                phases, p, block_read, sink_out):
    # win_s = 1024
    # hop_s = win_s // 8 # 87.5 % overlap

    warmup = win_s // hop_s - 1

    # # if len(sys.argv) < 3:
    # #     print("Usage: {:s} <source_filename> <output_filename> <rate> [samplerate]".format(sys.argv[0]))
    # #     print("""Examples:
    # #     # twice faster
    # #     {0} track_01.mp3 track_01_faster.wav 2.0
    # #     # twice slower
    # #     {0} track_02.flac track_02_slower.wav 0.5
    # #     # one and a half time faster, resampling first the input to 22050
    # #     {0} track_02.flac track_02_slower.wav 1.5 22050""".format(sys.argv[0]))
    # #     sys.exit(1)

    # source_filename = sf
    # output_filename = of
    # rate = r

    # samplerate = 0
    # source_in = source(source_filename, samplerate, hop_s)
    # samplerate = sr
    # p = pvoc(win_s, hop_s)

    # # allocate memory to store norms and phases
    # n_blocks = dur // hop_s + 1
    # # adding an empty frame at end of spectrogram
    # norms  = np.zeros((n_blocks + 1, win_s // 2 + 1), dtype = float_type)
    # phases = np.zeros((n_blocks + 1, win_s // 2 + 1), dtype = float_type)

    # block_read = 0
    # while True:
    #     # read from source
    #     samples, read = source_in()
    #     # compute fftgrain
    #     spec = p(samples)
    #     # store current grain
    #     norms[block_read] = spec.norm
    #     phases[block_read] = spec.phas
    #     # until end of file
    #     if read < hop_s: break
    #     # increment block counter
    #     block_read += 1

    # # just to make sure
    # #source_in.close()

    # sink_out = sink('journey_test.wav', samplerate)

    # interpolated time steps (j = alpha * i)
    steps = np.arange(0, n_blocks, rate, dtype=float_type)
    # initial phase
    phas_acc = phases[0]
    # excepted phase advance in each bin
    phi_advance = np.linspace(0, np.pi * hop_s,
                              win_s / 2 + 1).astype(float_type)

    new_grain = cvec(win_s)

    for (t, step) in enumerate(steps):

        frac = 1. - np.mod(step, 1.0)
        # get pair of frames
        t_norms = norms[int(step):int(step + 2)]
        t_phases = phases[int(step):int(step + 2)]

        # print step

        # print t_norms.shape
        # print t_phases.shape

        # compute interpolated frame
        new_grain.norm = frac * t_norms[0] + (1. - frac) * t_norms[1]
        new_grain.phas = phas_acc
        #print t, step, new_grain.norm
        #print t, step, phas_acc

        # psola
        samples = p.rdo(new_grain)
        if t > warmup:  # skip the first few frames to warm up phase vocoder
            # write to sink
            sink_out(samples, hop_s)

        # calculate phase advance
        dphas = t_phases[1] - t_phases[0] - phi_advance
        # unwrap angle to [-pi; pi]
        dphas = unwrap2pi(dphas)
        # cumulate phase, to be used for next frame
        phas_acc += phi_advance + dphas

    for t in range(warmup + 1):  # purge the last frames from the phase vocoder
        new_grain.norm[:] = 0
        new_grain.phas[:] = 0
        samples = p.rdo(new_grain)
Ejemplo n.º 15
0
 def test_unwrap2pi_fails_on_list(self):
     with self.assertRaises((TypeError, NotImplementedError)):
         unwrap2pi(["23.", "24.", 25.])
Ejemplo n.º 16
0
 def test_unwrap2pi(self):
     unwrap2pi(int(23))
     unwrap2pi(float(23.))
     unwrap2pi(int(23.))
     unwrap2pi(arange(10))
     unwrap2pi(arange(10).astype("int"))
     unwrap2pi(arange(10).astype("float"))
     unwrap2pi(arange(10).astype("float32"))
     unwrap2pi([1, 3, 5])
     unwrap2pi([23., 24., 25.])
     a = fvec(10)
     a[:] = 4.
     unwrap2pi(a)
     a = pi / 100. * arange(-600, 600).astype("float")
     unwrap2pi(a)