Exemplo n.º 1
0
 def testReduceChannels(self):
     n_in = 7
     n_out = 2
     n_samples = 10
     data_in = np.arange(n_in * n_samples)
     buf = AudioBuffer(n_samples, n_in)
     data_out = buf.reduce_channels(data_in, n_in, n_out)
     correct = [x for x in range(n_in * n_samples) if x % n_in == 0 or (x - 1) % n_in == 0]
     self.assertListFloatEqual(correct, data_out)
Exemplo n.º 2
0
 def setUp(self):
     self.n_channels = 2
     self.buff32 = AudioBuffer(16, self.n_channels)
     self.data_str = '\x01\x02\x03\x04'
     self.data_strs = [
         '\x01\x02\x03\x04', '\x01\x02\x03\x04', '\x01\x02', '\x01', '\x01'
     ]
     self.num_types = 5
     self.data12 = [''] * self.num_types
     for i in range(len(self.data_strs)):
         self.data12[i] = self.data_str * self.n_channels * 12
Exemplo n.º 3
0
 def testReduceChannels(self):
     n_in = 7
     n_out = 2
     n_samples = 10
     data_in = np.arange(n_in * n_samples)
     buf = AudioBuffer(n_samples, n_in)
     data_out = buf.reduce_channels(data_in, n_in, n_out)
     correct = [
         x for x in range(n_in * n_samples)
         if x % n_in == 0 or (x - 1) % n_in == 0
     ]
     self.assertListFloatEqual(correct, data_out)
Exemplo n.º 4
0
 def setUp(self):
     self.n_channels = 2
     self.buff32 = AudioBuffer(16, self.n_channels)
     self.data_str = '\x01\x02\x03\x04'
     self.data_strs = [
         '\x01\x02\x03\x04',
         '\x01\x02\x03\x04',
         '\x01\x02',
         '\x01',
         '\x01']
     self.num_types = 5
     self.data12 = [''] * self.num_types
     for i in range(len(self.data_strs)):
         self.data12[i] = self.data_str * self.n_channels * 12
Exemplo n.º 5
0
PLOT_POLAR = True
EXTERNAL_PLOT = False
PLAY_AUDIO = False
TIMEOUT = 1
# Setup mics
mic_layout = np.array([[-.03, 0], [.01, 0], [-.01, 0], [.03, 0]])
# Track whether we have quit or not
done = False

# Events for signaling new data is available
audio_produced_event = threading.Event()
data_produced_event = threading.Event()

# Setup data buffers - use 4 * buffer length in case data get's backed up
# at any point, so it will not be lost
in_buf = AudioBuffer(length=4 * FRAMES_PER_BUF, n_channels=NUM_CHANNELS_IN)
out_buf = AudioBuffer(length=4 * FRAMES_PER_BUF, n_channels=NUM_CHANNELS_OUT)


def read_in_data(in_data, frame_count, time_info, status_flags):
    if done:  # Must do this or calls to stop_stream may not succeed
        return None, pyaudio.paComplete
    write_num = in_buf.get_available_write()
    if write_num > frame_count:
        write_num = frame_count
    in_buf.write_bytes(in_data[:(write_num * SAMPLE_SIZE * NUM_CHANNELS_IN)])
    in_buf.notify_of_audio()
    return None, pyaudio.paContinue


def write_out_data(in_data, frame_count, time_info, status_flags):
Exemplo n.º 6
0
MIC_ABOVE = np.array([0, 0, 1])
CAMERA_LOC = np.array([0, 0, 0])

# Setup mics
mic_layout = np.array([[.03, 0], [-.01, 0], [.01, 0], [-.03, 0]])
# Track whether we have quit or not
done = False
switch_beamforming = False  # Switch beamforming from on to off or off to on

# Events for signaling new data is available
audio_produced_event = threading.Event()
data_produced_event = threading.Event()

