Esempio n. 1
0
 def play(self, x, frames_per_buffer=1024):
     """Play audio. If dropouts or buffer underruns occur try different
     values for the frames_per_buffer variable."""
     
     _Device.play(self, x)
     self._validate(frames_per_buffer)
     
     missing_frames = self._get_missing_frames(frames_per_buffer, len(x))
     
     # generate silence to fill up missing frames
     pad = Audio(channels=x.ch, fs=x.fs, nofsamples=missing_frames, dtype=x.samples.dtype)
     
     # append the missing frames to a copy of the audio to be played. We now have
     # audio that can be split into complete (full) buffers
     cpy = Audio(fs=x.fs, initialdata=x.samples)
     cpy.concat(pad)
     
     assert len(cpy)%frames_per_buffer == 0
     
     stream = self.pa.open(format                = self._data_format(x),
                           channels              = x.ch,
                           rate                  = x.fs,
                           frames_per_buffer     = frames_per_buffer,
                           output_device_index   = self._index_out,
                           input                 = False,
                           output                = True,
                           )
     try:
         self._logger.info("play: start")
         counter = 0
         
         # split the audio into chunks the size of one buffer, so we can
         # iterate over the audio in chunksizes of the same size as one buffer
         it = iter(np.split(cpy.samples, len(cpy)/frames_per_buffer))
         try:
             while True:
                 chunk = it.next()
                 stream.write(chunk.tostring(), num_frames=frames_per_buffer)
                 counter += 1
                 
         except StopIteration:
             pass
             
         finally:
             stream.stop_stream()
             
         self._logger.debug("chunks played  : %i"    %counter)
         self._logger.debug("samples played : %i"    %(counter*frames_per_buffer))
         self._logger.debug("duration       : %.3f"  %(counter*frames_per_buffer/x.fs))
         
     finally:
         self._logger.debug("play: close stream")
         stream.close()
         
     self._logger.info("play: done")
Esempio n. 2
0
 def setUp(self):
     self.x = Audio(fs=10, initialdata=np.zeros((10, 1)))
     self.x.samples[0] =  -1.0
     self.x.samples[1] =   1.0
     print(self.x)
     print(self.x.samples)
     print()
Esempio n. 3
0
 def play_rec(self, x, **kwargs):
     _Device.play_rec(self, x, **kwargs)
     self._logger.warn("*** Stub play_rec")
     
     # fake a signal with white noise
     n = Noise(channels=x.ch, fs=x.fs, nofsamples=x.nofsamples, gaindb=-60)
     n.convert_to_float(targetbits=32)
     y = Audio(fs=x.fs, initialdata=n.samples)
     return y
Esempio n. 4
0
 def test_int8(self):
     x = Audio(fs=10, initialdata=np.zeros((10, 1), dtype=np.int8))
     x.samples[0] = -128
     x.samples[1] =  127
     
     self.convert(x, targetbits=32)
     
     self.assertAlmostEqual(x.samples[0], -1.0,      places=20)
     self.assertAlmostEqual(x.samples[1], 127/128,   places=20)
Esempio n. 5
0
 def test_int16(self):
     x = Audio(fs=10, initialdata=np.zeros((10, 1), dtype=np.int16))
     x.samples[0] = -32768
     x.samples[1] =  32767
     
     self.convert(x, targetbits=32)
     
     self.assertAlmostEqual(x.samples[0], -1.0,          places=20)
     self.assertAlmostEqual(x.samples[1], 32767/32768,   places=20)
Esempio n. 6
0
 def rec(self, duration=None, channels=1, fs=96000, **kwargs):
     _Device.rec(self, duration=duration, channels=channels, fs=fs, **kwargs)
     self._logger.warn("*** Stub rec")
     
     # fake a signal with white noise
     n = Noise(channels=channels, fs=fs, duration=duration, gaindb=-60)
     n.convert_to_float(targetbits=32)
     y = Audio(fs=fs, initialdata=n.samples)
     return y
