浏览代码

add AudioStream and AudioStreamPlayer.

SakiKawasaki 4 年之前
父节点
当前提交
caeefd5712

+ 2 - 0
README.md

@@ -32,10 +32,12 @@
    -  [Input](https://ethosa.github.io/nodesnim/input.html)
    -  [Rect2](https://ethosa.github.io/nodesnim/rect2.html)
    -  [Vector2](https://ethosa.github.io/nodesnim/vector2.html)
+   -  [AudioStream](https://ethosa.github.io/nodesnim/audio_stream.html)
 -  Default nodes
    -  [Node](https://ethosa.github.io/nodesnim/node.html)
    -  [Canvas](https://ethosa.github.io/nodesnim/canvas.html)
    -  [Scene](https://ethosa.github.io/nodesnim/scene.html)
+   -  [AudioStreamPlayer](https://ethosa.github.io/nodesnim/audio_stream_player.html)
 -  Control nodes
    -  [Control](https://ethosa.github.io/nodesnim/control.html)
    -  [ColorRect](https://ethosa.github.io/nodesnim/color_rect.html)

+ 9 - 1
src/nodesnim.nim

@@ -14,10 +14,12 @@ import
   nodesnim/core/input,
   nodesnim/core/image,
   nodesnim/core/color_text,
+  nodesnim/core/audio_stream,
 
   nodesnim/nodes/node,
   nodesnim/nodes/scene,
   nodesnim/nodes/canvas,
+  nodesnim/nodes/audio_stream_player,
 
   nodesnim/nodescontrol/control,
   nodesnim/nodescontrol/color_rect,
@@ -37,9 +39,15 @@ import
   nodesnim/nodescontrol/popup
 
 export
+  # Third party
   opengl, glut,
+  # Main
   window, environment,
+  # Core
   vector2, rect2, enums, anchor, color, exceptions, input, image, color_text,
-  node, scene, canvas,
+  audio_stream,
+  # Default nodes
+  node, scene, canvas, audio_stream_player,
+  # Control nodes
   control, color_rect, texture_rect, label, button, box, hbox, vbox, grid_box, edittext,
   rich_label, rich_edit_text, scroll, progress_bar, slider, popup

+ 31 - 0
src/nodesnim/core/audio_stream.nim

@@ -0,0 +1,31 @@
+# author: Ethosa
+import
+  ../thirdparty/sdl2,
+  ../thirdparty/sdl2/mixer
+
+
+discard mixer.init(MIX_INIT_OGG)
+discard mixer.openAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)
+
+
+type
+  AudioStreamRef* = ref object
+    chunk*: ptr Chunk
+    channel*: cint
+    loop*: bool
+
+var
+  channel_num: cint = 2
+  current_channel: cint = 0
+
+
+proc loadAudio*(file: cstring, loop: bool = true): AudioStreamRef =
+  ## Loads a new AudioStream from file.
+  ##
+  ## Arguments:
+  ## - `file` is the audio path.
+  inc current_channel
+  if current_channel == channel_num:
+    channel_num *= 2
+    discard allocateChannels(channel_num)
+  AudioStreamRef(chunk: loadWAV(file), channel: current_channel, loop: loop)

+ 53 - 0
src/nodesnim/nodes/audio_stream_player.nim

@@ -0,0 +1,53 @@
+# author: Ethosa
+import
+  node,
+  ../thirdparty/sdl2/mixer,
+  ../core/enums,
+  ../core/audio_stream
+
+
+type
+  AudioStreamPlayerObj* {.final.} = object of NodeObj
+    paused*: bool
+    volume*: cint
+    stream*: AudioStreamRef
+  AudioStreamPlayerPtr* = ptr AudioStreamPlayerObj
+
+
+proc AudioStreamPlayer*(name: string, variable: var AudioStreamPlayerObj): AudioStreamPlayerPtr =
+  ## Creates a new AudioStreamPlayer pointer.
+  nodepattern(AudioStreamPlayerObj)
+  variable.pausemode = PAUSE
+  variable.paused = false
+  variable.volume = 64
+
+proc AudioStreamPlayer*(variable: var AudioStreamPlayerObj): AudioStreamPlayerPtr {.inline.} =
+  AudioStreamPlayer("AudioStreamPlayer", variable)
+
+
+method pause*(self: AudioStreamPlayerPtr) {.base.} =
+  if playing(self.stream.channel) > -1:
+    pause(self.stream.channel)
+
+method play*(self: AudioStreamPlayerPtr) {.base.} =
+  discard playChannel(
+    self.stream.channel, self.stream.chunk,
+    if self.stream.loop: -1 else: 1
+  )
+
+method resume*(self: AudioStreamPlayerPtr) {.base.} =
+  if paused(self.stream.channel) > -1:
+    resume(self.stream.channel)
+
+method setVolume*(self: AudioStreamPlayerPtr, value: cint) {.base.} =
+  ## Changes stream volume.
+  ##
+  ## Arguments:
+  ## - `volume` is a number in range `0..128`.
+  if value > 128:
+    self.volume = 128
+  elif value < 0:
+    self.volume = 0
+  else:
+    self.volume = value
+  discard volume(self.stream.channel, self.volume)

+ 1 - 0
tests/README.md

@@ -23,3 +23,4 @@
 21. [Use ProgressBar node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test21.nim)
 22. [Use Slider node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test22.nim)
 23. [Use Popup node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test23.nim)
+24. [Use AudioStreamPlayer node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test24.nim)

二进制
tests/assets/vug_ost_Amusing_Mechanisms.ogg


二进制
tests/assets/vug_ost_Movement.ogg


二进制
tests/assets/vug_ost_VNSH.ogg


+ 32 - 0
tests/test24.nim

@@ -0,0 +1,32 @@
+# --- Test 24. Use AudioStreamPlayer node. --- #
+import nodesnim
+
+
+Window("hello world")
+
+var
+  mainobj: SceneObj
+  main = Scene("Main", mainobj)
+
+  stream1 = loadAudio("assets/vug_ost_Weh.ogg")
+  stream2 = loadAudio("assets/vug_ost_Movement.ogg")
+
+  audio_obj: AudioStreamPlayerObj
+  audio = AudioStreamPlayer(audio_obj)
+
+  audio_obj1: AudioStreamPlayerObj
+  audio1 = AudioStreamPlayer(audio_obj1)
+
+audio.stream = stream1
+audio.setVolume(64)
+audio.play()
+
+when false:  # use more than one channel
+  audio1.stream = stream2
+  audio1.setVolume(64)
+  audio1.play()
+
+
+addScene(main)
+setMainScene("Main")
+windowLaunch()