Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

playing audio generate noise... #773

Closed
MenGuangwen0411 opened this issue Apr 6, 2021 · 0 comments
Closed

playing audio generate noise... #773

MenGuangwen0411 opened this issue Apr 6, 2021 · 0 comments

Comments

@MenGuangwen0411
Copy link

MenGuangwen0411 commented Apr 6, 2021

I use pyav on win10 as:

from future import print_function
from PyQt5.QtMultimedia import *
import time
import av

container = av.open(r'd:\king.mp4')
stream = next(s for s in container.streams if s.type == 'audio')

fifo = av.AudioFifo()
resampler = av.AudioResampler(
format=av.AudioFormat('s16').packed,
layout='stereo',
rate=48000,
)

qformat = QAudioFormat()
qformat.setByteOrder(QAudioFormat.LittleEndian)
qformat.setChannelCount(2)
qformat.setCodec('audio/pcm')
qformat.setSampleRate(48000)
qformat.setSampleSize(16)
qformat.setSampleType(QAudioFormat.SignedInt)

output = QAudioOutput(qformat)
output.setBufferSize(16 * 48000)

device = output.start()

print(qformat, output, device)

def decode_iter():
try:
for pi, packet in enumerate(container.demux(stream)):
for fi, frame in enumerate(packet.decode()):
yield pi, fi, frame
except:
return

for pi, fi, frame in decode_iter():

frame = resampler.resample(frame)
print(pi, fi, frame, output.state())

bytes_buffered = output.bufferSize() - output.bytesFree()
us_processed = output.processedUSecs()
us_buffered = 1000000 * bytes_buffered / (2 * 16 / 8) / 48000
print('pts: %.3f, played: %.3f, buffered: %.3f' % (frame.time or 0, us_processed / 1000000.0, us_buffered / 1000000.0))


data = frame.planes[0].to_bytes()
while data:
    written = device.write(data)
    if written:
        # print 'wrote', written
        data = data[written:]
    else:
        # print 'did not accept data; sleeping'
        time.sleep(0.033)

if False and pi % 100 == 0:
    output.reset()
    print(output.state(), output.error())
    device = output.start()

# time.sleep(0.05)

while output.state() == QAudio.ActiveState:
time.sleep(0.1)

It works but the sound is very noisy, although hearable, how to deal with the noise?

@PyAV-Org PyAV-Org locked and limited conversation to collaborators Dec 31, 2021
@jlaine jlaine converted this issue into discussion #857 Dec 31, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant