Exemple #1
0
    def play(self):
        # define stream chunk
        chunk = 1024
        # полное воспроизведение перевернутой версии песни 1ого игрока

        # open a wav format music
        f = wave.open("reversed.wav", "rb")
        # instantiate PyAudio
        p = pyaudio.PyAudio()
        # open stream
        stream = p.open(format=p.get_format_from_width(f.getsampwidth()),
                        channels=f.getnchannels(),
                        rate=f.getframerate(),
                        output=True)
        # read data
        data = f.readframes(chunk)

        # play stream
        while data:
            stream.write(data)
            data = f.readframes(chunk)

            # stop stream
        stream.stop_stream()
        stream.close()

        # close PyAudio
        p.terminate()
Exemple #2
0
    def proig_chasti(self):
        chunk = 1024
        # проигрывание одной из частей игрока 1
        # open a wav format music
        f = wave.open("chast" + self.chast + ".wav", "rb")
        # instantiate PyAudio
        p = pyaudio.PyAudio()
        # open stream
        stream = p.open(format=p.get_format_from_width(f.getsampwidth()),
                        channels=f.getnchannels(),
                        rate=f.getframerate(),
                        output=True)
        # read data
        data = f.readframes(chunk)

        # play stream
        while data:
            stream.write(data)
            data = f.readframes(chunk)

            # stop stream
        stream.stop_stream()
        stream.close()

        # close PyAudio
        p.terminate()
Exemple #3
0
    def play_end(self):
        chunk = 1024
        # воспроизводит результат записи второго игрока

        # open a wav format music
        f = wave.open("resultat.wav", "rb")
        # instantiate PyAudio
        p = pyaudio.PyAudio()
        # open stream
        stream = p.open(format=p.get_format_from_width(f.getsampwidth()),
                        channels=f.getnchannels(),
                        rate=f.getframerate(),
                        output=True)
        # read data
        data = f.readframes(chunk)

        # play stream
        while data:
            stream.write(data)
            data = f.readframes(chunk)

            # stop stream
        stream.stop_stream()
        stream.close()

        # close PyAudio
        p.terminate()
Exemple #4
0
    def zapic_chasti(self):
        # игрок 2 записывает одну из частей
        CHUNK = 1024
        FORMAT = pyaudio.paInt16
        CHANNELS = 2
        RATE = 44100
        RECORD_SECONDS = 5
        WAVE_OUTPUT_FILENAME = "output" + self.chast + ".wav"
        p = pyaudio.PyAudio()

        stream = p.open(format=FORMAT,
                        channels=CHANNELS,
                        rate=RATE,
                        input=True,
                        frames_per_buffer=CHUNK)

        print("* recording")

        frames = []

        for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
            data = stream.read(CHUNK)
            frames.append(data)

        print("* done recording")

        stream.stop_stream()
        stream.close()
        p.terminate()

        wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
        wf.setnchannels(CHANNELS)
        wf.setsampwidth(p.get_sample_size(FORMAT))
        wf.setframerate(RATE)
        wf.writeframes(b''.join(frames))
        wf.close()

        source = wave.open("output" + self.chast + ".wav", mode="rb")
        dest = wave.open("reversed" + self.chast + ".wav", mode="wb")

        dest.setparams(source.getparams())
        frames_count = source.getnframes()

        data = struct.unpack(">" + str(frames_count) + "i",
                             source.readframes(frames_count))
        newdata = data[::-1]

        newframes = struct.pack(">" + str(len(newdata)) + "i", *newdata)
        dest.writeframes(newframes)
        source.close()
        dest.close()