@@ -67,20 +67,20 @@ void DataQueue::setChannelCount (int nChans)
6767 m_readSamples.clear ();
6868 m_numChans = nChans;
6969 m_sampleNumbers.clear ();
70- m_lastReadSampleNumbers. clear () ;
70+ m_lastReadSampleNumber = 0 ;
7171
7272 for (int i = 0 ; i < nChans; ++i)
7373 {
7474 m_fifos.add (new AbstractFifo (m_maxSize));
7575 m_readSamples.push_back (0 );
76- m_sampleNumbers. add ( new std::vector<int64>());
76+ }
7777
78- for (int j = 0 ; j < m_numBlocks; j++)
79- {
80- m_sampleNumbers.getLast ()->push_back (0 );
81- }
82- m_lastReadSampleNumbers.push_back (0 );
78+ // Initialize per-stream sample numbers (one per block)
79+ for (int j = 0 ; j < m_numBlocks; j++)
80+ {
81+ m_sampleNumbers.push_back (0 );
8382 }
83+
8484 m_buffer.setSize (nChans, m_maxSize);
8585}
8686
@@ -98,10 +98,12 @@ void DataQueue::resize (int nBlocks)
9898 m_fifos[i]->setTotalSize (size);
9999 m_fifos[i]->reset ();
100100 m_readSamples[i] = 0 ;
101- m_sampleNumbers[i]->resize (nBlocks);
102- m_lastReadSampleNumbers[i] = 0 ;
103101 }
104102
103+ // Resize per-stream sample numbers
104+ m_sampleNumbers.resize (nBlocks);
105+ m_lastReadSampleNumber = 0 ;
106+
105107 m_readFTSSamples.clear ();
106108
107109 for (int i = 0 ; i < m_numFTSChans; ++i)
@@ -114,7 +116,7 @@ void DataQueue::resize (int nBlocks)
114116 m_FTSBuffer.setSize (m_numFTSChans, size);
115117}
116118
117- void DataQueue::fillSampleNumbers (int channel, int index, int size, int64 sampleNumber)
119+ void DataQueue::fillSampleNumbers (int index, int size, int64 sampleNumber)
118120{
119121 // Search for the next block start.
120122 int blockMod = index % m_blockSize;
@@ -142,7 +144,7 @@ void DataQueue::fillSampleNumbers (int channel, int index, int size, int64 sampl
142144 if ((blockStartPos + i) < (index + size))
143145 {
144146 latestSampleNumber = startSampleNumber + (i * m_blockSize);
145- m_sampleNumbers[channel]-> at ( blockIdx) = latestSampleNumber;
147+ m_sampleNumbers[blockIdx] = latestSampleNumber;
146148 }
147149 }
148150}
@@ -202,7 +204,11 @@ float DataQueue::writeChannel (const AudioBuffer<float>& buffer,
202204 0 ,
203205 size1);
204206
205- fillSampleNumbers (destChannel, index1, size1, sampleNumber);
207+ // Only fill sample numbers once per stream (using first channel write)
208+ if (destChannel == 0 )
209+ {
210+ fillSampleNumbers (index1, size1, sampleNumber);
211+ }
206212
207213 if (size2 > 0 )
208214 {
@@ -213,7 +219,10 @@ float DataQueue::writeChannel (const AudioBuffer<float>& buffer,
213219 size1,
214220 size2);
215221
216- fillSampleNumbers (destChannel, index2, size2, sampleNumber + size1);
222+ if (destChannel == 0 )
223+ {
224+ fillSampleNumbers (index2, size2, sampleNumber + size1);
225+ }
217226 }
218227 m_fifos[destChannel]->finishedWrite (size1 + size2);
219228
@@ -239,11 +248,11 @@ float DataQueue::writeAllChannels (const AudioBuffer<float>& buffer,
239248
240249 float maxUsage = 0 .0f ;
241250
242- // Fill sample numbers once for channel 0
243- fillSampleNumbers (0 , index1, size1, sampleNumber);
251+ // Fill sample numbers once for the stream (not per-channel)
252+ fillSampleNumbers (index1, size1, sampleNumber);
244253 if (size2 > 0 )
245254 {
246- fillSampleNumbers (0 , index2, size2, sampleNumber + size1);
255+ fillSampleNumbers (index2, size2, sampleNumber + size1);
247256 }
248257
249258 // Batch copy and update all channels
@@ -260,14 +269,6 @@ float DataQueue::writeAllChannels (const AudioBuffer<float>& buffer,
260269 m_buffer.copyFrom (destChannel, index2, buffer, srcChannel, size1, size2);
261270 }
262271
263- // Copy sample numbers from channel 0 (faster than calling fillSampleNumbers for each)
264- if (destChannel > 0 )
265- {
266- std::memcpy (m_sampleNumbers[destChannel]->data (),
267- m_sampleNumbers[0 ]->data (),
268- m_numBlocks * sizeof (int64));
269- }
270-
271272 // Update FIFO state - need to call prepareToWrite for channels > 0
272273 if (destChannel > 0 )
273274 {
@@ -278,7 +279,8 @@ float DataQueue::writeAllChannels (const AudioBuffer<float>& buffer,
278279 }
279280
280281 // Return usage from last channel (all should be the same)
281- return 1 .0f - (float ) m_fifos[m_numChans - 1 ]->getFreeSpace () / (float ) m_fifos[m_numChans - 1 ]->getTotalSize ();
282+ const float usage = 1 .0f - (float ) m_fifos[m_numChans - 1 ]->getFreeSpace () / (float ) m_fifos[m_numChans - 1 ]->getTotalSize ();
283+ return usage;
282284}
283285
284286/*
@@ -300,7 +302,7 @@ const SynchronizedTimestampBuffer& DataQueue::getTimestampBufferReference() cons
300302
301303bool DataQueue::startRead (std::vector<CircularBufferIndexes>& dataBufferIdxs,
302304 std::vector<CircularBufferIndexes>& timestampBufferIdxs,
303- Array< int64>& sampleNumbers ,
305+ int64& sampleNumber ,
304306 int nMax)
305307{
306308 // This should never happen, but it never hurts to be on the safe side.
@@ -344,37 +346,39 @@ bool DataQueue::startRead (std::vector<CircularBufferIndexes>& dataBufferIdxs,
344346 idx.size2 = 0 ;
345347 m_readFTSSamples[chan] = 0 ;
346348 }
349+ sampleNumber = m_lastReadSampleNumber;
347350 m_readInProgress = false ;
348351 return false ;
349352 }
350353
351- // Second pass: read the same number of samples from all channels
352- for ( int chan = 0 ; chan < m_numChans; ++chan)
353- {
354- CircularBufferIndexes& idx = dataBufferIdxs[chan] ;
354+ // Get sample number for the stream (using first channel's indices)
355+ CircularBufferIndexes& firstIdx = dataBufferIdxs[ 0 ];
356+ m_fifos. getUnchecked ( 0 )-> prepareToRead (samplesToRead, firstIdx. index1 , firstIdx. size1 , firstIdx. index2 , firstIdx. size2 );
357+ m_readSamples[ 0 ] = firstIdx. size1 + firstIdx. size2 ;
355358
356- m_fifos. getUnchecked (chan)-> prepareToRead (samplesToRead, idx. index1 , idx. size1 , idx. index2 , idx. size2 ) ;
357- m_readSamples[chan] = idx. size1 + idx. size2 ;
359+ int blockMod = firstIdx. index1 % m_blockSize ;
360+ int blockDiff = (blockMod == 0 ) ? 0 : (m_blockSize - blockMod) ;
358361
359- int blockMod = idx.index1 % m_blockSize;
360- int blockDiff = (blockMod == 0 ) ? 0 : (m_blockSize - blockMod);
362+ // If the next sample number block is within the data we're reading, include the translated sample number
363+ if (blockDiff < (firstIdx.size1 + firstIdx.size2 ))
364+ {
365+ int blockIdx = ((firstIdx.index1 + blockDiff) / m_blockSize) % m_numBlocks;
366+ sampleNumber = m_sampleNumbers[blockIdx] - blockDiff;
367+ }
368+ else
369+ {
370+ // If not, use the last sent sample number
371+ sampleNumber = m_lastReadSampleNumber;
372+ }
361373
362- // If the next sample number block is within the data we're reading, include the translated sample number in the output
363- int64 sampleNum;
374+ m_lastReadSampleNumber = sampleNumber + firstIdx.size1 + firstIdx.size2 ;
364375
365- if (blockDiff < (idx.size1 + idx.size2 ))
366- {
367- int blockIdx = ((idx.index1 + blockDiff) / m_blockSize) % m_numBlocks;
368- sampleNum = m_sampleNumbers[chan]->at (blockIdx) - blockDiff;
369- }
370- // If not, copy the last sent again
371- else
372- {
373- sampleNum = m_lastReadSampleNumbers[chan];
374- }
375-
376- sampleNumbers.set (chan, sampleNum); // expensive operation?
377- m_lastReadSampleNumbers[chan] = sampleNum + idx.size1 + idx.size2 ;
376+ // Read remaining channels with same parameters
377+ for (int chan = 1 ; chan < m_numChans; ++chan)
378+ {
379+ CircularBufferIndexes& idx = dataBufferIdxs[chan];
380+ m_fifos.getUnchecked (chan)->prepareToRead (samplesToRead, idx.index1 , idx.size1 , idx.index2 , idx.size2 );
381+ m_readSamples[chan] = idx.size1 + idx.size2 ;
378382 }
379383
380384 // Also find minimum for timestamp streams and read consistently
@@ -418,11 +422,7 @@ void DataQueue::stopRead()
418422 m_readInProgress = false ;
419423}
420424
421- void DataQueue::getSampleNumbersForBlock (int idx, Array<int64>& sampleNumbers ) const
425+ int64 DataQueue::getSampleNumberForBlock (int idx) const
422426{
423- sampleNumbers.clear ();
424- for (int chan = 0 ; chan < m_numChans; ++chan)
425- {
426- sampleNumbers.add ((*m_sampleNumbers[chan])[idx]);
427- }
427+ return m_sampleNumbers[idx];
428428}
0 commit comments