Example #1
0
    def run(self, args):
        # Loads basis if present
        if args.basis is not None:
            b = ld.LinearDecomposition().load(args.basis)
        else:
            b = None

        if args.basis is not None and b.data.right != {}:
            print "Basis doesn't have empty right side. Ignoring it."

        # Size of the decomposition (used when finding a basis too)
        if args.size is None:
            args.size = [None, None, None] # Simulate 3 values
        for i in range(len(args.size)):
            if args.size[i] == 'None' or args.size[i] == 'null':
                args.size[i] = None

        # Gather input spectrograms
        s_list = []
        s_meta = []
        for filename in args.piece:
            with open(filename, 'rb') as handler:
                s_list.append(spectrogram.Spectrogram().load(handler))
                s_meta.append(md.FileMetadata(handler))

        # Converts arguments
        size = int(args.size[0]) if args.size[0] is not None else None
        instrument = args.size[1] if args.size[1] is not None else ''
        note = args.size[2] if args.size[2] is not None else ''

        # Decompose
        d = self.compute(s_list,
                         size,
                         instrument,
                         note,
                         b,
                         args.beta,
                         args.min_delta,
                         args.max_iterations,
                         False)

        # Associates an activation metadata with its given spectrogram's
        # metadata
        for k, data, metadata in d.right():
            metadata.spectrogram_input = s_meta[k[-1]]

        # Checks if basis was provided
        if b is not None:
            # If provided, adds it as basis metadata for each activation
            meta = md.FileMetadata(args.basis)
            for k, data, metadata in d.right():
                metadata.basis_input = meta
        else:
            # Otherwise, the basis was computed right now, so we set its
            # metadata with the list of all spectrograms' metadata
            d.metadata.left[(args.size[1], args.size[2])].spectrogram_input = \
                    s_meta

        d.save(args.outfile)
Example #2
0
 def run(self, args):
     e = self.evaluate(args.id,
                       score.Score().load(args.estimated),
                       score.Score().load(args.reference),
                       args.frame_length, False)
     e.metadata.estimated_input = md.FileMetadata(args.estimated)
     e.metadata.reference_input = md.FileMetadata(args.reference)
     e.save(args.outfile)
Example #3
0
 def run(self, args):
     e = self.evaluate(args.id,
                       score.Score().load(args.estimated),
                       score.Score().load(args.reference),
                       args.onset_tolerance, args.duration_tolerance,
                       args.ignore_pitch, False)
     e.metadata.estimated_input = md.FileMetadata(args.estimated)
     e.metadata.reference_input = md.FileMetadata(args.reference)
     e.save(args.outfile)
Example #4
0
 def run(self, args):
     d = self.median_filter(ld.LinearDecomposition().load(args.infile),
                            args.window, False)
     meta = md.FileMetadata(args.infile)
     for k, data, metadata in d.right():
         metadata.activation_input = meta
     d.save(args.outfile)
Example #5
0
    def eof(self):
        self.scores = {}
        for channel in self.channels.keys():
            s = score.Score()
            for n in self.channels[channel].keys():
                onsets = self.channels[channel][n]['onset']
                offsets = self.channels[channel][n]['offset']
                onsets.sort()
                offsets.sort()

                if len(onsets) != len(offsets):
                    raise IOError, \
                        """Got different number of onsets and offsets for the
                        same note."""

                wrong_durations = [(off-on) < 0
                                   for on,off in zip(onsets,offsets)]
                if any(wrong_durations):
                    raise IOError, 'Got negative note durations.'

                s.append([note.Note(pitch=n, onset=on, offset=off)
                          for on,off in zip(onsets,offsets)])

            s.metadata.method_metadata = md.Metadata(type="midi")
            s.metadata.input = md.FileMetadata(self.file_handle)

            self.scores[channel] = s
Example #6
0
 def run(self, args):
     d = self.hard_sparsity(ld.LinearDecomposition().load(args.infile),
                       args.sparsity,
                       False)
     meta = md.FileMetadata(args.infile)
     for k, data, metadata in d.right():
         metadata.activation_input = meta
     d.save(args.outfile)
Example #7
0
    def run(self, args):
        d = self.compute(spectrogram.Spectrogram().load(args.infile),
                         args.note, args.instrument, args.number_frames,
                         args.frame_skip, False)

        d.metadata.left[(args.instrument, args.note)].spectrogram_input = \
                md.FileMetadata(args.infile)

        d.save(args.outfile)
