1 2 #include "audio_stream.h" 3 4 #include "sound/audiostr.h" 5 6 namespace scripting { 7 namespace api { 8 9 //**********HANDLE: Asteroid 10 ADE_OBJ(l_AudioStream, int, "audio_stream", "An audio stream handle"); 11 12 ADE_FUNC(play, 13 l_AudioStream, 14 "[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]", 15 "Starts playing the audio stream", 16 "boolean", 17 "true on success, false otherwise") 18 { 19 int streamHandle = -1; 20 float volume = -1.0f; 21 bool loop = false; 22 if (!ade_get_args(L, "o|fb", l_AudioStream.Get(&streamHandle), &volume, &loop)) { 23 return ADE_RETURN_FALSE; 24 } 25 26 if (streamHandle < 0) { 27 return ade_set_args(L, "b", false); 28 } 29 30 audiostream_play(streamHandle, volume, loop ? 1 : 0); 31 return ade_set_args(L, "b", true); 32 } 33 34 ADE_FUNC(pause, l_AudioStream, nullptr, "Pauses the audio stream", "boolean", "true on success, false otherwise") 35 { 36 int streamHandle = -1; 37 if (!ade_get_args(L, "o", l_AudioStream.Get(&streamHandle))) { 38 return ADE_RETURN_FALSE; 39 } 40 41 if (streamHandle < 0) { 42 return ADE_RETURN_FALSE; 43 } 44 45 audiostream_pause(streamHandle, true); 46 return ade_set_args(L, "b", true); 47 } 48 49 ADE_FUNC(unpause, l_AudioStream, nullptr, "Unpauses the audio stream", "boolean", "true on success, false otherwise") 50 { 51 int streamHandle = -1; 52 if (!ade_get_args(L, "o", l_AudioStream.Get(&streamHandle))) { 53 return ADE_RETURN_FALSE; 54 } 55 56 if (streamHandle < 0) { 57 return ADE_RETURN_FALSE; 58 } 59 60 audiostream_unpause(streamHandle, true); 61 return ade_set_args(L, "b", true); 62 } 63 64 ADE_FUNC(stop, 65 l_AudioStream, 66 nullptr, 67 "Stops the audio stream so that it can be started again later", 68 "boolean", 69 "true on success, false otherwise") 70 { 71 int streamHandle = -1; 72 if (!ade_get_args(L, "o", l_AudioStream.Get(&streamHandle))) { 73 return ADE_RETURN_FALSE; 74 } 75 76 if (streamHandle < 0) { 77 return ADE_RETURN_FALSE; 78 } 79 80 audiostream_stop(streamHandle); 81 return ade_set_args(L, "b", true); 82 } 83 84 ADE_FUNC(close, 85 l_AudioStream, 86 "[boolean fade = true]", 87 "Irrevocably closes the audio file and optionally fades the music before stopping playback. This invalidates the " 88 "audio stream handle.", 89 "boolean", 90 "true on success, false otherwise") 91 { 92 // We get ourself a pointer here so that we can invalidate the passed handle since otherwise it might be possible to 93 // still call functions on that handle 94 int* streamHandlePointer = nullptr; 95 bool fade = true; 96 if (!ade_get_args(L, "o|b", l_AudioStream.GetPtr(&streamHandlePointer), &fade)) { 97 return ADE_RETURN_FALSE; 98 } 99 100 if (streamHandlePointer == nullptr) { 101 return ADE_RETURN_FALSE; 102 } 103 104 if (*streamHandlePointer < 0) { 105 return ADE_RETURN_FALSE; 106 } 107 108 audiostream_close_file(*streamHandlePointer, fade); 109 110 // Invalidate the handle so that it cannot be reused 111 *streamHandlePointer = -1; 112 113 return ade_set_args(L, "b", true); 114 } 115 116 ADE_FUNC(isPlaying, 117 l_AudioStream, 118 nullptr, 119 "Determines if the audio stream is still playing", 120 "boolean", 121 "true when still playing, false otherwise") 122 { 123 int streamHandle = -1; 124 if (!ade_get_args(L, "o", l_AudioStream.Get(&streamHandle))) { 125 return ADE_RETURN_FALSE; 126 } 127 128 if (streamHandle < 0) { 129 return ADE_RETURN_FALSE; 130 } 131 132 return ade_set_args(L, "b", audiostream_is_playing(streamHandle) != 0); 133 } 134 135 ADE_FUNC(setVolume, l_AudioStream, "number volume", "Sets the volume of the audio stream, 0 - 1", "boolean", "true on success, false otherwise") 136 { 137 int streamHandle = -1; 138 float volume = -1.0f; 139 if (!ade_get_args(L, "of", l_AudioStream.Get(&streamHandle), &volume)) { 140 return ADE_RETURN_FALSE; 141 } 142 143 if (streamHandle < 0) { 144 return ADE_RETURN_FALSE; 145 } 146 audiostream_set_volume(streamHandle, volume); 147 return ADE_RETURN_TRUE; 148 } 149 150 ADE_FUNC(isValid, 151 l_AudioStream, 152 nullptr, 153 "Determines if the handle is valid", 154 "boolean", 155 "true if valid, false otherwise") 156 { 157 int streamHandle = -1; 158 if (!ade_get_args(L, "o", l_AudioStream.Get(&streamHandle))) { 159 return ADE_RETURN_FALSE; 160 } 161 162 return ade_set_args(L, "b", streamHandle >= 0); 163 } 164 165 } // namespace api 166 } // namespace scripting 167