Skip to content
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

Running from Python script! #48

Open
zack77789 opened this issue Aug 11, 2020 · 2 comments
Open

Running from Python script! #48

zack77789 opened this issue Aug 11, 2020 · 2 comments

Comments

@zack77789
Copy link

zack77789 commented Aug 11, 2020

Can someone show a short example how to run this from Python? I want to use this from Python script - function send and recv respectivelly ?

from amodem import config
from amodem import audio
from amodem import main

configuaration = config.Configuration()
interface = audio.Interface(configuration)
audio_library = "../../libportaudio64bit.dll"
interface.load(audio_library)

#src =
#dst =

main.send(config, src, dst, gain=1.0, extra_silence=0.0)

@zack77789
Copy link
Author

zack77789 commented Aug 17, 2020

Managed to call main.send function from Python script when saving to file, but couldn't get to work it when there is no output -o specified. The problem is how to specify dst variable when sending data without output ? What to give as an argument for FileType('wb', interface_factory)() ?

Exception: (-10000, b'PortAudio not initialized')
b'PortAudio V19.6.0-devel, revision 396fe4b6699ae929d3a685b3ef8a7e97396139a4'
Traceback (most recent call last):
File "..venv\lib\site-packages\amodem\audio.py", line 54, in player
return Stream(self, config=self.config, write=True)
File "..venv\lib\site-packages\amodem\audio.py", line 94, in init
self.interface.call(
File ".venv\lib\site-packages\amodem\audio.py", line 35, in call
return func(*args)
File "/.venv\lib\site-packages\amodem\audio.py", line 39, in _error_check
raise Exception(res, self._error_string(res))
Exception: (-10000, b'PortAudio not initialized')

import logging
import os

from amodem import async_reader
from amodem import audio
from amodem import calib
from amodem import main
from amodem.config import bitrates

def FileType(mode, interface_factory=None):
    def opener(fname):
        audio_interface = interface_factory() if interface_factory else None

        assert 'r' in mode or 'w' in mode
        if audio_interface is None and fname is None:
            fname = '-'

        if fname is None:
            assert audio_interface is not None
            if 'r' in mode:
                s = audio_interface.recorder()
                return async_reader.AsyncReader(stream=s, bufsize=s.bufsize)
            if 'w' in mode:
                return audio_interface.player()

        if fname == '-':
            if 'r' in mode:
                return _stdin
            if 'w' in mode:
                return _stdout

        return open(fname, mode)

    return opener

def interface_factory():
        return interface

def wrap(cls, stream, enable):
    return cls(stream) if enable else stream

log = logging.getLogger('__name__')

bitrate = os.environ.get('BITRATE', 1)
config = bitrates.get(int(bitrate))

interface = audio.Interface(config)
audio_library = "../../libportaudio64bit.dll"
interface.load(audio_library)

#src = wrap(Compressor, FileType('rb')("test.csv"), False)
src = FileType('rb')("test.csv")
dst = FileType('wb', interface_factory)

#dst = FileType('wb', interface_factory)(None)
#dst = FileType('wb', interface_factory)('test.pcm')

main.send(config, src, dst, gain=1.0, extra_silence=0.0)

@SamuelHaidu
Copy link

I can't understand how to use this with a personalized app. I will try debug to discover and maybe create a simple methods to send and receive data like a python socket

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants