コード例 #1
31
ファイル: test.py プロジェクト: Natthaphong/pydub
 def test_make_chunks(self):
     seg = self.seg1
     chunks = make_chunks(seg, 100)
     seg2 = chunks[0]
     for chunk in chunks[1:]:
         seg2 += chunk
     self.assertEqual(len(seg), len(seg2))
コード例 #2
1
 def play_speech(self, speech_file):
     """
     It open the audio and make the "queue" of chunks from it
     """
     self._speaking_chunks = make_chunks(
         AudioSegment.from_wav(speech_file), 1000)
     self._is_speaking = True
コード例 #3
0
    def _play(self, start, length, loops):
        self.isplaying = True

        millisecondchunk = 50 / 1000.0

        stream = self.audio.open(format=self.audio.get_format_from_width(
            self.pydubfile.sample_width),
                                 channels=self.pydubfile.channels,
                                 rate=self.pydubfile.frame_rate,
                                 output=True)

        if self.volume == 0:
            playchunk = AudioSegment.silent(duration=length * 1000)
        else:
            playchunk = self.pydubfile[start * 1000.0:(start + length) *
                                       1000.0] - (60 - (60 *
                                                        (self.volume / 100.0)))

        if self.fade > 0:
            playchunk = playchunk.fade_in(self.fade)

        self.time = start
        for chunks in make_chunks(playchunk, millisecondchunk * 1000):
            self.time += millisecondchunk
            stream.write(chunks._data)
            if not self.isplaying:
                # Fade out
                if self.fade < 0:
                    playchunk = (
                        self.pydubfile[self.time * 1000:self.time * 1000 -
                                       self.fade] -
                        (60 - (60 *
                               (self.volume / 100.0)))).fade_out(-self.fade)
                    for chunks in make_chunks(playchunk,
                                              millisecondchunk * 1000):
                        self.time += millisecondchunk
                        stream.write(chunks._data)

                stream.close()
                self.isplaying = False
                return
            if self.time >= start + length:
                break
            if self.reset:
                self.reset = False
                self._play(0, length, loops)

        if not loops == 1:
            if loops == -1:
                self._play(0, length, -1)
            else:
                self._play(0, length, loops - 1)
        else:
            stream.close()
            self.isplaying = False
コード例 #4
0
    def get_channels(self):
        if self.sound.channels == 1:
            for chunks in make_chunks(self.sound,
                                      int(self.sound.frame_count())):
                left = np.array(chunks.get_array_of_samples())
                return left, left

        for chunks in make_chunks(self.sound, int(self.sound.frame_count())):
            samps = chunks.get_array_of_samples()
            left = np.array(samps[::2])  # left ch
            right = np.array(samps[1::2])
        return left, right
コード例 #5
0
 def test_make_chunks(self):
     seg = self.seg1
     chunks = make_chunks(seg, 100)
     seg2 = chunks[0]
     for chunk in chunks[1:]:
         seg2 += chunk
     self.assertEqual(len(seg), len(seg2))
コード例 #6
0
ファイル: kitt_effect.py プロジェクト: TapirLab/kitt-effect
def calculate_energy_level_of_chunks(record, FPS):
    """Calculates energy level of each cunk in specified 1/FPS seconds

    The mean energy of each chunk in the audio file should be calculated to visualize
    voice activity. However, if the calculated mean energy is directly used,
    visualization does not produce the desired output. Thus, the histogram of chunk
    energies is processed. This can be considered as a primitive histogram
    equalization technique.

    Args:
        record: Normalized and DC removed AudioSegment object
        FPS: Number of frames per second that created video has

    Returns:
        energy_of_chunks: Histogram equalized mean energy of each chunk
    """
    # Divide record into chunks
    chunks = make_chunks(record, 1000 / FPS)
    # Create an empty array of length chunks to store mean energy of each
    energy_of_chunks = np.zeros(len(chunks))
    # Calculate mean energy of each chunk
    for (chunk, i) in zip(chunks, range(len(chunks))):
        tmp = np.array(chunk.get_array_of_samples().tolist(), np.float64)
        energy_of_chunks[i] = np.mean(tmp**2)

    # A pritmitive way of histogram equalization to visulize voice activity
    # First normalize, then take square root and multiply by 10
    energy_of_chunks = np.sqrt(energy_of_chunks / energy_of_chunks.max()) * 10

    return energy_of_chunks
コード例 #7
0
ファイル: musicplayer.py プロジェクト: ModernMAK/PiPlayer_PY
    def playInternal(self, start, length):
        if (self.state == MusicPlayerState.Initialized
                or self.state == MusicPlayerState.Loading
                or self.state == MusicPlayerState.Playing):
            print(
                "Cannot play a song which has not loaded, been initialized, or is already playing"
            )
            return
        self.state = MusicPlayerState.Playing
        millisecondchunk = 50 / 1000.0

        stream = self.audio.open(format=self.audio.get_format_from_width(
            self.pydubFile.sample_width),
                                 channels=self.pydubFile.channels,
                                 rate=self.pydubFile.frame_rate,
                                 output=True)

        playchunk = self.pydubFile[start * 1000.0:(start + length) * 1000.0]
        for chunks in make_chunks(playchunk, millisecondchunk * 1000):
            chunkAltered = chunks - (60 - (60 * (self.volume / 100.0)))
            self.time += millisecondchunk
            self.isPlaying = True
            stream.write(chunkAltered._data)
            if (self.state != MusicPlayerState.Playing):
                break
            if self.time >= start + length:
                self.state = MusicPlayerState.Ended
                break
        self.isPlaying = False
        stream.close()
