Debian Codegraph @

tests_literate/expected/audio.md

raw
1 ```futhark
2 def output_hz = 44100i64
3 def standard_pitch = 440.0f32
4
5 def pitch (i: i64): f32 =
6 standard_pitch * 2 ** (f32.i64 i/12)
7
8 def num_samples (duration: f32): i64 =
9 i64.f32 (f32.i64 output_hz * duration)
10
11 def sample (p: f32) (i: i64): f32 =
12 f32.sin (2 * f32.pi * f32.i64 i * p / f32.i64 output_hz)
13
14 def note (i: i64) (duration: f32): []f32 =
15 let p = pitch i
16 let n = num_samples duration
17 in tabulate n (sample p)
18
19 def break (duration: f32): []f32 =
20 replicate (num_samples duration) 0.0
21
22 entry left =
23 let c = note 3
24 let d = note 5
25 let e = note 7
26 let f = note 8
27 let g = note 10
28 in c 0.3
29 ++ d 0.3
30 ++ e 0.3
31 ++ c 0.3
32 ++ c 0.3
33 ++ d 0.3
34 ++ e 0.3
35 ++ c 0.3
36 ++ e 0.3
37 ++ f 0.3
38 ++ g 0.6
39 ++ e 0.3
40 ++ f 0.3
41 ++ g 0.6
42
43 entry right =
44 let c = note 3
45 let d = note 5
46 let e = note 7
47 let f = note 8
48 let g = note 10
49 in break (8 * 0.3)
50 ++ c 0.3
51 ++ d 0.3
52 ++ e 0.3
53 ++ c 0.3
54 ++ c 0.3
55 ++ d 0.3
56 ++ e 0.3
57 ++ c 0.3
58 ```
59
60 ```
61 > :audio left;
62 sampling_frequency: 44100
63 codec: ogg
64 ```
65
66 ![](audio-img/4f72a37a9b3cf4b8a68ce450f0f696d5-output.ogg)
67
68
69 ```
70 > :audio right;
71 sampling_frequency: 44100
72 codec: ogg
73 ```
74
75 ![](audio-img/53655790dd8b1739a749c772c9f6bc13-output.ogg)
76
77 ```futhark
78 entry stereo =
79 let [k] left: [k]f32 = left
80 let right = right :> [k]f32
81 in [left, right]
82 ```
83
84 ```
85 > :audio stereo
86 ```
87
88 ![](audio-img/9e12f26cca7539fa04a923f7b652d2e8-output.wav)
89
90 ```futhark
91 entry surround =
92 let [k] left: [k]f32 = left
93 let right = right :> [k]f32
94 in [left, right, right, right, right, right]
95 ```
96
97 ```
98 > :audio surround
99 ```
100
101 ![](audio-img/7e0c96822c449db1dd5712a2b809ff40-output.wav)
102
103
104 ```
105 > $loadaudio "mono.wav"
106 ```
107
108 ```
109 [[-0.9921875f64, -0.984375f64, -0.9765625f64]]
110 ```
111
112
113 ```
114 > $loadaudio "stereo.wav"
115 ```
116
117 ```
118 [[-0.9921875f64, -0.984375f64, -0.9765625f64],
119 [-0.96875f64, -0.9609375f64, -0.953125f64]]
120 ```