Beispiel #1
0
    def setup(self, channels=None, samplerate=None, blocksize=None,
            totalframes=None):
        super(AubioEncoder, self).setup(
            channels, samplerate, blocksize, totalframes)
        print ('setting up aubio with with', samplerate, blocksize)

        self.sink = aubio.sink(self.filename, samplerate, channels)
Beispiel #2
0
def apply_filter(path):
    from aubio import source, sink, digital_filter
    from os.path import basename, splitext

    # open input file, get its samplerate
    s = source(path)
    samplerate = s.samplerate

    # create an A-weighting filter
    f = digital_filter(7)
    f.set_a_weighting(samplerate)
    # alternatively, apply another filter

    # create output file
    o = sink("filtered_" + splitext(basename(path))[0] + ".wav", samplerate)

    total_frames = 0
    while True:
        samples, read = s()
        filtered_samples = f(samples)
        o(filtered_samples, read)
        total_frames += read
        if read < s.hop_size: break

    duration = total_frames / float(samplerate)
    print ("read {:s}".format(s.uri))
    print ("applied A-weighting filtered ({:d} Hz)".format(samplerate))
    print ("wrote {:s} ({:.2f} s)".format(o.uri, duration))
def vocoder(file_id):
	
	in_file = '/tmp/don_robot/output/' + file_id + ".mp3"
	out_file = '/tmp/don_robot/output/' + file_id + "-voc.mp3"

	samplerate = 44100
	f = source(in_file, samplerate, 256)
	g = sink(out_file, samplerate)
	total_frames, read = 0, 256

	win_s = 512                          # fft size
	hop_s = win_s // 2                   # hop size
	pv = pvoc(win_s, hop_s)              # phase vocoder

	while read:
		samples, read = f()
		spectrum = pv(samples)           # compute spectrum
		spectrum.norm *= .5             # reduce amplitude a bit .8
		spectrum.phas[:] = 0.            # zero phase
		new_samples = pv.rdo(spectrum)   # compute modified samples
		g(new_samples, read)             # write to output
 		total_frames += read

	format_str = "read {:d} samples from {:s}, written to {:s}"
	print(format_str.format(total_frames, f.uri, g.uri))
	return out_file
Beispiel #4
0
 def test_read_with(self):
     samplerate = 44100
     sink_path = get_tmp_sink_path()
     vec = fvec(128)
     with sink(sink_path, samplerate) as g:
         for _ in range(10):
             g(vec, 128)
Beispiel #5
0
 def test_close_file_twice(self):
     samplerate = 44100
     sink_path = get_tmp_sink_path()
     g = sink(sink_path, samplerate)
     g.close()
     g.close()
     del_tmp_sink_path(sink_path)
Beispiel #6
0
    def test_read_and_write_multi(self):

        if not len(list_of_sounds):
            self.skipTest('add some sound files in \'python/tests/sounds\'')

        for path in list_of_sounds:
            for samplerate, hop_size in zip([0, 44100, 8000, 32000],
                                            [512, 1024, 64, 256]):
                f = source(path, samplerate, hop_size)
                if samplerate == 0: samplerate = f.samplerate
                sink_path = get_tmp_sink_path()
                g = sink(sink_path, samplerate, channels=f.channels)
                total_frames = 0
                while True:
                    vec, read = f.do_multi()
                    g.do_multi(vec, read)
                    total_frames += read
                    if read < f.hop_size: break
                if 0:
                    print("read",
                          "%.2fs" % (total_frames / float(f.samplerate)),
                          end=' ')
                    print("(", total_frames, "frames", "in", end=' ')
                    print(f.channels, "channels", "in", end=' ')
                    print(total_frames / f.hop_size,
                          "blocks",
                          "at",
                          "%dHz" % f.samplerate,
                          ")",
                          end=' ')
                    print("from", f.uri, end=' ')
                    print("to", g.uri, end=' ')
                    print("in", g.channels, "channels")
                del_tmp_sink_path(sink_path)
Beispiel #7
0
    def __init__(self, win_size=512, samplerate=44100):

        # total number of audio frames read
        self._total_frames = 0

        # list of beats, in seconds since start
        self._beats = []

        # recording window and hop size
        self._win_size = win_size
        self._hop_size = win_size // 2  # hop size

        # audio sample rate
        self._samplerate = samplerate

        # tempo finder algorithm instance
        self._tempo_alg = tempo("default", self._win_size, self._hop_size, self._samplerate)
        logging.info("Tempo silence=%d" % self._tempo_alg.get_silence())
        self._tempo_alg.set_silence(-40)

        self.on_pause = False

        # pitch algorithm instance
        # self.pitch_alg = pitch("default", self.win_size, self.hop_size, self.samplerate)

        self._mono_vec = numpy.array([], dtype=numpy.float32)
        self._tempo_found_callback = TempoFinder.default_tempo_found_callback
        self._starting_millis = time() * 1000.

        self._stream = sounddevice.InputStream(
            channels=1, samplerate=float(self._samplerate), dtype='float32',
            latency='low', callback=self.audio_callback)

        if WRITE_WAV:
            self._out_file = sink(samplerate=int(self._samplerate))
Beispiel #8
0
 def test_close_file_twice(self):
     samplerate = 44100
     sink_path = get_tmp_sink_path()
     g = sink(sink_path, samplerate)
     g.close()
     g.close()
     del_tmp_sink_path(sink_path)
