| @@ -20,11 +20,6 @@ namespace ScreamReader · 19 unmodified lines |
| 20 | 20 | /// The port scream is broadcasting on. |
| 21 | 21 | /// </summary> |
| 22 | 22 | public static readonly int ScreamMulticastPort = 4010; |
| 23 | | − |
| 24 | | − /// <summary> |
| 25 | | − /// Default <see cref="waveFormat"/> is 44.1 kHz, 16 bits, stereo. |
| 26 | | − /// </summary> |
| 27 | | − public static readonly WaveFormat ScreamWaveFormat = new WaveFormat(); |
| 28 | 23 | #endregion |
| 29 | 24 | |
| 30 | 25 | #region instance variables |
| @@ -37,12 +32,7 @@ namespace ScreamReader · 6 unmodified lines |
| 37 | 32 | /// The port to listen to. |
| 38 | 33 | /// </summary> |
| 39 | 34 | protected int multicastPort { get; set; } |
| 40 | | − |
| 41 | | − /// <summary> |
| 42 | | − /// The used <see cref="waveFormat"/>. |
| 43 | | − /// </summary> |
| 44 | | − protected WaveFormat waveFormat { get; set; } |
| 45 | | − |
| 35 | + |
| 46 | 36 | private Semaphore startLock; |
| 47 | 37 | |
| 48 | 38 | private Semaphore shutdownLock; |
| @@ -81,7 +71,7 @@ namespace ScreamReader · 32 unmodified lines |
| 81 | 71 | /// Default c'tor that supports Scream's default settings. |
| 82 | 72 | /// </summary> |
| 83 | 73 | public UdpWaveStreamPlayer() |
| 84 | | − : this(ScreamMulticastAddress, ScreamMulticastPort, ScreamWaveFormat) |
| 74 | + : this(ScreamMulticastAddress, ScreamMulticastPort) |
| 85 | 75 | { |
| 86 | 76 | } |
| 87 | 77 | |
| @@ -90,26 +80,18 @@ namespace ScreamReader · 2 unmodified lines |
| 90 | 80 | /// </summary> |
| 91 | 81 | /// <param name="multicastAddress"></param> |
| 92 | 82 | /// <param name="multicastPort"></param> |
| 93 | | − /// <param name="waveFormat"></param> |
| 94 | | − public UdpWaveStreamPlayer(IPAddress multicastAddress, int multicastPort, WaveFormat waveFormat) |
| 83 | + public UdpWaveStreamPlayer(IPAddress multicastAddress, int multicastPort) |
| 95 | 84 | { |
| 96 | 85 | this.multicastAddress = multicastAddress; |
| 97 | 86 | this.multicastPort = multicastPort; |
| 98 | | − this.waveFormat = waveFormat; |
| 99 | 87 | |
| 100 | 88 | this.startLock = new Semaphore(1, 1); |
| 101 | 89 | this.shutdownLock = new Semaphore(0, 1); |
| 102 | | − this.output = new WaveOut() |
| 103 | | − { |
| 104 | | − DesiredLatency = 80 |
| 105 | | − }; |
| 106 | | − |
| 90 | + |
| 107 | 91 | this.udpClient = new UdpClient |
| 108 | 92 | { |
| 109 | 93 | ExclusiveAddressUse = false |
| 110 | 94 | }; |
| 111 | | − |
| 112 | | − this.Volume = 100; |
| 113 | 95 | } |
| 114 | 96 | |
| 115 | 97 | /// <summary> |
| @@ -123,17 +105,21 @@ namespace ScreamReader · 7 unmodified lines |
| 123 | 105 | |
| 124 | 106 | Task.Factory.StartNew(() => |
| 125 | 107 | { |
| 108 | + var currentRate = 129; |
| 109 | + var currentWidth = 16; |
| 126 | 110 | var localEp = new IPEndPoint(IPAddress.Any, this.multicastPort); |
| 127 | 111 | |
| 128 | 112 | this.udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); |
| 129 | 113 | this.udpClient.Client.Bind(localEp); |
| 130 | 114 | this.udpClient.JoinMulticastGroup(this.multicastAddress); |
| 131 | 115 | |
| 116 | + var rsws = new BufferedWaveProvider(new WaveFormat(44100, 16, 2)) { BufferDuration = TimeSpan.FromMilliseconds(200), DiscardOnBufferOverflow = true }; |
| 132 | 117 | |
| 133 | | − var rsws = new BufferedWaveProvider(new WaveFormat()) |
| 118 | + this.output = new WaveOut() |
| 134 | 119 | { |
| 135 | | − BufferDuration = TimeSpan.FromMilliseconds(500) |
| 120 | + DesiredLatency = 200 |
| 136 | 121 | }; |
| 122 | + this.Volume = 100; |
| 137 | 123 | |
| 138 | 124 | this.output.Init(rsws); |
| 139 | 125 | this.output.Play(); |
| @@ -145,15 +131,31 @@ namespace ScreamReader · 5 unmodified lines |
| 145 | 131 | try |
| 146 | 132 | { |
| 147 | 133 | Byte[] data = this.udpClient.Receive(ref localEp); |
| 148 | | − rsws.AddSamples(data, 0, data.Length); |
| 134 | + |
| 135 | + if (data[0] != currentRate || data[1] != currentWidth) |
| 136 | + { |
| 137 | + currentRate = data[0]; |
| 138 | + currentWidth = data[1]; |
| 139 | + this.output.Stop(); |
| 140 | + var rate = ((currentRate >= 128) ? 44100 : 48000) * (currentRate % 128); |
| 141 | + rsws = new BufferedWaveProvider(new WaveFormat(rate, currentWidth, 2)) { BufferDuration = TimeSpan.FromMilliseconds(200), DiscardOnBufferOverflow = true }; |
| 142 | + this.output = new WaveOut() |
| 143 | + { |
| 144 | + DesiredLatency = 200 |
| 145 | + }; |
| 146 | + this.Volume = 100; |
| 147 | + this.output.Init(rsws); |
| 148 | + this.output.Play(); |
| 149 | + } |
| 150 | + rsws.AddSamples(data, 2, data.Length - 2); |
| 149 | 151 | } catch (SocketException) { } // Usually when interrupted |
| 150 | 152 | } |
| 151 | 153 | }, this.cancellationTokenSource.Token); |
| 152 | 154 | |
| 153 | 155 | this.shutdownLock.WaitOne(); |
| 154 | 156 | |
| 155 | | − this.udpClient.Close(); |
| 156 | 157 | this.output.Stop(); |
| 158 | + this.udpClient.Close(); |
| 157 | 159 | }, this.cancellationTokenSource.Token); |
| 158 | 160 | } |
| 159 | 161 | |
| @@ -177,7 +179,6 @@ namespace ScreamReader · 17 unmodified lines |
| 177 | 179 | { |
| 178 | 180 | if (disposing) |
| 179 | 181 | { |
| 180 | | − this.udpClient.Dispose(); |
| 181 | 182 | this.startLock.Dispose(); |
| 182 | 183 | this.shutdownLock.Dispose(); |
| 183 | 184 | } |