Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 54a9226150
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 230 lines (201 sloc) 8.524 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using OpenTK.Audio;

namespace OggStream
{
    static class Program
    {
        static readonly string[] StreamFiles = new[] { "2test.ogg", "2test.ogg", "2test.ogg", "2test.ogg", "2test.ogg" };

        static void Main()
        {
#if TRACE
            Trace.Listeners.Add(new ConsoleTraceListener());
#endif
            Console.WindowHeight = StreamFiles.Length + 12;

            Console.WriteLine("Pr[e]pare, [P]lay, [S]top, Pa[u]se, [R]esume, [L]oop toggle, [Q]uit");
            Console.WriteLine("Faders (in/out) : Low-pass filter [F]/[G], Volume [V]/[B]");
            Console.WriteLine("[Up], [Down] : Change current sample");
            Console.WriteLine("[Shift] + Action : Do for all " + StreamFiles.Length + " streams");

            Console.SetCursorPosition(0, 8);
            Console.WriteLine(" # FX Buffering");

            using (new AudioContext())
            using (new OggStreamer())
            {
                bool quit = false;

                var streams = new OggStream[StreamFiles.Length];

                for (int i = 0; i < StreamFiles.Length; i++)
                {
                    streams[i] = new OggStream(StreamFiles[i])
                    {
#if TRACE
                        logX = 6, logY = 10 + i, LogHandler = Log
#endif
                    };
                    Console.SetCursorPosition(1, 10 + i);
                    Console.Write(i);
                }
                Console.SetCursorPosition(0, 10);
                Console.Write(">");
                foreach (var s in streams)
                    s.Prepare();

                int sIdx = 0;
                var activeSet = new List<OggStream>();

                while (!quit)
                {
                    var input = Console.ReadKey(true);

                    activeSet.Clear();
                    if ((input.Modifiers & ConsoleModifiers.Shift) == ConsoleModifiers.Shift)
                        activeSet.AddRange(streams);
                    else
                        activeSet.Add(streams[sIdx]);

                    var lower = char.ToLower(input.KeyChar);
                    if (input.Key == ConsoleKey.UpArrow) lower = '-';
                    if (input.Key == ConsoleKey.DownArrow) lower = '+';

                    switch (lower)
                    {
                        case 'e': activeSet.ForEach(x => x.Prepare()); break;
                        case 'p': activeSet.ForEach(x => x.Play()); break;
                        case 'u': activeSet.ForEach(x => x.Pause()); break;
                        case 's': activeSet.ForEach(x => x.Stop()); break;
                        case 'r': activeSet.ForEach(x => x.Resume()); break;

                        case 'l':
                            activeSet.ForEach(s =>
                            {
                                s.IsLooped = !s.IsLooped;
#if TRACE
                                Log(s.IsLooped ? "L" : " ", 3, s.logY);
#endif
                            });
                            break;

                        case 'v': FadeVolume(activeSet, true, 1); break;
                        case 'b': FadeVolume(activeSet, false, 1); break;

                        case 'f': FadeFilter(activeSet, true, 1); break;
                        case 'g': FadeFilter(activeSet, false, 1); break;

                        case '+':
                            Log(" ", 0, 10 + sIdx);
                            sIdx++;
                            if (sIdx > streams.Length - 1) sIdx = 0;
                            Log(">", 0, 10 + sIdx);
                            break;

                        case '-':
                            Log(" ", 0, 10 + sIdx);
                            sIdx--;
                            if (sIdx < 0) sIdx = streams.Length - 1;
                            Log(">", 0, 10 + sIdx);
                            break;

                        case 'q':
                            quit = true;
                            foreach (var cts in filterFades.Values) cts.Cancel();
                            foreach (var cts in volumeFades.Values) cts.Cancel();
                            foreach (var s in streams) s.Stop(); // nicer and more effective
                            foreach (var s in streams) s.Dispose();
                            break;
                    }
                }
            }
        }

        static readonly Dictionary<OggStream, CancellationTokenSource> volumeFades = new Dictionary<OggStream, CancellationTokenSource>();
        static void FadeVolume(List<OggStream> streams, bool @in, float duration)
        {
            foreach (var stream in streams)
            {
                var from = stream.Volume;
                var to = @in ? 1f : 0;
                var speed = @in ? 1 - @from : @from;

                lock (volumeFades)
                {
                    CancellationTokenSource token;
                    bool found = volumeFades.TryGetValue(stream, out token);
                    if (found)
                    {
                        token.Cancel();
                        volumeFades.Remove(stream);
                    }
                }
#if TRACE
                Log(@in ? "V" : "v", 4, stream.logY);
#endif

                var cts = new CancellationTokenSource();
                lock (volumeFades) volumeFades.Add(stream, cts);

                var sw = Stopwatch.StartNew();
                OggStream s = stream;
                Task.Factory.StartNew(() =>
                {
                    float step;
                    do
                    {
                        step = (float) Math.Min(sw.Elapsed.TotalSeconds / (duration * speed), 1);
                        s.Volume = (to - @from) * step + @from;
                        Thread.Sleep(1000 / 60);
                    } while (step < 1 && !cts.Token.IsCancellationRequested);
                    sw.Stop();

                    if (!cts.Token.IsCancellationRequested)
                    {
                        lock (volumeFades) volumeFades.Remove(s);
#if TRACE
                        Log(" ", 4, s.logY);
#endif
                    }
                }, cts.Token);
            }
        }

        static readonly Dictionary<OggStream, CancellationTokenSource> filterFades = new Dictionary<OggStream, CancellationTokenSource>();
        static void FadeFilter(List<OggStream> streams, bool @in, float duration)
        {
            foreach (var stream in streams)
            {
                var from = stream.LowPassHFGain;
                var to = @in ? 1f : 0;
                var speed = @in ? 1 - @from : @from;

                lock (filterFades)
                {
                    CancellationTokenSource token;
                    bool found = filterFades.TryGetValue(stream, out token);
                    if (found)
                    {
                        token.Cancel();
                        filterFades.Remove(stream);
                    }
                }
#if TRACE
                Log(@in ? "F" : "f", 5, stream.logY);
#endif
                var cts = new CancellationTokenSource();
                lock (filterFades) filterFades.Add(stream, cts);

                var sw = Stopwatch.StartNew();
                OggStream s = stream;
                Task.Factory.StartNew(() =>
                {
                    float step;
                    do
                    {
                        step = (float)Math.Min(sw.Elapsed.TotalSeconds / (duration * speed), 1);
                        s.LowPassHFGain = (to - @from) * step + @from;
                        Thread.Sleep(1000 / 60);
                    } while (step < 1 && !cts.Token.IsCancellationRequested);
                    sw.Stop();

                    if (!cts.Token.IsCancellationRequested)
                    {
                        lock (filterFades) filterFades.Remove(s);
#if TRACE
                        Log(" ", 5, s.logY);
#endif
                    }
                }, cts.Token);
            }
        }

        static readonly object logMutex = new object();
        static void Log(string text, int x, int y)
        {
            lock (logMutex)
            {
                Console.SetCursorPosition(Math.Min(Console.BufferWidth - 1, x), y);
                Console.Write(text);
                Console.SetCursorPosition(0, Console.WindowHeight - 1);
            }
        }
    }
}
Something went wrong with that request. Please try again.