# Path: bdf.py
# class BDFReader(object):
#   SAMPLE_RATE = 250
#   NUM_CHANNELS = 8
# 
#   def __init__(self, fileobj):
#     self.fileobj = fileobj
#     data = fileobj.read(256)
# 
#     # Ignore header, and assume we open our own files
#     assert(b"\xffBIOSEMI" == data[0:8])
# 
#     header_len = int(data[184:192].strip())
# 
#     assert float(data[244:252].strip()) == 1.0
# 
#     assert int(data[252:256].strip()) == 8 # Number of channels
#     
#     fileobj.read(header_len - 256)
# 
#     self.read_buffer = []
#     self._read_into_buffer()
# 
#   def readPacket(self):
#     if len(self.read_buffer) == 0:
#       self._read_into_buffer()
# 
#     if len(self.read_buffer) > 0:
#       return self.read_buffer.pop(0)
# 
#   def _read_into_buffer(self):
#     block_size = 3 * self.SAMPLE_RATE * 8
#     block = self.fileobj.read(block_size)
#     
#     if len(block) < block_size:
#       return True
# 
#     for sample in range(self.SAMPLE_RATE):
#       packet = ()
#       for channel in range(self.NUM_CHANNELS):
#             idx = (channel * self.SAMPLE_RATE + sample) * 3
#             chunk = block[idx:idx+3]
#             packet += struct.unpack('<i', chunk + (b'\0' if chunk[2] < 128 else b'\xff'))
#       #print packet
#       self.read_buffer += (packet,)
#       
#     return False
# 
# class WAVReader:
#   def __init__(self, fileobj):
#     self.wave = wave.open(fileobj)
# 
#     assert(self.wave.getsampwidth() == 3)
#     assert(self.wave.getframerate() == 250)
#     assert(self.wave.getnchannels() == 8)
# 
#   def readPacket(self):
# 
#     channels = [0] * 8
#     frame = self.wave.readframes(1)
#     for ch in range(8):
#         data = frame[ch * 3: ch * 3 + 3]
#         data = (b'\xff' if data[0] >> 7 else b'\0') + data
#         channels[ch] = struct.unpack('<i', data)[0]
# 
#     return channels


import time
import sys, signal, struct
import socket
import json
from bdf import BDFReader, WAVReader
from pyqtgraph import QtCore, QtGui



signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QtCore.QCoreApplication(sys.argv)
#app = QtGui.QApplication(sys.argv)

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

def send_packet(channels):
	data = json.dumps(channels)
	sock.sendto(data.encode('utf-8'), ('localhost', 8888))
	data = struct.pack('=f', channels[0] * 0.022351744455307063)
	sock.sendto(data, ('localhost', 9999))

filename = sys.argv[1]
filehandler = open(filename, 'rb')

if filename.lower().endswith('.wav'):
	bdf = WAVReader(filehandler)
else:

# Path: bdf.py
# class BDFReader(object):
#   SAMPLE_RATE = 250
#   NUM_CHANNELS = 8
# 
#   def __init__(self, fileobj):
#     self.fileobj = fileobj
#     data = fileobj.read(256)
# 
#     # Ignore header, and assume we open our own files
#     assert(b"\xffBIOSEMI" == data[0:8])
# 
#     header_len = int(data[184:192].strip())
# 
#     assert float(data[244:252].strip()) == 1.0
# 
#     assert int(data[252:256].strip()) == 8 # Number of channels
#     
#     fileobj.read(header_len - 256)
# 
#     self.read_buffer = []
#     self._read_into_buffer()
# 
#   def readPacket(self):
#     if len(self.read_buffer) == 0:
#       self._read_into_buffer()
# 
#     if len(self.read_buffer) > 0:
#       return self.read_buffer.pop(0)
# 
#   def _read_into_buffer(self):
#     block_size = 3 * self.SAMPLE_RATE * 8
#     block = self.fileobj.read(block_size)
#     
#     if len(block) < block_size:
#       return True
# 
#     for sample in range(self.SAMPLE_RATE):
#       packet = ()
#       for channel in range(self.NUM_CHANNELS):
#             idx = (channel * self.SAMPLE_RATE + sample) * 3
#             chunk = block[idx:idx+3]
#             packet += struct.unpack('<i', chunk + (b'\0' if chunk[2] < 128 else b'\xff'))
#       #print packet
#       self.read_buffer += (packet,)
#       
#     return False
# 
# class WAVReader:
#   def __init__(self, fileobj):
#     self.wave = wave.open(fileobj)
# 
#     assert(self.wave.getsampwidth() == 3)
#     assert(self.wave.getframerate() == 250)
#     assert(self.wave.getnchannels() == 8)
# 
#   def readPacket(self):
# 
#     channels = [0] * 8
#     frame = self.wave.readframes(1)
#     for ch in range(8):
#         data = frame[ch * 3: ch * 3 + 3]
#         data = (b'\xff' if data[0] >> 7 else b'\0') + data
#         channels[ch] = struct.unpack('<i', data)[0]
# 
#     return channels


# Path: replay_server.py
import time
import sys, signal, struct
import socket
import json
from bdf import BDFReader, WAVReader
from pyqtgraph import QtCore, QtGui




signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QtCore.QCoreApplication(sys.argv)
#app = QtGui.QApplication(sys.argv)

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

def send_packet(channels):
	data = json.dumps(channels)
	sock.sendto(data.encode('utf-8'), ('localhost', 8888))
	data = struct.pack('=f', channels[0] * 0.022351744455307063)
	sock.sendto(data, ('localhost', 9999))

filename = sys.argv[1]
filehandler = open(filename, 'rb')

if filename.lower().endswith('.wav'):
	bdf = WAVReader(filehandler)
else:

	bdf = BDFReader(filehandler)