Beispiel #9
0
def apply_filter(path, target):
    # open input file, get its samplerate
    s = aubio.source(path)
    samplerate = s.samplerate

    # create an A-weighting filter
    f = aubio.digital_filter(7)
    f.set_a_weighting(samplerate)

    # create output file
    o = aubio.sink(target, samplerate)

    total_frames = 0
    while True:
        # read from source
        samples, read = s()
        # filter samples
        filtered_samples = f(samples)
        # write to sink
        o(filtered_samples, read)
        # count frames read
        total_frames += read
        # end of file reached
        if read < s.hop_size: break

    # print some info
    duration = total_frames / float(samplerate)
    input_str = "input: {:s} ({:.2f} s, {:d} Hz)"
    output_str = "output: {:s}, A-weighting filtered ({:d} frames total)"
    print(input_str.format(s.uri, duration, samplerate))
    print(output_str.format(o.uri, total_frames))
Beispiel #10
0
def apply_filter(path, target):
    # open input file, get its samplerate
    s = aubio.source(path)
    samplerate = s.samplerate

    # create an A-weighting filter
    f = aubio.digital_filter(7)
    f.set_a_weighting(samplerate)

    # create output file
    o = aubio.sink(target, samplerate)

    total_frames = 0
    while True:
        # read from source
        samples, read = s()
        # filter samples
        filtered_samples = f(samples)
        # write to sink
        o(filtered_samples, read)
        # count frames read
        total_frames += read
        # end of file reached
        if read < s.hop_size:
            break

    # print some info
    duration = total_frames / float(samplerate)
    input_str = "input: {:s} ({:.2f} s, {:d} Hz)"
    output_str = "output: {:s}, A-weighting filtered ({:d} frames total)"
    print(input_str.format(s.uri, duration, samplerate))
    print(output_str.format(o.uri, total_frames))
Beispiel #11
0
    def _handle_detected_problem(self, matcher_name: str) -> None:
        now = datetime.datetime.now()
        datestamp = now.strftime("%Y-%m-%d")
        timestamp = now.strftime("%H:%M:%S")
        path = pathlib.Path(
            f"{tempfile.gettempdir()}/{self._name}_{datestamp}_{timestamp}.mp3"
        )

        # aubio only supports writing wav files, so use pydub to convert it to mp3
        with tempfile.NamedTemporaryFile(suffix=".wav") as f:
            with aubio.sink(f.name, self._source.samplerate) as output:
                for match_samples in self._window_samples:
                    output(match_samples, self._source.hop_size)

            wav_file = pydub.AudioSegment.from_wav(f)
            wav_file.export(
                path,
                format="mp3",
                tags={
                    "artist":
                    "Stream Monitor",
                    "title":
                    f"Problematic audio from stream {self._name} on {datestamp} at {timestamp}",
                },
            )

            try:
                self._problem_callback(self._name, matcher_name, path)
            finally:
                path.unlink()
Beispiel #12
0
def vocoder(file_id):

    in_file = '/tmp/don_robot/output/' + file_id + ".mp3"
    out_file = '/tmp/don_robot/output/' + file_id + "-voc.mp3"

    samplerate = 44100
    f = source(in_file, samplerate, 256)
    g = sink(out_file, samplerate)
    total_frames, read = 0, 256

    win_s = 512  # fft size
    hop_s = win_s // 2  # hop size
    pv = pvoc(win_s, hop_s)  # phase vocoder

    while read:
        samples, read = f()
        spectrum = pv(samples)  # compute spectrum
        spectrum.norm *= .5  # reduce amplitude a bit .8
        spectrum.phas[:] = 0.  # zero phase
        new_samples = pv.rdo(spectrum)  # compute modified samples
        g(new_samples, read)  # write to output
        total_frames += read

    format_str = "read {:d} samples from {:s}, written to {:s}"
    print(format_str.format(total_frames, f.uri, g.uri))
    return out_file
Beispiel #13
0
def applyFilter(path, target):
    # open input file, get its samplerate
    s = source(path)
    samplerate = s.samplerate

    # create an A-weighting filter
    f = digital_filter(7)
    f.set_a_weighting(samplerate)

    # create output file
    o = sink(target, samplerate)

    total_frames = 0
    while True:
        # read from source
        samples, read = s()
        # filter samples
        filtered_samples = f(samples)
        # write to sink
        o(filtered_samples, read)
        # count frames read
        total_frames += read
        # end of file reached
        if read < s.hop_size:
            break
Beispiel #14
0
 def test_read_with(self):
     samplerate = 44100
     sink_path = get_tmp_sink_path()
     vec = fvec(128)
     with sink(sink_path, samplerate) as g:
         for _ in range(10):
             g(vec, 128)
Beispiel #15
0
    def apply_filter(self):
        # open input file, get its samplerate
        s = aubio.source(self.path)
        samplerate = s.samplerate

        # create an A-weighting filter
        f = aubio.digital_filter(7)
        f.set_a_weighting(samplerate)

        # create output file and replace original uploaded by user. Append the word filter to end of filename
        new_file_path = self.path[:-4]
        new_file_path += "_filter.wav"
        o = aubio.sink(new_file_path, samplerate)

        total_frames = 0
        while True:
            # read from source
            samples, read = s()
            # filter samples
            filtered_samples = f(samples)
            # write to sink
            o(filtered_samples, read)
            # count frames read
            total_frames += read
            # end of file reached
            if read < s.hop_size:
                break
