romanz / amodem

Audio MODEM Communication Library in Python
Other
965 stars 121 forks source link

Running from Python script! #48

Open zack77789 opened 4 years ago

zack77789 commented 4 years ago

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 commented 4 years ago

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 commented 3 years ago

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