Coder Social home page Coder Social logo

Comments (8)

mgeier avatar mgeier commented on July 20, 2024

Wow, this is really strange!

I can reproduce the error on Linux using Python 2.
With Python 3, however, it works. Except the Python interpreter crashes at the end of the script, see #1.

When using play() in a script, you should make sure that the script doesn't end before the playback is finished (with blocking=True or stop() or wait()).

I have no clue what causes that problem, any ideas or further clues would be highly appreciated!

from python-sounddevice.

piotx avatar piotx commented on July 20, 2024

same problem on windows 7 with python 2.7

from python-sounddevice.

piotx avatar piotx commented on July 20, 2024

if the sound is played from within a class, it works (it doesn't make any sense to me, however..; python2.7, debian-unstable)

minimal example, which works:

main.py:

import module2
a = module2.TEST()
a.play()

module2.py:

import module1
snd = module1.Sound('startle50ms.wav')
#snd.play() wouldn't work but, when played from within a class, it's ok..

class Test(object):
    def __init__(self):
       self.x = None
    def play(self):
        snd.play()

module1.py:

import time

class Sound(object):
    def __init__(self, filename):
        self._sd = __import__('sounddevice')
        self._sf = __import__('soundfile')
        self.data, self.fs = self._sf.read(filename)
    def play(self):
        self._sd.play(self.data, self.fs)
        print 'sound played'
        time.sleep(1)
        #self._sd.wait()

this doesn't work:

main2.py:

import module3

module3.py:

import time

class Sound(object):
    def __init__(self, filename):
        self._sd = __import__('sounddevice')
        self._sf = __import__('soundfile')
        self.data, self.fs = self._sf.read(filename)
    def play(self):
        self._sd.play(self.data, self.fs)
        print 'sound played'
        time.sleep(1)
        #self._sd.wait()

snd = Sound('startle50ms.wav')
snd.play()

from python-sounddevice.

piotx avatar piotx commented on July 20, 2024

a more simple (working) example:

main.py:

import modulex
a = modulex.Test()
a.play()

modulex.py

import sounddevice as sd
import soundfile as sf
data, fs = sf.read('startle50ms.wav')
class Test(object):
    def play(self):
        sd.play(data, fs, blocking=True)

from python-sounddevice.

mgeier avatar mgeier commented on July 20, 2024

Still very strange, still no clue ...

It seems that playback doesn't work (on Python 2) during the import process. Once the import is done, it works again.

I don't know what's the reason for this, but you shouldn't have done that in the first place.
You should define a function in your module and then call it after (not during) importing the module.

Independent of the current issue, you would get a problem if you want to run the code in the module twice, since the module object is cached and it's content is only executed on the first import. On further imports, nothing will happen.

from python-sounddevice.

Jee-Bee avatar Jee-Bee commented on July 20, 2024

Probably related to this error. I have the same problem but with the playrec() function.
The weird thing is that i get in two situations two different results.

module.py:

import numpy as np

def simplefiedPlayRec(data, samplerate=None, input_channels=None, output_channels=None,
               repeats=None):
    try:
        import sounddevice as sd
        data = np.tile(data, repeats)
        data = np.repeat(data, output_channels).reshape((len(data), output_channels))
        print(np.shape(data))
        recarray = sd.playrec(data, samplerate)
        sd.wait()
        recarray = np.sum(np.array_split(recarray, repeats, axis=0))
        recarray /= repeats
        return(recarray)
    except ImportError:
        raise("No module named 'sounddevice'. ")

and main.py:

from module import simplefiedPlayRec  # optional
import numpy as np

T = 5
fs = 44100
t = np.arange(T*fs) / fs
signal = np.sin(2*np.pi*440*t)

rec = simplefiedPlayRec(signal, fs, output_channels=2, repeats=3)

if i load the module from another file. no sound; no recording and no error.
when signal running in the same file (so no import) i have no sound no reording but an error.
it is this one

TypeError: Unable to determine number of input channels

from python-sounddevice.

mgeier avatar mgeier commented on July 20, 2024

@Jee-Bee Thanks for your comment.

The TypeError you are mentioning is legitimate, you have to specify the number of input channels, see the docs.

Apart from that I cannot say anything, because your code is much to complicated.
You should remove all the tiling, repeating and splitting, just reducing it to the bare minimum.

Once you have done that, we can start digging for errors ...

from python-sounddevice.

mgeier avatar mgeier commented on July 20, 2024

This has probably something to do with Pythons import lock ...

There may be a way around this, but as I said above (#38 (comment)), playback/recording shouldn't be done during an import anyway.

So it is probably actually good that it doesn't work ...

from python-sounddevice.

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.