예제 #1
0
    def __init__(
        self,
        side_in,
        octaves=3,
        shift=-18,
        synth_type=sound_drivers.SUPER_COLLIDER,
        volume_type='linear',
        max_volume=.5,
        logger=None,
        log_level=logging.INFO,
        profile=False,
    ):
        self.volume_type = volume_type
        self.max_volume = max_volume

        if profile:
            if logger is None:
                self.logger = get_logger('Sonificator', level=log_level)
            else:
                self.logger = logger

            self.time_measurer = TimeMeasurer(self.logger)
            self.time_measurer.decorate_method(self, self.sonify, 'Sonified')

        Synth, init_audio = sound_drivers.get_driver(synth_type)

        init_audio()
        self.synth = Synth(levels=side_in * side_in,
                           octaves=octaves,
                           shift=shift)
        self.synth.play()
예제 #2
0
    def __init__(self, host, port, logger=None, profile=False):
        self.host = host
        self.port = port

        if logger is None:
            self.logger = get_logger('ImageRetriever')
        else:
            self.logger = logger

        if profile:
            self.time_measurer = TimeMeasurer(logger=self.logger)
            self.time_measurer.decorate_method(self, self.get_image,
                                               'Got image')
예제 #3
0
    def __init__(self,
                 host=None,
                 port=8090,
                 remote_host='localhost',
                 remote_port=8000,
                 frame_rate=24,
                 side_in=2**3,
                 octaves=6,
                 tone_shift=-18,
                 synth_type=sound_drivers.PY_GAME,
                 retriever_type=RetrieverTypes.PyGame,
                 log_level=logging.INFO,
                 profile=False,
                 save_images=False,
                 sigma=2,
                 initial_mul=32,
                 decrease=1.2,
                 **server_args):
        self.logger = get_logger('acoustic_sight_server.server',
                                 level=log_level)

        self.sio = socketio.AsyncServer()
        self.app = web.Application(middlewares=[IndexMiddleware()])
        self.sio.attach(self.app)

        self.clients = 0
        self.host = host
        self.port = port
        self.server_args = server_args

        self.remote_image_sonification = ImageSonificator(
            frame_rate=frame_rate,
            remote_host=remote_host,
            remote_port=remote_port,
            side_in=side_in,
            octaves=octaves,
            tone_shift=tone_shift,
            synth_type=synth_type,
            retriever_type=retriever_type,
            logger=self.logger,
            profile=profile,
            save_images=save_images,
            sigma=sigma,
            initial_mul=initial_mul,
            decrease=decrease,
        )

        self.setup_events()
예제 #4
0
import logging
import time

from acoustic_sight.tools import get_logger

logger = get_logger('test_run')


def test_run(init_audio, Synth, levels=16, interval=.125, await=.5):
    logger.setLevel(logging.DEBUG)
    init_audio()

    synth = Synth(levels=levels)
    logger.info('Used frequencies: %s' % synth.frequencies)

    for i in range(len(synth)):
        synth.play(i)
        logger.debug('Turn on {} Hz frequency.'.format(
            synth.tones[i].frequency))
        time.sleep(interval)

    time.sleep(await)

    synth.stop()
    logger.info('Stop all.')
예제 #5
0
from acoustic_sight.tools import get_logger


logger = get_logger('Synth')


def get_frequencies(base=440, octaves=3, levels=16, shift=-12):
    a = 2 ** (1/12)

    def freq(n):
        return base * a ** n

    step = octaves * 12 / levels
    frequences = [None] * levels

    for l in range(levels):
        frequences[l] = freq(step * l + shift)

    return frequences


class Synth:
    def __init__(self, base=440, octaves=3, levels=16, shift=-12):
        self.levels = levels
        self.tones = [None] * self.levels
        self.frequencies = get_frequencies(base, octaves, self.levels, shift)

        for i in range(self.levels):
            self.tones[i] = self.get_tone(self.frequencies[i])
        logger.info('All tones initialized.')
예제 #6
0
#!/usr/bin/env python3

from time import sleep

import cv2

from acoustic_sight import sound_drivers
from acoustic_sight.sonificator import Sonificator
from acoustic_sight.tools import get_logger
from playground.webcam.processors.cv2_frame_processor import CV2FrameProcessor

logger = get_logger('webcam')


class WebcamApp:
    def __init__(self,
                 side_in=16,
                 side_out=640,
                 fps=24,
                 max_volume=.5,
                 volume_type='exp',
                 octaves=3,
                 tone_shift=-18,
                 sonify=True,
                 show_windows=True,
                 synth_type=sound_drivers.SUPER_COLLIDER):
        self.side_in = side_in
        self.side_out = side_out
        self.fps = fps
        self.max_volume = max_volume
        self.volume_type = volume_type
예제 #7
0
import math

import numpy
import pygame

from acoustic_sight.synth import Synth
from acoustic_sight.tools import get_logger

