36 {
37 int blockAlign = stream.BitsPerSample * stream.Channels / 8;
38 int samplePos = stream.SamplePosition;
39 int sampleCount = _sampleLength;
40 int samplesRead;
41 bool end = false;
42
43 loop = loop && stream.IsLooping;
44 int lastSample = loop ? stream.LoopEndSample : stream.Samples;
45
46 VoidPtr blockAddr = _part1Address;
47 int blockRemaining = _part1Samples;
48
49 while (sampleCount > 0)
50 {
51
52 int blockSamples = Math.Min(blockRemaining, sampleCount);
53
54
55 if (end)
56 {
57 Memory.Fill(blockAddr, (uint) (blockSamples * blockAlign), 0);
58 }
59 else
60 {
61
62 if (samplePos <= lastSample && lastSample < samplePos + blockSamples)
63 {
64 blockSamples = lastSample - samplePos;
65 end = true;
66 }
67
68 samplesRead = stream.ReadSamples(blockAddr, blockSamples);
69 samplePos += samplesRead;
70
71 if (samplesRead < blockSamples)
72 {
73 blockSamples = samplesRead;
74 end = true;
75 }
76 else if (loop && end)
77 {
78 stream.Wrap();
79 if (samplePos == stream.SamplePosition)
80 {
81 samplePos = -1;
82 break;
83 }
84
85 samplePos = stream.SamplePosition;
86 end = false;
87 }
88 }
89
90 blockAddr += blockSamples * blockAlign;
91 blockRemaining -= blockSamples;
92
93
94 if (blockRemaining <= 0)
95 {
96 blockAddr = _part2Address;
97 blockRemaining = _part2Samples;
98 }
99
100 sampleCount -= blockSamples;
101 }
102 }