Exemplo n.º 1
0
    def __init__(self):
        self.__INPUT_SAMPLING_RATE = int(11025)
        self.__N_SAMPLES_WINDOW = int(1024)
        self.__N_SAMPLES_OVERLAP = int(0.5 * self.__N_SAMPLES_WINDOW)
        self.__WINDOW = 'hann'
        self.__CHROME_DRIVER_PATH = r"resources/chromedriver"

        self.__db = DBManager()
        self.__audio_manager = AudioBooksManager(self.__db,
                                                 self.__CHROME_DRIVER_PATH)
        self.__noise_manager = NoiseManager(self.__db)
Exemplo n.º 2
0
 def __init__(self, db=DBManager()):
     self.resources = {
         'airDryer': 'https://www.youtube.com/watch?v=PNAGqh2h3AA',
         'serverRoom': 'https://www.youtube.com/watch?v=gLLvXi1Usrc',
         'coffeeShop': 'https://www.youtube.com/watch?v=BOdLmxy06H0',
         'crowd': 'https://www.youtube.com/watch?v=IKB3Qiglyro',
         'peopleTalking': 'https://www.youtube.com/watch?v=PHBJNN-M_Mo',
         'cityRain': 'https://www.youtube.com/watch?v=eZe4Q_58UTU',
         'city1': 'https://www.youtube.com/watch?v=cDWZkXjDYsc',
         'city2': 'https://www.youtube.com/watch?v=YF3pj_3mdMc',
         'city3': 'https://www.youtube.com/watch?v=8s5H76F3SIs',
         'city4': 'https://www.youtube.com/watch?v=Vg1mpD1BICI'
     }
     self.ydl_opts = {
         'format':
         'bestaudio/best',
         ''' 'postprocessors': [{
             'key': 'FFmpegExtractAudio',
             'preferredcodec': 'mp3',
             'preferredquality': '192',
         }],'''
         'outtmpl':
         '',
     }
     self.db = db
