예제 #1
0
def forward_handler(addr, tags, data, source):
    print "---"
    print "source %s" % OSC.getUrlStr(source)
    print "addr   %s" % addr
    print "tags   %s" % tags
    print "data   %s" % data

    scale = EEGsynth.getfloat('processing', 'scale', config, r)
    if scale is None:
        scale = 1

    offset = EEGsynth.getfloat('processing', 'offset', config, r)
    if offset is None:
        offset = 0

    # apply the scale and offset
    for i in range(len(data)):
        data[i] = EEGsynth.rescale(data[i], scale, offset)

    # the results will be written to redis as "osc.1.faderA" etc.
    key = config.get('output', 'prefix') + addr.replace('/', '.')

    if tags == 'f' or tags == 'i':
        # it is a single value
        val = data[0]
        r.set(key, val)  # send it as control value
        if addr in button_list:
            r.publish(key, val)  # send it as trigger

    else:
        # it is a list, send it as a list of control values
        val = data
        r.set(key, val)
예제 #2
0
    # return the data with the new value in place
    return (data)


# keep a timer to send a packet every now and then
prevtime = time.time()

try:
    while True:
        time.sleep(config.getfloat('general', 'delay'))

        for chanindx in range(1, 512):
            chanstr = "channel%03d" % chanindx

            try:
                chanval = EEGsynth.getfloat('input', chanstr, config, r)
            except:
                # the channel is not configured in the ini file, skip it
                continue

            if chanval == None:
                # the value is not present in redis, skip it
                continue

            if EEGsynth.getint('compressor_expander', 'enable', config, r):
                # the compressor applies to all channels and must exist as float or redis key
                lo = EEGsynth.getfloat('compressor_expander', 'lo', config, r)
                hi = EEGsynth.getfloat('compressor_expander', 'hi', config, r)
                if lo is None or hi is None:
                    if debug > 1:
                        print "cannot apply compressor/expander"
예제 #3
0
try:
    r = redis.StrictRedis(host=config.get('redis','hostname'), port=config.getint('redis','port'), db=0)
    response = r.client_list()
    if debug>0:
        print "Connected to redis server"
except redis.ConnectionError:
    print "Error: cannot connect to redis server"
    exit()

# get the input and output options
input_channel, input_name = map(list, zip(*config.items('input')))
quantized_name, quantized_value = map(list, zip(*config.items('quantization')))

# get the list of values for each of the quantization levels
for i,name in enumerate(quantized_name):
    quantized_value[i] = EEGsynth.getfloat('quantization', name, config, r, multiple=True)

# find the level to which the data is to be quantized
index =  quantized_name.index('level')
level =  quantized_value[index]
# remove the level from the list
del quantized_name[index]
del quantized_value[index]

def find_nearest_idx(array,value):
    # find the index of the array element that is the nearest to the value
    idx = (np.abs(np.asarray(array)-value)).argmin()
    return idx

while True:
    time.sleep(config.getfloat('general', 'delay'))
예제 #4
0
파일: brain.py 프로젝트: svsobh/eegsynth
if debug > 2:
    print 'taper     = ', taper
    print 'frequency = ', frequency

try:
    while True:
        time.sleep(config.getfloat('general', 'delay'))

        band_items = config.items('band')
        bandname = []
        bandlo = []
        bandhi = []
        for item in band_items:
            # channel numbers are one-offset in the ini file, zero-offset in the code
            lohi = EEGsynth.getfloat('band', item[0], config, r, multiple=True)
            if debug > 2:
                print item[0], lohi
            # lohi = config.get('band', item[0]).split("-")
            bandname.append(item[0])
            bandlo.append(lohi[0])
            bandhi.append(lohi[1])
        if debug > 0:
            print bandname, bandlo, bandhi

        lock.acquire()
        if trigger.update:
            minval = trigger.minval
            maxval = trigger.maxval
            freeze = trigger.freeze
            trigger.update = False
예제 #5
0
    exit()

# wiringpi.wiringPiSetup()
wiringpi.wiringPiSetupGpio()

pinR = config.getint('output', 'red')
pinG = config.getint('output', 'green')
pinB = config.getint('output', 'blue')

wiringpi.pinMode(pinR, wiringpi.OUTPUT)
wiringpi.pinMode(pinG, wiringpi.OUTPUT)
wiringpi.pinMode(pinB, wiringpi.OUTPUT)

wiringpi.softPwmCreate(pinR, 0, 127)
wiringpi.softPwmCreate(pinG, 0, 127)
wiringpi.softPwmCreate(pinB, 0, 127)

while True:
    time.sleep(config.getfloat('general', 'delay'))

    valR = EEGsynth.getint('input', 'red',   config, r) * EEGsynth.getfloat('scaling', 'red',   config, r)
    valG = EEGsynth.getint('input', 'green', config, r) * EEGsynth.getfloat('scaling', 'green', config, r)
    valB = EEGsynth.getint('input', 'blue',  config, r) * EEGsynth.getfloat('scaling', 'blue',  config, r)

    print valR, valG, valB

    wiringpi.softPwmWrite(pinR, int(valR))
    wiringpi.softPwmWrite(pinG, int(valG))
    wiringpi.softPwmWrite(pinB, int(valB))

