Skip to content

Commit

Permalink
stimuli: retain provenance in playlist when play_item
Browse files Browse the repository at this point in the history
ref: #16

be consistent with the playlist case and set the producer_playlist_n
field also when extracting the single `AudioStim.data_generator`
via `Playlist.play_item`
  • Loading branch information
nzjrs committed Feb 11, 2022
1 parent 650bb60 commit 5722d59
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 8 additions & 5 deletions flyvr/audio/stimuli.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,17 @@ def describe(self):
'max_value': self.__max_value,
'min_value': self.__min_value}

def data_generator(self, producer_instance_n_override=None) -> Iterator[Optional[SampleChunk]]:
def data_generator(self, producer_instance_n_override=None, producer_playlist_n_override=None) -> Iterator[Optional[SampleChunk]]:
"""
Return a generator that yields the data member when next is called on it. Simply provides another interface to
the same data stored in the data member.
"""
while True:
self.num_samples_generated = self.num_samples_generated + self.data.shape[0]
chunk = SampleChunk(data=self.data, producer_identifier=self.identifier,
producer_instance_n=producer_instance_n_override or self.producer_instance_n)
producer_instance_n=producer_instance_n_override if producer_instance_n_override is not None else self.producer_instance_n,
producer_playlist_n=producer_playlist_n_override if producer_playlist_n_override is not None else -1)

self.trigger_next_callback(chunk)
yield chunk

Expand Down Expand Up @@ -865,16 +867,17 @@ def from_playlist_definition(cls, stim_playlist, basedirs, paused_fallback, defa
attenuator=attenuator)

def play_item(self, identifier):
for stim in self._stims:
for n, stim in enumerate(self._stims):
if stim.identifier == identifier:
SignalProducer.instances_created += 1
return stim.data_generator(-100 - SignalProducer.instances_created)
return stim.data_generator(producer_instance_n_override=-100 - SignalProducer.instances_created,
producer_playlist_n_override=n)
raise ValueError('%s not found' % identifier)

def play_pause(self, pause):
self.paused = pause

def data_generator(self, producer_instance_n_override=None) -> Iterator[Optional[SampleChunk]]:
def data_generator(self) -> Iterator[Optional[SampleChunk]]:
"""
Return a generator that yields each AudioStim in the playlist in succession. If shuffle_playback is set to true
then we will get a non-repeating randomized sequence of all stimuli, then they will be shuffled, and the process
Expand Down
12 changes: 12 additions & 0 deletions tests/audio/test_samplechunks.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,26 +318,31 @@ def test_play_item_produces_new_instance_chunk(stimplaylist):
# array in a loop, aka from the same chunk producer
ca0 = next(a)
assert ca0.producer_identifier == 'sin10hz'
assert ca0.producer_playlist_n == 0
assert ca0.data.shape == (1200,)
ca1 = next(a)
assert ca1.data.shape == (1200,)
assert ca1.producer_identifier == 'sin10hz'
assert ca1.producer_playlist_n == 0
assert not chunk_producers_differ(ca0, ca1)
ca2 = next(a)
assert ca2.data.shape == (1200,)
assert ca2.producer_identifier == 'sin10hz'
assert ca2.producer_playlist_n == 0
assert not chunk_producers_differ(ca0, ca2)

# first chunk on different AudioStim from the previous play_item
cb0 = next(b)
assert cb0.data.shape == (1200,)
assert cb0.producer_identifier == 'constant1'
assert cb0.producer_playlist_n == 1
assert chunk_producers_differ(ca0, cb0)

# first chunk on same AudioStim from the first sin10hz
cc0 = next(c)
assert cc0.data.shape == (1200,)
assert cc0.producer_identifier == 'sin10hz'
assert cc0.producer_playlist_n == 0
assert chunk_producers_differ(ca0, cc0)


Expand All @@ -348,35 +353,42 @@ def test_play_item_produces_new_instance_chunk_chunker(stimplaylist):

ca0 = next(a)
assert ca0.producer_identifier == 'sin10hz'
assert ca0.producer_playlist_n == 0
assert ca0.data.shape == (600,)
ca1 = next(a)
assert ca1.producer_identifier == 'sin10hz'
assert ca1.producer_playlist_n == 0
assert ca1.data.shape == (600,)
assert not chunk_producers_differ(ca0, ca1)
# loops back round to the start
ca2 = next(a)
assert ca2.producer_identifier == 'sin10hz'
assert ca2.producer_playlist_n == 0
assert ca2.data.shape == (600,)
assert not chunk_producers_differ(ca1, ca2)
assert not chunk_producers_differ(ca0, ca2)

cb0 = next(b)
assert cb0.producer_identifier == 'constant1'
assert cb0.producer_playlist_n == 1
assert cb0.data.shape == (600,)

assert chunk_producers_differ(ca0, cb0)
assert chunk_producers_differ(ca2, cb0)

cc0 = next(c)
assert cc0.producer_identifier == 'sin10hz'
assert cc0.producer_playlist_n == 0
assert cc0.data.shape == (600,)
cc1 = next(c)
assert cc1.producer_identifier == 'sin10hz'
assert cc1.producer_playlist_n == 0
assert cc1.data.shape == (600,)
assert not chunk_producers_differ(cc0, cc1)
# loops back round to the start
cc2 = next(c)
assert cc2.producer_identifier == 'sin10hz'
assert cc2.producer_playlist_n == 0
assert cc2.data.shape == (600,)
assert not chunk_producers_differ(cc1, cc2)
assert not chunk_producers_differ(cc0, cc2)
Expand Down

0 comments on commit 5722d59

Please sign in to comment.