-
Notifications
You must be signed in to change notification settings - Fork 0
/
LabSoundStarter.cpp
110 lines (89 loc) · 3.45 KB
/
LabSoundStarter.cpp
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
// SPDX-License-Identifier: BSD-2-Clause
// Copyright (C) 2020, The LabSound Authors. All rights reserved.
#if defined(_MSC_VER)
#if !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif
#if !defined(NOMINMAX)
#define NOMINMAX
#endif
#endif
#include "LabSound/LabSound.h"
#include "LabSoundDemo.h"
#include <chrono>
#include <string>
#include <thread>
#include <vector>
using namespace lab;
// Returns input, output
inline std::pair<AudioStreamConfig, AudioStreamConfig> GetDefaultAudioDeviceConfiguration(const bool with_input = false)
{
AudioStreamConfig inputConfig;
AudioStreamConfig outputConfig;
const std::vector<AudioDeviceInfo> audioDevices = lab::MakeAudioDeviceList();
const AudioDeviceIndex default_output_device = lab::GetDefaultOutputAudioDeviceIndex();
const AudioDeviceIndex default_input_device = lab::GetDefaultInputAudioDeviceIndex();
AudioDeviceInfo defaultOutputInfo, defaultInputInfo;
for (auto& info : audioDevices)
{
if (info.index == default_output_device.index) defaultOutputInfo = info;
else if (info.index == default_input_device.index) defaultInputInfo = info;
}
if (defaultOutputInfo.index != -1)
{
outputConfig.device_index = defaultOutputInfo.index;
outputConfig.desired_channels = std::min(uint32_t(2), defaultOutputInfo.num_output_channels);
outputConfig.desired_samplerate = defaultOutputInfo.nominal_samplerate;
}
if (with_input)
{
if (defaultInputInfo.index != -1)
{
inputConfig.device_index = defaultInputInfo.index;
inputConfig.desired_channels = std::min(uint32_t(1), defaultInputInfo.num_input_channels);
inputConfig.desired_samplerate = defaultInputInfo.nominal_samplerate;
}
else
{
throw std::invalid_argument("the default audio input device was requested but none were found");
}
}
return { inputConfig, outputConfig };
}
std::shared_ptr<AudioBus> MakeBusFromSampleFile(char const* const name, int argc, char** argv)
{
std::string path_prefix = asset_base;
const std::string path = path_prefix + name;
std::shared_ptr<AudioBus> bus = MakeBusFromFile(path, false);
if (!bus)
throw std::runtime_error("couldn't open " + path);
return bus;
}
int main(int argc, char *argv[]) try
{
std::unique_ptr<lab::AudioContext> context;
const auto defaultAudioDeviceConfigurations = GetDefaultAudioDeviceConfiguration();
context = lab::MakeRealtimeAudioContext(defaultAudioDeviceConfigurations.second, defaultAudioDeviceConfigurations.first);
lab::AudioContext& ac = *context.get();
auto musicClip = MakeBusFromSampleFile("samples/stereo-music-clip.wav", argc, argv);
if (!musicClip)
return EXIT_FAILURE;
std::shared_ptr<SampledAudioNode> musicClipNode;
std::shared_ptr<GainNode> gain;
gain = std::make_shared<GainNode>(ac);
gain->gain()->setValue(0.5f);
musicClipNode = std::make_shared<SampledAudioNode>(ac);
{
ContextRenderLock r(context.get(), "ex_simple");
musicClipNode->setBus(r, musicClip);
}
context->connect(context->device(), musicClipNode, 0, 0);
musicClipNode->schedule(0.0);
std::this_thread::sleep_for(std::chrono::seconds(6));
return EXIT_SUCCESS;
}
catch (const std::exception & e)
{
std::cerr << "unhandled fatal exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}