# Setup data buffers - use 4 * buffer length in case data get's backed up
# at any point, so it will not be lost
in_buf = AudioBuffer(length=4 * FRAMES_PER_BUF, n_channels=NUM_CHANNELS_IN)
out_buf = AudioBuffer(length=4 * FRAMES_PER_BUF, n_channels=NUM_CHANNELS_OUT)

# Setup record buffer
N_SECS_RECORD = 20
N_RECORD_FRAMES = N_SECS_RECORD * SAMPLE_RATE
record_buf = AudioBuffer(length=N_RECORD_FRAMES, n_channels=NUM_CHANNELS_OUT)


def read_in_data(in_data, frame_count, time_info, status_flags):
    if done:  # Must do this or calls to stop_stream may not succeed
        return None, pyaudio.paComplete
    write_num = in_buf.get_available_write()
    if write_num > frame_count:
        write_num = frame_count
    in_buf.write_bytes(in_data[:(write_num * SAMPLE_SIZE * NUM_CHANNELS_IN)])
Exemplo n.º 7
0
class AudioBufferTest(unittest.TestCase):

    def setUp(self):
        self.n_channels = 2
        self.buff32 = AudioBuffer(16, self.n_channels)
        self.data_str = '\x01\x02\x03\x04'
        self.data_strs = [
            '\x01\x02\x03\x04',
            '\x01\x02\x03\x04',
            '\x01\x02',
            '\x01',
            '\x01']
        self.num_types = 5
        self.data12 = [''] * self.num_types
        for i in range(len(self.data_strs)):
            self.data12[i] = self.data_str * self.n_channels * 12

    def data_of_length(self, length):
        return self.data_str * self.n_channels * length

    def testBufWrite(self):
        self.buff32.write_bytes(self.data_of_length(12))
        self.assertEquals(self.buff32.get_available_read(), 12)
        self.buff32.write_bytes(self.data_of_length(4))
        self.assertEquals(self.buff32.get_available_read(), 16)

    def testOverwrite(self):
        self.buff32.write_bytes(self.data_of_length(12))
        data6 = self.data_of_length(6)
        caught = False
        try:
            self.buff32.write_bytes(data6)
        except ValueError as e:
            print e.message
            caught = True
        self.assertEquals(caught, True)

    def testOverRead(self):
        data4 = self.data_of_length(4)
        self.buff32.write_bytes(data4)
        data_read = self.buff32.read_bytes(12)
        self.assertEquals(data_read, data4)

    def testWriteSamples(self):
        data = list(numpy.random.rand(12 * self.n_channels))
        data4 = list(numpy.random.rand(4 * self.n_channels))
        self.buff32.write_samples(data)
        self.assertEquals(self.buff32.get_available_read(), 12)
        self.buff32.write_samples(data4)
        self.assertEquals(self.buff32.get_available_read(), 16)

    def testReadSamples(self):
        data = list(numpy.random.rand(12 * self.n_channels))
        data4 = list(numpy.random.rand(4 * self.n_channels))
        self.buff32.write_samples(data)
        self.assertEquals(self.buff32.get_available_read(), 12)
        data_read = self.buff32.read_samples(12)
        self.assertListFloatEqual(data, data_read)
        self.buff32.write_samples(data4)
        self.assertEquals(self.buff32.get_available_read(), 4)
        data_read = self.buff32.read_samples(4)
        self.assertListFloatEqual(data4, data_read)

    def testWriteArray(self):
        data = numpy.random.rand(12 * self.n_channels)
        data4 = list(numpy.random.rand(4 * self.n_channels))
        self.buff32.write_samples(data)
        self.assertEquals(self.buff32.get_available_read(), 12)
        self.buff32.write_samples(data4)
        self.assertEquals(self.buff32.get_available_read(), 16)

    def testReadArray(self):
        data = numpy.random.rand(12 * self.n_channels)
        data4 = numpy.random.rand(4 * self.n_channels)
        self.buff32.write_samples(data)
        self.assertEquals(self.buff32.get_available_read(), 12)
        data_read = self.buff32.read_samples(12)
        self.assertListFloatEqual(data, data_read)
        self.buff32.write_samples(data4)
        self.assertEquals(self.buff32.get_available_read(), 4)
        data_read = self.buff32.read_samples(4)
        self.assertListFloatEqual(data_read, data4)

    def testRead(self):
        self.buff32.write_bytes(self.data_of_length(12))
        read_data = self.buff32.read_bytes(12)
        print read_data
        self.assertEquals(read_data, self.data_of_length(12))

    def testWraparoundWrite(self):
        self.buff32.write_bytes(self.data_of_length(12))
        self.buff32.read_bytes(12)
        self.buff32.write_bytes(self.data_of_length(12))
        self.assertEquals(self.buff32.get_available_read(), 12)

    def testWraparoundRead(self):
        self.buff32.write_bytes(self.data_of_length(12))
        self.buff32.read_bytes(12)
        self.buff32.write_bytes(self.data_of_length(12))
        self.assertEquals(self.buff32.get_available_read(), 12)
        data_read = self.buff32.read_bytes(12)
        self.assertEquals(data_read, self.data_of_length(12))
        self.assertEquals(self.buff32.get_available_read(), 0)

    def testInvalidData(self):
        caught = False
        try:
            self.buff32.write_bytes([2]*12)
        except ValueError as e:
            print e.message
            caught = True
        self.assertEquals(caught, True)

    def testMultipleWraps(self):
        self.buff32.write_bytes(self.data_of_length(12))
        self.buff32.read_bytes(8)
        self.buff32.write_bytes(self.data_of_length(12))
        self.buff32.read_bytes(16)
        self.buff32.write_bytes(self.data_of_length(12))
        self.buff32.read_bytes(9)
        self.buff32.write_bytes(self.data_of_length(12))
        self.buff32.read_bytes(11)
        self.buff32.write_bytes(self.data_of_length(12))
        data_read = self.buff32.read_bytes(12)
        self.assertEquals(self.buff32.get_available_read(), 4)
        self.assertEquals(data_read, self.data_of_length(12))

    def testReduceChannels(self):
        n_in = 7
        n_out = 2
        n_samples = 10
        data_in = np.arange(n_in * n_samples)
        buf = AudioBuffer(n_samples, n_in)
        data_out = buf.reduce_channels(data_in, n_in, n_out)
        correct = [x for x in range(n_in * n_samples) if x % n_in == 0 or (x - 1) % n_in == 0]
        self.assertListFloatEqual(correct, data_out)

    def assertListFloatEqual(self, list1, list2):
        if not len(list1) == len(list2):
            raise AssertionError("Lists differ in lenght. Cannot be equal")
        for i in range(len(list1)):
            self.assertLessEqual(abs(list1[i] - list2[i]), 1e-4)

    def tearDown(self):
        print "Completed"