コード例 #8
0
 def play_music(self, music_file):
     """
     It open the audio and make the "queue" of chunks from it
     """
     self._music_chunks = make_chunks(
         AudioSegment.from_wav(music_file), 1000)
     self._is_music = True
コード例 #9
0
def audio_split(filename, isMP3, sourceDir,chunk_length_ms):

    """
    #Calculating the length of each chunk depepnding upon the input file
    with contextlib.closing(wave.open(sourceDir + filename,'r')) as f:
        frames = f.getnframes()
        rate = f.getframerate()
        duration = frames / float(rate)
    chunk_length_ms=duration/15
    chunk_length_ms=int(chunk_length_ms)*1000
    """

    if isMP3:
        myaudio = AudioSegment.from_wav(sourceDir +filename) 
    else:
        myaudio = AudioSegment.from_wav(sourceDir +filename) 

    chunks = make_chunks(myaudio, chunk_length_ms) 
    direflaskctory_name=os.path.splitext(filename)[0]
    
    #Iterating through chunks
    #Export all the individual chunks as wav files into the speaker's directory 
    for i, chunk in enumerate(chunks):
        chunk_name = os.path.splitext(filename)[0]+"_{0}.wav".format(i)
        print("exporting", chunk_name)
        chunk.export("./uploads/"+chunk_name, format="wav") 
コード例 #10
0
def custom_file(audio_name, test_size=0.2):
    logging.info('starting speechEmotionsAnalysis')
    #load emotions recognition model
    model = joblib.load('emotionsAnalysis\\resources\\emotions_model.pkl')
    logging.info('emotions_model loaded')

    myaudio = AudioSegment.from_file(audio_name, "wav")
    logging.info('wav audio loaded')

    chunk_length_ms = 3000
    chunks = make_chunks(myaudio, chunk_length_ms)  #Make chunks of three secs
    logging.info('chunks created')
    # creating a new folder in resources if not already exists
    current_path = os.getcwd()
    new_path = os.path.join(current_path,
                            "emotionsAnalysis\\resources\\wav_chunks")
    if (not os.path.isdir(new_path)):
        os.mkdir(new_path)

    y = []
    #Export all of the individual chunks as wav files
    for i, chunk in enumerate(chunks):
        file = "emotionsAnalysis\\resources\\wav_chunks\\chunk{0}.wav".format(
            i)
        logging.info("exporting" + file)
        chunk.export(file, format="wav")
        feature = extract_feature(file, mfcc=True, chroma=True, mel=True)
        y.append(feature)
        os.remove(file)
    logging.info('feature extraction done')
    y_pred = model.predict(y)
    logging.info('prediction done')
    plot_pie_chart(y_pred)
コード例 #11
0
    def splice_single_audio(self,
                            data_dir,
                            results_dir,
                            filename,
                            chunk_length_ms=3000):
        """ Splices single audio into segments of specific length or less

        :param data_dir: data directory
        :param results_dir: results directory
        :param filename: name of file to be spliced
        :param chunk_length_ms: length of audio chunk in milliseconds
        :return:
        """

        file_path = data_dir + "/" + filename
        print(file_path)

        # Create new file if not existent
        file_path_new = results_dir + "/"
        print(file_path_new)
        if not os.path.exists(file_path_new):
            os.makedirs(file_path_new)

        # Make chunks of length chunk_length_ms
        audio = AudioSegment.from_file(file_path)
        chunks = make_chunks(audio, chunk_length_ms)

        # Export all of the individual chunks as wav files
        for i, chunk in enumerate(chunks):
            chunk_name = file_path_new + filename.split(
                self.extension)[0] + "_chunk{0}".format(i) + self.extension
            print "exporting", chunk_name
            chunk.export(chunk_name, format=self.extension.split(".")[1])
コード例 #12
0
ファイル: play.py プロジェクト: whale8/pytap
 def volume(self, decibel):  # 音割れする,別の手法にすべきか
     self.pause()  # underrun 防止
     self.__set_segment()
     self.seg += decibel
     self.chunks = make_chunks(self.seg, self.chunk_ms)
     self.__set_stream()
     self.play()
