Esempio n. 1
0
def ogg_to_wav(source, target):
    """
	source : source audio file
	target : target audio file
	"""
    x, fs, enc = oggread(source)
    WavFileName = target
    wavwrite(x, WavFileName, fs, enc='pcm24')
Esempio n. 2
0
    def predict(self, data, **kwargs):
        """
        Used in the predict phase, after training.  Override
        """
        frame1 = pd.read_csv(settings.MIDI_FEATURE_PATH)
        frame2 = pd.read_csv(settings.FEATURE_PATH)

        frame = pd.concat([frame1,frame2],axis=0)
        non_predictors = kwargs.get('non_predictors')
        target = kwargs.get('target_var')

        frame.index = range(frame.shape[0])

        alg = RandomForestTrain()
        good_names = [i for i in frame.columns if i not in non_predictors]
        for c in good_names:
            frame[c] = frame[c].astype(float)

        for c in good_names:
            frame[c] = frame[c].real

        clf = alg.train(np.asarray(frame[good_names]),frame[target],**alg.args)

        evolutions = 2
        track_count = 100
        patterns_to_pick = int(math.floor(track_count/4))
        remixes_to_make = int(math.floor(track_count/4))
        additions_to_make = int(math.floor(track_count/4))
        patterns = generate_patterns(track_count,data)
        for z in xrange(0,evolutions):
            new_quality, quality, patterns = rate_tracks(patterns, clf)
            patterns = patterns[0:patterns_to_pick]
            for i in xrange(0,remixes_to_make):
                patterns.append(remix(random.choice(patterns[:patterns_to_pick]), random.choice(patterns[:patterns_to_pick])))
            #for i in xrange(0,additions_to_make):
            #    patterns.append(add_song(random.choice(patterns[:patterns_to_pick]), random.choice(patterns[:patterns_to_pick])))
            new_patterns = []
            for p in patterns:
                if p not in new_patterns:
                    new_patterns.append(p)
            patterns = new_patterns
            patterns += generate_patterns(track_count - len(patterns), data)
        new_quality, quality, patterns = rate_tracks(patterns, clf)

        feats = []
        for (i,p) in enumerate(patterns):
            time = strftime("%m-%d-%Y-%H%M%S", gmtime())
            fname = time+random.choice(words)+".mid"
            oggpath = write_and_convert(p,fname)
            dat, fs, enc = oggread(oggpath)
            f = process_song(dat[:settings.MUSIC_TIME_LIMIT * fs,:],fs)
            feats.append(f)
        feats = pd.DataFrame(feats)
        feats['label_code'] = [2] * feats.shape[0]
        feats['label'] = ["generated"] * feats.shape[0]
        feats.to_csv(os.path.abspath(os.path.join(settings.DATA_PATH,"generated_midi_features.csv")))

        return data
Esempio n. 3
0
def generate_note(mfile):
    generate_midi(mfile)
    ofile = convert_to_ogg(mfile)
    try:
        data, fs, enc = oggread(ofile)
    except Exception:
        log.exception("Could not read sound file.")
        raise IOError
    return data, fs, enc
Esempio n. 4
0
def evaluate_midi_quality(pattern, clf):
    midi_path = write_midi_to_file(pattern)
    oggpath = convert_to_ogg_tmp(midi_path)
    data, fs, enc = oggread(oggpath)
    maxl = fs * settings.MUSIC_TIME_LIMIT
    upto = fs * 2
    if upto>len(data):
        log.error("Input data is too short")
        raise Exception
    data = data[:maxl,:]
    features = process_song(data,fs)
    quality = clf.predict_proba(features)[0,1]
    os.remove(oggpath)
    os.remove(midi_path)
    return quality
Esempio n. 5
0
def read_sound(fpath, limit=settings.MUSIC_TIME_LIMIT):
    try:
        data, fs, enc = oggread(fpath)
        upto = fs * limit
    except IOError:
        log.error("Could not read file at {0}".format(fpath))
        raise IOError
    if data.shape[0] < upto:
        log.error("Music file at {0} not long enough.".format(fpath))
        raise ValueError
    try:
        if len(data.shape) == 1 or data.shape[1] != 2:
            data = np.vstack([data, data]).T
    except Exception:
        log.error("Invalid dimension count for file at {0}. Do you have left and right channel audio?".format(fpath))
        raise ValueError
    data = data[0:upto, :]
    return data, fs, enc
Esempio n. 6
0
def read_sound(fpath, limit = settings.MUSIC_TIME_LIMIT):
    try:
        data, fs, enc = oggread(fpath)
        upto = fs* limit
    except IOError:
        log.exception("Could not read file")
        raise IOError
    if data.shape[0]<upto:
        log.error("Music file not long enough.")
        raise ValueError
    try:
        if data.shape[1]!=2:
            log.error("Invalid dimension count. Do you have left and right channel audio?")
            raise ValueError
    except Exception:
        log.error("Invalid dimension count. Do you have left and right channel audio?")
        raise ValueError
    data = data[0:upto,:]
    return data, fs, enc