Exemplo n.º 8
0
class AudioBufferTest(unittest.TestCase):
    def setUp(self):
        self.n_channels = 2
        self.buff32 = AudioBuffer(16, self.n_channels)
        self.data_str = '\x01\x02\x03\x04'
        self.data_strs = [
            '\x01\x02\x03\x04', '\x01\x02\x03\x04', '\x01\x02', '\x01', '\x01'
        ]
        self.num_types = 5
        self.data12 = [''] * self.num_types
        for i in range(len(self.data_strs)):
            self.data12[i] = self.data_str * self.n_channels * 12

    def data_of_length(self, length):
        return self.data_str * self.n_channels * length

    def testBufWrite(self):
        self.buff32.write_bytes(self.data_of_length(12))
        self.assertEquals(self.buff32.get_available_read(), 12)
        self.buff32.write_bytes(self.data_of_length(4))
        self.assertEquals(self.buff32.get_available_read(), 16)

    def testOverwrite(self):
        self.buff32.write_bytes(self.data_of_length(12))
        data6 = self.data_of_length(6)
        caught = False
        try:
            self.buff32.write_bytes(data6)
        except ValueError as e:
            print e.message
            caught = True
        self.assertEquals(caught, True)

    def testOverRead(self):
        data4 = self.data_of_length(4)
        self.buff32.write_bytes(data4)
        data_read = self.buff32.read_bytes(12)
        self.assertEquals(data_read, data4)

    def testWriteSamples(self):
        data = list(numpy.random.rand(12 * self.n_channels))
        data4 = list(numpy.random.rand(4 * self.n_channels))
        self.buff32.write_samples(data)
        self.assertEquals(self.buff32.get_available_read(), 12)
        self.buff32.write_samples(data4)
        self.assertEquals(self.buff32.get_available_read(), 16)

    def testReadSamples(self):
        data = list(numpy.random.rand(12 * self.n_channels))
        data4 = list(numpy.random.rand(4 * self.n_channels))
        self.buff32.write_samples(data)
        self.assertEquals(self.buff32.get_available_read(), 12)
        data_read = self.buff32.read_samples(12)
        self.assertListFloatEqual(data, data_read)
        self.buff32.write_samples(data4)
        self.assertEquals(self.buff32.get_available_read(), 4)
        data_read = self.buff32.read_samples(4)
        self.assertListFloatEqual(data4, data_read)

    def testWriteArray(self):
        data = numpy.random.rand(12 * self.n_channels)
        data4 = list(numpy.random.rand(4 * self.n_channels))
        self.buff32.write_samples(data)
        self.assertEquals(self.buff32.get_available_read(), 12)
        self.buff32.write_samples(data4)
        self.assertEquals(self.buff32.get_available_read(), 16)

    def testReadArray(self):
        data = numpy.random.rand(12 * self.n_channels)
        data4 = numpy.random.rand(4 * self.n_channels)
        self.buff32.write_samples(data)
        self.assertEquals(self.buff32.get_available_read(), 12)
        data_read = self.buff32.read_samples(12)
        self.assertListFloatEqual(data, data_read)
        self.buff32.write_samples(data4)
        self.assertEquals(self.buff32.get_available_read(), 4)
        data_read = self.buff32.read_samples(4)
        self.assertListFloatEqual(data_read, data4)

    def testRead(self):
        self.buff32.write_bytes(self.data_of_length(12))
        read_data = self.buff32.read_bytes(12)
        print read_data
        self.assertEquals(read_data, self.data_of_length(12))

    def testWraparoundWrite(self):
        self.buff32.write_bytes(self.data_of_length(12))
        self.buff32.read_bytes(12)
        self.buff32.write_bytes(self.data_of_length(12))
        self.assertEquals(self.buff32.get_available_read(), 12)

    def testWraparoundRead(self):
        self.buff32.write_bytes(self.data_of_length(12))
        self.buff32.read_bytes(12)
        self.buff32.write_bytes(self.data_of_length(12))
        self.assertEquals(self.buff32.get_available_read(), 12)
        data_read = self.buff32.read_bytes(12)
        self.assertEquals(data_read, self.data_of_length(12))
        self.assertEquals(self.buff32.get_available_read(), 0)

    def testInvalidData(self):
        caught = False
        try:
            self.buff32.write_bytes([2] * 12)
        except ValueError as e:
            print e.message
            caught = True
        self.assertEquals(caught, True)

    def testMultipleWraps(self):
        self.buff32.write_bytes(self.data_of_length(12))
        self.buff32.read_bytes(8)
        self.buff32.write_bytes(self.data_of_length(12))
        self.buff32.read_bytes(16)
        self.buff32.write_bytes(self.data_of_length(12))
        self.buff32.read_bytes(9)
        self.buff32.write_bytes(self.data_of_length(12))
        self.buff32.read_bytes(11)
        self.buff32.write_bytes(self.data_of_length(12))
        data_read = self.buff32.read_bytes(12)
        self.assertEquals(self.buff32.get_available_read(), 4)
        self.assertEquals(data_read, self.data_of_length(12))

    def testReduceChannels(self):
        n_in = 7
        n_out = 2
        n_samples = 10
        data_in = np.arange(n_in * n_samples)
        buf = AudioBuffer(n_samples, n_in)
        data_out = buf.reduce_channels(data_in, n_in, n_out)
        correct = [
            x for x in range(n_in * n_samples)
            if x % n_in == 0 or (x - 1) % n_in == 0
        ]
        self.assertListFloatEqual(correct, data_out)

    def assertListFloatEqual(self, list1, list2):
        if not len(list1) == len(list2):
            raise AssertionError("Lists differ in lenght. Cannot be equal")
        for i in range(len(list1)):
            self.assertLessEqual(abs(list1[i] - list2[i]), 1e-4)

    def tearDown(self):
        print "Completed"
