def analyze_line_in(self):
		# Start with these as our initial guesses - will calculate a rolling mean / std 
		# as we get input data.
		mean = [12.0 for _ in range(audio_setup.Audio.POSSIBLE_COLUMNS)]
		std = [0.5 for _ in range(audio_setup.Audio.POSSIBLE_COLUMNS)]
		recent_samples = np.empty((stabilize.Lights.MAX_SAMPLES, audio_setup.Audio.POSSIBLE_COLUMNS))
		num_samples = 0

		while True:
			changed = self.midi.read_events()
			if self.midi.buttons['music_mode_intensity'] or self.midi.buttons['music_mode_offset'] :
				
				# Read the audio stream and make it blow chunks (aka get a chunk of data and the length of that data)
				size, chunk = self.input.read()
				if size > 0:
					# This is NORMAL music mode (aka boring, aka works fine, aka looks less like a seizure) 
					try:
						# Calculate the levels
						data = calculate_levels(chunk, 
												audio_setup.Audio.PERIOD_SIZE, 
												audio_setup.Audio.SAMPLE_RATE, 
												audio_setup.Audio.FREQUENCY_LIMITS,
												audio_setup.Audio.COLUMNS,
												audio_setup.Audio.NUM_CHANNELS)
						if not np.isfinite(np.sum(data)):
							# Bad data --- skip it
							continue
							
						# Interpret the data 
						converted_data = self.convert_data(data, mean, std)
				
						# Turn the music data into light (Computer Scientist Alchemy)
						self.convert_matrix_to_packet(converted_data)
					
						# Keep a running average of the values to denoise the input audio
						mean, std, recent_samples, num_samples = stabilize.compute_running_average(data, mean, std, recent_samples, num_samples)

					except ValueError as e:
						# TODO: This is most likely occuring due to extra time in calculating
						# mean/std every n samples which causes more to be read than expected the
						# next time around.  Would be good to update mean/std in separate thread to
						# avoid this --- but for now, skip it when we run into this error is good 
						# enough ;)
						logging.debug("skipping update: " + str(e))
						continue

			# If not in music mode, and the MIDI controller changed, update the lights
			elif changed:
				self.update_lights()

			if changed:
				self.update_volume()
			# Nothing happened. Nap time.
			else:
				# waste time so that we don't eat too much CPU
				time.sleep(.005)
Esempio n. 2
0
def analyze_line_in(led_strip, hacker_school=True):
    start_time = time.time()

    while True:
        if hacker_school and time.time() - start_time > 60 * 2:
            hacker_school_display()
            start_time = time.time()

        size, chunk = input.read()
        if size > 0:
            L = (len(chunk)/2 * 2)
            chunk = chunk[:L]
            data = calculate_levels(chunk, SAMPLE_RATE, frequency_limits)
            led_strip.display_data(data[::-1])
Esempio n. 3
0
 def audio_process(self, session, buffer):
     data = calculate_levels(buffer, self.samplerate, self.frequency_limits, self.channels, self.bits)
     led_strip.display_data(data[::-1])
Esempio n. 4
0
def analyze_audio_file(led_strip, path):
    for chunk, sample_rate in read_musicfile_in_chunks(path, play_audio=True):
        data = calculate_levels(chunk, sample_rate, frequency_limits)
        led_strip.display_data(data)