OSDN Git Service

d74cb37c5b30b9b6e863ba6c543e9cf41325e6a0
[strokestylet/CsWin10Desktop3.git] / FDK24 / メディア / サウンド / WASAPI / Sound.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.Linq;
5 using CSCore;
6
7 namespace FDK.メディア.サウンド.WASAPI
8 {
9         public class Sound : IDisposable
10         {
11                 public long 長さsample
12                 {
13                         get { return this._SampleSource.Length; }
14                 }
15
16                 public double 長さsec
17                 {
18                         get { return this.長さsample / ( this.WaveFormat.Channels * this.WaveFormat.SampleRate ); }
19                 }
20
21                 public long 位置sample
22                 {
23                         get { return this._SampleSource.Position; }
24                         set { this._SampleSource.Position = value; }
25                 }
26
27                 public double 位置sec
28                 {
29                         get
30                         {
31                                 return this.位置sample / ( this.WaveFormat.Channels * this.WaveFormat.SampleRate );
32                         }
33                         set
34                         {
35                                 long position = (long) ( value * this.WaveFormat.SampleRate * this.WaveFormat.Channels + 0.5 ); // +0.5 は四捨五入
36                                 position -= ( position % this.WaveFormat.Channels );    // チャンネル数の倍数にする。
37
38                                 if( ( 0 > position ) || ( this.長さsample <= position ) )
39                                         throw new ArgumentOutOfRangeException();
40
41                                 this.位置sample = position;
42                         }
43                 }
44                         
45                 public WaveFormat WaveFormat
46                 {
47                         get { return this._SampleSource.WaveFormat; }
48                 }
49
50                 public CSCore.ISampleSource SampleSource
51                 {
52                         get { return this._SampleSource; }
53                 }
54
55                 /// <summary>
56                 /// 音量。0.0(無音)~1.0(原音)。
57                 /// </summary>
58                 public float Volume
59                 {
60                         get { return this._Volume; }
61                         set
62                         {
63                                 if( ( 0.0f > value ) || ( 1.0f < value ) )
64                                         throw new ArgumentOutOfRangeException();
65
66                                 this._Volume = value;
67                         }
68                 }
69
70                 /// <summary>
71                 /// Sound の生成は、コンストラクタではなく Device.CreateSound() で行うこと。
72                 /// (Device 内部で持っている Mixer への参照が必要なため。)
73                 /// </summary>
74                 /// <param name="path">サウンドファイルパス</param>
75                 /// <param name="mixer">使用する Mixer。</param>
76                 internal Sound( string path, Mixer mixer )
77                 {
78                         this._MixerRef = new WeakReference<Mixer>( mixer );
79                         this._WaveSource = new Decoder( path, mixer.WaveFormat );
80                         this._SampleSource = this._WaveSource.ToSampleSource();
81                 }
82
83                 public void Dispose()
84                 {
85                         this.Stop();
86
87                         FDK.Utilities.解放する( ref this._SampleSource );
88                         FDK.Utilities.解放する( ref this._WaveSource );
89                         this._MixerRef = null;
90                 }
91
92                 public void Play()
93                 {
94                         Mixer mixer;
95                         if( this._MixerRef.TryGetTarget( out mixer ) )
96                                 mixer.AddSound( this );
97                 }
98
99                 public void Stop()
100                 {
101                         Mixer mixer;
102                         if( this._MixerRef.TryGetTarget( out mixer ) )
103                                 mixer.RemoveSound( this );
104                 }
105
106                 private CSCore.IWaveSource _WaveSource = null;
107                 private CSCore.ISampleSource _SampleSource = null;
108                 private System.WeakReference<Mixer> _MixerRef = null;
109                 private float _Volume = 1.0f;
110         }
111 }