Beispiel #16
0
def apply_filter(path):
    from aubio import source, sink, digital_filter
    from os.path import basename, splitext

    # open input file, get its samplerate
    s = source(path)
    samplerate = s.samplerate

    # create an A-weighting filter
    f = digital_filter(7)
    f.set_a_weighting(samplerate)
    # alternatively, apply another filter

    # create output file
    o = sink("filtered_" + splitext(basename(path))[0] + ".wav", samplerate)

    total_frames = 0
    while True:
        samples, read = s()
        filtered_samples = f(samples)
        o(filtered_samples, read)
        total_frames += read
        if read < s.hop_size: break

    duration = total_frames / float(samplerate)
    print("read {:s}".format(s.uri))
    print("applied A-weighting filtered ({:d} Hz)".format(samplerate))
    print("wrote {:s} ({:.2f} s)".format(o.uri, duration))
Beispiel #17
0
 def test_many_sinks(self):
     for i in range(100):
         g = sink('/tmp/f.wav', 0)
         write = 256
         for n in range(200):
             vec = fvec(write)
             g(vec, write)
         del g
Beispiel #18
0
 def test_read_and_write_multi(self, hop_size, samplerate, path):
     try:
         f = source(path, samplerate, hop_size)
     except RuntimeError as e:
         self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e)))
     if samplerate == 0: samplerate = f.samplerate
     sink_path = get_tmp_sink_path()
     g = sink(sink_path, samplerate, channels = f.channels)
     total_frames = 0
     while True:
         vec, read = f.do_multi()
         g.do_multi(vec, read)
         total_frames += read
         if read < f.hop_size: break
     del_tmp_sink_path(sink_path)
Beispiel #19
0
 def test_many_sinks(self):
     from tempfile import mkdtemp
     import os.path
     import shutil
     tmpdir = mkdtemp()
     sink_list = []
     for i in range(many_files):
         path = os.path.join(tmpdir, 'f-' + str(i) + '.wav')
         g = sink(path, 0)
         sink_list.append(g)
         write = 32
         for _ in range(200):
             vec = fvec(write)
             g(vec, write)
         g.close()
     shutil.rmtree(tmpdir)
Beispiel #20
0
 def test_many_sinks(self):
     from tempfile import mkdtemp
     import os.path
     import shutil
     tmpdir = mkdtemp()
     sink_list = []
     for i in range(many_files):
         path = os.path.join(tmpdir, 'f-' + str(i) + '.wav')
         g = sink(path, 0)
         sink_list.append(g)
         write = 32
         for _ in range(200):
             vec = fvec(write)
             g(vec, write)
         g.close()
     shutil.rmtree(tmpdir)
Beispiel #21
0
 def test_read(self):
     for path in list_of_sounds:
         for samplerate, hop_size in zip([0, 44100, 8000, 32000], [512, 1024, 64, 256]):
             f = source(path, samplerate, hop_size)
             if samplerate == 0: samplerate = f.samplerate
             g = sink('/tmp/f.wav', samplerate)
             total_frames = 0
             while True:
                 vec, read = f()
                 g(vec, read)
                 total_frames += read
                 if read < f.hop_size: break
             print "read", "%.2fs" % (total_frames / float(f.samplerate) ),
             print "(", total_frames, "frames", "in",
             print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",
             print "from", f.uri,
             print "to", g.uri
Beispiel #22
0
    def __call__(self, pipe):
        for pool in pipe:
            framesize = 2048
            buff = pool["buffer"]

            sink = aubio.sink(self.outfile, 0, self.mediafile.channels)
            out_samples = numpy.array_split(
                buff, int(float(self.mediafile.duration) / int(framesize)),
                axis=1)

            for frame in out_samples:
                amount = frame[0].shape[0]
                sink.do_multi(frame, amount)

            sink.close()
            del sink

            yield {"out": self.outfile}
Beispiel #23
0
def apply_filter(path, params={}):
    from aubio import source, sink, digital_filter
    from os.path import basename, splitex, splitextt
    s = source(path)
    f = digital_filter(7)
    f.set_a_weighting(s.samplerate)
    #f = digital_filter(3)
    #f.set_biquad(...)
    o = sink("filtered_" + splitext(basename(path))[0] + ".wav")
    # Total number of frames read
    total_frames = 0

    while True:
        samples, read = s()
        filtered_samples = f(samples)
        o(samples, read)
        total_frames += read
        if read < s.hop_size: break
    print "filtered", s.uri, "to", o.uri, "using an A-weighting filter"
Beispiel #24
0
    def __call__(self, pipe):
        for pool in pipe:
            framesize = 2048
            buff = pool["buffer"]

            sink = aubio.sink(self.outfile, 0, self.mediafile.channels)
            out_samples = numpy.array_split(
                buff,
                int(float(self.mediafile.duration) / int(framesize)),
                axis=1)

            for frame in out_samples:
                amount = frame[0].shape[0]
                sink.do_multi(frame, amount)

            sink.close()
            del sink

            yield {"out": self.outfile}
def apply_filter(path, params = {}):
    from aubio import source, sink, digital_filter
    from os.path import basename, splitex, splitextt
    s = source(path)
    f = digital_filter(7)
    f.set_a_weighting(s.samplerate)
    #f = digital_filter(3)
    #f.set_biquad(...)
    o = sink("filtered_" + splitext(basename(path))[0] + ".wav")
    # Total number of frames read
    total_frames = 0

    while True:
        samples, read = s()
        filtered_samples = f(samples)
        o(samples, read)
        total_frames += read
        if read < s.hop_size: break
    print "filtered", s.uri, "to", o.uri, "using an A-weighting filter"
