Git Product home page Git Product logo

Comments (19)

mgeier avatar mgeier commented on July 28, 2024

Thanks for this bug report!

Your solution avoids the exception, but it adds quotation marks to the device names.
Also, I guess it turns your device into u'Prim\\xe4rer Soundaufnahmetreiber' instead of showing the umlaut correctly, right?

For now, I can only think of a solution that checks if Python 2 or 3 is used and uses encode(...) in the former case and does nothing in the latter. Similar to http://stackoverflow.com/a/13848698/500098.

However, this is quite unwieldy just for supporting Python 2. Any better ideas?

from python-sounddevice.

raecke avatar raecke commented on July 28, 2024

You are right. The stackoverflow-solution is not ideal. I adapted a decorator from django.utils.encoding. Applying this decorator to the class (not the function), and adding
from __future__ import unicode_literals behaves as it should.

def python_2_unicode_compatible(klass):
    """
    A decorator that (re)defines __unicode__ and __str__ and __repr__ methods under Python 2.
    Under Python 3 it does nothing.

    To support Python 2 and 3 with a single code base, define a __str__ method
    returning text and apply this decorator to the class.
    """
    if _sys.version_info.major < 3:
        if '__repr__' in klass.__dict__:
            klass.__urepr__ = klass.__repr__
            klass.__repr__ = lambda self: self.__urepr__().encode('utf-8')
        if '__str__' in klass.__dict__:
            klass.__unicode__ = klass.__str__
            klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
    return klass

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

Thanks for the modification, but I think this is still too complicated.

And I don't really like the unicode_literals thing, what about using "native strings" everywhere?

I implemented this in #31, can you please try if this works for your umlaut-afflicted device name?
Please try it in both Python 2 and 3.

from python-sounddevice.

raecke avatar raecke commented on July 28, 2024

Tested it with python2. This leads to a mix of different encodings ('mbcs' for DirectSound and 'utf8' for everything else). Recoding everything to utf8 which is not already utf8 is needed.

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

Thanks for testing.

Out of curiosity, how do the different encodings look? Could you provide a screenshot?

from python-sounddevice.

raecke avatar raecke commented on July 28, 2024

Out of curiosity, how do the different encodings look? Could you provide a screenshot?

It depends how I decode the string. If I try to decode mbcs (which is cp1252 on my machine) as utf8 I get an exception. If I try to decode utf8 as mbcs I get:

print u"primärer kopfhörer".encode('utf8').decode('mbcs')
primärer kopfhörer

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

Thanks, but how does the output of python -m sounddevice look when using #31?

from python-sounddevice.

raecke avatar raecke commented on July 28, 2024

It depends on the codepage of the terminal:

C:\> chcp 850 && python -m sounddevice
Aktive Codepage: 850.
   0 Microsoft Soundmapper - Input, MME (2 in, 0 out)