Esempio n. 7
0
import numpy
import cmath
import scikits.audiolab as audio
import matplotlib.pyplot as plt

#from music to array
(inputsignal, samplingrate, bits) = audio.oggread('Free_as_a_Bird.ogg')

#fast furier transform using numpy
transformedsignal = numpy.fft.fft(inputsignal)

#plot using matlibplot
norm = lambda x: cmath.polar(x)[0]
plt.plot([norm(x) for x in transformedsignal])
plt.show()

#source http://jeremykun.com/2012/07/18/the-fast-fourier-transform/
Esempio n. 8
0
    def predict(self, data, **kwargs):
        """
        Used in the predict phase, after training.  Override
        """
        p = Pool(4, maxtasksperchild=50)
        audio_dir = kwargs['audio_dir']
        timeout = kwargs['timeout']
        oll = kwargs['only_labelled_lines']
        pff = kwargs['processed_files_limit']

        all_files = []
        for ad in os.listdir(audio_dir):
            ad_path = os.path.abspath(os.path.join(audio_dir,ad))
            if os.path.isdir(ad_path):
                files = os.listdir(ad_path)
                all_files += [os.path.abspath(os.path.join(ad_path,f)) for f in files]
            else:
                all_files += [ad_path]
        self.all_files = [f for f in all_files if f.endswith(".ogg")]
        frames = []
        counter = 0
        for f in self.all_files:
            season,episode = self.extract_season(f)
            if season is None or (season==11 and episode==6):
                continue
            subtitle_frame = data[((data['season']==season) & (data['episode']==episode))]
            if subtitle_frame.shape[0]==0:
                continue

            #To cause loop to end early, remove if needed
            if oll:
                label_frame = subtitle_frame[(subtitle_frame['label']!="")]
                if label_frame.shape[0]==0:
                    continue
            if pff is not None and isinstance(pff, int) and counter>=pff:
                break

            counter+=1
            log.info("On file {0} Season {1} Episode {2}".format(counter,season,episode))
            f_data, fs, enc  = oggread(f)
            subtitle_frame = subtitle_frame.sort('start')
            subtitle_frame.index = range(subtitle_frame.shape[0])
            samps = []
            good_rows = []
            for i in xrange(0,subtitle_frame.shape[0]):
                start = subtitle_frame['start'].iloc[i]
                end = subtitle_frame['end'].iloc[i]
                if end-start>6 or (subtitle_frame['label'][i]=='' and oll):
                    continue
                samp = f_data[(start*fs):(end*fs),:]
                samps.append({'samp' : samp, 'fs' : fs})
                good_rows.append(i)
            r = p.imap(process_subtitle, samps,chunksize=1)
            sf = subtitle_frame.iloc[good_rows]
            results = []
            for i in range(len(samps)):
                try:
                    results.append(r.next(timeout=timeout))
                except TimeoutError:
                    results.append(None)
            good_rows = [i for i in xrange(0,len(results)) if results[i]!=None]
            audio_features = [i for i in results if i!=None]
            good_sf = sf.iloc[good_rows]
            good_sf.index = range(good_sf.shape[0])
            audio_frame = pd.DataFrame(audio_features)
            audio_frame.index = range(audio_frame.shape[0])
            df = pd.concat([good_sf,audio_frame],axis=1)
            df = df.fillna(-1)
            df.index = range(df.shape[0])
            frames.append(df)
            lab_df_shape = df[df['label']!=''].shape[0]
            log.info("Processed {0} lines, {1} of which were labelled".format(df.shape[0],lab_df_shape))
        p.close()
        p.join()
        log.info("Done processing episodes.")
        data = pd.concat(frames,axis=0)
        data.index = range(data.shape[0])
        data.index = range(data.shape[0])

        for c in list(data.columns):
            data[c] = data[c].real
        for k in CHARACTERS:
            for i in CHARACTERS[k]:
                data['label'][data['label']==i] = k
        self.label_codes = {k:i for (i,k) in enumerate(set(data['label']))}
        reverse_label_codes = {self.label_codes[k]:k for k in self.label_codes}
        data['label_code'] = [self.label_codes[k] for k in data['label']]
        self.seq = SequentialValidate()

        #Do cv to get error estimates
        cv_frame = data[data['label']!=""]
        self.seq.train(cv_frame,**self.seq.args)
        self.res = self.seq.results
        self.res = self.res[['line', 'label','label_code','result_code','result_label']]

        exact_percent, adj_percent = compute_error(self.res)
        log.info("Exact match percent: {0}".format(exact_percent))
        log.info("Adjacent match percent: {0}".format(adj_percent))
        #Predict in the frame
        alg = RandomForestTrain()
        target = cv_frame['label_code']
        non_predictors = ["label","line","label_code"]
        train_names = [l for l in list(cv_frame.columns) if l not in non_predictors]
        train_data = cv_frame[train_names]
        predict_data = data[train_names]
        clf = alg.train(train_data,target,**alg.args)
        data['result_code'] = alg.predict(predict_data)
        data['result_label'] = [reverse_label_codes[k] for k in data['result_code']]
        return data