def record_sink(sink_path):
    """Record an audio file using pysoundcard."""

    from aubio import sink
    from pysoundcard import Stream

    hop_size = 256
    duration = 5  # in seconds
    s = Stream(block_length=hop_size)
    g = sink(sink_path, samplerate=s.sample_rate)

    s.start()
    total_frames = 0
    while total_frames < duration * s.sample_rate:
        vec = s.read(hop_size)
        # mix down to mono
        mono_vec = vec.sum(-1) / float(s.input_channels)
        g(mono_vec, hop_size)
        total_frames += hop_size
    s.stop()
def record_sink(sink_path):
    """Record an audio file using pysoundcard."""

    from aubio import sink
    from pysoundcard import Stream

    hop_size = 256
    duration = 5 # in seconds
    s = Stream(block_length = hop_size)
    g = sink(sink_path, samplerate = s.sample_rate)

    s.start()
    total_frames = 0
    while total_frames < duration * s.sample_rate:
        vec = s.read(hop_size)
        # mix down to mono
        mono_vec = vec.sum(-1) / float(s.input_channels)
        g(mono_vec, hop_size)
        total_frames += hop_size
    s.stop()
    def audio_filter(self):
        """
        Taken from https://github.com/aubio/aubio/tree/master/python/demos
        This was used for testing high and low pass filters which clean up the unwanted low and high frequencies.
        This was done in an attempt to remove unwanted noise from the microphone signal. It may be useful in the
        future
        """
        # open input file, get its samplerate
        s = aubio.source(self.filename)
        samplerate = s.samplerate

        target = "./audio/filteredAudio/filter.wav"

        # A-weighting filter is 7, C-weighting is 5
        # See https://en.wikipedia.org/wiki/A-weighting for a brief overview
        f = aubio.digital_filter(7)
        f.set_a_weighting(samplerate)

        # create output file
        o = aubio.sink(target, samplerate)

        total_frames = 0
        while True:
            # read from source
            samples, read = s()
            # filter samples
            filtered_samples = f(samples)
            # write to sink
            o(filtered_samples, read)
            # count frames read
            total_frames += read
            # end of file reached
            if read < s.hop_size:
                break

        # print some info
        duration = total_frames / float(samplerate)
        input_str = "input: {:s} ({:.2f} s, {:d} Hz)"
        output_str = "output: {:s}, A-weighting filtered ({:d} frames total)"
        print(input_str.format(s.uri, duration, samplerate))
        print(output_str.format(o.uri, total_frames))
    def check_write_and_read(self, samplerate, channels, hop_size, blocks,
                             write_samples):
        expected_mono = np.sum(write_samples, axis=0) / write_samples.shape[0]
        out = get_tmp_sink_path()
        snk = aubio.sink(out, samplerate, channels=channels)
        for i in range(blocks):
            snk.do_multi(write_samples, hop_size)
        # close the sink before reading from it
        snk.close()

        src = aubio.source(out, samplerate, hop_size)
        for i in range(blocks):
            read_samples, read = src.do_multi()
            assert_equal(read_samples, write_samples)
            assert_equal(read, hop_size)

        src.seek(0)
        for i in range(blocks):
            read_samples, read = src()
            assert_equal(read, hop_size)
            assert_equal(read_samples, expected_mono)
Beispiel #30
0
 def test_read_and_write_multi(self, hop_size, samplerate, path):
     orig_samplerate = parse_file_samplerate(soundfile)
     try:
         if orig_samplerate is not None and orig_samplerate < samplerate:
             # upsampling should emit a warning
             with assert_warns(UserWarning):
                 f = source(soundfile, samplerate, hop_size)
         else:
             f = source(soundfile, samplerate, hop_size)
     except RuntimeError as e:
         err_msg = '{:s} (hop_s = {:d}, samplerate = {:d})'
         skipTest(err_msg.format(str(e), hop_size, samplerate))
     if samplerate == 0: samplerate = f.samplerate
     sink_path = get_tmp_sink_path()
     g = sink(sink_path, samplerate, channels = f.channels)
     total_frames = 0
     while True:
         vec, read = f.do_multi()
         g.do_multi(vec, read)
         total_frames += read
         if read < f.hop_size: break
     del_tmp_sink_path(sink_path)
Beispiel #31
0
 def test_read_and_write_multi(self, hop_size, samplerate, path):
     orig_samplerate = parse_file_samplerate(soundfile)
     try:
         if orig_samplerate is not None and orig_samplerate < samplerate:
             # upsampling should emit a warning
             with assert_warns(UserWarning):
                 f = source(soundfile, samplerate, hop_size)
         else:
             f = source(soundfile, samplerate, hop_size)
     except RuntimeError as e:
         err_msg = '{:s} (hop_s = {:d}, samplerate = {:d})'
         skipTest(err_msg.format(str(e), hop_size, samplerate))
     if samplerate == 0: samplerate = f.samplerate
     sink_path = get_tmp_sink_path()
     g = sink(sink_path, samplerate, channels=f.channels)
     total_frames = 0
     while True:
         vec, read = f.do_multi()
         g.do_multi(vec, read)
         total_frames += read
         if read < f.hop_size: break
     del_tmp_sink_path(sink_path)