コード例 #13
0
ファイル: play.py プロジェクト: whale8/pytap
    def __play_song(self):
        self.__set_segment()
        self.chunks = make_chunks(self.seg, self.chunk_ms)
        self.chunk_count = 0
        self.__set_stream()
        self.duration = self.seg.duration_seconds  # 再生時間
        self.rate = self.seg.frame_rate  # サンプリングレート
        self.channels = self.seg.channels  # (1:mono, 2:stereo)
        self.sample_width = self.seg.sample_width  # byte (1:8bit, 2:16bit ...)
        self.max_db = self.seg.max_dBFS

        while not self.is_stoped:
            if self.chunk_count >= len(self.chunks):  # 最後まで再生して終了
                self.play_count += 1  # next song
                break

            with self.pause_condition:
                chunk = self.chunks[self.chunk_count]
                data = chunk._data
                self.chunk_count += 1
                self.db = chunk.dBFS
                self.progress = self.chunk_count / len(self.chunks)
                self.stream.write(data)

                while self.is_paused:
                    self.stream.stop_stream()
                    # ALSA lib pcm.c:8526:(snd_pcm_recover) underrun occurred
                    self.pause_condition.wait()
                    self.stream.start_stream()  # resume

        self.stream.close()  # terminate the stream
コード例 #14
0
def Slices(directory,home):
    #os.chdir(directory)
    print("Starting slicing.")
    for direct in os.listdir(directory):
        for music in os.listdir("{0}\{1}".format(directory,direct)):
            #process wav file
            name = music[:-4]
            os.chdir("{0}\{1}".format(directory,direct))

            myaudio = AudioSegment.from_file(music , "wav") 
            chunk_length_ms = 1000 # pydub calculates in millisec
            chunks = make_chunks(myaudio, chunk_length_ms) #Make chunks of one sec

            for i, chunk in enumerate(chunks):
                if i < 10:
                    chunk_name = name + ".0{0}.wav".format(i)
                else:
                    chunk_name = name + ".{0}.wav".format(i)

                print("exporting {0}".format(chunk_name))
                chunk.export(chunk_name, format="wav")
            os.system('del {0}'.format(music))

        
            os.chdir(home)
    
    print("Finished slicing.")
コード例 #15
0
    def convert_callback(self):
        if not self._file_url.get():
            self._status.set("please choose audio file")
        elif not self._Dist_url.get():
            self._status.set("please choose Destination directory")
        else:
            self._status.set("processing")
            self.freeze_controls()
            AUDIO_FILE = str(self._file_url.get())
            filename = "game_log_" + datetime.datetime.now().strftime(
                "%Y-%m-%d_%H-%M-%S") + ".pdf"
            destination = self._Dist_url.get()
            kss = Keyword_Spotting_Service()

            myaudio = AudioSegment.from_file(AUDIO_FILE, "wav")
            chunk_length_ms = 1000  # pydub calculates in millisec
            chunks = make_chunks(myaudio,
                                 chunk_length_ms)  # Make chunks of one sec
            # Export all of the individual chunks as wav files
            predictedSentence = ""
            for i, chunk in enumerate(chunks):
                chunk_name = "sample_AI\chunk{0}.wav".format(i)
                chunk.export(chunk_name, format="wav")
            for i, chunk in enumerate(chunks):

                predictedSentence += kss.predict("sample_AI\chunk" + str(i) +
                                                 ".wav") + " "
            self._pdf = pdfkit.from_string(
                "prediction is: " + predictedSentence,
                path.join(destination, filename))
            self._status.set("Log file generated!")
            self.unfreeze_controls()
コード例 #16
0
def start_splitting(chunk_length_ms, wav_file, path_to_write_chunks):
    """
    Starts audio splitting into chunks of length specified
    """

    # append a slash at the end of the path to save wav files
    # if it is not there already
    if not path_to_write_chunks.endswith('/'):
        path_to_write_chunks += '/'

    # If it doesn't exist, create directory for output file
    if not os.path.exists(path_to_write_chunks):
        os.makedirs(path_to_write_chunks)

    file_name = wav_file.split('/')[-1].split(".")[0]
    print "splitting audio files into chunks of", chunk_length_ms / 1000.0, "seconds :", file_name
    myaudio = AudioSegment.from_wav(wav_file)
    chunks = make_chunks(myaudio, chunk_length_ms)
    #Export all of the individual chunks as wav files
    for i, chunk in enumerate(chunks):
        chunk_name = file_name + "_" + str(i) + '.wav'

        # if the last chunk is not of length, then pad it with silence
        if len(chunk) < chunk_length_ms:
            print "Padding last file with", (
                chunk_length_ms - len(chunk)) / 1000.0, "seconds of silence"
            chunk = chunk + AudioSegment.silent(
                duration=chunk_length_ms - len(chunk),
                frame_rate=chunk.frame_rate)

        chunk.export(path_to_write_chunks + chunk_name, format="wav")
    return len(chunks)
