summaryrefslogtreecommitdiffstats
path: root/src/audio_core/device/device_session.cpp
blob: 095fc96ce16c7e2798e4134416084f15f597e574 (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
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "audio_core/audio_core.h"
#include "audio_core/audio_manager.h"
#include "audio_core/device/audio_buffer.h"
#include "audio_core/device/device_session.h"
#include "audio_core/sink/sink_stream.h"
#include "core/core.h"
#include "core/memory.h"

namespace AudioCore {

DeviceSession::DeviceSession(Core::System& system_) : system{system_} {}

DeviceSession::~DeviceSession() {
    Finalize();
}

Result DeviceSession::Initialize(std::string_view name_, SampleFormat sample_format_,
                                 u16 channel_count_, size_t session_id_, u32 handle_,
                                 u64 applet_resource_user_id_, Sink::StreamType type_) {
    if (stream) {
        Finalize();
    }
    name = fmt::format("{}-{}", name_, session_id_);
    type = type_;
    sample_format = sample_format_;
    channel_count = channel_count_;
    session_id = session_id_;
    handle = handle_;
    applet_resource_user_id = applet_resource_user_id_;

    if (type == Sink::StreamType::In) {
        sink = &system.AudioCore().GetInputSink();
    } else {
        sink = &system.AudioCore().GetOutputSink();
    }
    stream = sink->AcquireSinkStream(system, channel_count, name, type);
    initialized = true;
    return ResultSuccess;
}

void DeviceSession::Finalize() {
    if (initialized) {
        Stop();
        sink->CloseStream(stream);
        stream = nullptr;
    }
}

void DeviceSession::Start() {
    stream->SetPlayedSampleCount(played_sample_count);
    stream->Start();
}

void DeviceSession::Stop() {
    if (stream) {
        played_sample_count = stream->GetPlayedSampleCount();
        stream->Stop();
    }
}

void DeviceSession::AppendBuffers(std::span<AudioBuffer> buffers) const {
    auto& memory{system.Memory()};

    for (size_t i = 0; i < buffers.size(); i++) {
        Sink::SinkBuffer new_buffer{
            .frames = buffers[i].size / (channel_count * sizeof(s16)),
            .frames_played = 0,
            .tag = buffers[i].tag,
            .consumed = false,
        };

        if (type == Sink::StreamType::In) {
            std::vector<s16> samples{};
            stream->AppendBuffer(new_buffer, samples);
        } else {
            std::vector<s16> samples(buffers[i].size / sizeof(s16));
            memory.ReadBlockUnsafe(buffers[i].samples, samples.data(), buffers[i].size);
            stream->AppendBuffer(new_buffer, samples);
        }
    }
}

void DeviceSession::ReleaseBuffer(AudioBuffer& buffer) const {
    if (type == Sink::StreamType::In) {
        auto& memory{system.Memory()};
        auto samples{stream->ReleaseBuffer(buffer.size / sizeof(s16))};
        memory.WriteBlockUnsafe(buffer.samples, samples.data(), buffer.size);
    }
}

bool DeviceSession::IsBufferConsumed(u64 tag) const {
    if (stream) {
        return stream->IsBufferConsumed(tag);
    }
    return true;
}

void DeviceSession::SetVolume(f32 volume) const {
    if (stream) {
        stream->SetSystemVolume(volume);
    }
}

u64 DeviceSession::GetPlayedSampleCount() const {
    if (stream) {
        return stream->GetPlayedSampleCount();
    }
    return 0;
}

} // namespace AudioCore