/// <summary> /// Positions the stream at the start of the next frame. /// </summary> /// <param name="syncWord"></param> /// <returns></returns> private void SeekNextFrame(SyncWord syncWord) { const int bytesPerSample = 4; const int bytesPerWord = 2; const int channelCount = 2; long streamLength = Bass.BASS_ChannelGetLength(_handle); long currentPosition = 0; if (streamLength > 0) { currentPosition = Bass.BASS_ChannelGetPosition(_handle); } SyncFifoBuffer syncFifoBuffer = new SyncFifoBuffer(syncWord); float[] readBuffer = new float[channelCount]; bool success = false; int sampleIndex = 0; int maxSampleIndex = (syncWord.MaxFrameSize / bytesPerWord) + syncWord.WordLength; while (!success && sampleIndex < maxSampleIndex) { // For float streams we get one float value for each 16bit word int bytesRead = Bass.BASS_ChannelGetData(_handle, readBuffer, readBuffer.Length * bytesPerSample); if (bytesRead <= 0) { // End of stream break; } int samplesRead = bytesRead / bytesPerSample; int readSample = 0; while (!success && readSample < samplesRead) { // Convert float value to word UInt16 word = (UInt16)(readBuffer[readSample] * 32768); // Add word to fifo buffer syncFifoBuffer.Write(word); // Check Sync word if (syncFifoBuffer.IsMatch()) { long pos = currentPosition + (sampleIndex - syncWord.WordLength + 1) * bytesPerWord; Bass.BASS_ChannelSetPosition(_handle, pos); success = true; } sampleIndex++; readSample++; } } if (!success && streamLength > 0) { Bass.BASS_ChannelSetPosition(_handle, currentPosition); } }
public SyncFifoBuffer(SyncWord syncWord) { _syncWord = syncWord; _buffer = new UInt16[syncWord.WordLength]; }
public SyncFifoBuffer(SyncWord syncWord) { _syncWord = syncWord; _buffer = new UInt16[syncWord.WordLength]; }
/// <summary> /// Determines if the stream contains frames with the specified syncwords. /// </summary> /// <param name="syncWord">Syncword to search for.</param> /// <returns></returns> private bool IsEncoded(SyncWord syncWord) { const int framesToCheck = 5; const int bytesPerSample = 4; const int bytesPerWord = 2; const int channelCount = 2; long streamLength = Bass.BASS_ChannelGetLength(_handle); long currentPosition = 0; if (streamLength > 0) { currentPosition = Bass.BASS_ChannelGetPosition(_handle); if (currentPosition != 0) { Bass.BASS_ChannelSetPosition(_handle, 0); } } SyncFifoBuffer syncFifoBuffer = new SyncFifoBuffer(syncWord); float[] readBuffer = new float[channelCount]; int lastSyncWordPosition = -1; int lastFrameSize = -1; int frameCount = 0; bool result = false; int sampleIndex = 0; int maxSampleIndex = (syncWord.MaxFrameSize / bytesPerWord) * framesToCheck + syncWord.WordLength; while (!result && sampleIndex < maxSampleIndex) { int bytesRead = Bass.BASS_ChannelGetData(_handle, readBuffer, readBuffer.Length * bytesPerSample); if (bytesRead <= 0) { // End of stream break; } int samplesRead = bytesRead / bytesPerSample; int readSample = 0; while (!result && readSample < samplesRead) { // Convert float value to word UInt16 word = (UInt16)(readBuffer[readSample] * 32768); // Add word to fifo buffer syncFifoBuffer.Write(word); // Check Sync word if (syncFifoBuffer.IsMatch()) { int newSyncWordPosition = (sampleIndex - syncWord.WordLength + 1) * bytesPerWord; if (lastSyncWordPosition != -1) { int thisFrameSize = newSyncWordPosition - lastSyncWordPosition; if (lastFrameSize != -1) { if (thisFrameSize != lastFrameSize) { break; } } lastFrameSize = thisFrameSize; frameCount++; } lastSyncWordPosition = newSyncWordPosition; result = (frameCount == framesToCheck); } sampleIndex++; readSample++; } } if (streamLength > 0) { Bass.BASS_ChannelSetPosition(_handle, currentPosition); } return(result); }
/// <summary> /// Positions the stream at the start of the next frame. /// </summary> /// <param name="syncWord"></param> /// <returns></returns> private void SeekNextFrame(SyncWord syncWord) { const int bytesPerSample = 4; const int bytesPerWord = 2; const int channelCount = 2; long streamLength = Bass.BASS_ChannelGetLength(_handle); long currentPosition = 0; if (streamLength > 0) currentPosition = Bass.BASS_ChannelGetPosition(_handle); SyncFifoBuffer syncFifoBuffer = new SyncFifoBuffer(syncWord); float[] readBuffer = new float[channelCount]; bool success = false; int sampleIndex = 0; int maxSampleIndex = (syncWord.MaxFrameSize / bytesPerWord) + syncWord.WordLength; while (!success && sampleIndex < maxSampleIndex) { // For float streams we get one float value for each 16bit word int bytesRead = Bass.BASS_ChannelGetData(_handle, readBuffer, readBuffer.Length * bytesPerSample); if (bytesRead <= 0) // End of stream break; int samplesRead = bytesRead / bytesPerSample; int readSample = 0; while (!success && readSample < samplesRead) { // Convert float value to word UInt16 word = (UInt16)(readBuffer[readSample] * 32768); // Add word to fifo buffer syncFifoBuffer.Write(word); // Check Sync word if (syncFifoBuffer.IsMatch()) { long pos = currentPosition + (sampleIndex - syncWord.WordLength + 1) * bytesPerWord; Bass.BASS_ChannelSetPosition(_handle, pos); success = true; } sampleIndex++; readSample++; } } if (!success && streamLength > 0) Bass.BASS_ChannelSetPosition(_handle, currentPosition); }
/// <summary> /// Determines if the stream contains frames with the specified syncwords. /// </summary> /// <param name="syncWord">Syncword to search for.</param> /// <returns></returns> private bool IsEncoded(SyncWord syncWord) { const int framesToCheck = 5; const int bytesPerSample = 4; const int bytesPerWord = 2; const int channelCount = 2; long streamLength = Bass.BASS_ChannelGetLength(_handle); long currentPosition = 0; if (streamLength > 0) { currentPosition = Bass.BASS_ChannelGetPosition(_handle); if (currentPosition != 0) Bass.BASS_ChannelSetPosition(_handle, 0); } SyncFifoBuffer syncFifoBuffer = new SyncFifoBuffer(syncWord); float[] readBuffer = new float[channelCount]; int lastSyncWordPosition = -1; int lastFrameSize = -1; int frameCount = 0; bool result = false; int sampleIndex = 0; int maxSampleIndex = (syncWord.MaxFrameSize / bytesPerWord) * framesToCheck + syncWord.WordLength; while (!result && sampleIndex < maxSampleIndex) { int bytesRead = Bass.BASS_ChannelGetData(_handle, readBuffer, readBuffer.Length * bytesPerSample); if (bytesRead <= 0) // End of stream break; int samplesRead = bytesRead / bytesPerSample; int readSample = 0; while (!result && readSample < samplesRead) { // Convert float value to word UInt16 word = (UInt16)(readBuffer[readSample] * 32768); // Add word to fifo buffer syncFifoBuffer.Write(word); // Check Sync word if (syncFifoBuffer.IsMatch()) { int newSyncWordPosition = (sampleIndex - syncWord.WordLength + 1) * bytesPerWord; if (lastSyncWordPosition != -1) { int thisFrameSize = newSyncWordPosition - lastSyncWordPosition; if (lastFrameSize != -1) { if (thisFrameSize != lastFrameSize) break; } lastFrameSize = thisFrameSize; frameCount++; } lastSyncWordPosition = newSyncWordPosition; result = (frameCount == framesToCheck); } sampleIndex++; readSample++; } } if (streamLength > 0) Bass.BASS_ChannelSetPosition(_handle, currentPosition); return result; }