Exemplo n.º 3
0
class DataManager:
    def __init__(self):
        self.__INPUT_SAMPLING_RATE = int(11025)
        self.__N_SAMPLES_WINDOW = int(1024)
        self.__N_SAMPLES_OVERLAP = int(0.5 * self.__N_SAMPLES_WINDOW)
        self.__WINDOW = 'hann'
        self.__CHROME_DRIVER_PATH = r"resources/chromedriver"

        self.__db = DBManager()
        self.__audio_manager = AudioBooksManager(self.__db,
                                                 self.__CHROME_DRIVER_PATH)
        self.__noise_manager = NoiseManager(self.__db)

    def main(self, filename='', mode='', download=0, noises=[], limit=0):
        try:
            if download:
                logging.info('Downloading audio books for training model')
                self.__audio_manager.downloadData()
                logging.info('Downloading noise audios for training model')
                self.__noise_manager.downloadData()
            logging.info('Retrieving audio-noise combinations')
            file_combinations = self.__db.modelTrainGetCombination(
                self.__INPUT_SAMPLING_RATE, noises, limit)
            with File(filename, mode) as f:
                logging.info(
                    'Creating group for SPS:%d and FFT:%d' %
                    (self.__INPUT_SAMPLING_RATE, self.__N_SAMPLES_WINDOW))
                main_group = f.create_group(
                    np.string_(
                        'SPS%dFFT%d' %
                        (self.__INPUT_SAMPLING_RATE, self.__N_SAMPLES_WINDOW)))
                main_group.attrs.create(np.string_('SAMPLE_RATE'),
                                        np.string_(self.__INPUT_SAMPLING_RATE))
                main_group.attrs.create(np.string_('FFT_SIZE'),
                                        np.string_(self.__N_SAMPLES_WINDOW))
                for idx, file_combination in enumerate(file_combinations):
                    try:
                        logging.info('Loading data')
                        clean_info = self.__db.audioBookGetById(
                            file_combination[1])
                        clean = self.load_audio(clean_info[0][9],
                                                normalized=False)
                        if idx > 0:
                            if file_combination[2] != file_combinations[idx -
                                                                        1][2]:
                                noise_info = self.__db.noiseGetById(
                                    file_combination[2])
                                noise = self.load_audio(noise_info[0][3],
                                                        normalized=False)
                        else:
                            noise_info = self.__db.noiseGetById(
                                file_combination[2])
                            noise = self.load_audio(noise_info[0][3],
                                                    normalized=False)

                        if clean.duration_seconds > noise.duration_seconds:
                            logging.info(
                                'Clipping clean audio to fit noise audio duration'
                            )
                            clean = clean[:noise.duration_seconds]

                        logging.info('Overlaying noise and clean audios')
                        dirty = clean.overlay(noise)
                        clean_samples = np.array(clean.get_array_of_samples(),
                                                 dtype=np.float32)
                        clean_sampling_rate = clean.frame_rate
                        dirty_samples = np.array(dirty.get_array_of_samples(),
                                                 dtype=np.float32)
                        dirty_sampling_rate = dirty.frame_rate
                        logging.info('Processing data')
                        dirty_freq, dirty_time, dirty_db, dirty_phase = self.__prepateInput(
                            dirty_samples, dirty_sampling_rate)
                        clean_freq, clean_time, clean_db, clean_phase = self.__prepateInput(
                            clean_samples, clean_sampling_rate)
                        logging.info('Storing data')
                        self.__store_h5_data(main_group, file_combination,
                                             clean_info[0], noise_info[0],
                                             clean_freq, clean_time, clean_db,
                                             clean_phase, dirty_freq,
                                             dirty_time, dirty_db, dirty_phase)
                    except ResamplingError as e:
                        logging.warning(str(e), exc_info=True)

        except Exception as e:
            logging.error(str(e), exc_info=True)
            raise

    def __resample(self, input_signal, input_sampling_rate):
        if input_sampling_rate % self.__INPUT_SAMPLING_RATE:
            raise ResamplingError(
                'Downsampling factor is not integer number\n'
                '\tInput sampling rate: %d\n' % input_sampling_rate +
                '\tTarget sampling rate: %d\n' % self.__INPUT_SAMPLING_RATE)
        factor = input_sampling_rate / self.__INPUT_SAMPLING_RATE
        logger.info(
            'Input sampling rate is different from the expected by the model.\n'
            + '\rInput sampling rate: ' + str(input_sampling_rate) + '\n' +
            '\rModel sampling rate: ' + str(self.__INPUT_SAMPLING_RATE) +
            '\n' + 'Resampling input signal by factor: ' + str(factor))
        in_signal = decimate(input_signal, int(factor))
        return in_signal

    def __prepateInput(self, input_signal, sampling_rate):
        if sampling_rate != self.__INPUT_SAMPLING_RATE:
            input_signal = self.__resample(input_signal, sampling_rate)
        freq, time, stft = spectrogram(
            input_signal,
            fs=self.__INPUT_SAMPLING_RATE,
            window=get_window(self.__WINDOW, self.__N_SAMPLES_WINDOW),
            # nperseg=None,
            noverlap=self.__N_SAMPLES_OVERLAP,
            nfft=self.__N_SAMPLES_WINDOW,
            # detrend='constant',
            return_onesided=True,
            scaling='spectrum',
            axis=-1,
            mode='complex')
        db_values = amplitude_to_db(np.abs(stft))
        db_values = np.transpose(db_values)[:, np.newaxis, :]
        phase = np.angle(stft)
        return [freq, time, db_values, phase]

    def __store_h5_data(self, main_group, file_combination, clean_info,
                        noise_info, clean_freq, clean_time, clean_db,
                        clean_phase, dirty_freq, dirty_time, dirty_db,
                        dirty_phase):
        combination_group = main_group.create_group(
            np.string_('COMBINATION@ID_%d' % file_combination[0]))
        combination_group.attrs.create(np.string_('COMBINATION@ID'),
                                       np.int32(file_combination[0]))
        combination_group.attrs.create(np.string_('COMBINATION@SAMPLE_RATE'),
                                       np.float64(self.__INPUT_SAMPLING_RATE))
        combination_group.attrs.create(np.string_('CLEAN@ID'),
                                       np.int32(clean_info[0]))
        combination_group.attrs.create(np.string_('CLEAN@BOOK_DUMMY_NAME'),
                                       np.string_(clean_info[1]))
        combination_group.attrs.create(np.string_('CLEAN@BOOK_NAME'),
                                       clean_info[2])
        combination_group.attrs.create(np.string_('CLEAN@BOOK_AUTHOR'),
                                       clean_info[3])
        combination_group.attrs.create(np.string_('CLEAN@BOOK_URL'),
                                       np.string_(clean_info[4]))
        combination_group.attrs.create(np.string_('CLEAN@BOOK_LANGUAGE'),
                                       clean_info[5])
        combination_group.attrs.create(np.string_('CLEAN@BOOK_N_TRACK'),
                                       np.int32(clean_info[7]))
        combination_group.attrs.create(np.string_('CLEAN@TRACK_NAME'),
                                       np.string_(clean_info[8]))
        combination_group.attrs.create(np.string_('CLEAN@TRACK_SAMPLE_RATE'),
                                       np.float64(clean_info[11]))
        combination_group.attrs.create(np.string_('NOISE@ID'),
                                       np.int32(noise_info[0]))
        combination_group.attrs.create(np.string_('NOISE@NAME'), noise_info[1])
        combination_group.attrs.create(np.string_('NOISE@URL'),
                                       np.string_(noise_info[2]))
        combination_group.attrs.create(np.string_('NOISE@ORIGINAL_N_CHANNEL'),
                                       np.int8(noise_info[4]))
        combination_group.attrs.create(
            np.string_('NOISE@ORIGINAL_SAMPLE_RATE'),
            np.float64(noise_info[5]))
        clean_group = combination_group.create_group(r'CLEAN')
        clean_group.create_dataset('FREQ', data=clean_freq)
        clean_group.create_dataset('TIME', data=clean_time)
        clean_group.create_dataset('DB', data=clean_db)
        clean_group.create_dataset('PHASE', data=clean_phase)
        clean_group.attrs.create(np.string_('FFT@SIZE'),
                                 np.int32(self.__N_SAMPLES_WINDOW))
        clean_group.attrs.create(np.string_('FFT@N_SAMPLES_OVERLAP'),
                                 np.int32(self.__N_SAMPLES_OVERLAP))
        clean_group.attrs.create(np.string_('FFT@WINDOW'),
                                 np.string_(self.__WINDOW))
        dirty_group = combination_group.create_group(r'DIRTY')
        dirty_group.create_dataset('FREQ', data=dirty_freq)
        dirty_group.create_dataset('TIME', data=dirty_time)
        dirty_group.create_dataset('DB', data=dirty_db)
        dirty_group.create_dataset('PHASE', data=dirty_phase)
        dirty_group.attrs.create(np.string_('FFT@SIZE'),
                                 np.int32(self.__N_SAMPLES_WINDOW))
        dirty_group.attrs.create(np.string_('FFT@N_SAMPLES_OVERLAP'),
                                 np.int32(self.__N_SAMPLES_OVERLAP))
        dirty_group.attrs.create(np.string_('FFT@WINDOW'),
                                 np.string_(self.__WINDOW))

    @staticmethod
    def load_audio(path, normalized=True):
        ext = os.path.splitext(path)[1][1:]
        logging.info('Loading audio ' + path + ' with file type ' + ext)
        rawSound = AudioSegment.from_file(path, ext)
        if rawSound.channels != 1:
            logging.info(
                'Audio contains more than one channel. Setting to single channel'
            )
            rawSound = rawSound.set_channels(1)
        if normalized:
            logging.info('Normalize audio')
            return effects.normalize(rawSound)
        else:
            return rawSound