Example #8
0
 def run(self, args):
     new_s = self.trim_notes(score.Score().load(args.infile),
                             args.minimum_duration, args.maximum_duration,
                             args.minimum_pitch, args.maximum_pitch,
                             args.minimum_onset, args.maximum_onset,
                             args.minimum_offset, args.maximum_offset,
                             False)
     new_s.metadata.input = md.FileMetadata(args.infile)
     new_s.save(args.outfile)
Example #9
0
 def run(self, args):
     s = self.convert(ld.LinearDecomposition().load(args.infile),
                      args.instrument, args.frequency, args.minimum_length,
                      False)
     s.metadata.input = md.FileMetadata(args.infile)
     s.save(args.outfile)
Example #10
0
    def convert(self,
                wav_file,
                window_length=2048,
                dft_length=2048,
                window_step=1024,
                spectrum_type='magnitude',
                save_metadata=True,
                wav_rate=None,
                wav_data=None):
        """Converts a WAV file to a spectrogram.

        Args:
            wav_file: handler for an open wav file.
            window_length: window length for dft, in samples. Default: 2048.
            dft_length: dft length used. Default: 2048.
            window_step: step between windows, in samples. Default: 1024.
            spectrum_type: type of spectrum extracted. Default: 'magnitude'.
            save_metadata: flag indicating whether the metadata should be
                           computed. Default: True.

        Returns:
            Spectrogram object.
        """
        # Sets metadata
        s = spectrogram.Spectrogram()
        s.metadata.sampling_configuration.window_length = window_length
        s.metadata.sampling_configuration.dft_length = dft_length
        s.metadata.sampling_configuration.window_step = window_step
        s.metadata.sampling_configuration.spectrum_type = spectrum_type

        if wav_data is None:
            from_data = False
        else:
            from_data = True

        if isinstance(wav_file, basestring):
            wav_file = open(wav_file, "rb")

        if save_metadata:
            s.metadata.input = md.FileMetadata(wav_file)

        # Calculates data
        if not from_data:
            rate, data = self.load_audio(wav_file, fs=wav_rate)
        else:
            rate = wav_rate
            data = wav_data

        #data /= numpy.max(numpy.abs(data)) # Normalization to -1/+1 range

        data -= numpy.mean(data)
        data /= numpy.var(data)**(0.5)

        if data.ndim > 1:
            data = numpy.sum(data, axis=1)

        s.metadata.min_time = 0.0
        s.metadata.min_freq = 0.0
        s.metadata.max_time = len(data) / float(rate)
        s.metadata.max_freq = 0.5 * rate

        s.metadata.sampling_configuration.fs = rate
        s.metadata.sampling_configuration.ofs = \
                s.metadata.sampling_configuration.fs / \
                float(s.metadata.sampling_configuration.window_step)

        nSamples = len(data)

        if nSamples == 0:
            raise ValueError("File '%s' has no audio" % wav_file.name)

        window = scipy.signal.hamming(dft_length, sym=False)

        #magnitude spectrum is the absolute value (real part) of the complex spectrum
        magnitude_spectrum = numpy.abs(
            self.stft(data,
                      n_fft=dft_length,
                      hop_length=window_step,
                      win_length=window_length,
                      window=window,
                      center=True))

        if s.metadata.sampling_configuration.spectrum_type == 'sqrt':
            magnitude_spectrum = numpy.sqrt(magnitude_spectrum)

        if s.metadata.sampling_configuration.spectrum_type == 'power':
            magnitude_spectrum = magnitude_spectrum**2

        if s.metadata.sampling_configuration.spectrum_type == 'log':
            magnitude_spectrum = numpy.log10(
                numpy.sqrt(magnitude_spectrum) + 10**(-6))

        if s.metadata.sampling_configuration.spectrum_type == 'db':
            magnitude_spectrum = 20 * numpy.log10(magnitude_spectrum +
                                                  numpy.finfo(numpy.float).eps)

        s.data = copy.deepcopy(magnitude_spectrum)
        # print type(s.data), s.data.shape
        #pylab.show()

        return s
Example #11
0
 def run(self, args):
     new_s = self.trim(spectrogram.Spectrogram().load(args.infile),
                       args.minimum_frequency, args.maximum_frequency,
                       args.minimum_time, args.maximum_time, False)
     new_s.metadata.input = md.FileMetadata(args.infile)
     new_s.save(args.outfile)