예제 #6
0
            pattern = EEGsynth.getint('input','pattern', config, r, default=0)

            if pattern!=previous:
                # get the corresponding sequence
                pattern = EEGsynth.getint('input','pattern', config, r, default=0)
                try:
                  sequence = config.get('sequence',"pattern{:d}".format(pattern))
                except:
                  sequence = '0'
                # immediately start playing the new sequence
                previous = pattern
                print 'break'
                break

            # use a default rate of 90 bpm
            rate = EEGsynth.getfloat('input','rate', config, r)
            if rate is None:
                rate = 90

            # use a default transposition of 48
            transpose = EEGsynth.getint('input','transpose', config, r)
            if transpose is None:
                transpose = 48

            # assume that the rate value is between 0 and 127, map it exponentially from 20 to 2000
            # it should not get too low, otherwise the code with the sleep below becomes unresponsive
            rate = 20*math.exp(rate*math.log(100)/127)

            if debug>0:
                print pattern, rate, transpose, note
예제 #7
0
for chanindx in range(f.getNSignals()):
    if f.getSignalFreqs()[chanindx] != f.getSignalFreqs()[0]:
        raise IOError('unequal SignalFreqs')
    if f.getNSamples()[chanindx] != f.getNSamples()[0]:
        raise IOError('unequal NSamples')

block = 0
blocksize = 1
begsample = 0
endsample = 0
adjust = 1

# this approximates the real time streaming speed
desired = blocksize / (fSample *
                       EEGsynth.getfloat('playback', 'speed', config, r))
elapsed = desired
naptime = 0.02

while True:

    # measure the time that it takes
    now = time.time()

    if endsample > nSamples - 1:
        if debug > 0:
            print "End of file reached, jumping back to start"
        begsample = 0
        endsample = 0
        block = 0
        continue
예제 #8
0
if debug>0:
    print '===== input variables ====='
    for name,variable in zip(input_name, input_variable):
        print name, '=', variable
    print '===== output equations ====='
    for name,equation in zip(output_name, output_equation):
        print name, '=', equation
    print '============================'


while True:
    time.sleep(config.getfloat('general', 'delay'))

    actual_value = [];
    for name in input_name:
        actual_value.append(EEGsynth.getfloat('input', name, config, r))

    for key,equation in zip(output_name, output_equation):
        for name,value in zip(input_name, actual_value):
            if value is None and equation.count(name)>0:
                # there are undefined variables in this equation
                break
            else:
                equation = equation.replace(name, str(value))
        else:
            # this section should not run if there are undefined variables in an equation
            val = eval(equation)
            if debug>1:
                print key, '=', equation, '=', val
            r.set(key, val)             # send it as control value
예제 #9
0
]  # the key name that matches in the input and output section of the *.ini file
list2 = []  # the key name in REDIS
list3 = []  # the key name in OSC
for i in range(len(list_input)):
    for j in range(len(list_output)):
        if list_input[i][0] == list_output[j][0]:
            list1.append(list_input[i][0])
            list2.append(list_input[i][1])
            list3.append(list_output[j][1])

while True:
    time.sleep(config.getfloat('general', 'delay'))

    for key1, key2, key3 in zip(list1, list2, list3):

        val = EEGsynth.getfloat('input', key1, config, r, multiple=True)
        if any(item is None for item in val):
            # the control value is not present in redis, skip it
            continue
        else:
            val = [float(x) for x in val]

        if EEGsynth.getint('compressor_expander', 'enable', config, r):
            # the compressor applies to all channels and must exist as float or redis key
            lo = EEGsynth.getfloat('compressor_expander', 'lo', config, r)
            hi = EEGsynth.getfloat('compressor_expander', 'hi', config, r)
            if lo is None or hi is None:
                if debug > 1:
                    print "cannot apply compressor/expander"
            else:
                # apply the compressor/expander
예제 #10
0
# control values are only relevant when different from the previous value
previous_val = {}
for channel in range(1, 16):
    name = 'channel{}'.format(channel)
    previous_val[name] = None

try:
    while True:
        time.sleep(config.getfloat('general', 'delay'))

        # loop over the control values
        for channel in range(0, 16):
            # channels are one-offset in the ini file, zero-offset in the code
            name = 'channel{}'.format(channel + 1)
            val = EEGsynth.getfloat('control', name, config, r)
            if val is None:
                if debug > 2:
                    print name, 'not available'
                continue

            if EEGsynth.getint('compressor_expander', 'enable', config, r):
                # the compressor applies to all channels and must exist as float or redis key
                lo = EEGsynth.getfloat('compressor_expander', 'lo', config, r)
                hi = EEGsynth.getfloat('compressor_expander', 'hi', config, r)
                if lo is None or hi is None:
                    if debug > 1:
                        print "cannot apply compressor/expander"
                else:
                    # apply the compressor/expander
                    val = EEGsynth.compress(val, lo, hi)
예제 #11
0
            serialport = initialize_serial()
            init_serial = True

        if use_midi and not init_midi:
            # this might not be running at the start
            midiport = initialize_midi()
            init_midi = True

        if use_midi and not previous_use_midi:
            midisync.enable()
            previous_use_midi = True
        elif not use_midi and previous_use_midi:
            midisync.disable()
            previous_use_midi = False

        rate = EEGsynth.getfloat('input', 'rate', config, r)
        if rate is None:
            time.sleep(config.getfloat('general', 'delay'))
            continue

        offset = EEGsynth.getfloat('input', 'offset', config, r, default=64)
        # the offset value from 0-127 gets scaled to a value from -1 to +1 seconds
        offset = (offset-64)/(127/2)

        multiplier = EEGsynth.getfloat('input', 'multiplier', config, r, default=1)
        # the multiplier value from 0-127 gets scaled to a value from 1/4 to 16/4
        idx = find_nearest_idx(multiplier_mid, multiplier)
        multiplier = multiplier_val[idx]

        # update the thread that synchronizes over MIDI
        midisync.setRate(rate*multiplier)