public override int Read(byte[] buffer, int offset, int count) { int totalBytesRead = 0; if (phase == 0) { int bytesRequired = Math.Min(count - totalBytesRead, delayBytes - phasePos); for (int i = 0; i < bytesRequired; i++) { buffer[offset + totalBytesRead + i] = 0; } phasePos += bytesRequired; totalBytesRead += bytesRequired; if (phasePos >= delayBytes) // finished playing delay { phasePos = 0; phase = 1; } } if (phase == 1) { int bytesRequired = Math.Min(count - totalBytesRead, spByteLength - phasePos); for (int i = 0; i < bytesRequired; i++) { buffer[offset + totalBytesRead + i] = 0; } phasePos += bytesRequired; totalBytesRead += bytesRequired; if (phasePos >= spByteLength) // finished playing delay { phasePos = 0; phase = 2; } } if (phase == 2) { int bytesRequired = Math.Min(count - totalBytesRead, delayBytes - phasePos); for (int i = 0; i < bytesRequired; i++) { buffer[offset + totalBytesRead + i] = 0; } phasePos += bytesRequired; totalBytesRead += bytesRequired; if (phasePos >= delayBytes) // finished playing delay { phasePos = 0; phase = 3; } } if (phase == 3) { while (totalBytesRead < count) { int bytesRead = sound.Read(buffer, offset + totalBytesRead, count - totalBytesRead); if (bytesRead == 0) { phase = 4; break; } totalBytesRead += bytesRead; } } if (phase == 4) { for (int i = totalBytesRead; i < count - totalBytesRead; i++) { buffer[i] = 0; } totalBytesRead += count - totalBytesRead; phase = 5; } return(totalBytesRead); }
/// <summary> /// NoiseStream will keep looping until being stopped from outside /// </summary> public override int Read(byte[] buffer, int offset, int count) { int totalBytesRead = 0; if (phase == 0) { int bytesRequired = Math.Min(count - totalBytesRead, delayBytes - phasePos); for (int i = 0; i < bytesRequired; i++) { buffer[offset + totalBytesRead + i] = 0; } phasePos += bytesRequired; totalBytesRead += bytesRequired; if (phasePos >= delayBytes) // finished playing delay { phasePos = 0; phase = 1; } } if (phase == 1) { int bytesRequired = Math.Min(count - totalBytesRead, spByteLength - phasePos); for (int i = 0; i < bytesRequired; i++) { buffer[offset + totalBytesRead + i] = 0; } phasePos += bytesRequired; totalBytesRead += bytesRequired; if (phasePos >= spByteLength) // finished playing delay { phasePos = 0; phase = 2; } } if (phase == 2) { int bytesRequired = Math.Min(count - totalBytesRead, delayBytes_afterSP - phasePos); for (int i = 0; i < bytesRequired; i++) { buffer[offset + totalBytesRead + i] = 0; } phasePos += bytesRequired; totalBytesRead += bytesRequired; if (phasePos >= delayBytes_afterSP) // finished playing delay { phasePos = 0; phase = 3; } } if (phase == 3) { while (totalBytesRead < count) { int bytesRead = sound.Read(buffer, offset + totalBytesRead, count - totalBytesRead); if (bytesRead == 0) { if (sound.Position == 0) { // something wrong with the source stream throw new ArgumentException("something wrong"); } sound.Position = 0; } totalBytesRead += bytesRead; } } return(totalBytesRead); }