File: audio_stream.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (180 lines) | stat: -rw-r--r-- 4,459 bytes parent folder | download | duplicates (2)
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

#include "audio_stream.h"

#include "sound/audiostr.h"

namespace scripting {
namespace api {

//**********HANDLE: Asteroid
ADE_OBJ_NO_MULTI(l_AudioStream, int, "audio_stream", "An audio stream handle");

ADE_FUNC(play,
	l_AudioStream,
   "[number volume = -1.0 /* By default sets the last used volume of this stream, if applicable. Otherwise, uses preset volume of the stream type */, boolean loop = false]",
    "Starts playing the audio stream",
	"boolean",
	"true on success, false otherwise")
{
	int streamHandle = -1;
	float volume = -1.0f;
	bool loop = false;
	if (!ade_get_args(L, "o|fb", l_AudioStream.Get(&streamHandle), &volume, &loop)) {
		return ADE_RETURN_FALSE;
	}

	if (streamHandle < 0) {
		return ade_set_args(L, "b", false);
	}

	audiostream_play(streamHandle, volume, loop ? 1 : 0);
	return ade_set_args(L, "b", true);
}

ADE_FUNC(pause, l_AudioStream, nullptr, "Pauses the audio stream", "boolean", "true on success, false otherwise")
{
	int streamHandle = -1;
	if (!ade_get_args(L, "o", l_AudioStream.Get(&streamHandle))) {
		return ADE_RETURN_FALSE;
	}

	if (streamHandle < 0) {
		return ADE_RETURN_FALSE;
	}

	audiostream_pause(streamHandle, true);
	return ade_set_args(L, "b", true);
}

ADE_FUNC(unpause, l_AudioStream, nullptr, "Unpauses the audio stream", "boolean", "true on success, false otherwise")
{
	int streamHandle = -1;
	if (!ade_get_args(L, "o", l_AudioStream.Get(&streamHandle))) {
		return ADE_RETURN_FALSE;
	}

	if (streamHandle < 0) {
		return ADE_RETURN_FALSE;
	}

	audiostream_unpause(streamHandle, true);
	return ade_set_args(L, "b", true);
}

ADE_FUNC(stop,
	l_AudioStream,
	nullptr,
	"Stops the audio stream so that it can be started again later",
	"boolean",
	"true on success, false otherwise")
{
	int streamHandle = -1;
	if (!ade_get_args(L, "o", l_AudioStream.Get(&streamHandle))) {
		return ADE_RETURN_FALSE;
	}

	if (streamHandle < 0) {
		return ADE_RETURN_FALSE;
	}

	audiostream_stop(streamHandle);
	return ade_set_args(L, "b", true);
}

ADE_FUNC(close,
	l_AudioStream,
	"[boolean fade = true]",
	"Irrevocably closes the audio file and optionally fades the music before stopping playback. This invalidates the "
	"audio stream handle.",
	"boolean",
	"true on success, false otherwise")
{
	// We get ourself a pointer here so that we can invalidate the passed handle since otherwise it might be possible to
	// still call functions on that handle
	int* streamHandlePointer = nullptr;
	bool fade = true;
	if (!ade_get_args(L, "o|b", l_AudioStream.GetPtr(&streamHandlePointer), &fade)) {
		return ADE_RETURN_FALSE;
	}

	if (streamHandlePointer == nullptr) {
		return ADE_RETURN_FALSE;
	}

	if (*streamHandlePointer < 0) {
		return ADE_RETURN_FALSE;
	}

	audiostream_close_file(*streamHandlePointer, fade);

	// Invalidate the handle so that it cannot be reused
	*streamHandlePointer = -1;

	return ade_set_args(L, "b", true);
}

ADE_FUNC(isPlaying,
	l_AudioStream,
	nullptr,
	"Determines if the audio stream is still playing",
	"boolean",
	"true when still playing, false otherwise")
{
	int streamHandle = -1;
	if (!ade_get_args(L, "o", l_AudioStream.Get(&streamHandle))) {
		return ADE_RETURN_FALSE;
	}

	if (streamHandle < 0) {
		return ADE_RETURN_FALSE;
	}

	return ade_set_args(L, "b", audiostream_is_playing(streamHandle) != 0);
}

ADE_FUNC(setVolume,	l_AudioStream, "number volume", "Sets the volume of the audio stream, 0 - 1", "boolean", "true on success, false otherwise")
{
	int streamHandle = -1;
	float volume = -1.0f;
	if (!ade_get_args(L, "of", l_AudioStream.Get(&streamHandle), &volume)) {
		return ADE_RETURN_FALSE;
	}

	if (streamHandle < 0) {
		return ADE_RETURN_FALSE;
	}
	audiostream_set_volume(streamHandle, volume);
	return ADE_RETURN_TRUE;
}

ADE_FUNC(getDuration, l_AudioStream, nullptr, "Gets the duration of the stream", "number", "the duration in float seconds or nil if invalid")
{
	int streamHandle = -1;
	if (!ade_get_args(L, "o", l_AudioStream.Get(&streamHandle))) {
		return ADE_RETURN_NIL;
	}

	if (streamHandle < 0) {
		return ADE_RETURN_NIL;
	}

	return ade_set_args(L, "f", (float)audiostream_get_duration(streamHandle));
}

ADE_FUNC(isValid,
	l_AudioStream,
	nullptr,
	"Determines if the handle is valid",
	"boolean",
	"true if valid, false otherwise")
{
	int streamHandle = -1;
	if (!ade_get_args(L, "o", l_AudioStream.Get(&streamHandle))) {
		return ADE_RETURN_FALSE;
	}

	return ade_set_args(L, "b", streamHandle >= 0);
}

} // namespace api
} // namespace scripting