Esempio n. 7
0
 def test_int32(self):
     x = Audio(fs=10, initialdata=np.zeros((10, 1), dtype=np.int32))
     x.samples[0] = -2147483648
     x.samples[1] =  2147483647
     
     self.convert(x, targetbits=32)
     
     # resolution is lost in this conversion, however we should be close enough.
     self.assertAlmostEqual(x.samples[0], -1.0, places=20) # exact value
     self.assertAlmostEqual(x.samples[1],  1.0, places=20) # close enough
Esempio n. 8
0
 def check_values(self, values, expected, position):
     x = Audio(fs=10, initialdata=values)
     
     peak, idx = x.peak()
     self.assertTrue(len(peak)==1)
     self.assertTrue(len(idx)==1)
     
     print("index: %3i  peak: %f" %(idx, peak))
     print(x)
     
     self.assertAlmostEqual(peak, expected, places=3)
     self.assertEqual(idx, position)
Esempio n. 9
0
 def check_values(self, values, expected, position):
     x = Audio(fs=10, initialdata=values)
     
     peak, idx = x.peak()
     self.assertTrue(len(peak)==2)
     self.assertTrue(len(idx)==2)
     
     print("index: %s  peak: %s" %(idx, peak))
     print(x)
     
     self.assertAlmostEqual(peak[0], expected[0], places=3)
     self.assertAlmostEqual(peak[1], expected[1], places=3)
     
     self.assertEqual(idx[0], position[0])
     self.assertEqual(idx[1], position[1])
Esempio n. 10
0
 def test_set_samples(self):
     x = Audio(nofsamples=300, fs=600)
     print(x)
     self.assertAlmostEqual(x.duration, 0.5, places=7)
Esempio n. 11
0
 def setUp(self):
     self.x = Audio(channels=4)
     print(self.x)
Esempio n. 12
0
 def setUp(self):
     self.x = Audio()
     print(self.x)
Esempio n. 13
0
 def rec(self, duration=None, channels=1, fs=96000, frames_per_buffer=1024, dtype=np.float32):
     """Record. If dropouts or buffer underruns occur try different
     values for the frames_per_buffer variable."""
     
     _Device.rec(self, duration=duration, channels=channels, fs=fs)
     self._validate(frames_per_buffer)
     
     missing_frames = self._get_missing_frames(frames_per_buffer, int(duration*fs))
     
     nofsamples = missing_frames+int(duration*fs)
     
     rec = Audio(channels=channels, fs=fs, nofsamples=nofsamples, dtype=dtype)
     
     assert len(rec)%frames_per_buffer == 0
     
     stream = self.pa.open(format                = self._data_format(rec),
                           channels              = rec.ch,
                           rate                  = rec.fs,
                           frames_per_buffer     = frames_per_buffer,
                           input_device_index    = self._index_in,
                           input                 = True,
                           output                = False,
                           )
     try:
         self._logger.info("rec: start")
         counter = 0
         
         # split the audio into chunks the size of one buffer, so we can
         # iterate over the audio in chunksizes of the same size as one buffer
         it_in = iter(np.split(rec.samples, len(rec)/frames_per_buffer))
         try:
             while True:
                 chunk_in    = it_in.next()
                 raw_1d      = np.fromstring(stream.read(frames_per_buffer),
                                             dtype=rec.samples.dtype)
                 # because we use an iterator chunk_in is a sliding window in the rec variable
                 chunk_in[:] = raw_1d.reshape((frames_per_buffer, rec.ch))
                 
                 counter += 1
                 
         except StopIteration:
             pass
             
         finally:
             stream.stop_stream()
             
         self._logger.debug("chunks recorded : %i" %counter)
         self._logger.debug("samples recorded: %i" %(counter*frames_per_buffer))
         self._logger.debug("duration        : %.3f" %(counter*frames_per_buffer/rec.fs))
         
     finally:
         self._logger.debug("rec: close stream")
         stream.close()
         
     # remove the padding (empty frames) added to fill the last buffer. Trim
     # at the start, since we can treat that as latency.
     rec.trim(start=missing_frames, end=None)
     
     self._logger.debug("rec: trimmed %i samples from the start" %missing_frames)
     self._check_if_clipped(rec)
     self._logger.info("rec: done")
     
     return rec
