def detect(self): sent = "Series {+ input_file = \"%s\"-> input: SoundFileSource { filename = /input_file inSamples = 2048}-> Windowing {size = 2048}-> Spectrum -> PowerSpectrum -> Transposer -> MaxArgMax ->Transposer -> selection: Selector {disable = 0}-> CsvSink { filename = \"wode.csv\" }+ done = (input/hasData == false)}" % self.filename file = open("wodefile.mrs", "w") file.write(sent) file.close() mng = marsyas.MarSystemManager() fnet = mng.create("Series", "featureNetwork") result = [] system = marsyas.system_from_script_file("wodefile.mrs") get = system.getControl while get("SoundFileSource/input/mrs_bool/hasData").to_bool(): system.tick() result.extend( get("Selector/selection/mrs_realvec/processedData").to_realvec( )) result[-1] *= 44100 / 2048 sum = 0.0 for i in range(0, len(result)): sum += result[i] avg = np.array(sum / len(result) / 1000) clfs = [SVC()] for clf in clfs: clf.fit(self.X, self.y) y_pred = clf.predict(avg) self.play(y_pred[0])
def build_network(): # Assemble the network mng = marsyas.MarSystemManager() pnet = mng.create("Series", "pnet") voices = mng.create("Fanout", "voices") voices.addMarSystem(mng.create("SineSource", "src1")) filt_noise = mng.create("Series", "filt_noise") filt_noise.addMarSystem(mng.create("NoiseSource", "src2")) filt_noise.addMarSystem(mng.create("Biquad", "biquad")) filt_noise.updControl("Biquad/biquad/mrs_real/frequency", marsyas.MarControlPtr.from_real(400.0)) voices.addMarSystem(filt_noise) mix = mng.create("Sum", "mix") gain = mng.create("Gain", "gain") dest = mng.create("AudioSink", "dest") pnet.addMarSystem(voices) pnet.addMarSystem(mix) pnet.addMarSystem(gain) pnet.addMarSystem(dest) pnet.linkControl("mrs_real/f1", "Fanout/voices/SineSource/src1/mrs_real/frequency") pnet.linkControl( "mrs_real/f2", "Fanout/voices/Series/filt_noise/Biquad/biquad/mrs_real/frequency") pnet.updControl("mrs_real/israte", marsyas.MarControlPtr.from_real(44100.0)) pnet.updControl("AudioSink/dest/mrs_bool/initAudio", marsyas.MarControlPtr.from_bool(True)) return pnet
def create(net): msm = marsyas.MarSystemManager() composite = msm.create("Gain/id") # will be overwritten if (len(net) == 2): composite = msm.create(net[0]) msyslist = map(create,net[1]) msyslist = map(composite.addMarSystem,msyslist) else: composite = msm.create(net) return composite
def run(inFilename, outFilename, medianFilter): mng = marsyas.MarSystemManager() # Create network to extract pitch inNet = mng.create("Series", "series") # Add the MarSystems inNet.addMarSystem(mng.create("SoundFileSource", "src")) inNet.addMarSystem(mng.create("Stereo2Mono", "s2m")) inNet.addMarSystem(mng.create("ShiftInput", "si")) fanout = mng.create("Fanout", "fanout") fanout.addMarSystem(mng.create("AubioYin", "yin")) fanout.addMarSystem(mng.create("Rms", "rms")) inNet.addMarSystem(fanout) # Create network to output audio outNet = mng.create("Series", "series") outNet.addMarSystem(mng.create("SineSource", "src")) outNet.addMarSystem(mng.create("Gain", "gain")) outNet.addMarSystem(mng.create("SoundFileSink", "sink")) # Update controls inNet.updControl("SoundFileSource/src/mrs_string/filename", marsyas.MarControlPtr.from_string(inFilename)) osrate = inNet.getControl("mrs_real/osrate").to_real() outNet.updControl("SoundFileSink/sink/mrs_real/israte", osrate) outNet.updControl("SoundFileSink/sink/mrs_string/filename", marsyas.MarControlPtr.from_string(outFilename)) pitchData = [] rmsData = [] while inNet.getControl("SoundFileSource/src/mrs_bool/hasData").to_bool(): pitch = inNet.getControl("mrs_realvec/processedData").to_realvec()[0] rms = inNet.getControl("mrs_realvec/processedData").to_realvec()[1] pitchData.append(pitch) rmsData.append(rms) inNet.tick() outPitches = medfilt1(pitchData, medianFilter) outRms = np.asarray(rmsData) outRms *= 5 for i in range(0, len(outPitches)): outNet.updControl("SineSource/src/mrs_real/frequency", outPitches[i]) outNet.updControl("Gain/gain/mrs_real/gain", outRms[i]) outNet.tick()
def energy(request, recordingId): recording = Recording.objects.get(pk=int(recordingId)) audioFile = os.path.join(settings.OPENMIR_FILE_PATH, "audio", (str(recording.audioFilename))) startSec = float(request.GET.get('startSec', '0')) endSec = float(request.GET.get('endSec', '1.000')) # Variables from request winSize = int(request.GET.get('winSize', '1024')) hopSize = int(request.GET.get('hopSize', '1024')) # Create net mng = marsyas.MarSystemManager() net = mng.create("Series", "series") # Add the MarSystems net.addMarSystem(mng.create("SoundFileSource", "src")) net.addMarSystem(mng.create("Rms", "Rms")) # # Update Marsyas controls net.updControl("SoundFileSource/src/mrs_string/filename", marsyas.MarControlPtr.from_string(audioFile)) # Calculate values soundFileSampleRate = net.getControl( "SoundFileSource/src/mrs_real/osrate").to_real() samplesToSkip = int(soundFileSampleRate * (startSec)) secPerTick = hopSize / soundFileSampleRate durationSec = endSec - startSec ticksToRun = int(durationSec / secPerTick) # Move to the correct position in the file net.updControl("SoundFileSource/src/mrs_natural/moveToSamplePos", samplesToSkip) net.updControl("mrs_natural/inSamples", hopSize) outData = [] for i in range(0, ticksToRun): currentTime = (i * secPerTick) + startSec data = net.getControl("mrs_realvec/processedData").to_realvec()[0] outData.append([currentTime, data]) net.tick() responseJson = simplejson.dumps(outData) return HttpResponse(responseJson, mimetype='application/json')
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
def marsyasplay(sfname): mng = marsyas.MarSystemManager() net = mng.create("Series", "series") net.addMarSystem(mng.create("SoundFileSource", "src")) net.addMarSystem(mng.create("BinauralCARFAC", "carfac")) net.updControl("SoundFileSource/src/mrs_string/filename", marsyas.MarControlPtr.from_string(sfname)) outData = net.getControl("mrs_realvec/processedData") data = np.zeros((96, 200, 1)) while net.getControl("SoundFileSource/src/mrs_bool/hasData").to_bool(): net.tick() a = np.array(outData.to_realvec()).reshape((96, 200, 1)) data = np.append(data, a, axis=2) obj = mlab.contour3d(data, contours=4, transparent=True) mlab.show()
def run_script(script_file, output_file, *input_files): """ This function assists in running Marsyas scripts. """ # Build the system. msm = marsyas.MarSystemManager() system = marsyas.system_from_script_file(script_file) # Define some variables for i, this_input in enumerate(input_files): system.updControl( "SoundFileSource/input_{}/mrs_string/filename".format(i), marsyas.MarControlPtr.from_string(this_input)) system.updControl("SoundFileSink/output/mrs_string/filename", marsyas.MarControlPtr.from_string(output_file)) # Tick the system. while (system.getControl( "SoundFileSource/input_0/mrs_bool/hasData").to_bool()): system.tick()
def key_detection(ingress): """ Runs a key detection algorithm over the ingress realvec or list. """ if type(ingress) != "list": list = list(ingress) # Build the system. msm = marsyas.MarSystemManager() system = marsyas.system_from_script_file(script_file) # Define some variables for i, this_input in enumerate(input_files): system.updControl( "RealVecSource/input_{}/mrs_string/filename".format(i), marsyas.MarControlPtr.from_string(this_input)) system.updControl("SoundFileSink/output/mrs_string/filename", marsyas.MarControlPtr.from_string(output_file)) # Tick the system. while (system.getControl( "RealVecSource/input_0/mrs_bool/hasData").to_bool()): system.tick()
import marsyas from itertools import izip_longest, izip from collections import namedtuple import math from data.DB_Helper import * # Notes: # No beat features msm = marsyas.MarSystemManager() def to_list(realvec): cols = realvec.getCols() rows = realvec.getRows() return [[realvec[r * cols + c] for c in range(cols)] for r in range(rows)] def get_control(msys, type, name): func = 'to_' + type type = 'mrs_' + type return getattr(msys.getControl(type + '/' + name), func)() def obs_names(msys): return [ attr for attr in get_control(msys, 'string', 'onObsNames').split(',') if attr
def spectrogram(request, recordingId): recording = Recording.objects.get(pk=int(recordingId)) audioFile = os.path.join(settings.OPENMIR_AUDIO_FILE_PATH, (str(recording.audioFilename))) startSec = float(request.GET.get('startSec', '0')) endSec = float(request.GET.get('endSec', '1.000')) lowHz = int(request.GET.get('lowHz', '0')) highHz = int(request.GET.get('highHz', 44100 / 2)) # Variables from request winSize = int(request.GET.get('winSize', '1024')) # TODO(sness) - Make hopSize work hopSize = winSize # hopSize = int(request.GET.get('hopSize', '1024')) width = request.GET.get('width', 'native') height = request.GET.get('height', 'native') spectrumType = request.GET.get('spectrumType', 'decibels') #spectrumType = request.GET.get('spectrumType', 'magnitude') # Marsyas network mng = marsyas.MarSystemManager() net = mng.create("Series", "series") net.addMarSystem(mng.create("SoundFileSource", "src")) # net.addMarSystem(mng.create("Stereo2Mono", "s2m")); net.addMarSystem(mng.create("ShiftInput", "si")) net.addMarSystem(mng.create("Windowing", "win")) net.addMarSystem(mng.create("Spectrum", "spk")) net.addMarSystem(mng.create("PowerSpectrum", "pspk")) # Update Marsyas controls net.updControl("PowerSpectrum/pspk/mrs_string/spectrumType", marsyas.MarControlPtr.from_string(str(spectrumType))) net.updControl("SoundFileSource/src/mrs_string/filename", marsyas.MarControlPtr.from_string(audioFile)) net.updControl("SoundFileSource/src/mrs_natural/inSamples", hopSize) net.updControl("ShiftInput/si/mrs_natural/winSize", winSize) net.updControl("mrs_natural/inSamples", int(hopSize)) # Sample rate and samples per tick networkSampleRate = net.getControl("mrs_real/osrate").to_real() soundFileSampleRate = net.getControl( "SoundFileSource/src/mrs_real/osrate").to_real() insamples = net.getControl( "SoundFileSource/src/mrs_natural/inSamples").to_natural() # Calculate values samplesToSkip = int(soundFileSampleRate * (startSec)) durationSec = (endSec - startSec) ticksToRun = int(durationSec * networkSampleRate) _height = winSize / 2 # Move to the correct position in the file net.updControl("SoundFileSource/src/mrs_natural/moveToSamplePos", samplesToSkip) # The array to be displayed to the user out = np.zeros((_height, ticksToRun), dtype=np.double) # Tick the network until we are done for x in range(0, ticksToRun): net.tick() data = net.getControl("mrs_realvec/processedData").to_realvec() for y in range(0, _height): out[(_height - y - 1), x] = data[y] # Normalize and make black on white out /= np.max(np.abs(out)) out = 1.0 - out nyquist = 44100 / 2. bins = out.shape[0] lowBin = int((bins / nyquist) * lowHz) highBin = int((bins / nyquist) * highHz) halfWinSize = int(hopSize / 2) out = out[halfWinSize - highBin:halfWinSize - lowBin, :] # Resize and convert the array to an image if (height == "native") and (width == "native"): height = winSize / 2 width = hopSize * durationSec if (height != "native") and (width == "native"): pxPerItem = int(height) / float(winSize / 2.) # TODO(sness) - Why do we have to multiply this by 4? Check the math above width = int(ticksToRun * pxPerItem) * 4 # out = smp.imresize(out,(int(height),int(width))) # im = smp.toimage(out) resize = imresize(out, (int(height), int(width))) im = toimage(resize) # Output a png response = HttpResponse(mimetype="image/png") im.save(response, "PNG") return response
def yin(request, recordingId): recording = Recording.objects.get(pk=int(recordingId)) # audioFile = "/global/scratch/sness/openmir/audio/%s.wav" % (str(recording.name)) audioFile = os.path.join(settings.OPENMIR_FILE_PATH, "audio", (str(recording.audioFilename))) startSec = float(request.GET.get('startSec', '0')) endSec = float(request.GET.get('endSec', '1.000')) highHzCutoff = int(request.GET.get('highHzCutoff', '3000')) lowHzCutoff = int(request.GET.get('lowHzCutoff', '0')) highHzWrap = int(request.GET.get('highHzWrap', '3000')) lowHzWrap = int(request.GET.get('lowHzWrap', '0')) tolerance = float(request.GET.get('tolerance', '0.15')) energyCutoff = float(request.GET.get('energyCutoff', '0.')) medianFilter = float(request.GET.get('medianFilter', '1')) histogramBins = int(request.GET.get('histogramBins', '0')) # Variables from request winSize = int(request.GET.get('winSize', '1024')) hopSize = int(request.GET.get('hopSize', '1024')) # Create net mng = marsyas.MarSystemManager() net = mng.create("Series", "series") # Add the MarSystems net.addMarSystem(mng.create("SoundFileSource", "src")) net.addMarSystem(mng.create("Stereo2Mono", "s2m")) net.addMarSystem(mng.create("ShiftInput", "si")) # Fanout for calculating both Yin and Rms fanout = mng.create("Fanout", "fanout") fanout.addMarSystem(mng.create("AubioYin", "yin")) fanout.addMarSystem(mng.create("Rms", "rms")) net.addMarSystem(fanout) # Update Marsyas controls net.updControl("SoundFileSource/src/mrs_string/filename", marsyas.MarControlPtr.from_string(audioFile)) net.updControl("ShiftInput/si/mrs_natural/winSize", winSize) net.updControl("mrs_natural/inSamples", int(hopSize)) fanout.updControl("AubioYin/yin/mrs_real/tolerance", tolerance) # Calculate values soundFileSampleRate = net.getControl( "SoundFileSource/src/mrs_real/osrate").to_real() samplesToSkip = int(soundFileSampleRate * (startSec)) secPerTick = hopSize / soundFileSampleRate durationSec = endSec - startSec ticksToRun = int(durationSec / secPerTick) # Move to the correct position in the file net.updControl("SoundFileSource/src/mrs_natural/moveToSamplePos", samplesToSkip) outTimes = [] outPitches = [] for i in range(0, ticksToRun): currentTime = (i * secPerTick) + startSec pitch = net.getControl("mrs_realvec/processedData").to_realvec()[0] rms = net.getControl("mrs_realvec/processedData").to_realvec()[1] if pitch > highHzCutoff: pitch = highHzCutoff if pitch < lowHzCutoff: pitch = lowHzCutoff pitch = ((pitch - lowHzWrap) % (highHzWrap - lowHzWrap)) + lowHzWrap if rms < energyCutoff: pitch = -1. outTimes.append(currentTime) outPitches.append(pitch) net.tick() if medianFilter > 1: outPitches = medfilt1(outPitches, medianFilter) # if histogramBins > 1: # for i in range(0, len(outPitches)): # outPitches[i] = outPitches[i] outData = [] for i in range(0, len(outTimes)): outData.append([outTimes[i], outPitches[i]]) responseJson = simplejson.dumps(outData) return HttpResponse(responseJson, mimetype='application/json')
def pitchcontour_embed(request): # Redirect all output to stderr sys.stdout = sys.stderr pitches = [] rmses = [] output = "" data = "" buffer_size = int(request.GET.get('buffer_size', '1024')) print "buffer_size=%i" % buffer_size hop_size = int(request.GET.get('hop_size', '1024')) print "hop_size=%i" % hop_size tolerance = float(request.GET.get('tolerance', '0.7')) print "tolerance=%i" % tolerance median = float(request.GET.get('median', '11')) print "median=%i" % median #sfname = "/home/sness/wavfiles/tiny.wav" sfname = "/home/sness/wavfiles/A36_N4__2_.wav" print "sfname=%s" % sfname mng = marsyas.MarSystemManager() print "la" # Create net net = mng.create("Series","series") net.addMarSystem(mng.create("SoundFileSource", "src")) # Create fanout fanout = mng.create("Fanout","fanout") # Create the Yin series yin_series = mng.create("Series","yin_series") yin_series.addMarSystem(mng.create("ShiftInput", "si")); yin_series.addMarSystem(mng.create("Yin", "yin")); yin_series.addMarSystem(mng.create("Gain", "gain")); # Create the Yin series rms_series = mng.create("Series","rms_series") rms_series.addMarSystem(mng.create("Rms", "rms")); rms_series.addMarSystem(mng.create("Gain", "gain")); # Add these system to the main network fanout.addMarSystem(yin_series) fanout.addMarSystem(rms_series) net.addMarSystem(fanout) net.updControl("mrs_natural/inSamples", hop_size) net.updControl("SoundFileSource/src/mrs_string/filename", marsyas.MarControlPtr.from_string(sfname)) net.updControl("Fanout/fanout/Series/yin_series/ShiftInput/si/mrs_natural/winSize", buffer_size); yin_series.updControl("Yin/yin/mrs_natural/inSamples",buffer_size); yin_series.updControl("Yin/yin/mrs_real/tolerance",tolerance); max_rms = 0 while net.getControl("SoundFileSource/src/mrs_bool/hasData").to_bool(): pitch = net.getControl("Fanout/fanout/Series/yin_series/Yin/yin/mrs_realvec/processedData").to_realvec() pitches.append(pitch[0]) rms = net.getControl("Fanout/fanout/Series/rms_series/Rms/rms/mrs_realvec/processedData").to_realvec() rmses.append(rms[0]) if (rms[0] > max_rms): max_rms = rms[0] net.tick() print "pitches=" print pitches print "rmses=" print rmses sound_file_len = 2.439 width = 600. x_offset = 100 y_offset = 20 # Generate a pretty SVG string for the pitch contour pitchcontour = "" for i in range(0,len(pitches)): if (pitches[i] < 5000): # filter out those really high pitches that YIN gives time = int(((hop_size / 44100.0) * float(i)) / sound_file_len * width) + x_offset pitchcontour += "%i %i" % (time, (200.0 - pitches[i] / 50.0) + y_offset) if i < (len(pitches) - 1): pitchcontour += "," median_filtered_pitches = [] median_filtered_pitches = median_filter(pitches,int(median)) #print median_filtered_pitches # Generate a pretty SVG string for the median filtered pitch contour median_filtered_pitchcontour = "" for i in range(0,len(median_filtered_pitches)): if (median_filtered_pitches[i] < 5000): time = int(((hop_size / 44100.0) * float(i)) / sound_file_len * width) + x_offset median_filtered_pitchcontour += "%i %i" % (time, (200.0 - median_filtered_pitches[i] / 50.0) + y_offset) # median_filtered_pitchcontour += "%i %i" % (i * 2, 200.0 - median_filtered_pitches[i] / 20.0) if i < (len(median_filtered_pitches) - 1): median_filtered_pitchcontour += "," # Generate a pretty SVG string for the rms contour rmscontour = "" for i in range(0,len(rmses)): if (rmses[i] < 5000): # filter out those really high rmses that YIN gives time = int(((hop_size / 44100.0) * float(i)) / sound_file_len * width) + x_offset rmscontour += "%i %i" % (time, 200 - ((rmses[i] / max_rms) * 200.)) if i < (len(rmses) - 1): rmscontour += "," return render_to_response('dtw/pitchcontour-embed.html', {'pitches' : pitches, 'pitchcontour' : pitchcontour, 'median_filtered_pitchcontour' : median_filtered_pitchcontour, 'rmscontour' : rmscontour, 'numpitches' : len(pitches), }, {}, mimetype="application/xhtml+xml")
def index(request): buffer_size = int(request.GET.get('buffer_size', '1024')) print "buffer_size=%i" % buffer_size hop_size = int(request.GET.get('hop_size', '1024')) print "hop_size=%i" % hop_size #sfname = "/Users/sness/venus/assets/recordings/1007/H2/2010/01/01/1/audio.wav" #sfname = "/home/sness/wavfiles/tiny.wav" #sfname = "/home/sness/nDEV/venus_orchive-assets/recordings/1007/H2/2010/01/01/1/audio.wav" sfname = "/home/sness/wavfiles/audio.wav" print "sfname=%s" % sfname mng = marsyas.MarSystemManager() # Create net net = mng.create("Series","series") # Make a SoundFileSource and convert it to mono net.addMarSystem(mng.create("SoundFileSource", "src")); net.addMarSystem(mng.create("Stereo2Mono", "s2m")); # A fanout that will do both RMS and Flux calculations fanout = mng.create("Fanout","fanout"); net.addMarSystem(fanout); # The branch to do the RMS rms_series = mng.create("Series","rms_series"); rms_series.addMarSystem(mng.create("Rms", "rms")); fanout.addMarSystem(rms_series); # The branch to do the Flux flux_series = mng.create("Series","flux_series"); flux_series.addMarSystem(mng.create("ShiftInput", "si")); flux_series.addMarSystem(mng.create("Windowing", "win")); flux_series.addMarSystem(mng.create("Spectrum","spk")); flux_series.addMarSystem(mng.create("PowerSpectrum", "pspk")); flux_series.addMarSystem(mng.create("Flux", "flux")); fanout.addMarSystem(flux_series); # Update the controls with required values net.updControl("SoundFileSource/src/mrs_string/filename", marsyas.MarControlPtr.from_string(sfname)) print "############################## la ##############################" rms_python_array = [] flux_python_array = [] while net.getControl("SoundFileSource/src/mrs_bool/hasData").to_bool(): data = net.getControl("mrs_realvec/processedData").to_realvec() rms = data[0] flux = data[1] rms_python_array.append(rms) flux_python_array.append(flux) net.tick() # Convert these arrays to numpy vectors rms_array = np.float32(rms_python_array) flux_array = np.float32(flux_python_array) # Normalize these arrays rms_array *= 1.0/rms_array.max() flux_array *= 1.0/flux_array.max() print rms_array print flux_array # Calculate the RGBA values rgba = [] for i in range(0,len(rms_array)): # The RMS and Flux values rms = rms_array[i] flux = flux_array[i] # Alpha is just the RMS value alpha = rms * 0.5 # Map the flux to a mix of red and green red = flux green = 1.0 - flux blue = 0 # Add these values as a tuple to the array rgba rgba.append((red,green,blue,alpha)) output = "" hop_width_ms = int((hop_size / 44100.0) * 1000) for i in range(0, len(rgba)): start_time_ms = int(((hop_size / 44100.0) * float(i))*1000) end_time_ms = start_time_ms + hop_width_ms red_int = int(rgba[i][0] * 255) green_int = int(rgba[i][1] * 255) blue_int = int(rgba[i][2] * 255) color = ('%02x%02x%02x' % (red_int, green_int, blue_int)).upper() output += "%i,%i,%i,,%f,0x%s\n" % (i, start_time_ms, end_time_ms, rgba[i][3], color) return render_to_response('orchive/index.html', {'output':output}, {})
def run(audioFile, outFile, startSec, endSec, lowHz, highHz, winSize, hopSize, spectrumType, widthPx, heightPx): # Marsyas network mng = marsyas.MarSystemManager() net = mng.create("Series", "series") net.addMarSystem(mng.create("SoundFileSource", "src")) net.addMarSystem(mng.create("Stereo2Mono", "s2m")) net.addMarSystem(mng.create("ShiftInput", "si")) net.addMarSystem(mng.create("Windowing", "win")) net.addMarSystem(mng.create("Spectrum", "spk")) net.addMarSystem(mng.create("PowerSpectrum", "pspk")) # Update Marsyas controls net.updControl("PowerSpectrum/pspk/mrs_string/spectrumType", marsyas.MarControlPtr.from_string(str(spectrumType))) net.updControl("SoundFileSource/src/mrs_string/filename", marsyas.MarControlPtr.from_string(audioFile)) net.updControl("SoundFileSource/src/mrs_natural/inSamples", hopSize) net.updControl("ShiftInput/si/mrs_natural/winSize", winSize) net.updControl("mrs_natural/inSamples", int(hopSize)) # Sample rate and samples per tick networkSampleRate = net.getControl("mrs_real/osrate").to_real() soundFileSampleRate = net.getControl( "SoundFileSource/src/mrs_real/osrate").to_real() insamples = net.getControl( "SoundFileSource/src/mrs_natural/inSamples").to_natural() # Calculate values samplesToSkip = int(soundFileSampleRate * (startSec)) durationSec = (endSec - startSec) ticksToRun = int(durationSec * networkSampleRate) _height = winSize / 2 # Move to the correct position in the file net.updControl("SoundFileSource/src/mrs_natural/moveToSamplePos", samplesToSkip) # The array to be displayed to the user out = np.zeros((_height, ticksToRun), dtype=np.double) # Tick the network until we are done for x in range(0, ticksToRun): net.tick() data = net.getControl("mrs_realvec/processedData").to_realvec() for y in range(0, _height): out[(_height - y - 1), x] = data[y] # Normalize and make black on white out /= np.max(np.abs(out)) out = 1.0 - out nyquist = 44100 / 2. bins = out.shape[0] lowBin = int((bins / nyquist) * lowHz) highBin = int((bins / nyquist) * highHz) halfWinSize = int(hopSize / 2) out = out[halfWinSize - highBin:halfWinSize - lowBin, :] # Resize and convert the array to an image if (heightPx == 0) and (widthPx == 0): heightPx = winSize / 2 widthPx = hopSize * durationSec if (heightPx != 0) and (widthPx == 0): pxPerItem = int(heightPx) / float(winSize / 2.) # TODO(sness) - Why do we have to multiply this by 4? Check the math above widthPx = int(ticksToRun * pxPerItem) * 4 out = smp.imresize(out, (int(heightPx), int(widthPx))) im = smp.toimage(out) im.save(outFile, "PNG")