コード例 #17
0
def process_vid(vid_filepath, output_filepath):
    command = "ffmpeg -y -i " + vid_filepath + " -ar 16000 -vn resources/output.wav"
    subprocess.call(command, shell=True)

    # Instantiates a client
    client = speech.SpeechClient()

    # The name of the audio file to transcribe
    file_name = os.path.join(os.path.dirname(__file__), 'resources',
                             'output.wav')

    myaudio = AudioSegment.from_file(file_name, "wav")
    chunk_length_ms = 30000  # pydub calculates in millisec
    chunks = make_chunks(myaudio, chunk_length_ms)  #Make chunks of one sec

    #Convert chunks to raw audio data which you can then feed to HTTP stream
    with open(output_filepath, 'w+') as outf:
        for i, chunk in enumerate(chunks):
            print("chunk " + str(i) + " of " + str(len(chunks) - 1))
            raw_audio_data = chunk.raw_data

            # Loads the audio into memory
            audio = types.RecognitionAudio(content=raw_audio_data)

            config = types.RecognitionConfig(
                encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
                sample_rate_hertz=16000,
                language_code='en-US')

            # Detects speech in the audio file
            response = client.recognize(config, audio)

            for result in response.results:
                outf.write(result.alternatives[0].transcript)
                outf.write(". ")
コード例 #18
0
def play(seg):
    import pyaudio
    from pydub.utils import make_chunks

    p = pyaudio.PyAudio()
    stream = p.open(format=p.get_format_from_width(seg.sample_width),
                    channels=seg.channels,
                    rate=seg.frame_rate,
                    output=True)

    try:
        # break audio into half-second chunks (to allows keyboard interrupts)
        i = 0
        for chunk in make_chunks(seg, 500):
            print("Chunking :", i, end="\r")
            stream.write(chunk._data)
            i += 1
    # except:
    #     # We catch ALL exceptions and interrupts and raise it
    #     print("interrupted")
    #     raise
    finally:
        print("CLEANING UP")
        stream.stop_stream()
        stream.close()

        p.terminate()
コード例 #19
0
ファイル: transcribe.py プロジェクト: floverfelt/podcasts-nlp
def wav_to_text(prefix, filename, counter):

    output_filename = prefix + '_txt/' + prefix + '_' + str(counter) + '.txt'

    if path.exists(output_filename):
        # Skip already completed files
        return

    print('Beginning ' + output_filename)
    output_txt = open(output_filename, "w+")

    audio = AudioSegment.from_wav(filename)
    # recognize_sphinx works best on smaller chunks
    chunks = make_chunks(audio, 30000)

    for chunk in chunks:
        chunk_filename = 'temp_chunked_audio_wav/chunk.wav'
        chunk.export(chunk_filename, format='wav')

        r = sr.Recognizer()
        with sr.AudioFile(chunk_filename) as source:
            audio = r.record(source)  # read the chunk
            rec = r.recognize_sphinx(audio)  # recognize the chunk as text
            output_txt.write(rec)
        os.remove(chunk_filename)

    output_txt.close()
コード例 #20
0
def splitMp3(names, mp3FilePath, size=1000):
    storeDir = "uploadr/static/voice/"
    chunks = make_chunks(AudioSegment.from_file(mp3FilePath, "mp3"),
                         size)  # 将文件切割
    for i, chunk in enumerate(chunks):
        if i < len(names):
            chunk.export(storeDir + names[i] + ".mp3", format="mp3")
コード例 #21
0
def play_music():

    # この関数は別スレッドで実行するため
    # メインスレッドで定義した以下の2つの変数を利用できるように global 宣言する
    global is_gui_running, audio_data, now_playing_sec

    # pydubのmake_chunksを用いて音楽ファイルのデータを切り出しながら読み込む

    size_frame_music = 1  # 10ミリ秒毎に読み込む

    idx = 0

    # make_chunks関数を使用して一定のフレーム毎に音楽ファイルを読み込む
    #
    # なぜ再生するだけのためにフレーム毎の処理をするのか?
    # 音楽ファイルに対しても何らかの処理を行えるようにするため(このサンプルプログラムでは行っていない)
    # おまけに,再生位置も計算することができる
    for chunk in make_chunks(audio_data, size_frame_music):

        # GUIが終了してれば,この関数の処理も終了する
        if is_gui_running == False:
            break

        # pyaudioの再生ストリームに切り出した音楽データを流し込む
        stream_play.write(chunk._data)
        # 現在の再生位置を計算(単位は秒)
        now_playing_sec = (idx * size_frame_music) / 1000.

        idx += 1
コード例 #22
0
def split_audio(labels=labels,
                chunk_length=2000,
                file=None,
                audio_path=audio_path):
    if not file:
        filename = song_path + "\\" + input(
            "Which file would you like to split: ") + ".wav"
    else:
        filename = file
        for name in labels:
            if fnmatch.fnmatchcase(filename, '*' + name + '*'):
                label = name
                break
        else:
            print("Label not Found. Please add label aptly. saved as other")
            label = "other"
        audio = AudioSegment.from_file(filename, "wav")
        chunks = make_chunks(audio, chunk_length)
        count = max(check_dir(label))
        print(count)
        for i, chunk in enumerate(chunks):
            chunk_name = label + "_{0}.wav".format(i + count)
            print("exporting", chunk_name)
            file_string = str(audio_path + "\\{0}\\" +
                              chunk_name).format(label)
            chunk.export(file_string, format="wav")