Exemplo n.º 4
0
 def __init__(self, db=DBManager(), driver=None):
     self.db = db
     self.__languages = ['spanish']
     self.__librivoxScraper = LibrivoxScraper(driver)
     self.__books = {}
Exemplo n.º 5
0
class Controller:
    db = DBManager()
    an = Analyzer()

    # add new merch info database
    def add_new_merch(self, new_merch_info):
        return self.db.add_new_merchandise(new_merch_info)

    # add new tour date to database
    def add_tour_date(self, tour_date_info):
        return self.db.add_new_tour_date(tour_date_info)

    # start db
    def start_db_manager(self):
        self.db = DBManager()
        self.db.drop_database()
        self.db.startup_database()
        self.db.add_test_data()

        # self.db.show_all()
        # self.db.table_check()

    # This gets the information from the database and puts it into the form that will be needed by the merchandise GUI.
    def get_merch_info_for_merch_window(self):
        list_of_merch_tuples = self.db.get_table_data("merchandise")
        list_of_merch_list = []

        # This puts the items into a list, organized by the way they'll be needed for the merchandise_screen.
        for item in list_of_merch_tuples:
            item_list=[]
            item_list.append(item[0])
            item_list.append(item[1])
            item_list.append(item[3])
            item_list.append(item[4])
            item_list.append(item[2])
            item_list.append(item[5])
            list_of_merch_list.append(item_list)

        return list_of_merch_list

    # display sales info in SalesPage (not implemented)
    def get_sales_info_for_sales_window(self):
        list_of_sales_tuples = self.db.get_table_data("sales")
        list_of_sales_list = []

        # This puts the items into a list, organized by the way they'll be needed for the sales_screen.
        for item in list_of_sales_tuples:
            item_list=[]
            item_list.append(item[0])
            item_list.append(item[1])
            item_list.append(item[2])
            item_list.append(item[4])
            item_list.append(item[3])
            list_of_sales_list.append(item_list)

        return list_of_sales_list

    # display tour info in SchedulePage
    def get_tour_info_for_tour_window(self):
        list_of_tour_tuples = self.db.get_table_data("tour_schedule")
        list_of_tour_list = []

        # This puts the items into a list, organized by the way they'll be needed for the tour_screen.
        for item in list_of_tour_tuples:
            item_list=[]
            item_list.append(item[0])
            item_list.append(item[7])
            item_list.append(item[6])
            item_list.append(item[5])
            item_list.append(item[1])
            item_list.append(item[2])
            item_list.append(item[3])
            item_list.append(item[4])
            item_list.append(item[8])
            item_list.append(item[10])
            item_list.append(item[9])
            list_of_tour_list.append(item_list)

        return list_of_tour_list

    # show all db listings (for testing)
    def show_all(self):
        self.db.show_all()

    # call best_units_total
    def show_best_selling_units(self):
        best_selling = self.db.show_best_units_sold()
        self.an.best_units_total(best_selling)

    # call best_units_gross
    def show_best_gross_units(self):
        best_gross = self.db.show_best_units_sold_gross()
        self.an.best_units_gross(best_gross)

    # call best_units_net
    def show_best_net(self):
        best_net = self.db.show_best_units_sold_net()
        self.an.best_units_net(best_net)

    # close db
    def close_database(self):
        return self.db.close_database()
Exemplo n.º 6
0
 def start_db_manager(self):
     self.db = DBManager()
     self.db.drop_database()
     self.db.startup_database()
     self.db.add_test_data()