audioplayer.gd 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. extends Control
  2. @onready var audio_player = $AudioStreamPlayer
  3. @onready var file_dialog = $FileDialog
  4. @onready var waveform_display = $WaveformPreview
  5. var outfile_path = "not_loaded"
  6. #signal recycle_outfile_trigger
  7. var rect_focus = false
  8. var mouse_pos_x
  9. #Used for waveform preview
  10. var voice_preview_generator : Node = null
  11. var stream : AudioStreamWAV = null
  12. func _ready():
  13. #Setup file dialogue to access system files and only accept wav files
  14. #get_window().files_dropped.connect(_on_files_dropped)
  15. file_dialog.access = FileDialog.ACCESS_FILESYSTEM
  16. file_dialog.file_mode = FileDialog.FILE_MODE_OPEN_FILE
  17. file_dialog.filters = ["*.wav ; WAV audio files"]
  18. file_dialog.connect("file_selected", Callable(self, "_on_file_selected"))
  19. audio_player.connect("finished", Callable(self, "_on_audio_finished"))
  20. if get_meta("loadenable") == true:
  21. $RecycleButton.hide()
  22. $LoadButton.show()
  23. else:
  24. $LoadButton.hide()
  25. $RecycleButton.show()
  26. $WavError.hide()
  27. # Load the voice preview generator for waveform visualization
  28. voice_preview_generator = preload("res://addons/audio_preview/voice_preview_generator.tscn").instantiate()
  29. add_child(voice_preview_generator)
  30. voice_preview_generator.texture_ready.connect(_on_texture_ready)
  31. #func _on_files_dropped(files):
  32. #if files[0].get_extension() == "wav" or files[0].get_extension() == "WAV":
  33. #audio_player.stream = AudioStreamWAV.load_from_file(files[0])
  34. #if audio_player.stream.stereo == true: #checks if stream is stereo, not sure what this will do with a surround sound file
  35. #audio_player.stream = null #empties audio stream so stereo audio cant be played back
  36. #$WavError.show()
  37. #else:
  38. #voice_preview_generator.generate_preview(audio_player.stream) #this generates the waveform graphic
  39. #Global.infile = files[0] #this sets the global infile variable to the audio file path
  40. #print(Global.infile)
  41. #else:
  42. #$WavError.show()
  43. func _on_close_button_button_down() -> void:
  44. $WavError.hide()
  45. func _on_load_button_button_down() -> void:
  46. file_dialog.popup_centered()
  47. func _on_file_selected(path: String):
  48. audio_player.stream = AudioStreamWAV.load_from_file(path)
  49. Global.infile_stereo = audio_player.stream.stereo
  50. #if audio_player.stream.stereo == true:
  51. ##audio_player.stream = null
  52. ##$WavError.show()
  53. voice_preview_generator.generate_preview(audio_player.stream)
  54. Global.infile = path
  55. print("Infile set: " + Global.infile)
  56. reset_playback()
  57. func reset_playback():
  58. $LoopRegion.size.x = 0
  59. $Playhead.position.x = 0
  60. $PlayButton.text = "Play"
  61. $Timer.stop()
  62. Global.trim_infile = false
  63. func play_outfile(path: String):
  64. outfile_path = path
  65. audio_player.stream = AudioStreamWAV.load_from_file(path)
  66. print(audio_player.stream)
  67. if audio_player.stream == null:
  68. voice_preview_generator._reset_to_blank()
  69. reset_playback()
  70. return
  71. voice_preview_generator.generate_preview(audio_player.stream)
  72. reset_playback()
  73. func recycle_outfile(path: String):
  74. audio_player.stream = AudioStreamWAV.load_from_file(path)
  75. Global.infile_stereo = audio_player.stream.stereo
  76. #if audio_player.stream.stereo == true:
  77. ##audio_player.stream = null
  78. ##$WavError.show()
  79. voice_preview_generator.generate_preview(audio_player.stream)
  80. Global.infile = path
  81. print("Infile set: " + Global.infile)
  82. reset_playback()
  83. func _on_play_button_button_down() -> void:
  84. var playhead_position
  85. #check if trim markers are set and set playhead position to correct location
  86. if $LoopRegion.size.x == 0:
  87. playhead_position = 0
  88. else:
  89. playhead_position = $LoopRegion.position.x
  90. $Playhead.position.x = playhead_position
  91. #check if audio is playing, to decide if this is a play or stop button
  92. if audio_player.stream:
  93. if audio_player.playing:
  94. audio_player.stop()
  95. $Timer.stop()
  96. $PlayButton.text = "Play"
  97. else:
  98. $PlayButton.text = "Stop"
  99. if $LoopRegion.size.x == 0: #loop position is not set, play from start of file
  100. audio_player.play()
  101. else:
  102. var length = $AudioStreamPlayer.stream.get_length()
  103. var pixel_to_time = length / 399
  104. audio_player.play(pixel_to_time * $LoopRegion.position.x)
  105. if $LoopRegion.position.x + $LoopRegion.size.x < 399:
  106. $Timer.start(pixel_to_time * $LoopRegion.size.x)
  107. #timer for ending playback at end of loop
  108. func _on_timer_timeout() -> void:
  109. _on_play_button_button_down() #"press" stop button
  110. func _on_audio_finished():
  111. $PlayButton.text = "Play"
  112. # This function will be called when the waveform texture is ready
  113. func _on_texture_ready(image_texture: ImageTexture):
  114. # Set the generated texture to the TextureRect (waveform display node)
  115. waveform_display.texture = image_texture
  116. # Called every frame. 'delta' is the elapsed time since the previous frame.
  117. func _process(delta: float) -> void:
  118. if $AudioStreamPlayer.playing:
  119. var length = $AudioStreamPlayer.stream.get_length()
  120. var total_distance = 399.0
  121. var speed = total_distance / length
  122. $Playhead.position.x += speed * delta
  123. if $Playhead.position.x >= 399:
  124. $Playhead.position.x = 0
  125. if rect_focus == true:
  126. if get_local_mouse_position().x > mouse_pos_x:
  127. $LoopRegion.size.x = clamp(get_local_mouse_position().x - mouse_pos_x, 0, $Panel.size.x - (mouse_pos_x - $Panel.position.x))
  128. else:
  129. $LoopRegion.size.x = clamp(mouse_pos_x - get_local_mouse_position().x, 0, (mouse_pos_x - $Panel.position.x))
  130. $LoopRegion.position.x = clamp(get_local_mouse_position().x, $Panel.position.x, $Panel.position.x + $Panel.size.x)
  131. #func _on_recycle_button_button_down() -> void:
  132. #if outfile_path != "not_loaded":
  133. #recycle_outfile_trigger.emit(outfile_path)
  134. func _on_button_button_down() -> void:
  135. if audio_player.stream:
  136. if audio_player.playing: #if audio is playing allow user to skip around the sound file
  137. $Timer.stop()
  138. var length = $AudioStreamPlayer.stream.get_length()
  139. var pixel_to_time = length / 399
  140. $Playhead.position.x = get_local_mouse_position().x
  141. if $LoopRegion.size.x == 0 or get_local_mouse_position().x > $LoopRegion.position.x + $LoopRegion.size.x: #loop position is not set or click is after loop position, play to end of file
  142. audio_player.seek(pixel_to_time * get_local_mouse_position().x)
  143. else: #if click position is before the loop position play from there and stop at the end of the loop position
  144. audio_player.seek(pixel_to_time * get_local_mouse_position().x)
  145. if $LoopRegion.position.x + $LoopRegion.size.x < 399:
  146. $Timer.start(pixel_to_time * ($LoopRegion.position.x + $LoopRegion.size.x - get_local_mouse_position().x))
  147. else:
  148. mouse_pos_x = get_local_mouse_position().x
  149. $LoopRegion.position.x = mouse_pos_x
  150. rect_focus = true
  151. func _on_button_button_up() -> void:
  152. rect_focus = false
  153. if get_meta("loadenable") == true:
  154. print("got meta")
  155. if $LoopRegion.size.x > 0:
  156. Global.trim_infile = true
  157. var length = $AudioStreamPlayer.stream.get_length()
  158. var pixel_to_time = length / 399
  159. Global.infile_start = pixel_to_time * $LoopRegion.position.x
  160. Global.infile_stop = Global.infile_start + (pixel_to_time * $LoopRegion.size.x)
  161. print(Global.trim_infile)
  162. print(Global.infile_start)
  163. print(Global.infile_stop)
  164. else:
  165. Global.trim_infile = false
  166. print(Global.trim_infile)