コード例 #23
0
ファイル: kss4.py プロジェクト: dupontinquiries/kss
    def chunkAudio(self, v):
        pvc = v.getFullVideo()
        # create tandem mp3 audio
        af = workD.append("footage").append("chunks").append(f"chunk_{randomString(7)}.mp3")
        audioclip = AudioClip(af.aPath())
        a = AudioSegment.from_mp3(nameAP)
        packets = make_chunks(a, chuLenMS)
        # make 5 minute segments to process simultaneously
        n = pvc.duration // 300
        subclips = list()
        for i in range(n):
            ts = n * 300
            tf = (n + 1) * 300
            tf = max(tf, pvc.duration)
            subclips.append(pvc.subclip(ts, tf))

        self.tmpChunks = list()
        self.tmpCounter = 0

        print(f"chunkAudio session {randomString(4)}")
        executor = concurrent.futures.ProcessPoolExecutor(61)
        futures = [executor.submit(self.appendChunks, subclips[i], i)
                   for i in range(len(subclips))]
        # run code in the meantime
        concurrent.futures.wait(futures)
        # order in case concurrent was out of order
        self.tmpChunks = sorted(self.tmpChunks, key=lambda element: (element[0], element[1]))
        for i in range(len(self.tmpChunks)):
            i1 = max(0, i - spreadCalc)
            i2 = min(len(self.tmpChunks), i + spreadCalc)
            self.tmpChunks[i].sv = sum(list(map(lambda x: x.volume, self.tmpChunks[i1:i2])) / max(1, i2 - i1))
        print(self.tmpChunks)
        os.remove(af.aPath())
        exit()
コード例 #24
0
ファイル: player.py プロジェクト: amiramitai/nomix_player
    def run(self):
        # Open an audio segment
        sound = AudioSegment.from_file(self.filepath)
        player = pyaudio.PyAudio()

        stream = player.open(format=player.get_format_from_width(
            sound.sample_width),
                             channels=sound.channels,
                             rate=sound.frame_rate,
                             output=True)

        # PLAYBACK LOOP
        start = 0
        length = sound.duration_seconds
        volume = 100.0
        playchunk = sound[start * 1000.0:(start + length) *
                          1000.0] - (60 - (60 * (volume / 100.0)))
        millisecondchunk = 50 / 1000.0

        while self.loop:
            self.time = start
            for chunks in make_chunks(playchunk, millisecondchunk * 1000):
                self.time += millisecondchunk
                stream.write(chunks._data)
                if not self.loop:
                    break
                if self.time >= start + length:
                    break

        stream.close()
        player.terminate()
コード例 #25
0
ファイル: preprocessing.py プロジェクト: GavinLiu-AI/PianoGAN
def make_audio_chunks(seconds, dest_dir):
    """
    Function used to convert audio into shorter audio clips, and save audio clips to files.

    :param seconds: desired clip length
    :param dest_dir: output directory
    """
    paths = prep_utils.get_absolute_file_paths(DATASET_DIR, ".wav")

    start_time = time.time()
    for audio_path in paths:
        prep_utils.display_progress_eta(current_item=audio_path,
                                        total_items=paths,
                                        start_time=start_time)

        audio = AudioSegment.from_file(audio_path)
        chunk_length_ms = seconds * 1000  # 20 seconds
        chunks = make_chunks(audio, chunk_length_ms)
        chunks.pop(-1)

        # Export all of the individual chunks as wav files
        for i, chunk in enumerate(chunks):
            _, chunk_name = os.path.split(
                os.path.splitext(audio_path)[0] + "_chunk_{0}.wav".format(i))
            chunk.export(dest_dir + chunk_name, format="wav")

    print("\n\nChunks export completed.")
コード例 #26
0
def split_file_to_short_wav(file_path, split_duration):
    """
    spits wav file to chunks of X seconds (3 currently)
    :param fileName: the file that being taken to examination
    :return: list of the chun
    """
    # todo DELETE OLD EXAMINED FILES?

    file_name = Path(file_path).stem  # return only name without extension
    # Load your audio.
    audio_file = AudioSegment.from_wav(file_path)
    # split into X seconds chunks
    chunk_seconds = split_duration
    chunk_length_ms = chunk_seconds * 1000  # pydub calculates in millisec
    audio_file_chunks = make_chunks(audio_file,
                                    chunk_length_ms)  # Make chunks of X sec
    # Export all of the individual chunks as wav files-taken from overflow
    # TODO future change to start and end times of chunck

    for i, chunk in enumerate(audio_file_chunks):
        chunk_sec_start = i * clfGlobals.split_by_sec
        chunk_name = f'sec_start_{chunk_sec_start}.wav'
        chunk_path = f'examined_files/{file_name}_{chunk_name}'
        print("exporting ", chunk_path)

        chunk.export(chunk_path, format="wav")
コード例 #27
0
 def play_music(self, music_file):
     """
     It open the audio and make the "queue" of chunks from it
     """
     self._music_chunks = make_chunks(AudioSegment.from_wav(music_file),
                                      1000)
     self._is_music = True
コード例 #28
0
 def play_speech(self, speech_file):
     """
     It open the audio and make the "queue" of chunks from it
     """
     self._speaking_chunks = make_chunks(AudioSegment.from_wav(speech_file),
                                         1000)
     self._is_speaking = True