Exemplo n.º 9
0
FRAMES_PER_BUF = 2048  # Do not go below 64, or above 2048
FFT_LENGTH = FRAMES_PER_BUF
WINDOW_LENGTH = FFT_LENGTH
HOP_LENGTH = WINDOW_LENGTH / 2
NUM_CHANNELS_IN = 2
NUM_CHANNELS_OUT = 2
DO_PLOT = True
PLOT_FREQ = 1  # For PLOT_FREQ = n, will plot every n loops
PLOT_CUTOFF_FREQ = 8000
TIMEOUT = 2  # Number of seconds to wait for new samples before giving up

# Track whether we have quit or not
done = False

# Setup data buffers
in_buf = AudioBuffer(length=4 * FRAMES_PER_BUF, n_channels=NUM_CHANNELS_IN)
out_buf = AudioBuffer(length=4 * FRAMES_PER_BUF, n_channels=NUM_CHANNELS_OUT)


def read_in_data(in_data, frame_count, time_info, status_flags):
    if done:  # Must do this or calls to stop_stream may not succeed
        return None, pyaudio.paComplete
    write_num = in_buf.get_available_write()
    if write_num > frame_count:
        write_num = frame_count
    in_buf.write_bytes(in_data[:(write_num * SAMPLE_SIZE * NUM_CHANNELS_IN)])
    in_buf.notify_of_audio()
    return None, pyaudio.paContinue


