Example #1
0
    def _assemble_network(self):
        '''
        Helper function to construct the Marsyas network for preprocessing and feature extraction
        '''
        
        mng = marsyas.MarSystemManager()
        self._net = mng.create("Series", "extract_features")
        self._net.addMarSystem(mng.create("AudioSource", "src"))
        self._net.addMarSystem(mng.create("MixToMono", "mono"))
        
        if self._dsf > 1:
            # butterworth low-pass filter
            f_order = 9
            b, a = butter(f_order, 0.9/self._dsf, 'lowpass')
            bcoeffs = marsyas.realvec(1, f_order+1)
            acoeffs = marsyas.realvec(1, f_order+1)
            for i in range(f_order+1):
                bcoeffs[i] = b[i]
                acoeffs[i] = a[i]
            self._net.addMarSystem(mng.create("Filter", "lowpass"))

            self._net.updControl("Filter/lowpass/mrs_realvec/ncoeffs", marsyas.MarControlPtr.from_realvec(bcoeffs))
            self._net.updControl("Filter/lowpass/mrs_realvec/dcoeffs", marsyas.MarControlPtr.from_realvec(acoeffs))

            self._net.addMarSystem(mng.create("DownSampler", "down"))
            self._net.updControl("DownSampler/down/mrs_natural/factor", self._dsf)

        self._net.addMarSystem(mng.create("ShiftInput", "slice"))
        self._net.addMarSystem(mng.create("Windowing", "win"))
        self._net.addMarSystem(mng.create("Spectrum", "fft"))
        self._net.addMarSystem(mng.create("PowerSpectrum", "pspec"))

        if self._featuredef["type"] == "MFCC":
            # takes DFT as input
            self._net.addMarSystem(mng.create("MFCC", "mfcc"))
            self._net.updControl("MFCC/mfcc/mrs_natural/coefficients", int(self._featuredef["coefficients"]))

        # create control links
        self._net.linkControl("mrs_bool/hasData", "AudioSource/src/mrs_bool/hasData")

        # set algorithm parameters
        self._net.updControl("ShiftInput/slice/mrs_natural/winSize", self._w)
        self._net.updControl("mrs_natural/inSamples", self._h*self._dsf)
        self._net.updControl("mrs_real/israte", 44100.0)
        self._net.updControl("ShiftInput/slice/mrs_bool/reset", marsyas.MarControlPtr.from_bool(True))
        self._net.updControl("Windowing/win/mrs_string/type", "Hamming")
        self._net.updControl("PowerSpectrum/pspec/mrs_string/spectrumType", "power")
        self._net.updControl("AudioSource/src/mrs_bool/initAudio", marsyas.MarControlPtr.from_bool(True));
def main():
    try:
        filename_input = sys.argv[1]
        filename_output = sys.argv[2]
    except:
        print "USAGE: ./in_out.py input_filename.wav output_filename.wav"
        exit(1)

    input_net = make_input(filename_input)
    output_net = make_output(filename_output)

    notempty = input_net.getControl("SoundFileSource/src/mrs_bool/hasData")
    input_net_end_control = input_net.getControl("mrs_realvec/processedData")
    output_net_begin_control = output_net.getControl(
        "RealvecSource/real_src/mrs_realvec/data")
    output_net_begin = marsyas.realvec(512)
    while notempty.to_bool():
        ### get input data
        input_net.tick()
        input_net_end = input_net_end_control.to_realvec()

        ### do something with it
        for i in range(input_net_end.getSize()):
            output_net_begin[i] = 0.5 * input_net_end[i]
        output_net_begin_control.setValue_realvec(output_net_begin)
        if PLOT:
            pylab.plot(input_net_end, label="input")
            pylab.plot(output_net_begin, label="output")
            pylab.legend()
            pylab.show()

        ### set output data
        output_net.tick()
Example #3
0
def main():
    try:    
        filename_input = sys.argv[1]
        filename_output = sys.argv[2]
    except:
        print "USAGE: ./in_out.py input_filename.wav output_filename.wav"
        exit(1)
    
    input_net = make_input(filename_input)
    output_net = make_output(filename_output)

    notempty = input_net.getControl("SoundFileSource/src/mrs_bool/hasData")
    input_net_end_control = input_net.getControl("mrs_realvec/processedData")
    output_net_begin_control = output_net.getControl(
        "RealvecSource/real_src/mrs_realvec/data")
    output_net_begin = marsyas.realvec(512)
    while notempty.to_bool():
        ### get input data
        input_net.tick()
        input_net_end = input_net_end_control.to_realvec()

        ### do something with it
        for i in range(input_net_end.getSize()):
            output_net_begin[i] = 0.5*input_net_end[i]
        output_net_begin_control.setValue_realvec(output_net_begin)
        if PLOT:
            pylab.plot(input_net_end, label="input")
            pylab.plot(output_net_begin, label="output")
            pylab.legend()
            pylab.show()

        ### set output data
        output_net.tick()
