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