def mono_to_stereo(track, track2): p.check(track.get_nchannels() == 1 and track2.get_nchannels() == 1, details="non mono Tracks") p.check_same_params(track, track2) return Track(np.column_stack((track.get_data(), track2.get_data())), track.get_size(), 2, track.get_samplewidth(), track.get_framerate())
def write (self): p.check(not(self.infinite), details ="cannot completly load an infinite stream") try: return self.wave_signal.writeframesraw(self.track.get_raw_data()) except Exception as e: p.eprint("Error occured while writting the frames to destination", self.file) print(e)
def crossfade_exp(track1, track2, factor, t): p.check_same_params(track1, track2) return add(fade_exp(track1, factor, t), fade_inv(track2, factor, t), t, a1=0.9, a2=1)
def __init__ (self, track=None, inputStreamSoundCard=None): ## selecting the valid source tin = (track is None) iin = (inputStreamSoundCard is None) p.check(tin != iin) if (tin): self.source = inputStreamSoundCard elif (iin): self.source = track self.normalized_data = self.source.get_data()/2**(8 * self.source.get_samplewidth()) self.cursor = 0 self.traces = {} ## configuring the pyqtgraph pg.setConfigOptions(antialias=True) self.app = QtGui.QApplication(sys.argv) self.win = pg.GraphicsWindow(title='Vumeter') self.win.setWindowTitle('Vumeter') self.win.setGeometry(5, 115, 1910, 1070) self.x = np.arange(0, self.chunk, 1) half = self.chunk//2 wf_xlabels = [(0, '0'), (half, str(half)), (self.chunk, str(self.chunk))] wf_xaxis = pg.AxisItem(orientation='bottom') wf_xaxis.setTicks([wf_xlabels]) wf_ylabels = [(0, '0'), (0.5, '0.5'), (1, '1')] wf_yaxis = pg.AxisItem(orientation='left') wf_yaxis.setTicks([wf_ylabels]) self.waveform = self.win.addPlot( title='WAVEFORM', row=1, col=1, axisItems={'bottom': wf_xaxis, 'left': wf_yaxis}, )
def stop_play(self): """! stop_play [] Stop playing from sound card """ sd = self.stream_in.pop("soundcard") p.check_non_none(sd, details="Sound card not opened") sd.close()
def open(self, mode): p.check(self.launched, details = "cannot open already launched stream") try: self.wave_signal = wave.open(self.file, mode) self.launched = True except: p.eprint("IOError occured while opening file {!r} in {!r} mode".format(self.file, mode))
def add(track, track2, t, a1=0.5, a2=0.5): #p.check((a1, a2), lambda x: x[0] + x[1] == 1, details="Sum of amplitudes must be 1") p.check_same_params(track, track2) r = t * track2.get_framerate() extention_frames_b = track2.get_size() - r extention_frames_f = track.get_size() - r return Track( a1 * track.extend_with_zeroes_behind(extention_frames_b) + a2 * track2.extend_with_zeroes_front(extention_frames_f), extention_frames_b + extention_frames_f + r, track.get_nchannels(), track.get_samplewidth(), track.get_framerate())
def write(self, file_path, track_id): """! Write track in output stream write [file_path, track_id] @param file_path path of the output file @param track_id Id of the track to be written Write the track with id *track_id* in *file_path* """ track = self.av_tracks.get(track_id) p.check_non_none(track, details="Invalid track ID") stream = Output(file_path, track) stream.write()
def stop_record(self, track_id, nframes): """! Stop recording from soundcard stop_record [track_id, nframes] @param track_id: Id of track to store result @param nframes number of frames to store Stop recording from sound card and store *nframes* frames in *track_id* """ sd = self.stream_in.pop("soundcard") p.check_non_none(sd, details="Sound card not opened") self.av_tracks.update({track_id: sd.read(nframes)}) sd.close()
def amplitude(self, track_id_in, track_id_out, a): """! Multiply amplitude of a track amplitude [track_id_in, track_id_out, a] @param track_id_in Id of input track @param track_id_out Id of output track @param a multiplying factor Multiply amplitude of the track *track_id_in* by a factor of *a* and stores it in *track_id_out* """ track = self.av_tracks.get(track_id_in) p.check_non_none(track, details="Invalid track ID") self.av_tracks.update({track_id_out: op.amplitude(track, a)})
def nullify(self, track_id_in, track_id_out, start=0, end=None): """! Nullify a track nullify [track_id_in, track_id_out, start=0, end=None] @param track_id_in Id of input track @param track_id_out Id of output track @param start second when to start @param end second when to end Nullify the track *track_id_in* and stores it in *track_id_out* """ track = self.av_tracks.get(track_id_in) p.check_non_none(track, details="Invalid track ID") self.av_tracks.update({track_id_out: op.nullify(track, start, end)})
def fadeinv(self, track_id_in, track_id_out, factor, t): """! Fade inverse a track fadeinv [track_id_in, track_id_out, factor, t] @param track_id_in Id of input track @param track_id_out Id of output track @param factor fading factor @param t second when to start the fade Fade inverse the track *track_id_in*, with factor *factor* starting from *t*, and stores it in *track_id_out* """ track = self.av_tracks.get(track_id_in) p.check_non_none(track, details="Invalid track ID") self.av_tracks.update({track_id_out: op.fade_inv(track, factor, t)})
def convolve(self, track_id_in1, track_id_in2, track_id_out): """! Convolve two tracks convolve [track_id_in1, track_id_in2, track_id_out] @param track_id_in1 Id of first input track @param track_id_in2 Id of second input track @param track_id_out Id of output track Convolve *track_id_in1* and *track_id_in2* and stores it in *track_id_out* """ track1 = self.av_tracks.get(track_id_in1) track2 = self.av_tracks.get(track_id_in2) p.check_non_none(track1, details="Invalid first track ID") p.check_non_none(track2, details="Invalid second track ID") self.av_tracks.update({track_id_out: op.convolve(track1, track2)})
def __init__(self, data, nframes, nchannels, samplewidth=2, framerate=44100): self.size = nframes self.nchannels = nchannels self.samplewidth = samplewidth self.framerate = framerate self.time = framerate * nframes if (type(data) == bytes): p.check(nframes * samplewidth * nchannels == len(data)) self.data = self.byte_float_converter(data) else: p.check(data.shape == (nframes, nchannels)) self.data = np.array(data)
def stereo(self, track_id_in1, track_id_in2, track_id_out): """! Combine two tracks in stereo (must have the same properties) stereo [track_id_in1, track_id_in2, track_id_out] @param track_id_in1 Id of first input track @param track_id_in2 Id of second input track @param track_id_out Id of output track Join *track_id_in1* and *track_id_in2* in stereo and stores it in *track_id_out* (must have the same format) """ track1 = self.av_tracks.get(track_id_in1) track2 = self.av_tracks.get(track_id_in2) p.check_non_none(track1, details="Invalid first track ID") p.check_non_none(track2, details="Invalid second track ID") self.av_tracks.update( {track_id_out: op.mono_to_stereo(track1, track2)})
def play(self, track_id, device=sd.default.device): """! Play track from soundcard play [track_id, device=sd.default.device] @param track_id Id of track to be played @param device device from which to play (check sounddevice library to change default value) Start playing *track_id* from sound card """ track = self.av_tracks.get(track_id) p.check_non_none(track, details="Invalid track ID") sd = OutputStream_SoundCard(track, device=device) sd.write() self.stream_out.update({"soundcard": sd})
def __init__(self, processor): p.check_instance(processor, Processor, details="Processor given not instance of processor") self.p = processor self.op_ctrl = { "stop": self.p.stop, "execute": self.p.execute, "reset": self.p.reset, "tracks" : self.p.tracks, "streams": self.p.streams, "show" : self.p.show, } self.op_d = { "open" : self.p.openn, "close" : self.p.close, "read" : self.p.read, "write" : self.p.write, "free" : self.p.free, "record" : self.p.record, "stop_record" : self.p.stop_record, "play" : self.p.play, "stop_play" : self.p.stop_play, "sine" : self.p.sine, "constant" : self.p.constant, "silence" : self.p.silence, "nullify" : self.p.nullify, "fade" : self.p.fade, "fadeinv" : self.p.fadeinv, "amplitude" : self.p.amplitude, "crossfade" : self.p.crossfade, "stereo" : self.p.stereo, "mix" : self.p.mix, "vumeter" : self.p.vumeter, } self.current_op = None
def crossfade(self, track_id_in1, track_id_in2, track_id_out, factor, t): """! Crossfade two tracks crossfade [id_track_in1, track_id_in2, track_id_out, factor, t] @param track_id_in1 Id of first input track @param track_id_in2 Id of second input track @param track_id_out Id of output track @param factor fading factor @param t second when to start the fade Crossfade *track_id_in1* and *track_id_in2* with factor *factor* starting from *t*, and stores it in *track_id_out* """ track1 = self.av_tracks.get(track_id_in1) track2 = self.av_tracks.get(track_id_in2) p.check_non_none(track1, details="Invalid first track ID") p.check_non_none(track2, details="Invalid second track ID") self.av_tracks.update( {track_id_out: op.crossfade_exp(track1, track2, factor, t)})
def mix(self, track_id_in1, track_id_in2, track_id_out, t, a1=0.5, a2=0.5): """! Mix two tracks mix [track_id_in1, track_id_in2, track_id_out, a1=0.5, a2=0.5] @param track_id_in1 Id of first input track @param track_id_in2 Id of second input track @param track_id_out Id of output track @param t second when to start the fade @param a1 amplitude of first track @param a2 amplitude of second track Mix *track_id_in1* with amplitude *a1* and *track_id_in2* with amplitude *a2* and stores it in *track_id_out* """ track1 = self.av_tracks.get(track_id_in1) track2 = self.av_tracks.get(track_id_in2) p.check_non_none(track1, details="Invalid first track ID") p.check_non_none(track2, details="Invalid second track ID") self.av_tracks.update( {track_id_out: op.add(track1, track2, t, a1, a2)})
def read(self, file_id, track_id, t="all"): """! Read track from input stream read [file_id, track_id, t="all"] @param file_id Id of the input stream @param track_id Id to store the track @param t Time in seconds to read from file (default all) Read t seconds of *file_id* and stores the track with *track_id* in the available tracks """ if (t == "all"): s = self.stream_in.pop(file_id) p.check_non_none(s, details="Invalid stream ID") track = s.read_all() s.close() else: s = self.stream_in.get(file_id) p.check_non_none(s, details="Invalid stream ID") fs = s.frame_rate() track = s.read_n_frames(int(float(t) * fs)) self.av_tracks.update({track_id: track})
def get_size (self): p.check(self.launched, details ="cannot return size of unopened stream") if (self.infinite): return m.inf else: return self.wave_parameters[3]
def get_current_pos(self): p.check(self.launched, details ="cannot return pointer of unopened stream") return self.wave_signal.tell()
def set_reading_pos (self, pos): p.check(self.launched, details ="cannot modify pointer of unopened stream") p.check_in_range(pos, endExclusive=self.size()) self.wave_signal.set(pos)
def set_as_mono(self): p.check(self.launched, details ="cannot verify if mono for unopened stream") self.wave_signal.setnchannels(1)
def __init__(self, decoder): p.check_instance(decoder, Decoder, details="Decoder given not instance of decoder") self.decoder = decoder
def amplitude(track, a): p.check(a, lambda x: x >= 0 and x <= 1, details="Amplitude must be between 0 and 1") return Track(a * track.get_data(), track.get_size(), track.get_nchannels(), track.get_samplewidth(), track.get_framerate())
def set_size (self, n): p.check(self.launched, details ="cannot return size of unopened stream") self.wave_signal.setnframes(n)
def set_frame_rate(self, n): p.check(self.launched, details ="cannot obtain frame rate for unopened stream") self.wave_signal.setframerate(n)
def set_sample_width (self, n): p.check(self.launched, details ="cannot obtain sample width for unopened stream") self.wave_signal.setsampwidth(n)
def convolve(track, track2): p.check_same_params(track, track2) return Track(np.convolve(track.get_data(), track2.get_data()), track.get_size(), track.get_nchannels(), track.get_samplewidth(), track.get_framerate())