Example #4
0
def main():
    try:    
        filename_input = sys.argv[1]
    except:
        print "USAGE: ./mostitch.py input_filename.wav"
        exit(1)
    # read the slices    
    slices = read_in_file_with_stats( filename_input )
    print(len(slices))
    # get NN
    dataset = array([s.stats for s in slices])
    #window = triangle(buffsize)
    window = hann_window(buffsize)
    #window = flat(buffsize)
    #flat(buffsize)#saw(buffsize)#triangle(buffsize) #flat(buffsize) #triangle(buffsize)
    for slice in slices:
        slice.rv *= window
    #params = flann.build_index(dataset, algorithm="autotuned", target_precision=0.9, log_level = "info")
    params = flann.build_index(dataset, algorithm="kdtree", target_precision=0.9, log_level = "info")
    output_net = make_output()
    this_net = output_net
    slicecnt = 0
    for slice in slices:
        load_slice( output_net, slicecnt, slice )
        slicecnt += 1

    sme = StreamMetricExtractor()
    schedule_control = output_net.getControl(
        grainuri + "/mrs_realvec/schedule")
    schedsize = 3 # size of a schedule
    nn = schedsize
    i = 0

    this_net.updControl("AudioSink/dest/mrs_bool/initAudio",marsyas.MarControlPtr.from_bool(True))
    while 1:
        schedule = marsyas.realvec(nn * buffsize)
        for j in range(0,buffsize):
            schedule[j*nn+0] = j
            schedule[j*nn+1] = j + (i % slicecnt)
            schedule[j*nn+2] = 0.25
        # this sets a schedule
        output_net_begin_control = this_net.getControl("RealvecGrainSource/real_src/mrs_realvec/schedule")
        output_net_begin_control.setValue_realvec(schedule)
        # this commits the schedule
        this_net.updControl("RealvecGrainSource/real_src/mrs_bool/schedcommit",marsyas.MarControlPtr.from_bool(True))
        print "\t".join([str(x) for x in schedule])
        this_net.tick()
        i = i + 1
def to_realvec(input_file):
    """
    Transforms an input file into a `marsyas.realvec` of data for further passing, also makes the track mono.
    """
    # Build the system.
    msm = marsyas.MarSystemManager()
    system = marsyas.system_from_script_file("marsystems/to_realvec.mrs")
    # Define some variables
    system.updControl("mrs_string/file", marsyas.MarControlPtr.from_string(input_file))
    # This is a dumb way of doing it but I can't figure out a better way.
    egress = marsyas.realvec()
    # Tick the system.
    while (not system.getControl("mrs_bool/done").to_bool()):
        system.tick()
        tick_data = system.getControl("mrs_realvec/processedData").to_realvec()
        egress.appendRealvec(tick_data)
    return egress
Example #6
0
def to_realvec(input_file):
    """
    Transforms an input file into a `marsyas.realvec` of data for further passing, also makes the track mono.
    """
    # Build the system.
    msm = marsyas.MarSystemManager()
    system = marsyas.system_from_script_file("marsystems/to_realvec.mrs")
    # Define some variables
    system.updControl("mrs_string/file",
                      marsyas.MarControlPtr.from_string(input_file))
    # This is a dumb way of doing it but I can't figure out a better way.
    egress = marsyas.realvec()
    # Tick the system.
    while (not system.getControl("mrs_bool/done").to_bool()):
        system.tick()
        tick_data = system.getControl("mrs_realvec/processedData").to_realvec()
        egress.appendRealvec(tick_data)
    return egress
Example #7
0
    def step(self):
        state = self.state
        new_slice = self.sme.operate()
        results, dists = self.flann.nn_index(array([new_slice.stats]),int(state["topn"]), checks=self.params["checks"]);
        result = results[0]
        self.post_result_hook( result, dists[0] )
        self.cond_learn( new_slice )
        # here's the granular part
        ngrains = self.generate_ngrains()
        # this is a bad smell, we could make this an object!
        schedule = marsyas.realvec(schedsize * ngrains)        
        for j in range(0,ngrains):
            # in the next ___ of a second
            delay = self.generate_delay()
            schedule[j*schedsize + 0] = self.generate_delay()
            choice = self.choose( result )
            schedule[j*schedsize + 1] = choice # choose the slice
            amp = self.generate_amp()
            schedule[j*schedsize + 2] = amp
            self.post_schedule_grain( delay, choice, amp )
        self.schedule_and_play( schedule )
	self.process_zmq()
