def test_music_delivery_callback(self, lib_mock): sp_audioformat = spotify.ffi.new('sp_audioformat *') sp_audioformat.channels = 2 audio_format = spotify.AudioFormat(sp_audioformat) num_frames = 10 frames_size = audio_format.frame_size() * num_frames frames = spotify.ffi.new('char[]', frames_size) frames[0:3] = [b'a', b'b', b'c'] frames_void_ptr = spotify.ffi.cast('void *', frames) callback = mock.Mock() callback.return_value = num_frames session = tests.create_real_session(lib_mock) session.on('music_delivery', callback) result = _SessionCallbacks.music_delivery(session._sp_session, sp_audioformat, frames_void_ptr, num_frames) callback.assert_called_once_with(session, mock.ANY, mock.ANY, num_frames) self.assertEqual(callback.call_args[0][1]._sp_audioformat, sp_audioformat) self.assertEqual(callback.call_args[0][2][:5], b'abc\x00\x00') self.assertEqual(result, num_frames)
def setUp(self): self._sp_audioformat = spotify.ffi.new('sp_audioformat *') self._sp_audioformat.sample_type = ( spotify.SampleType.INT16_NATIVE_ENDIAN) self._sp_audioformat.sample_rate = 44100 self._sp_audioformat.channels = 2 self.audio_format = spotify.AudioFormat(self._sp_audioformat)
def music_delivery(sp_session, sp_audioformat, frames, num_frames): if not spotify.session_instance: return 0 if spotify.session_instance.num_listeners( SessionEvent.MUSIC_DELIVERY) == 0: logger.debug('Got music delivery, but no event listener') return 0 logger.debug('Got music delivery of %d frames', num_frames) audio_format = spotify.AudioFormat(sp_audioformat) buffer_ = ffi.buffer(frames, audio_format.frame_size() * num_frames) frames_bytes = buffer_[:] return spotify.session_instance.call(SessionEvent.MUSIC_DELIVERY, spotify.session_instance, audio_format, frames_bytes, num_frames)
def music_delivery(sp_session, sp_audioformat, frames, num_frames): if not spotify._session_instance: return 0 if spotify._session_instance.num_listeners( SessionEvent.MUSIC_DELIVERY) == 0: logger.debug('Music delivery, but no listener') return 0 audio_format = spotify.AudioFormat(sp_audioformat) frames_buffer = ffi.buffer(frames, audio_format.frame_size() * num_frames) frames_bytes = frames_buffer[:] num_frames_consumed = spotify._session_instance.call( SessionEvent.MUSIC_DELIVERY, spotify._session_instance, audio_format, frames_bytes, num_frames) logger.debug('Music delivery of %d frames, %d consumed', num_frames, num_frames_consumed) return num_frames_consumed