Beispiel #32
0
def Robotize(inputfilename, outputfilename):

    samplerate = 44100
    f = source(inputfilename, samplerate, 256)
    g = sink(outputfilename, samplerate)
    total_frames, read = 0, 256

    win_s = 512                          # fft size
    hop_s = win_s // 2                   # hop size
    pv = pvoc(win_s, hop_s)              # phase vocoder

    while read:
        samples, read = f()
        spectrum = pv(samples)           # compute spectrum
        # spectrum.norm *= .8             # reduce amplitude a bit
        spectrum.phas[:] = 0.            # zero phase
        new_samples = pv.rdo(spectrum)   # compute modified samples
        g(new_samples, read)             # write to output
        total_frames += read

    format_str = "read {:d} samples from {:s}, written to {:s}"
    print(format_str.format(total_frames, f.uri, g.uri))
Beispiel #33
0
 def test_many_sinks_not_closed(self):
     from tempfile import mkdtemp
     import os.path
     import shutil
     tmpdir = mkdtemp()
     sink_list = []
     try:
         for i in range(many_files):
             path = os.path.join(tmpdir, 'f-' + str(i) + '.wav')
             g = sink(path, 0)
             sink_list.append(g)
             write = 256
             for n in range(200):
                 vec = fvec(write)
                 g(vec, write)
     except Exception:
         pass
     else:
         self.fail("does not fail on too many files open")
     for g in sink_list:
         g.close()
     shutil.rmtree(tmpdir)
def record_sink(sink_path):
    """Record an audio file using pysoundcard."""

    from aubio import sink
    from pysoundcard import Stream

    hop_size = 256
    duration = 5 # in seconds
    s = Stream(blocksize = hop_size, channels = 1)
    g = sink(sink_path, samplerate = int(s.samplerate))

    s.start()
    total_frames = 0
    try:
        while total_frames < duration * s.samplerate:
            vec = s.read(hop_size)
            # mix down to mono
            mono_vec = vec.sum(-1) / float(s.channels[0])
            g(mono_vec, hop_size)
            total_frames += hop_size
    except KeyboardInterrupt:
        duration = total_frames / float(s.samplerate)
        print("stopped after %.2f seconds" % duration)
    s.stop()
Beispiel #35
0
def record_sink(sink_path):
    """Record an audio file using pysoundcard."""

    from aubio import sink
    from pysoundcard import Stream

    hop_size = 256
    duration = 5  # in seconds
    s = Stream(blocksize=hop_size, channels=1)
    g = sink(sink_path, samplerate=int(s.samplerate))

    s.start()
    total_frames = 0
    try:
        while total_frames < duration * s.samplerate:
            vec = s.read(hop_size)
            # mix down to mono
            mono_vec = vec.sum(-1) / float(s.channels[0])
            g(mono_vec, hop_size)
            total_frames += hop_size
    except KeyboardInterrupt:
        duration = total_frames / float(s.samplerate)
        print("stopped after %.2f seconds" % duration)
    s.stop()
Beispiel #36
0
 def test_wrong_samplerate(self):
     with self.assertRaises(RuntimeError):
         sink(get_tmp_sink_path(), -1)
Beispiel #37
0
 def test_wrong_filename(self):
     with self.assertRaises(RuntimeError):
         sink('')
Beispiel #38
0
 def test_wrong_channels_too_large(self):
     with assert_raises(RuntimeError):
         sink(get_tmp_sink_path(), 44100, 202020)
Beispiel #39
0
def slice_source_at_stamps(source_file, timestamps, timestamps_end=None,
                           output_dir=None, samplerate=0, hopsize=256):
    """ slice a sound file at given timestamps """

    if timestamps is None or len(timestamps) == 0:
        raise ValueError("no timestamps given")

    if timestamps[0] != 0:
        timestamps = [0] + timestamps
        if timestamps_end is not None:
            timestamps_end = [timestamps[1] - 1] + timestamps_end

    if timestamps_end is not None:
        if len(timestamps_end) != len(timestamps):
            raise ValueError("len(timestamps_end) != len(timestamps)")
    else:
        timestamps_end = [t - 1 for t in timestamps[1:]] + [_max_timestamp]

    regions = list(zip(timestamps, timestamps_end))
    #print regions

    source_base_name, _ = os.path.splitext(os.path.basename(source_file))
    if output_dir is not None:
        if not os.path.isdir(output_dir):
            os.makedirs(output_dir)
        source_base_name = os.path.join(output_dir, source_base_name)

    def new_sink_name(source_base_name, timestamp, samplerate):
        """ create a sink based on a timestamp in samples, converted in seconds """
        timestamp_seconds = timestamp / float(samplerate)
        return source_base_name + "_%011.6f" % timestamp_seconds + '.wav'

    # open source file
    _source = source(source_file, samplerate, hopsize)
    samplerate = _source.samplerate

    total_frames = 0
    slices = []

    while True:
        # get hopsize new samples from source
        vec, read = _source.do_multi()
        # if the total number of frames read will exceed the next region start
        if len(regions) and total_frames + read >= regions[0][0]:
            #print "getting", regions[0], "at", total_frames
            # get next region
            start_stamp, end_stamp = regions.pop(0)
            # create a name for the sink
            new_sink_path = new_sink_name(source_base_name, start_stamp, samplerate)
            # create its sink
            _sink = sink(new_sink_path, samplerate, _source.channels)
            # create a dictionary containing all this
            new_slice = {'start_stamp': start_stamp, 'end_stamp': end_stamp, 'sink': _sink}
            # append the dictionary to the current list of slices
            slices.append(new_slice)

        for current_slice in slices:
            start_stamp = current_slice['start_stamp']
            end_stamp = current_slice['end_stamp']
            _sink = current_slice['sink']
            # sample index to start writing from new source vector
            start = max(start_stamp - total_frames, 0)
            # number of samples yet to written be until end of region
            remaining = end_stamp - total_frames + 1
            #print current_slice, remaining, start
            # not enough frames remaining, time to split
            if remaining < read:
                if remaining > start:
                    # write remaining samples from current region
                    _sink.do_multi(vec[:, start:remaining], remaining - start)
                    #print "closing region", "remaining", remaining
                    # close this file
                    _sink.close()
            elif read > start:
                # write all the samples
                _sink.do_multi(vec[:, start:read], read - start)
        total_frames += read
        if read < hopsize:
            break