コード例 #29
0
def chunck_wav(file_path, chunk_length_ms=1000):
    raw_audio = AudioSegment.from_file(file_path, "wav")
    audio = trim_silence(raw_audio)
    chunks = make_chunks(audio, chunk_length_ms)
    chunks[-1] = pad_chuck(chunks[-1], chunk_length_ms)
    assert (len(chunks[-1]) == chunk_length_ms)
    return chunks
コード例 #30
0
ファイル: player.py プロジェクト: mRokita/sMusic-core
 def __get_stream(self):
     self.__segment = AudioSegment.from_file(self.__path)
     self.__chunks = make_chunks(self.__segment, 100)
     return self.__pyaudio.open(format=self.__pyaudio.get_format_from_width(self.__segment.sample_width),
                                channels=self.__segment.channels,
                                rate=self.__segment.frame_rate,
                                output=True)
コード例 #31
0
def makechunks(path):
    count = 0
    chunk_lim = 3500
    folders = glob.glob(path + '*')
    for folder in folders:
        waves = glob.glob(folder + '/**/*.wav', recursive=True)
        print('w', waves)
        if len(waves) == 0:
            return 10
        for i in waves:
            count += 1
            w = i
            myaudio = AudioSegment.from_file(i)
            chunk_length_ms = 4000
            chunks = make_chunks(myaudio, chunk_length_ms)
            print(chunks)
            for i, chunk in enumerate(chunks):
                chunk_name = w.split('.')[0] + str(
                    count) + "chunk{0}.wav".format(i)
                print(chunk_name)
                print("exporting", chunk_name)
                if len(chunk) > chunk_lim:
                    print(len(chunk))
                    print(chunk_name)
                    chunk.export(folder + '/' + chunk_name, format="wav")
コード例 #32
0
ファイル: split.py プロジェクト: JanFschr/TTSDatasetCreator
def split_single(output, name, basefilename, sound_file, frame_rate, length):
    audio_data = sound_file.set_sample_width(2).set_channels(1).set_frame_rate(
        frame_rate)
    chunk_length_ms = length * 60 * 1000  # pydub calculates in millisec
    if chunk_length_ms == 0 or audio_data.duration_seconds * 1000 < chunk_length_ms:
        chunks = [audio_data]
    else:
        chunks = make_chunks(audio_data,
                             chunk_length_ms)  # Make chunks of length minutes

    output_dir = "{0}/{1}".format(output, frame_rate)

    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)

    if len(chunks) == 1:
        chunk = chunks[0]
        chunk_name = "{0}/{1}.wav".format(output_dir, basefilename)
        print(chunk_name)
        chunk.export(chunk_name, format="wav")
    else:
        for i, chunk in enumerate(chunks):
            chunk_name = "{0}/{1}_part{2:0>3d}.wav".format(
                output_dir, basefilename, i)
            print(chunk_name)
            chunk.export(chunk_name, format="wav")
コード例 #33
0
def upload2():

    cantidad = request.form['cantidad']
    #fichero=request.form['archivo'];

    audio_data = audio2.query.all()
    for audio in audio_data:
        music = audio.nombre

    fichero = target + music

    print(fichero)

    dst = fichero.replace(".mp3", ".wav")

    sound = AudioSegment.from_file(dst)

    chunk_length_ms = (int(255) * 1000) / int(cantidad)
    chunks = make_chunks(sound, (chunk_length_ms))

    music2 = music.replace(".mp3", "")

    for i, chunk in enumerate(chunks):
        if (i != int(cantidad)):
            chunk_name = (music2 + "{0}.wav").format(i)
            print("exporting", chunk_name)
            chunk.export(chunk_name, format="wav")
        else:
            break

    return render_template("upload.html")
コード例 #34
0
ファイル: transcriber.py プロジェクト: bgramming/transcriber
def cut_and_send(infile, outfile, length):
    # print(infile)
    # print(outfile)
    # print(length)
    # return
    myaudio = AudioSegment.from_file(infile, "wav")
    chunk_length_ms = length  # pydub calculates in millisec
    chunks = make_chunks(myaudio, chunk_length_ms)  # Make chunks of one sec

    for i, chunk in enumerate(chunks):
        chunk_name = "chunk{0}.wav".format(i)
        print("exporting", chunk_name)
        chunk.export(chunk_name, format="wav")
        r = sr.Recognizer()
        with sr.AudioFile(chunk_name) as source:
            audio = r.record(source)
        # recognize speech using Google Speech Recognition
        try:
            # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
            # instead of `r.recognize_google(audio)`
            txt = r.recognize_google(audio) + " "
            with open(outfile, 'a') as f:
                f.write(txt)
        except sr.UnknownValueError:
            print("Ehm... sorry not understood this one.")
        except sr.RequestError as e:
            print("Request failed; {0}".format(e))
        os.remove(chunk_name)