def write_out_data(in_data, frame_count, time_info, status_flags):
Exemplo n.º 10
0
FRAMES_PER_BUF = 2048  # Do not go below 64, or above 2048
FFT_LENGTH = FRAMES_PER_BUF
WINDOW_LENGTH = FFT_LENGTH
HOP_LENGTH = WINDOW_LENGTH / 2
NUM_CHANNELS_IN = 2
NUM_CHANNELS_OUT = 2
DO_PLOT = True
PLOT_FREQ = 1  # For PLOT_FREQ = n, will plot every n loops
PLOT_CUTOFF_FREQ = 8000
TIMEOUT = 2  # Number of seconds to wait for new samples before giving up

# Track whether we have quit or not
done = False

# Setup data buffers
in_buf = AudioBuffer(length=4 * FRAMES_PER_BUF, n_channels=NUM_CHANNELS_IN)
out_buf = AudioBuffer(length=4 * FRAMES_PER_BUF, n_channels=NUM_CHANNELS_OUT)


def read_in_data(in_data, frame_count, time_info, status_flags):
    if done:  # Must do this or calls to stop_stream may not succeed
        return None, pyaudio.paComplete
    write_num = in_buf.get_available_write()
    if write_num > frame_count:
        write_num = frame_count
    in_buf.write_bytes(in_data[:(write_num * SAMPLE_SIZE * NUM_CHANNELS_IN)])
    in_buf.notify_of_audio()
    return None, pyaudio.paContinue


def write_out_data(in_data, frame_count, time_info, status_flags):