summaryrefslogtreecommitdiffstats
path: root/src/audio_core/in/audio_in_system.h
blob: 1c51546381f8829999e19d3bc9508ef369710e03 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <atomic>
#include <memory>
#include <span>
#include <string>

#include "audio_core/common/common.h"
#include "audio_core/device/audio_buffers.h"
#include "audio_core/device/device_session.h"
#include "core/hle/service/audio/errors.h"

namespace Core {
class System;
}

namespace Kernel {
class KEvent;
}

namespace AudioCore::AudioIn {

constexpr SessionTypes SessionType = SessionTypes::AudioIn;

struct AudioInParameter {
    /* 0x0 */ s32_le sample_rate;
    /* 0x4 */ u16_le channel_count;
    /* 0x6 */ u16_le reserved;
};
static_assert(sizeof(AudioInParameter) == 0x8, "AudioInParameter is an invalid size");

struct AudioInParameterInternal {
    /* 0x0 */ u32_le sample_rate;
    /* 0x4 */ u32_le channel_count;
    /* 0x8 */ u32_le sample_format;
    /* 0xC */ u32_le state;
};
static_assert(sizeof(AudioInParameterInternal) == 0x10,
              "AudioInParameterInternal is an invalid size");

struct AudioInBuffer {
    /* 0x00 */ AudioInBuffer* next;
    /* 0x08 */ VAddr samples;
    /* 0x10 */ u64 capacity;
    /* 0x18 */ u64 size;
    /* 0x20 */ u64 offset;
};
static_assert(sizeof(AudioInBuffer) == 0x28, "AudioInBuffer is an invalid size");

enum class State {
    Started,
    Stopped,
};

/**
 * Controls and drives audio input.
 */
class System {
public:
    explicit System(Core::System& system, Kernel::KEvent* event, size_t session_id);
    ~System();

    /**
     * Get the default audio input device name.
     *
     * @return The default audio input device name.
     */
    std::string_view GetDefaultDeviceName() const;

    /**
     * Get the default USB audio input device name.
     * This is preferred over non-USB as some games refuse to work with the BuiltInHeadset
     * (e.g Let's Sing).
     *
     * @return The default USB audio input device name.
     */
    std::string_view GetDefaultUacDeviceName() const;

    /**
     * Is the given initialize config valid?
     *
     * @param device_name - The name of the requested input device.
     * @param in_params   - Input parameters, see AudioInParameter.
     * @return Result code.
     */
    Result IsConfigValid(std::string_view device_name, const AudioInParameter& in_params) const;

    /**
     * Initialize this system.
     *
     * @param device_name             - The name of the requested input device.
     * @param in_params               - Input parameters, see AudioInParameter.
     * @param handle                  - Unused.
     * @param applet_resource_user_id - Unused.
     * @return Result code.
     */
    Result Initialize(std::string device_name, const AudioInParameter& in_params, u32 handle,
                      u64 applet_resource_user_id);

    /**
     * Start this system.
     *
     * @return Result code.
     */
    Result Start();

    /**
     * Stop this system.
     *
     * @return Result code.
     */
    Result Stop();

    /**
     * Finalize this system.
     */
    void Finalize();

    /**
     * Start this system's device session.
     */
    void StartSession();

    /**
     * Get this system's id.
     */
    size_t GetSessionId() const;

    /**
     * Append a new buffer to the device.
     *
     * @param buffer - New buffer to append.
     * @param tag    - Unique tag of the buffer.
     * @return True if the buffer was appended, otherwise false.
     */
    bool AppendBuffer(const AudioInBuffer& buffer, u64 tag);

    /**
     * Register all appended buffers.
     */
    void RegisterBuffers();

    /**
     * Release all registered buffers.
     */
    void ReleaseBuffers();

    /**
     * Get all released buffers.
     *
     * @param tags - Container to be filled with the released buffers' tags.
     * @return The number of buffers released.
     */
    u32 GetReleasedBuffers(std::span<u64> tags);

    /**
     * Flush all appended and registered buffers.
     *
     * @return True if buffers were successfully flushed, otherwise false.
     */
    bool FlushAudioInBuffers();

    /**
     * Get this system's current channel count.
     *
     * @return The channel count.
     */
    u16 GetChannelCount() const;

    /**
     * Get this system's current sample rate.
     *
     * @return The sample rate.
     */
    u32 GetSampleRate() const;

    /**
     * Get this system's current sample format.
     *
     * @return The sample format.
     */
    SampleFormat GetSampleFormat() const;

    /**
     * Get this system's current state.
     *
     * @return The current state.
     */
    State GetState();

    /**
     * Get this system's name.
     *
     * @return The system's name.
     */
    std::string GetName() const;

    /**
     * Get this system's current volume.
     *
     * @return The system's current volume.
     */
    f32 GetVolume() const;

    /**
     * Set this system's current volume.
     *
     * @param volume The new volume.
     */
    void SetVolume(f32 volume);

    /**
     * Does the system contain this buffer?
     *
     * @param tag - Unique tag to search for.
     * @return True if the buffer is in the system, otherwise false.
     */
    bool ContainsAudioBuffer(u64 tag) const;

    /**
     * Get the maximum number of usable buffers (default 32).
     *
     * @return The number of buffers.
     */
    u32 GetBufferCount() const;

    /**
     * Get the total number of samples played by this system.
     *
     * @return The number of samples.
     */
    u64 GetPlayedSampleCount() const;

    /**
     * Is this system using a USB device?
     *
     * @return True if using a USB device, otherwise false.
     */
    bool IsUac() const;

private:
    /// Core system
    Core::System& system;
    /// (Unused)
    u32 handle{};
    /// (Unused)
    u64 applet_resource_user_id{};
    /// Buffer event, signalled when a buffer is ready
    Kernel::KEvent* buffer_event;
    /// Session id of this system
    size_t session_id{};
    /// Device session for this system
    std::unique_ptr<DeviceSession> session;
    /// Audio buffers in use by this system
    AudioBuffers<BufferCount> buffers{BufferCount};
    /// Sample rate of this system
    u32 sample_rate{};
    /// Sample format of this system
    SampleFormat sample_format{SampleFormat::PcmInt16};
    /// Channel count of this system
    u16 channel_count{};
    /// State of this system
    std::atomic<State> state{State::Stopped};
    /// Name of this system
    std::string name{};
    /// Volume of this system
    f32 volume{1.0f};
    /// Is this system's device USB?
    bool is_uac{false};
};

} // namespace AudioCore::AudioIn