コード例 #35
0
ファイル: test3.py プロジェクト: emwav3/speech-splicer
def main():
    # Load audio file and print the length
    audio = AudioSegment.from_wav("C:\MB Track 32.wav")
    print("audio length:\t\t" + str(audio.__len__() / 1000) + "seconds")

    # Strip off the first few seconds and create chunks of audio
    # audio = audio[4000:]


    chuck_start_end_list = []
    running = True
    average_loudness = audio.rms
    # silence_threshold = average_loudness * db_to_float(-21)
    chunks = make_chunks(audio, splicing_resolution)
    print("chunk list length:\t" + str(len(chunks)))

    build = audio[:0]
    i = 0
    start = 0
    stop = 0
    while i < (len(chunks)):
        start_stop = [0, 0]
        if chunks[i].rms <= silence_threshold:
            while i < (len(chunks)) and chunks[i].rms <= silence_threshold:
                print("low" + str(i))
                print(chunks[i].rms)
                i += 1
            start = i
        else:
            while i < (len(chunks)) and chunks[i].rms > silence_threshold:
                print("high" + str(i))
                print(chunks[i].rms)
                i += 1
        stop = i
        # print("pass")
        if ( start != stop):
            chuck_start_end_list.append([start, stop])
    # build = chunk_list[i]
    #     build.export(("test/test{0}.mp3".format(str(i))), format="mp3")
    # build.export("test2.mp3", format="mp3")


    print((chuck_start_end_list))
    print("Chuck start stop list length: " + str(len(chuck_start_end_list)))
    print("silence threshold:\t" + str(silence_threshold))

    # For testing only -- prints segment sizes, showing larger segment
    large_chunks = 0
    for chunk in chuck_start_end_list:
        size = (chunk[1] - chunk[0])

        if size > 32:
            print("----------" + str(size))
            large_chunks += 1
        else:
            print(size)
    print(large_chunks)
コード例 #36
0
ファイル: test.py プロジェクト: emwav3/speech-splicer
def chunks():
    audio = AudioSegment.from_mp3("track2.mp3")
    audio = audio
    chunks = make_chunks(audio, 200)
    empty = audio[:0]
    for chunk in chunks:
        if chunk.rms > 430:
            empty += chunk
    empty.export("test.mp3", format="mp3")
コード例 #37
0
    def _play(self, start, length):
        self.isplaying = True
        if AudioSegment:
            millisecondchunk = 50 / 1000.0
            playchunk = self.pydubfile[start*1000.0:(start+length)*1000.0] - (60 - (60 * (self.volume/100.0)))
            self.time = start
            self.audio.play(playchunk.get_array_of_samples(), blocking=False)

            # For some reason it does not like the seperated chunks, so we play it non-
            # We might be able to use self.audio.get_stream().time to improve accuracy
            for chunks in make_chunks(playchunk, millisecondchunk*1000):
                self.time += millisecondchunk

                time.sleep(millisecondchunk)
                if not self.isplaying:
                    break
                if self.time >= start+length:
                    break
        else:
            startframe = int(round(start * self.wave_reference.getframerate()))
            samplelen = int(round(length * self.wave_reference.getframerate()))
            remaining = samplelen
            chunk = 1024
            try:
                self.wave_reference.setpos(startframe)
            except wave.Error:
                self.isplaying = False
                return

            if remaining >= 1024:
                data = audioop.mul(self.wave_reference.readframes(chunk),self.wave_reference.getsampwidth(), self.volume/100.0)
                remaining -= chunk
            else:
                data = audioop.mul(self.wave_reference.readframes(remaining),self.wave_reference.getsampwidth(), self.volume/100.0)
                remaining = 0

            # play stream
            self.audio.play(data.get_array_of_samples(), blocking=False)

            while len(data) > 0 and self.isplaying:

                time.sleep(float(self.wave_reference.getframerate()))
                self.time = float(self.wave_reference.tell()) / float(self.wave_reference.getframerate())
                if remaining >= 1024:
                    data = audioop.mul(self.wave_reference.readframes(chunk),self.wave_reference.getsampwidth(), self.volume/100.0)
                    remaining -= chunk
                else:
                    data = audioop.mul(self.wave_reference.readframes(remaining),self.wave_reference.getsampwidth(), self.volume/100.0)
                    remaining = 0

        self.audio.stop()
        self.isplaying = False
コード例 #38
0
ファイル: audio.py プロジェクト: modrzew/ekoie
def mix_segments(segments, slice_length=500):
    """Mixes two tracks together

    Given two tracks 1 and 2, output becomes something like this:
    1 2 1 2 1 2 1 2...
    """
    segments_count = len(segments)
    # Cut to the shortest segment
    shortest_length = min(len(segment) for segment in segments)
    segments = [segment[:shortest_length] for segment in segments]
    slices = [make_chunks(segment, slice_length) for segment in segments]
    first = slices[0][0]
    for i, s in enumerate(slices[0][1:], start=1):
        first += slices[i % segments_count][i]
    return first
コード例 #39
0
ファイル: audio.py プロジェクト: modrzew/ekoie
def volume_changer(segment, slice_length=250):
    """Changes volume of the track on set interval

    The track becomes something like this:
    H L H L H L H L...
    where H means high volume, and L stands for low (reduced) volume.
    """
    # Split segment into equally sized slices
    slices = make_chunks(segment, slice_length)
    result = slices[0]
    for i, s in enumerate(slices[1:]):
        if i % 2 == 0:
            s -= 15
        result += s
    return result
