private static bool IsEncoded(int stream, SyncWord syncWord) { const int framesToCheck = 5; const int bytesPerSample = 4; const int bytesPerWord = 2; const int channelCount = 2; long streamLength = Bass.BASS_ChannelGetLength(stream); long currentPosition = 0; if (streamLength > 0) { currentPosition = Bass.BASS_ChannelGetPosition(stream); if (currentPosition != 0) Bass.BASS_ChannelSetPosition(stream, 0); } SyncFifoBuffer syncFifoBuffer = new SyncFifoBuffer(syncWord); float[] readBuffer = new float[channelCount]; int lastSyncWordPosition = -1; int lastFrameSize = -1; int frameCount = 0; bool result = false; bool endOfStream = false; int sampleIndex = 0; int maxSampleIndex = (syncWord.MaxFrameSize / bytesPerWord) * framesToCheck + syncWord.WordLength; while (!result && !endOfStream && sampleIndex < maxSampleIndex) { int bytesRead = Bass.BASS_ChannelGetData(stream, readBuffer, readBuffer.Length * bytesPerSample); endOfStream = bytesRead <= 0; if (!endOfStream) { 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++; } } else endOfStream = true; } if (streamLength > 0) Bass.BASS_ChannelSetPosition(stream, currentPosition); return result; }
private static bool SyncToWord(int stream, SyncWord syncWord) { const int bytesPerSample = 4; const int bytesPerWord = 2; const int channelCount = 2; long streamLength = Bass.BASS_ChannelGetLength(stream); long currentPosition = 0; if (streamLength > 0) currentPosition = Bass.BASS_ChannelGetPosition(stream); SyncFifoBuffer syncFifoBuffer = new SyncFifoBuffer(syncWord); float[] readBuffer = new float[channelCount]; bool result = false; bool endOfStream = false; int sampleIndex = 0; int maxSampleIndex = (syncWord.MaxFrameSize / bytesPerWord) + syncWord.WordLength; while (!result && !endOfStream && sampleIndex < maxSampleIndex) { // For float streams we get one float value for each 16bit word int bytesRead = Bass.BASS_ChannelGetData(stream, readBuffer, readBuffer.Length * bytesPerSample); endOfStream = bytesRead <= 0; if (!endOfStream) { 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()) { long pos = currentPosition + (sampleIndex - syncWord.WordLength + 1) * bytesPerWord; //Log.Debug("PureAudio: Sync to next frame: changing position from {0} to {1}", currentPosition, pos); Bass.BASS_ChannelSetPosition(stream, pos); result = true; } sampleIndex++; readSample++; } } } if (!result && streamLength > 0) Bass.BASS_ChannelSetPosition(stream, currentPosition); return result; }