Example #8
0
def flat(size):
    v = marsyas.realvec( size )
    for i in range(0,size):
        v[i] = 1
    return v
Example #9
0
def saw(size):
    v = marsyas.realvec( size )
    for i in range(0,size):
        v[i] = i/(size)
    return v
Example #10
0
def triangle( size ):
    v = marsyas.realvec( size )
    half = size/2
    for i in range(0,size):
        v[i] = 1.0 - abs(half - i)/half
    return v
Example #11
0
def hann_window( size ):
    v = marsyas.realvec( size )
    for i in range(0,size):
        v[i] = hann( i, size)
    return v
Example #12
0
net = marsyas_util.create(fir_filtering)
snet = marsyas_util.mar_refs(fir_filtering)

net.updControl("mrs_natural/inSamples", 1024)
net.updControl(snet["sft"]+"/mrs_natural/winSize", 2048)
net.updControl(snet["win"]+"/mrs_string/type", "Hanning" )
net.updControl(snet['src']+"/mrs_string/filename", "input.wav")
net.updControl(snet['asnk']+"/mrs_bool/initAudio", marsyas.MarControlPtr.from_bool(True))

# ... but these lines here are new.
# We will operate the product and enable the use of a fixed mask:
net.updControl(snet['prod']+"/mrs_bool/use_mask", marsyas.MarControlPtr.from_bool(True))

# Now, we will define the shape of our FIR filter in the frequency domain.
# This is a low-pass filter.
vec = marsyas.realvec(2048+2)
for i in xrange (len(vec)/2):
    if i>30:
        vec[2*i]=0
        vec[1+(2*i)]=0
    else:
        vec[2*i]=1
        vec[1+(2*i)]=1

# Last, we set the mask to be our desired value.
net.updControl(snet['prod']+"/mrs_realvec/mask", marsyas.MarControlPtr.from_realvec(vec))

# Let's run and listen to the results!
while 1:
        net.tick()
net.updControl("mrs_natural/inSamples", 1024)
net.updControl(snet["sft"] + "/mrs_natural/winSize", 2048)
net.updControl(snet["win"] + "/mrs_string/type", "Hanning")
net.updControl(snet['src'] + "/mrs_string/filename", "input.wav")
net.updControl(snet['asnk'] + "/mrs_bool/initAudio",
               marsyas.MarControlPtr.from_bool(True))

# ... but these lines here are new.
# We will operate the product and enable the use of a fixed mask:
net.updControl(snet['prod'] + "/mrs_bool/use_mask",
               marsyas.MarControlPtr.from_bool(True))

# Now, we will define the shape of our FIR filter in the frequency domain.
# This is a low-pass filter.
vec = marsyas.realvec(2048 + 2)
for i in xrange(len(vec) / 2):
    if i > 30:
        vec[2 * i] = 0
        vec[1 + (2 * i)] = 0
    else:
        vec[2 * i] = 1
        vec[1 + (2 * i)] = 1

# Last, we set the mask to be our desired value.
net.updControl(snet['prod'] + "/mrs_realvec/mask",
               marsyas.MarControlPtr.from_realvec(vec))

# Let's run and listen to the results!
while 1:
    net.tick()
Example #14
0
from marsyas import * 
import marsyas
import marsyas_util
import random

mars = MarSystemManager()
buffsize=512
series = ["Series/output", ["RealvecGrainSource/real_src", "AudioSink/dest"]]
print "Make Network"
this_net = marsyas_util.create(series)
print "Set InSamples"
this_net.updControl("mrs_natural/inSamples", buffsize)
print "Set Rate"
this_net.updControl("mrs_real/israte", 44100.0)
print "Make grains"
grain1 = marsyas.realvec(buffsize)
grain2 = marsyas.realvec(buffsize)
grain3 = marsyas.realvec(buffsize)
grain4 = marsyas.realvec(3*buffsize)
for i in range(0, buffsize):
    grain1[i] = math.sin(i*440/buffsize)
    grain2[i] = random.uniform(-1,1)
    grain3[i] = math.sin(math.tan(math.sin(math.tan(i/40))))

for i in range(0, buffsize*3):
    grain4[i] = math.sin(math.tan(i/100))



data_uri = "RealvecGrainSource/real_src/mrs_realvec/data"