コード例 #40
0
ファイル: audio.py プロジェクト: modrzew/ekoie
    def run(self):
        player = pyaudio.PyAudio()
        stream = player.open(
            format=player.get_format_from_width(self.segment.sample_width),
            channels=self.segment.channels,
            rate=self.segment.frame_rate,
            output=True,
        )

        # break audio into quarter-second chunks (to allows interrupts)
        for i, chunk in enumerate(make_chunks(self.segment, 250)):
            if self._notifier:
                self._notifier(i*250)
            if not self._playing:
                break
            stream.write(chunk._data)

        stream.stop_stream()
        stream.close()
        player.terminate()
コード例 #41
0
def main():
    x = pause_threshold
    audio = AudioSegment.from_wav("MB Track 3.wav")
    audio = audio[4000:]
    chunks = make_chunks(audio, splicing_resolution)
    build = audio[:0]
    # silence_threshold = audio.rms * db_to_float(-22.5)
    print(silence_threshold)
    resets = 0
    chunk_list = []
    for i in range(len(chunks)):

        if chunks[i].rms < silence_threshold:
            if (0 > x):
                x = pause_threshold
                print("reset x")
                resets += 1
                chunk_list.append(build)
                build = audio[:0]
            else:

                print("decrement x")
            x -= 1

        else:

            build += chunks[i]
    build = audio[:0]
    print(len(chunk_list))
    for i in range(1, len(chunk_list)):
        print("ex")
        build = chunk_list[i]
        build.export(("test/test{0}.mp3".format(str(i))), format="mp3")
    print("resets: " + str(resets))
    build.export("test2.mp3", format="mp3")
    print("silence threshold: " + str(silence_threshold))
コード例 #42
0
import argparse
import os
from pydub import AudioSegment
from pydub.utils import make_chunks

parser = argparse.ArgumentParser(description='A script for splitting audio files into segements')
parser.add_argument('-p', '--path', help='The path to the folder to process', required=True)
# An example would be: --path '/Users/danielpett/githubProjects/britishMuseumSoundCloud/originals/unprocessed/'
parser.add_argument('-d', '--destination', help='The destination folder', required=True)
# An example would be: --path '/Users/danielpett/githubProjects/britishMuseumSoundCloud/chunked/'
parser.add_argument('-l', '--length', help='Length of chunk', required=True)
# An example would be --length 10000

# Parse arguments
args = parser.parse_args()
path = args.path

# Loop through the files
for file in os.listdir(path):
    print('Now processing: ' + file)
    myaudio = AudioSegment.from_file(os.path.join(path,file), "mp3")
    # Make chunks of length specified
    chunk_length_ms = args.length
    chunks = make_chunks(myaudio, int(args.length))
    for i, chunk in enumerate(chunks):
        processedFileName = os.path.splitext(file)[0]
        chunk_name = args.destination + processedFileName + "_Chunk{0}.mp3".format(i)
        print "Now exporting: " , chunk_name
        chunk.export(chunk_name, format="mp3")
コード例 #43
0
print "sample_width=", sample_width 
print "channel_count=", channel_count
print "duration_in_sec=", duration_in_sec 
print "frame_rate=", sample_rate
bit_rate =16  #assumption , you can extract from mediainfo("test.wav") dynamically


wav_file_size = (sample_rate * bit_rate * channel_count * duration_in_sec) / 8
print "wav_file_size = ",wav_file_size


file_split_size = 10000000  # 10Mb OR 10, 000, 000 bytes
total_chunks =  wav_file_size // file_split_size

#Get chunk size by following method #There are more than one ofcourse
#for  duration_in_sec (X) --&gt;  wav_file_size (Y)
#So   whats duration in sec  (K) --&gt; for file size of 10Mb
#  K = X * 10Mb / Y

chunk_length_in_sec = math.ceil((duration_in_sec * 10000000 ) /wav_file_size)   #in sec
chunk_length_ms = chunk_length_in_sec * 1000
chunks = make_chunks(myaudio, chunk_length_ms)

#Export all of the individual chunks as wav files

for i, chunk in enumerate(chunks):
    chunk_name = "chunk{0}.mp3".format(i)
    print "exporting", chunk_name
    chunk.export(chunk_name, format="mp3")
コード例 #44
0
ファイル: coto.py プロジェクト: KatarzynaStudzinska/ADlipiec
from pydub import AudioSegment
from pydub.utils import make_chunks

myaudio = AudioSegment.from_file("artofwar.wav" , "wav")
chunk_length_ms = 100000 # pydub calculates in millisec
chunks = make_chunks(myaudio, chunk_length_ms) #Make chunks of one sec

#Export all of the individual chunks as wav files
licz = 0
for i, chunk in enumerate(chunks):
    chunk_name = "art{0}.wav".format(i)
    print "exporting", chunk_name
    chunk.export(chunk_name, format="wav")
    licz +=2
    if licz > 0:
        break