>  1 Mikrofon (Realtek High Definiti, MME (2 in, 0 out)
   2 Microsoft Soundmapper - Output, MME (0 in, 2 out)
<  3 Lautsprecher/Kopfh÷rer (Realtek, MME (0 in, 2 out)
   4 Primõrer Soundaufnahmetreiber, Windows DirectSound (2 in, 0 out)
   5 Mikrofon (Realtek High Definition Audio), Windows DirectSound (2 in, 0 out)
   6 Primõrer Soundtreiber, Windows DirectSound (0 in, 2 out)
   7 Lautsprecher/Kopfh÷rer (Realtek High Definition Audio), Windows DirectSound
(0 in, 2 out)
   8 Lautsprecher/Kopfh├Ârer (Realtek High Definition Audio), Windows WASAPI (0
in, 2 out)
   9 Mikrofon (Realtek High Definition Audio), Windows WASAPI (2 in, 0 out)
  10 Speakers (Realtek HD Audio output), Windows WDM-KS (0 in, 2 out)
  11 Mikrofon (Realtek HD Audio Mic input), Windows WDM-KS (2 in, 0 out)
C:\>chcp 1252 && python -m sounddevice
Aktive Codepage: 1252.
   0 Microsoft Soundmapper - Input, MME (2 in, 0 out)
>  1 Mikrofon (Realtek High Definiti, MME (2 in, 0 out)
   2 Microsoft Soundmapper - Output, MME (0 in, 2 out)
<  3 Lautsprecher/Kopfhörer (Realtek, MME (0 in, 2 out)
   4 Primärer Soundaufnahmetreiber, Windows DirectSound (2 in, 0 out)
   5 Mikrofon (Realtek High Definition Audio), Windows DirectSound (2 in, 0 out)
   6 Primärer Soundtreiber, Windows DirectSound (0 in, 2 out)
   7 Lautsprecher/Kopfhörer (Realtek High Definition Audio), Windows DirectSound
(0 in, 2 out)
   8 Lautsprecher/Kopfhörer (Realtek High Definition Audio), Windows WASAPI (0
in, 2 out)
   9 Mikrofon (Realtek High Definition Audio), Windows WASAPI (2 in, 0 out)
  10 Speakers (Realtek HD Audio output), Windows WDM-KS (0 in, 2 out)
  11 Mikrofon (Realtek HD Audio Mic input), Windows WDM-KS (2 in, 0 out)
C:\>chcp 65001 && python -m sounddevice
Aktive Codepage: 65001.
   0 Microsoft Soundmapper - Input, MME (2 in, 0 out)
>  1 Mikrofon (Realtek High Definiti, MME (2 in, 0 out)
   2 Microsoft Soundmapper - Output, MME (0 in, 2 out)
<  3 Lautsprecher/Kopfh�rer (Realtek, MME (0 in, 2 out)
   4 Prim�rer Soundaufnahmetreiber, Windows DirectSound (2 in, 0 out)
   5 Mikrofon (Realtek High Definition Audio), Windows DirectSound (2 in, 0 out)
   6 Prim�rer Soundtreiber, Windows DirectSound (0 in, 2 out)
   7 Lautsprecher/Kopfh�rer (Realtek High Definition Audio), Windows DirectSound
(0 in, 2 out)
   8 Lautsprecher/Kopfhörer (Realtek High Definition Audio), Windows WASAPI (0
in, 2 out)
   9 Mikrofon (Realtek High Definition Audio), Windows WASAPI (2 in, 0 out)
  10 Speakers (Realtek HD Audio output), Windows WDM-KS (0 in, 2 out)
  11 Mikrofon (Realtek HD Audio Mic input), Windows WDM-KS (2 in, 0
out)Traceback (most recent call last):
  File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "C:\Python27\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "c:\noppi\misc\python-sounddevice\sounddevice.py", line 2544, in <module>
    print(query_devices())
IOError: [Errno 0] Error

from python-sounddevice.

raecke avatar raecke commented on July 28, 2024

Results with python3:

C:\>chcp 850 && python -m sounddevice
Aktive Codepage: 850.
Traceback (most recent call last):
  File "c:\Program Files (x86)\Python35-32\lib\runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\Program Files (x86)\Python35-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\noppi\misc\python-sounddevice\sounddevice.py", line 2544, in <module>
    print(query_devices())
  File "c:\Program Files (x86)\Python35-32\lib\encodings\cp850.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\ufffd' in position 191: character maps to <undefined>
C:\>chcp 1252 && python -m sounddevice
Aktive Codepage: 1252.
Traceback (most recent call last):
  File "c:\Program Files (x86)\Python35-32\lib\runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\Program Files (x86)\Python35-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\noppi\misc\python-sounddevice\sounddevice.py", line 2544, in <module>
    print(query_devices())
  File "c:\Program Files (x86)\Python35-32\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\ufffd' in position 191: character maps to <undefined>
C:\>chcp 65001 && python -m sounddevice
Aktive Codepage: 65001.
   0 Microsoft Soundmapper - Input, MME (2 in, 0 out)
>  1 Mikrofon (Realtek High Definiti, MME (2 in, 0 out)
   2 Microsoft Soundmapper - Output, MME (0 in, 2 out)
<  3 Lautsprecher/Kopfh�rer (Realtek, MME (0 in, 2 out)
   4 Primärer Soundaufnahmetreiber, Windows DirectSound (2 in, 0 out)
   5 Mikrofon (Realtek High Definition Audio), Windows DirectSound (2 in, 0 out)
   6 Primärer Soundtreiber, Windows DirectSound (0 in, 2 out)
   7 Lautsprecher/Kopfhörer (Realtek High Definition Audio), Windows DirectSound (0 in, 2 out)
   8 Lautsprecher/Kopfhörer (Realtek High Definition Audio), Windows WASAPI (0 in, 2 out)
   9 Mikrofon (Realtek High Definition Audio), Windows WASAPI (2 in, 0 out)
  10 Speakers (Realtek HD Audio output), Windows WDM-KS (0 in, 2 out)
  11 Mikrofon (Realtek HD Audio Mic input), Windows WDM-KS (2 in, 0 out)

from python-sounddevice.

raecke avatar raecke commented on July 28, 2024

The encoding for MME devicenames seems also to be mbcs. This should be fixed in line 623.

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

Thanks a lot for the detailed tests!

I've made a new attempt at solving this: #32. Can you please try if that works?

The encoding for MME devicenames seems also to be mbcs. This should be fixed in line 623.

OK, but in your last example, DirectSound and WASAPI show the same kind of artifact ...
Is that the same with #32?

from python-sounddevice.

raecke avatar raecke commented on July 28, 2024

#32 works with the limitation, that the encoding for MME is not correct. Should I open a new issue for this? It also a problem for python3.

OK, but in your last example, DirectSound and WASAPI show the same kind of artifact ...
Is that the same with #32?

The result is the same, because the test is with python3, and python3 is not affected by the changes.

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

Thanks, I've added another commit (e33bd89) to #32 which enables MBCS for MME.

Does it now work for all codepages on Python 2 and 3?

from python-sounddevice.

raecke avatar raecke commented on July 28, 2024

Yes it works in the terminal with cp850, cp1252, and cp65001. The only thing that is not perfect, is the behaviour in a jupyter notebook. If I type sounddevice.query_devices(), I get the wrong characters, because utf-8 is interpreted as mbcs.

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

Thanks for testing! Any idea how we could make it work for Jupyter notebooks?
Does the problem occur for both Python 2 and 3?
Does sys.stdout.encoding have the wrong value?

from python-sounddevice.

raecke avatar raecke commented on July 28, 2024

In the Jupyter notebook sys.stdout.encoding is UTF-8.
The command print sounddevice.query_devices() gives the correct result.
The command sounddevice.query_devices() interprets the result as MBCS.

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

Thanks for checking this out!

I guess this last issue has to be solved on the Jupyter side of things, right?

Shall I merge #32 as is or is there something else to do?

from python-sounddevice.

raecke avatar raecke commented on July 28, 2024

Shall I merge #32 as is or is there something else to do?

I think there is nothing else to do.

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

I just merged #32, thanks for your help!

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.