Beispiel #40
0
def slice_source_at_stamps(source_file, timestamps, timestamps_end=None,
                           output_dir=None, samplerate=0, hopsize=256,
                           create_first=False):
    """Slice a sound file at given timestamps.

    This function reads `source_file` and creates slices, new smaller
    files each starting at `t` in `timestamps`, a list of integer
    corresponding to time locations in `source_file`, in samples.

    If `timestamps_end` is unspecified, the slices will end at
    `timestamps_end[n] = timestamps[n+1]-1`, or the end of file.
    Otherwise, `timestamps_end` should be a list with the same length
    as `timestamps` containing the locations of the end of each slice.

    If `output_dir` is unspecified, the new slices will be written in
    the current directory. If `output_dir` is a string, new slices
    will be written in `output_dir`, after creating the directory if
    required.

    The default `samplerate` is 0, meaning the original sampling rate
    of `source_file` will be used. When using a sampling rate
    different to the one of the original files, `timestamps` and
    `timestamps_end` should be expressed in the re-sampled signal.

    The `hopsize` parameter simply tells :class:`source` to use this
    hopsize and does not change the output slices.

    If `create_first` is True and `timestamps` does not start with `0`, the
    first slice from `0` to `timestamps[0] - 1` will be automatically added.

    Parameters
    ----------
    source_file : str
        path of the resource to slice
    timestamps : :obj:`list` of :obj:`int`
        time stamps at which to slice, in samples
    timestamps_end : :obj:`list` of :obj:`int` (optional)
        time stamps at which to end the slices
    output_dir : str (optional)
        output directory to write the slices to
    samplerate : int (optional)
        samplerate to read the file at
    hopsize : int (optional)
        number of samples read from source per iteration
    create_first : bool (optional)
        always create the slice at the start of the file

    Examples
    --------
    Create two slices: the first slice starts at the beginning of the
    input file `loop.wav` and lasts exactly one second, starting at
    sample `0` and ending at sample `44099`; the second slice starts
    at sample `44100` and lasts until the end of the input file:

    >>> aubio.slice_source_at_stamps('loop.wav', [0, 44100])

    Create one slice, from 1 second to 2 seconds:

    >>> aubio.slice_source_at_stamps('loop.wav', [44100], [44100 * 2 - 1])

    Notes
    -----
    Slices may be overlapping. If `timestamps_end` is `1` element
    shorter than `timestamps`, the last slice will end at the end of
    the file.
    """

    if not timestamps:
        raise ValueError("no timestamps given")

    if timestamps[0] != 0 and create_first:
        timestamps = [0] + timestamps
        if timestamps_end is not None:
            timestamps_end = [timestamps[1] - 1] + timestamps_end

    if timestamps_end is not None:
        if len(timestamps_end) == len(timestamps) - 1:
            timestamps_end = timestamps_end + [_max_timestamp]
        elif len(timestamps_end) != len(timestamps):
            raise ValueError("len(timestamps_end) != len(timestamps)")
    else:
        timestamps_end = [t - 1 for t in timestamps[1:]] + [_max_timestamp]

    regions = list(zip(timestamps, timestamps_end))

    source_base_name, _ = os.path.splitext(os.path.basename(source_file))
    if output_dir is not None:
        if not os.path.isdir(output_dir):
            os.makedirs(output_dir)
        source_base_name = os.path.join(output_dir, source_base_name)

    def _new_sink_name(source_base_name, timestamp, samplerate):
        # create name based on a timestamp in samples, converted in seconds
        timestamp_seconds = timestamp / float(samplerate)
        return source_base_name + "_%011.6f" % timestamp_seconds + '.wav'

    # open source file
    _source = source(source_file, samplerate, hopsize)
    samplerate = _source.samplerate

    total_frames = 0
    slices = []

    while True:
        # get hopsize new samples from source
        vec, read = _source.do_multi()
        # if the total number of frames read will exceed the next region start
        while regions and total_frames + read >= regions[0][0]:
            # get next region
            start_stamp, end_stamp = regions.pop(0)
            # create a name for the sink
            new_sink_path = _new_sink_name(source_base_name, start_stamp,
                                           samplerate)
            # create its sink
            _sink = sink(new_sink_path, samplerate, _source.channels)
            # create a dictionary containing all this
            new_slice = {'start_stamp': start_stamp, 'end_stamp': end_stamp,
                         'sink': _sink}
            # append the dictionary to the current list of slices
            slices.append(new_slice)

        for current_slice in slices:
            start_stamp = current_slice['start_stamp']
            end_stamp = current_slice['end_stamp']
            _sink = current_slice['sink']
            # sample index to start writing from new source vector
            start = max(start_stamp - total_frames, 0)
            # number of samples yet to written be until end of region
            remaining = end_stamp - total_frames + 1
            # not enough frames remaining, time to split
            if remaining < read:
                if remaining > start:
                    # write remaining samples from current region
                    _sink.do_multi(vec[:, start:remaining], remaining - start)
                    # close this file
                    _sink.close()
            elif read > start:
                # write all the samples
                _sink.do_multi(vec[:, start:read], read - start)
        total_frames += read
        # remove old slices
        slices = list(filter(lambda s: s['end_stamp'] > total_frames,
                             slices))
        if read < hopsize:
            break
    # create python/tests/sounds if needed
    output_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    output_dir = os.path.join(output_dir, 'tests', 'sounds')
    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)

    filenames = ['44100Hz_1f_silence.wav',
                 '22050Hz_5s_brownnoise.wav',
                 '32000Hz_127f_sine440.wav',
                ]
    samplerates = [44100, 22050, 32000]
    durations = [1, 5*22050, 127]

    for fname, samplerate, duration in zip(filenames, samplerates, durations):
        output_name = os.path.join(output_dir, fname)
        g = sink(output_name, samplerate)
        total_frames = 0
        while total_frames < duration:
            write = min(hop_size, duration - total_frames)
            if 'brownnoise' in fname:
                vec = np.random.rand(write).astype(float_type) * 2. - 1.
            elif 'sine' in fname:
                freq = 440
                t = np.arange(write).astype(float_type) + total_frames
                vec = np.sin(2. * np.pi * freq * t / float(samplerate))
            else:
                # silence
                vec = fvec(write)
            g(vec, write)
            total_frames += write
        outstr = "wrote {:2f}s".format(total_frames / float(samplerate))
