File size: 4,862 Bytes
5efaea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86bcfb1
5efaea1
 
 
 
 
 
 
 
 
 
 
 
 
828afe0
5efaea1
 
 
 
 
 
 
 
 
 
 
 
 
828afe0
5efaea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f4c513
5efaea1
828afe0
5efaea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa2f948
5efaea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import base64
from io import BytesIO

import matplotlib.pyplot as plt
from pages.common import AUDIO_PATH
from scipy.io import wavfile

from toph.audio.effect import SimplePanner
from toph.audio.playable import Chain, Silence, SineWave, Wave
from toph.audio.spatial import SpatialPanner
from toph.audio.stage import AudioStage
from toph.axis.spatial import LinearSpatialAxis


def ax_gen(domain, range_):

    do_scale = max(domain) - min(domain)
    ra_scale = max(range_) - min(range_)

    def foo(dp):

        dx = dp - min(domain)
        sdx = dx / do_scale
        return min(range_) + sdx * ra_scale

    return foo


def sonify_y_ax(xs, ys, mode, gaps=0.5):
    """ """
    with AudioStage() as stage:

        space_ax = LinearSpatialAxis(
            domain=((min(xs), max(xs)), (min(ys), max(ys))),
            range_=((-50.0, 50.0), (-45.0, 90.0)),
        )

        # TODO: replace with linear axis when implemented
        lin_ax = ax_gen(domain=(min(ys), max(ys)), range_=(110, 880))

        simple_lr_ax = ax_gen(domain=(min(xs), max(xs)), range_=(-0.98, +0.99))

        marks = []

        for x, y in zip(xs, ys):

            if mode == "full_spatial":

                r, az, el = space_ax((x, y))

                marks.extend(
                    [
                        Wave(AUDIO_PATH).add_effect(SpatialPanner(az, el, r * 2)),
                        Silence(secs=gaps),
                    ]
                )

            elif mode == "pitch":

                # keep the y as 0 to get a good r
                az = simple_lr_ax(x)
                freq = lin_ax(y)

                print(az, freq)
                marks.extend(
                    [
                        SineWave(vol=0.15, f=freq, secs=0.5).add_effect(
                            SimplePanner(dir=az)
                        ),
                        Silence(secs=gaps),
                    ]
                )

            elif mode == "both":
                # keep the y as 0 to get a good r
                r, az, el = space_ax((x, y))
                freq = lin_ax(y)

                marks.extend(
                    [
                        SineWave(vol=0.15, f=freq, secs=0.5).add_effect(
                            SpatialPanner(az, el, r)
                        ),
                        Silence(secs=gaps),
                    ]
                )

        return stage.get_ndarray(Chain(*marks))


def sonify_x_ax(xs, ys, x_mode, y_mode, gaps=0.5):
    """ """
    with AudioStage(frame_rate=48000) as stage:

        # TODO: replace with linear axis when implemented

        lin_ax = ax_gen(domain=(min(xs), max(xs)), range_=(-1.0, +1.0))

        pitch_ax = ax_gen(domain=(min(ys), max(ys)), range_=(110, 880))

        x_deltas = [xs[idx + 1] - xs[idx] for idx in range(len(xs) - 1)]
        x_deltas.append(0.0)

        gap_ax = ax_gen(domain=(min(x_deltas), max(x_deltas)), range_=(0.25, 1.25))

        marks = []

        for idx, (x, y) in enumerate(zip(xs, ys)):

            audio_src = (
                SineWave(vol=0.15, f=440, secs=0.5)
                if y_mode == "static"
                else SineWave(vol=0.15, f=pitch_ax(y), secs=0.5)
            )
            if x_mode == "spatial":

                marks.extend(
                    [
                        audio_src.add_effect(SimplePanner(dir=lin_ax(x))),
                        Silence(secs=gaps),
                    ]
                )

            elif x_mode == "gaps":

                marks.append(audio_src)
                if x_deltas[idx] > 0.0:
                    marks.append(Silence(secs=gap_ax(x_deltas[idx])))

        return stage.get_ndarray(Chain(*marks))


def get_audio_comp(arr, frame_rate, visible=True, autoplay=False) -> str:
    """Takes an audio array
    returns an audio component
    """
    v_file = BytesIO()
    wavfile.write(
        v_file,
        rate=frame_rate,
        # todo: handle formats other than int16, make it better
        data=arr,
    )
    v_file.seek(0)
    b64 = base64.b64encode(v_file.read()).decode()

    md = f"""<audio {"style='display: none'" if not visible else ''} controls {'autoplay="true"' if autoplay else ''}>
        """
    src = f"""<source src="data:audio/wav;base64,{b64}" type="audio/wav">"""

    return md + src + "</audio>"


SONIFICATION_FUNCTIONS = {"sonify_y_ax": sonify_y_ax, "sonify_x_ax": sonify_x_ax}


def plot_four(datasets, idxs, xlim=None, ylim=None, s=75):
    fig, axes = plt.subplots(2, 2, figsize=(8, 8))

    for i, ax in enumerate(axes.flat):
        ax.scatter(datasets[idxs[i]]["x"], datasets[idxs[i]]["y"], s=100)
        ax.set_title(f"Option {i + 1}")
        ax.grid(True)

        if xlim:
            ax.set_xlim(xlim)
        if ylim:
            ax.set_ylim(ylim)

        ax.set_xticks([])
        ax.set_yticks([])

    plt.tight_layout()
    return fig