Esempio n. 14
0
 def play_rec(self, x, frames_per_buffer=1024):
     """Play audio and record from input. If dropouts or buffer underruns occur
     try different values for the frames_per_buffer variable."""
     
     _Device.play_rec(self, x)
     self._validate(frames_per_buffer)
     
     missing_frames = self._get_missing_frames(frames_per_buffer, len(x))
     
     # generate silence to fill up missing frames
     pad = Audio(channels=x.ch, fs=x.fs, nofsamples=missing_frames, dtype=x.samples.dtype)
     
     # append the missing frames to a copy of the audio to be played. We now have
     # audio that can be split into complete (full) buffers
     cpy = Audio(fs=x.fs, initialdata=x.samples)
     cpy.concat(pad)
     
     assert len(cpy)%frames_per_buffer == 0
     
     rec = Audio(channels=cpy.ch, fs=cpy.fs, nofsamples=len(cpy), dtype=cpy.samples.dtype)
     
     stream = self.pa.open(format                = self._data_format(x),
                           channels              = x.ch,
                           rate                  = x.fs,
                           frames_per_buffer     = frames_per_buffer,
                           input_device_index    = self._index_in,
                           output_device_index   = self._index_out,
                           input                 = True,
                           output                = True,
                           )
     try:
         self._logger.info("play_rec: start")
         counter = 0
         
         # split the audio into chunks the size of one buffer, so we can
         # iterate over the audio in chunksizes of the same size as one buffer
         it_out = iter(np.split(cpy.samples, len(cpy)/frames_per_buffer))
         it_in  = iter(np.split(rec.samples, len(rec)/frames_per_buffer))
         try:
             while True:
                 chunk_out   = it_out.next()
                 chunk_in    = it_in.next()
                 
                 stream.write(chunk_out.tostring(), num_frames=frames_per_buffer)
                 
                 raw_1d      = np.fromstring(stream.read(frames_per_buffer),
                                             dtype=rec.samples.dtype)
                 # because we use an iterator chunk_in is a sliding window in the rec variable
                 chunk_in[:] = raw_1d.reshape((frames_per_buffer, rec.ch))
                 
                 counter += 1
                 
         except StopIteration:
             pass
             
         finally:
             stream.stop_stream()
             
         self._logger.debug("chunks played  : %i"    %counter)
         self._logger.debug("samples played : %i"    %(counter*frames_per_buffer))
         self._logger.debug("duration       : %.3f"  %(counter*frames_per_buffer/x.fs))
         
     finally:
         self._logger.debug("play_rec: close stream")
         stream.close()
         
     # remove the padding (empty frames) added to fill the last buffer. Trim
     # at the start, since we can treat that as latency.
     rec.trim(start=missing_frames, end=None)
     
     self._logger.debug("play_rec: trimmed %i samples from the start" %missing_frames)
     self._check_if_clipped(rec)
     self._logger.info("play_rec: done")
     
     return rec
Esempio n. 15
0
 def test_set_duration(self):
     x = Audio(duration=1.5, fs=600)
     print(x)
     self.assertEqual(len(x), 900)
Esempio n. 16
0
 def test_set_duration_and_channels(self):
     x = Audio(duration=1.5, fs=600, channels=5)
     print(x)
     self.assertEqual(len(x), 900)
Esempio n. 17
0
 def get_impulse(self, x):
     """Extract the impulse response. Returns an Audio instance.
     """
     imp = _MLS_base.get_impulse(self, x)
     y = Audio(fs=self.fs, initialdata=imp)
     return y