logger = get_logger('pg_tools')


def init_audio(frequency=22050 * 4, channels=1):
    logger.debug('Initializing PyGame mixer...')
    pygame.mixer.pre_init(frequency, -16, channels, 2**12)
    pygame.init()
    logger.info('PyGame mixer initialized: {}'.format(pygame.mixer.get_init()))


def get_max_amplitude():
    bits_per_sample = abs(pygame.mixer.get_init()[1])
    return 2**(bits_per_sample - 1) - 1


def get_tone_samples_array(frequency, repeat=10, time_shift=0.):
    period = 1. / frequency
    sample_rate = pygame.mixer.get_init()[0]
    frame_size = math.floor(sample_rate * repeat * period)
    frame_period = repeat * period
    time_vector = numpy.linspace(start=time_shift,
                                 stop=time_shift + frame_period,
                                 num=frame_size)
예제 #8
0
import numpy as np
import numpy.fft as fft
import pyaudio
from collections import deque

from acoustic_sight.synth import Synth, get_frequencies
from acoustic_sight.tools import get_logger


logger = get_logger('pa_tools')


class PAState:
    def __init__(self, pa_instance: pyaudio.PyAudio, bitrate: int, channels: int):
        self.pa_instance = pa_instance
        self.bitrate = bitrate
        self.channels = channels


_pa_state: PAState = None


FFT = 'fft'
RFFT = 'rfft'


def init_audio(bitrate=96000, channels=1):
    logger.debug('Initializing PyGame mixer...')
    global _pa_state
    _pa_state = PAState(pyaudio.PyAudio(), bitrate, channels)
    logger.info('PyAudio initialized: {}'.format(_pa_state))
예제 #9
0
import supriya

from acoustic_sight.tools import get_logger
from acoustic_sight.synth import Synth


logger = get_logger('sc_tools')


def init_audio(*args, **kwargs):
    import os
    logger.debug('Patching PATH by adding "/usr/local/bin"...')
    os.environ['PATH'] = '/usr/local/bin:{path}'.format(path=os.environ['PATH'])

    logger.debug('Starting SuperCollider server...')
    server_options = supriya.servertools.ServerOptions(maximum_synthdef_count=2**16)
    server = supriya.servertools.Server.get_default_server()
    server.boot(server_options=server_options)
    logger.info('SuperCollider server started: {}'.format(server))


def stop_audio(*args, **kwargs):
    supriya.servertools.Server.get_default_server().quit()


def get_tone_synthdef(amplitude=1., frequency=440., gate=1.):
    builder = supriya.synthdeftools.SynthDefBuilder(
        amplitude=amplitude,
        frequency=frequency,
        gate=gate,
    )
예제 #10
0
    def __init__(self,
                 remote_host='localhost',
                 remote_port=8000,
                 frame_rate=24,
                 side_in=2**3,
                 octaves=6,
                 tone_shift=-18,
                 sonify=True,
                 show_image=False,
                 synth_type=sound_drivers.SUPER_COLLIDER,
                 retriever_type=RetrieverTypes.PyGame,
                 logger=None,
                 log_level=logging.INFO,
                 profile=False,
                 save_images=False,
                 sigma=2,
                 initial_mul=32,
                 decrease=1.2,
                 **kwargs):
        if logger is None:
            self.logger = get_logger('ImageSonificator', level=log_level)
        else:
            self.logger = logger

        if profile:
            self.time_measurer = TimeMeasurer(logger=self.logger)

            self.time_measurer.decorate_method(self, self.next, 'Full cycle')
            self.time_measurer.decorate_method(self, self.get_data,
                                               'Data retrieved')
            self.time_measurer.decorate_method(self,
                                               self.process_full_size_image,
                                               'Full size process')
            self.time_measurer.decorate_method(self, self. await, 'Awaited')
            self.time_measurer.decorate_method(self, self.save_image,
                                               'Saved original')

        self.remote_host = remote_host
        self.remote_port = remote_port
        self.frame_rate = frame_rate
        self.side_in = side_in

        self.sonify = sonify
        self.sonificator = None

        self.save_images = save_images
        self.image_saver = None

        self.show_image = show_image
        self.cv2 = None

        self.rpi_cam_client = get_client(retriever_type)(self.remote_host,
                                                         self.remote_port,
                                                         profile=profile)

        self.started = False

        self.last_time_checkpoint = time.time()

        if sonify:
            self.sonificator = Sonificator(
                side_in=side_in,
                octaves=octaves,
                shift=tone_shift,
                synth_type=synth_type,
                profile=profile,
                **kwargs,
            )
        if save_images:
            self.image_saver = PILImageSaver(data_dir=DATA_DIR,
                                             in_parallel=True)

        if show_image:
            import cv2
            self.cv2 = cv2

        self.transforamtion = CannyTransformation(self,
                                                  sigma=sigma,
                                                  initial_mul=initial_mul,
                                                  decrease=decrease)