Beispiel #42
0
 def test_wrong_samplerate(self):
     with assert_raises(RuntimeError):
         sink(get_tmp_sink_path(), -1)
Beispiel #43
0
    if len(sys.argv) < 3:
        print "usage: %s <inputfile> <duration>" % sys.argv[0]
        sys.exit(1)
    source_file = sys.argv[1]
    duration = float(sys.argv[2])
    source_base_name, source_ext = os.path.splitext(os.path.basename(source_file))

    hopsize = 256
    slice_n, total_frames_written, read = 0, 0, hopsize

    def new_sink_name(source_base_name, slice_n, duration=duration):
        return source_base_name + "_%02.3f" % (slice_n * duration) + ".wav"

    f = source(source_file, 0, hopsize)
    samplerate = f.samplerate
    g = sink(new_sink_name(source_base_name, slice_n), samplerate)

    # print "new slice:", slice_n, 0, "+", 0, "=", 0
    while read == hopsize:
        vec, read = f()
        start_of_next_region = int(duration * samplerate * (slice_n + 1))
        remaining = start_of_next_region - total_frames_written
        # number of samples remaining is less than what we got
        if remaining <= read:
            # write remaining samples from current region
            g(vec[0:remaining], remaining)
            # close this file
            del g
            # print "new slice", slice_n, total_frames_written, "+", remaining, "=", start_of_next_region
            slice_n += 1
            # create a new file for the new region
Beispiel #44
0
 def test_wrong_samplerate_too_large(self):
     with assert_raises(RuntimeError):
         sink(get_tmp_sink_path(), 1536001, 2)
Beispiel #45
0
import sys
from aubio import source, sink, pvoc, tss

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('usage: %s <inputfile> <outputfile_transient> <outputfile_steady>' % sys.argv[0])
        sys.exit(1)

    samplerate = 44100
    win_s = 1024       # fft size
    hop_s = win_s // 4 # block size
    threshold = 0.5

    f = source(sys.argv[1], samplerate, hop_s)
    g = sink(sys.argv[2], samplerate)
    h = sink(sys.argv[3], samplerate)

    pva = pvoc(win_s, hop_s)    # a phase vocoder
    pvb = pvoc(win_s, hop_s)    # another phase vocoder
    t = tss(win_s, hop_s)       # transient steady state separation

    t.set_threshold(threshold)

    read = hop_s

    while read:
        samples, read = f()               # read file
        spec = pva(samples)               # compute spectrum
        trans_spec, stead_spec = t(spec)  # transient steady-state separation
        transients = pva.rdo(trans_spec)  # overlap-add synthesis of transients
Beispiel #46
0
 def test_wrong_channels(self):
     with assert_raises(RuntimeError):
         sink(get_tmp_sink_path(), 44100, -1)
Beispiel #47
0
 def test_wrong_samplerate_too_large(self):
     with self.assertRaises(RuntimeError):
         sink(get_tmp_sink_path(), 1536001, 2)
Beispiel #48
0
 def test_wrong_channels_too_large(self):
     with self.assertRaises(RuntimeError):
         sink(get_tmp_sink_path(), 44100, 202020)
Beispiel #49
0
 def test_wrong_filename(self):
     with assert_raises(RuntimeError):
         sink('')
#! /usr/bin/env python

import sys
from aubio import source, sink, pvoc

if __name__ == '__main__':
  if len(sys.argv) < 2:
    print 'usage: %s <inputfile> <outputfile>' % sys.argv[0]
    sys.exit(1)
  samplerate = 44100
  f = source(sys.argv[1], samplerate, 256)
  g = sink(sys.argv[2], samplerate)
  total_frames, read = 0, 256

  win_s = 512                 # fft size
  hop_s = win_s / 2           # hop size
  pv = pvoc(win_s, hop_s)                            # phase vocoder

  while read:
    samples, read = f()
    spectrum = pv(samples)            # compute spectrum
    #spectrum.norm *= .8               # reduce amplitude a bit
    spectrum.phas[:] = 0.             # zero phase
    new_samples = pv.rdo(spectrum)    # compute modified samples
    g(new_samples, read)              # write to output
    total_frames += read

  print "wrote", total_frames, "from", f.uri, "to", g.uri

  