Esempio n. 9
0
    def predict(self, data, **kwargs):
        """
        Used in the predict phase, after training.  Override
        """
        p = Pool(4, maxtasksperchild=50)
        audio_dir = kwargs['audio_dir']
        timeout = kwargs['timeout']
        oll = kwargs['only_labelled_lines']
        pff = kwargs['processed_files_limit']

        all_files = []
        for ad in os.listdir(audio_dir):
            ad_path = os.path.abspath(os.path.join(audio_dir, ad))
            if os.path.isdir(ad_path):
                files = os.listdir(ad_path)
                all_files += [
                    os.path.abspath(os.path.join(ad_path, f)) for f in files
                ]
            else:
                all_files += [ad_path]
        self.all_files = [f for f in all_files if f.endswith(".ogg")]
        frames = []
        counter = 0
        for f in self.all_files:
            season, episode = self.extract_season(f)
            if season is None or (season == 11 and episode == 6):
                continue
            subtitle_frame = data[((data['season'] == season) &
                                   (data['episode'] == episode))]
            if subtitle_frame.shape[0] == 0:
                continue

            #To cause loop to end early, remove if needed
            if oll:
                label_frame = subtitle_frame[(subtitle_frame['label'] != "")]
                if label_frame.shape[0] == 0:
                    continue
            if pff is not None and isinstance(pff, int) and counter >= pff:
                break

            counter += 1
            log.info("On file {0} Season {1} Episode {2}".format(
                counter, season, episode))
            f_data, fs, enc = oggread(f)
            subtitle_frame = subtitle_frame.sort('start')
            subtitle_frame.index = range(subtitle_frame.shape[0])
            samps = []
            good_rows = []
            for i in xrange(0, subtitle_frame.shape[0]):
                start = subtitle_frame['start'].iloc[i]
                end = subtitle_frame['end'].iloc[i]
                if end - start > 6 or (subtitle_frame['label'][i] == ''
                                       and oll):
                    continue
                samp = f_data[(start * fs):(end * fs), :]
                samps.append({'samp': samp, 'fs': fs})
                good_rows.append(i)
            r = p.imap(process_subtitle, samps, chunksize=1)
            sf = subtitle_frame.iloc[good_rows]
            results = []
            for i in range(len(samps)):
                try:
                    results.append(r.next(timeout=timeout))
                except TimeoutError:
                    results.append(None)
            good_rows = [
                i for i in xrange(0, len(results)) if results[i] != None
            ]
            audio_features = [i for i in results if i != None]
            good_sf = sf.iloc[good_rows]
            good_sf.index = range(good_sf.shape[0])
            audio_frame = pd.DataFrame(audio_features)
            audio_frame.index = range(audio_frame.shape[0])
            df = pd.concat([good_sf, audio_frame], axis=1)
            df = df.fillna(-1)
            df.index = range(df.shape[0])
            frames.append(df)
            lab_df_shape = df[df['label'] != ''].shape[0]
            log.info("Processed {0} lines, {1} of which were labelled".format(
                df.shape[0], lab_df_shape))
        p.close()
        p.join()
        log.info("Done processing episodes.")
        data = pd.concat(frames, axis=0)
        data.index = range(data.shape[0])
        data.index = range(data.shape[0])

        for c in list(data.columns):
            data[c] = data[c].real
        for k in CHARACTERS:
            for i in CHARACTERS[k]:
                data['label'][data['label'] == i] = k
        self.label_codes = {k: i for (i, k) in enumerate(set(data['label']))}
        reverse_label_codes = {
            self.label_codes[k]: k
            for k in self.label_codes
        }
        data['label_code'] = [self.label_codes[k] for k in data['label']]
        self.seq = SequentialValidate()

        #Do cv to get error estimates
        cv_frame = data[data['label'] != ""]
        self.seq.train(cv_frame, **self.seq.args)
        self.res = self.seq.results
        self.res = self.res[[
            'line', 'label', 'label_code', 'result_code', 'result_label'
        ]]

        exact_percent, adj_percent = compute_error(self.res)
        log.info("Exact match percent: {0}".format(exact_percent))
        log.info("Adjacent match percent: {0}".format(adj_percent))
        #Predict in the frame
        alg = RandomForestTrain()
        target = cv_frame['label_code']
        non_predictors = ["label", "line", "label_code"]
        train_names = [
            l for l in list(cv_frame.columns) if l not in non_predictors
        ]
        train_data = cv_frame[train_names]
        predict_data = data[train_names]
        clf = alg.train(train_data, target, **alg.args)
        data['result_code'] = alg.predict(predict_data)
        data['result_label'] = [
            reverse_label_codes[k] for k in data['result_code']
        ]
        return data