aboutsummaryrefslogtreecommitdiff
path: root/source/WindowsFormsApplication1/AudioResourceClient.cs
blob: 717d43a704413857ece6bc85d3964b8a74d17231 (plain) (blame)
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using NAudio;
using NAudio.Wave;
using System.Threading;
using Newtonsoft.Json;
using AxWMPLib;
using System.Windows.Forms;
using WMPLib;

namespace ShiftOS
{
    public class AudioData
    {
        public string StartURL { get; set; }
        public Dictionary<string, List<string>> Files { get; set; }
        public Dictionary<string, List<Visualizer>> Visualizers = null;
    }

    public class Audio
    {
        public static string Name
        {
            get
            {
                string res = null;
                try
                {
                    if (_wmp != null)
                    {
                        if (_wmp.currentMedia != null)
                        {
                            res = _wmp.currentMedia.name;
                        }

                    }
                }
                catch
                {

                }
                return res;
            }
        }

        private static AudioData _ad = null;
        public static WindowsMediaPlayer _wmp = null;
        public static bool running = true;
        
        public static int CurrentPosition
        {
            get
            {
                try
                {
                    if (_wmp != null)
                    {
                        return (int)_wmp.controls.currentPosition;
                    }
                    else
                    {
                        return 0;
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }

        public static int CurrentPositionMS
        {
            get
            {
                try
                {
                    if (_wmp != null)
                    {
                        return (int)(_wmp.controls.currentPosition * 100);
                    }
                    else
                    {
                        return 0;
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }


        public static void LoadAudioData()
        {
            var t = new Thread(new ThreadStart(() =>
            {
                _wmp = new WindowsMediaPlayer();
                _wmp.settings.autoStart = true;
                _wmp.settings.volume = API.LoadedSettings.MusicVolume;
            }));
            t.Start();
            _ad = JsonConvert.DeserializeObject<AudioData>(Properties.Resources.MusicData);
        }

        public static void Play(string list)
        {
            _forceStop = false;
            var lst = _ad.Files[list];
            var rnd = new Random();
            int i = rnd.Next(0, lst.Count);
            _wmp.URL = _ad.StartURL + list + "/" + _ad.Files[list][i] + ".mp3";
            _wmp.PlayStateChange += (o) =>
            {
                if (o == (int)WMPPlayState.wmppsMediaEnded)
                {
                    var t = new System.Windows.Forms.Timer();
                    t.Interval = 1000;
                    t.Tick += (object s, EventArgs a) =>
                    {
                        t.Stop();
                        Stopped?.Invoke(_wmp, new EventArgs());
                    };
                    t.Start();
                }
            };

        }

        public static event EventHandler Stopped;

        public static bool _forceStop = false;

        public static void Mute()
        {
            _wmp.settings.volume = 0;
        }

        public static void VolumeUp()
        {
            if(_wmp.settings.volume < 90)
            {
                _wmp.settings.volume += 10;
            }
        }

        public static void VolumeDown()
        {
            if(_wmp.settings.volume > 0)
            {
                _wmp.settings.volume -= 10;
            }
        }

        public static void Pause()
        {
            _wmp.controls.pause();
        }

        public static void Unpause()
        {
            _wmp.controls.play();
        }

        public static void ForceStop()
        {
            new Thread(new ThreadStart(() =>
            {
                _forceStop = true;
                _wmp.controls.stop();
            })).Start();
        }

        internal static Visualizer GetVisualizer()
        {
            Visualizer v = null;
            foreach(var vis in _ad.Visualizers[Name])
            {
                if (vis.startTime <= CurrentPosition && vis.endTime >= CurrentPosition)
                    v = vis;
            }
            return v;
        }
    }

    public class Visualizer
    {
        public int startTime { get; set; }
        public int endTime { get; set; }
        public VisualizerType type { get; set; }
        public bool R { get; set; }
        public bool G { get; set; }
        public bool B { get; set; }
    }

    public enum VisualizerType
    {
        Pulse,
        BuildUp,
        CalmDown
    }

}