import os
import pandas as pd
import librosa
import glob
import matplotlib
import librosa.display

# Read and load input
input_wav = raw_input("Enter input file name: ")
output_wav = raw_input("Enter output file name: ")

# Manipulate audio to add robot effect
samplerate = 44100
f = source(input_wav, samplerate, 256)
g = sink(output_wav, samplerate)
total_frames, read = 0, 256

win_s = 512  # fft size
hop_s = win_s // 2  # hop size
pv = pvoc(win_s, hop_s)  # phase vocoder

while read:
    samples, read = f()
    spectrum = pv(samples)  # compute spectrum
    #spectrum.norm *= .8             # reduce amplitude a bit
    spectrum.phas[:] = 0.  # zero phase
    new_samples = pv.rdo(spectrum)  # compute modified samples
    g(new_samples, read)  # write to output
    total_frames += read
Beispiel #52
0
    output_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    output_dir = os.path.join(output_dir, 'tests', 'sounds')
    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)

    filenames = [
        '44100Hz_1f_silence.wav',
        '22050Hz_5s_brownnoise.wav',
        '32000Hz_127f_sine440.wav',
    ]
    samplerates = [44100, 22050, 32000]
    durations = [1, 5 * 22050, 127]

    for fname, samplerate, duration in zip(filenames, samplerates, durations):
        output_name = os.path.join(output_dir, fname)
        g = sink(output_name, samplerate)
        total_frames = 0
        while total_frames < duration:
            write = min(hop_size, duration - total_frames)
            if 'brownnoise' in fname:
                vec = np.random.rand(write).astype(float_type) * 2. - 1.
            elif 'sine' in fname:
                freq = 440
                t = np.arange(write).astype(float_type) + total_frames
                vec = np.sin(2. * np.pi * freq * t / float(samplerate))
            else:
                # silence
                vec = fvec(write)
            g(vec, write)
            total_frames += write
        outstr = "wrote {:2f}s".format(total_frames / float(samplerate))
Beispiel #53
0
# open stream
buffer_size = 1024
pyaudio_format = pyaudio.paFloat32
n_channels = 1
samplerate = 44100
stream = p.open(format=pyaudio_format,
                channels=n_channels,
                rate=samplerate,
                input=True,
                frames_per_buffer=buffer_size)

if len(sys.argv) > 1:
    # record 5 seconds
    output_filename = sys.argv[1]
    record_duration = 5 # exit 1
    outputsink = aubio.sink(sys.argv[1], samplerate)
    total_frames = 0
else:
    # run forever
    outputsink = None
    record_duration = None

# setup pitch
tolerance = 0.8
win_s = 4096 # fft size
hop_s = buffer_size # hop size
pitch_o = aubio.pitch("default", win_s, hop_s, samplerate)
pitch_o.set_unit("midi")
pitch_o.set_tolerance(tolerance)

print("*** starting recording")
Beispiel #54
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))
period = float(samplerate) /  pitch
# create a sine lookup table
tablelen = 1000
sinetable = arange(tablelen + 1, dtype = 'float32')
sinetable = 0.7 * sin(twopi * sinetable/tablelen)
sinetone = zeros((duration,), dtype = 'float32')

# compute sinetone at floating point period
for i in range(duration):
    x = int((i % period) / float(period) * tablelen)
    sinetone[i] = (sinetable[x] + sinetable[x+1]) / 2

# apply some envelope
float_ramp = arange(duration, dtype = 'float32')
sinetone *= exp( - e * float_ramp / duration / decay)
sinetone[:attack] *= exp( e * ( float_ramp[:attack] / attack - 1 ) )

if 0:
    import matplotlib.pyplot as plt
    plt.plot(sinetone)
    plt.show()

my_sink = sink(sys.argv[1], samplerate)

total_frames = 0
while total_frames + blocksize < duration:
    my_sink(sinetone[total_frames:total_frames+blocksize], blocksize)
    total_frames += blocksize
my_sink(sinetone[total_frames:duration], duration - total_frames)
Beispiel #56
0
    # 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)]
Beispiel #57
0
import sys
from aubio import source, sink

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print('usage: %s <inputfile> <outputfile> [samplerate] [hop_size]' % sys.argv[0])
        sys.exit(1)

    if len(sys.argv) > 3: samplerate = int(sys.argv[3])
    else: samplerate = 0
    if len(sys.argv) > 4: hop_size = int(sys.argv[4])
    else: hop_size = 256

    f = source(sys.argv[1], samplerate, hop_size)
    if samplerate == 0: samplerate = f.samplerate
    g = sink(sys.argv[2], samplerate, f.channels)

    total_frames, read = 0, hop_size
    while read:
        vec, read = f.do_multi()
        g.do_multi(vec, read)
        total_frames += read
    outstr = "wrote %.2fs" % (total_frames / float(samplerate))
    outstr += " (%d frames in" % total_frames
    outstr += " %d blocks" % (total_frames // f.hop_size)
    outstr += " of %d channels" % f.channels
    outstr += " at %dHz)" % f.samplerate
    outstr += " from " + f.uri
    outstr += " to " + g.uri
    print(outstr)