1/**
2 * Copyright (c) 2006-2023 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20
21#include "wrap_RecordingDevice.h"
22#include "wrap_Audio.h"
23
24#include "sound/SoundData.h"
25namespace love
26{
27namespace audio
28{
29
30RecordingDevice *luax_checkrecordingdevice(lua_State *L, int idx)
31{
32 return luax_checktype<RecordingDevice>(L, idx);
33}
34
35int w_RecordingDevice_start(lua_State *L)
36{
37 RecordingDevice *d = luax_checkrecordingdevice(L, 1);
38
39 int samples = d->getMaxSamples();
40 int samplerate = d->getSampleRate();
41 int bitdepth = d->getBitDepth();
42 int channels = d->getChannelCount();
43
44 if (lua_gettop(L) > 1)
45 {
46 samples = (int) luaL_checkinteger(L, 2);
47 samplerate = (int) luaL_optinteger(L, 3, RecordingDevice::DEFAULT_SAMPLE_RATE);
48 bitdepth = (int) luaL_optinteger(L, 4, RecordingDevice::DEFAULT_BIT_DEPTH);
49 channels = (int) (int) luaL_optinteger(L, 5, RecordingDevice::DEFAULT_CHANNELS);
50 }
51
52 bool success = false;
53 luax_catchexcept(L, [&]() { success = d->start(samples, samplerate, bitdepth, channels); });
54
55 luax_pushboolean(L, success);
56 return 1;
57}
58
59int w_RecordingDevice_stop(lua_State *L)
60{
61 RecordingDevice *d = luax_checkrecordingdevice(L, 1);
62 love::sound::SoundData *s = nullptr;
63
64 luax_catchexcept(L, [&](){ s = d->getData(); });
65
66 d->stop();
67
68 if (s != nullptr)
69 {
70 luax_pushtype(L, s);
71 s->release();
72 }
73 else
74 lua_pushnil(L);
75
76 return 1;
77}
78
79int w_RecordingDevice_getData(lua_State *L)
80{
81 RecordingDevice *d = luax_checkrecordingdevice(L, 1);
82 love::sound::SoundData *s = nullptr;
83
84 luax_catchexcept(L, [&](){ s = d->getData(); });
85
86 if (s != nullptr)
87 {
88 luax_pushtype(L, s);
89 s->release();
90 }
91 else
92 lua_pushnil(L);
93
94 return 1;
95}
96
97int w_RecordingDevice_getSampleCount(lua_State *L)
98{
99 RecordingDevice *d = luax_checkrecordingdevice(L, 1);
100 lua_pushnumber(L, d->getSampleCount());
101 return 1;
102}
103
104int w_RecordingDevice_getSampleRate(lua_State *L)
105{
106 RecordingDevice *d = luax_checkrecordingdevice(L, 1);
107 lua_pushnumber(L, d->getSampleRate());
108 return 1;
109}
110
111int w_RecordingDevice_getBitDepth(lua_State *L)
112{
113 RecordingDevice *d = luax_checkrecordingdevice(L, 1);
114 lua_pushnumber(L, d->getBitDepth());
115 return 1;
116}
117
118int w_RecordingDevice_getChannelCount(lua_State *L)
119{
120 RecordingDevice *d = luax_checkrecordingdevice(L, 1);
121 lua_pushnumber(L, d->getChannelCount());
122 return 1;
123}
124
125int w_RecordingDevice_getName(lua_State *L)
126{
127 RecordingDevice *d = luax_checkrecordingdevice(L, 1);
128 lua_pushstring(L, d->getName());
129 return 1;
130}
131
132int w_RecordingDevice_isRecording(lua_State *L)
133{
134 RecordingDevice *d = luax_checkrecordingdevice(L, 1);
135 lua_pushboolean(L, d->isRecording());
136 return 1;
137}
138
139static const luaL_Reg w_RecordingDevice_functions[] =
140{
141 { "start", w_RecordingDevice_start },
142 { "stop", w_RecordingDevice_stop },
143 { "getData", w_RecordingDevice_getData },
144 { "getSampleCount", w_RecordingDevice_getSampleCount },
145 { "getSampleRate", w_RecordingDevice_getSampleRate },
146 { "getBitDepth", w_RecordingDevice_getBitDepth },
147 { "getChannelCount", w_RecordingDevice_getChannelCount },
148 { "getName", w_RecordingDevice_getName },
149 { "isRecording", w_RecordingDevice_isRecording },
150 { 0, 0 }
151};
152
153extern "C" int luaopen_recordingdevice(lua_State *L)
154{
155 int ret = luax_register_type(L, &RecordingDevice::type, w_RecordingDevice_functions, nullptr);
156 return ret;
157}
158
159} //audio
160} //love
161