Coder Social home page Coder Social logo

Playing a file with PyAudio about pyogg HOT 8 CLOSED

teampyogg avatar teampyogg commented on September 23, 2024
Playing a file with PyAudio

from pyogg.

Comments (8)

DieTapete avatar DieTapete commented on September 23, 2024 1

Right, sorry that was just that one file i used for testing.. i thought it was similar to some issue that i had beforehand when it tried to "play" the header of the wave file. So everything is fine. Closing this issue now.

from pyogg.

mattgwwalker avatar mattgwwalker commented on September 23, 2024

from pyogg.

DieTapete avatar DieTapete commented on September 23, 2024

Yes, I tried those examples and managed to succeed to play a sound file. However none of the examples actually use streaming to play a file..

from pyogg.

DieTapete avatar DieTapete commented on September 23, 2024

I managed to get it kind of working but it sounds weird (a bit too fast and like with a phaser effect):

import pyaudio
from pyogg import OpusFile, OpusFileStream

def play(filename: str):
    p = pyaudio.PyAudio()
    stream = OpusFileStream(filename)
    out_stream = p.open(format=p.get_format_from_width(width=2),
                         channels=stream.channels,
                         rate=stream.frequency,
                         output=True,
                         output_device_index=1)
    
    print("stream.bytes_per_sample {} channels: {}, frequency:{}".format(stream.bytes_per_sample, stream.channels, stream.frequency))
    
    while True:
        # Read the next part of the stream
        buf = stream.get_buffer_as_array()        
        # Check if we've reached the end of the stream
        if buf is None:
            break
        out_stream.write(buf)
        
    out_stream.stop_stream()
    out_stream.close()
    p.terminate()

from pyogg.

Zuzu-Typ avatar Zuzu-Typ commented on September 23, 2024

Hey there,

well, you've basically already found the solution yourself.
The only thing you need to change is how you pass on the buffer.

out_stream.write() seems to be expecting 8-bit unsigned integer values (i.e. bytes) by default and thus, when you pass the audio data array containing 16-bit signed integers, it tries to interpret those as 8-bit, resulting in double speed and clipping.

There's two ways of changing this. Either you use the get_buffer() function instead of the get_buffer_as_array() function (which is called internally anyways, so it's not like you'd lose anything), or you add another argument to the out_stream.write:

out_stream.write(buf, len(buf))

Both seem to work.

By the way, you can use stream.bytes_per_sample instead of the constant width of 2 in the get_format_from_width() function.

Cheers
--Zuzu_Typ--

from pyogg.

DieTapete avatar DieTapete commented on September 23, 2024

Thank you for that quick reply @Zuzu-Typ! I got it working now. :)

from pyogg.

DieTapete avatar DieTapete commented on September 23, 2024

BTW: I still get a cracking sound in the beginning of the file. I avoid it for now by just skipping the first buffer.. is that the right way to go?

from pyogg.

Zuzu-Typ avatar Zuzu-Typ commented on September 23, 2024

Thank you for that quick reply @Zuzu-Typ! I got it working now. :)

You're welcome (:

BTW: I still get a cracking sound in the beginning of the file. I avoid it for now by just skipping the first buffer.. is that the right way to go?

Hm.. I've tried two different files, but didn't get any cracking, at least I didn't notice any.
Could you please try using a different file to check if it's doing the same thing?
This is one of the two files I've tested: https://github.com/TeamPyOgg/PyOgg/blob/master/examples/left-right-demo-5s.opus

from pyogg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.