-
Notifications
You must be signed in to change notification settings - Fork 9
/
Play.fs
163 lines (149 loc) · 6.42 KB
/
Play.fs
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
//
// FSound - F# Sound Processing Library
// Copyright (c) 2022 by Albert Pang <[email protected]>
// All rights reserved.
//
// This file is a part of FSound
//
// FSound is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// FSound is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
namespace FSound
module Play =
open NAudio.Wave
open FSound.IO
open System.IO
/// <summary>
/// Creates an instance of ISampleProvider based on the samples in the given
/// SampleSeq object
/// </summary>
/// <param name="sampleRate">Sampling rate in Hz</param>
/// <param name="bytesPerSample">Bit depth in number of bytes</param>
/// <param name="samples">SampleSeq object</param>
let getSampleSeqProvider sampleRate bytesPerSample (samples: SampleSeq) =
let maxAmp = 2.0 ** (float (bytesPerSample * 8 - 1))
let convert s = float32 ( s / maxAmp )
let stream = SampleSeqStream(samples, convert)
let nChannels = samples.NumChannels
{ new ISampleProvider with
member p.Read(buffer : float32 [], offset, count) =
stream.Read(buffer, offset, count)
member p.WaveFormat =
WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, nChannels)
}
///
/// <summary>
/// Plays a SampleSeq using ISampleProvider of NAudio synchronously.
/// </summary>
/// <param name="sampleRate">Sampling rate</param>
/// <param name="bytesPerSample">Bit depth in number of bytes</param>
/// <param name="samples">SampleSeq object</param>
/// <returns>unit</returns>
///
let playSync sampleRate bytesPerSample (samples: SampleSeq) =
let provider = getSampleSeqProvider sampleRate bytesPerSample samples
use wo = new WaveOutEvent()
wo.Init(provider)
wo.Play()
while wo.PlaybackState = PlaybackState.Playing do
System.Threading.Thread.Sleep(100);
///
/// <summary>
/// Plays a SampleSeq using ISampleProvider of NAudio asynchronously
/// </summary>
/// <param name="sampleRate">Sampling rate</param>
/// <param name="bytesPerSample">Bit depth in number of bytes</param>
/// <param name="samples">SampleSeq object</param>
/// <returns>unit</returns>
///
let playAsync sampleRate bytesPerSample (samples: SampleSeq) =
let provider = getSampleSeqProvider sampleRate bytesPerSample samples
let wo = new WaveOutEvent()
wo.Init(provider)
wo.Play()
///
/// <summary>Play a SampleSeq object using NAudio synchronously</summary>
/// <param name="sampleRate">Sampling frequency in Hz</param>
/// <param name="bytesPerSample">Bit depth in number of bytes</param>
/// <param name="samples">Sample sequence object to be played</param>
/// <returns>unit</returns>
///
let playSampleSeq sampleRate bytesPerSample (samples : SampleSeq) =
playSync sampleRate bytesPerSample samples
/// <summary>
/// Obsolete play function which calculates whole of sample sequence then
/// write to a memory stream before playing. Less efficient than the
/// latest play2 function which uses the ISampleProvider interface in
/// NAudio which allows the function to compute and read the samples by
/// small blocks, hence less latency before playback actually starts
/// </summary>
/// <param name="sampleRate"></param>
/// <param name="bytesPerSample"></param>
/// <param name="samples"></param>
let playSampleSeq0 sampleRate bytesPerSample (samples : SampleSeq) =
async {
let nChannel = samples.NumChannels
let format = WaveFormat(sampleRate, bytesPerSample * 8, nChannel)
use ms = new MemoryStream()
use writer = new BinaryWriter(ms)
let bytesWritten = packSampleSequence bytesPerSample writer samples
// reset stream position to 0
ms.Position <- 0L
use wavestream = new RawSourceWaveStream(ms, format)
use wo = new WaveOut()
wo.Init(wavestream)
wo.Play()
do! Async.Sleep(bytesWritten / nChannel / sampleRate * 1000)
}
|> Async.Start
///
/// <summary>Play a list of sequence of samples, each sequence represents a
/// channel</summary>
/// <param name="sampleRate">Sampling frequency in Hz</param>
/// <param name="bytesPerSample">Bit depth in number of bytes</param>
/// <param name="sequences">List of sequence of samples (float)</param>
/// <returns>unit</returns>
///
let play sampleRate bytesPerSample sequences =
match sequences with
| [] -> ()
| [ mono ] -> playSampleSeq sampleRate bytesPerSample (Mono mono)
| [ left; right ] ->
playSampleSeq sampleRate bytesPerSample (Stereo(left, right))
| multi -> playSampleSeq sampleRate bytesPerSample (Multi multi)
/// <summary>
/// Convenience function to play a sequence of floats as samples
/// </summary>
/// <param name="sampleRate">Sampling frequency in Hz</param>
/// <param name="bytesPerSample">Bit depth in number of bytes</param>
/// <param name="sequence">Sequence of floats to play</param>
/// <returns>unit</returns>
let playMono sampleRate bytesPerSample sequence =
playSampleSeq sampleRate bytesPerSample (Mono sequence)
/// <summary>
/// Convenience function to play a sequence of pairs of floats as sample
/// values for the left and right channel
/// </summary>
/// <param name="sampleRate">Sampling frequency in Hz</param>
/// <param name="bytesPerSample">Bit depth in number of bytes</param>
/// <param name="sequences">Sequence of pairs of floats</param>
/// <returns>unit</returns>
let playStereo sampleRate bytesPerSample sequences =
playSampleSeq sampleRate bytesPerSample (Stereo2 sequences)
///
/// <summary>Plays a SoundFile using NAudio as a background job</summary>
/// <param name="sf">SoundFile object</param>
/// <returns>unit</returns>
///
let playSoundFile (sf : SoundFile) =
sf.Samples |> playSampleSeq (int sf.SamplingRate) sf.BytesPerSample