audioplayer.gd 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. $LoopRegion.size.x = 0
  57. $Playhead.position.x = 0
  58. func play_outfile(path: String):
  59. outfile_path = path
  60. audio_player.stream = AudioStreamWAV.load_from_file(path)
  61. voice_preview_generator.generate_preview(audio_player.stream)
  62. func recycle_outfile(path: String):
  63. audio_player.stream = AudioStreamWAV.load_from_file(path)
  64. Global.infile_stereo = audio_player.stream.stereo
  65. #if audio_player.stream.stereo == true:
  66. ##audio_player.stream = null
  67. ##$WavError.show()
  68. voice_preview_generator.generate_preview(audio_player.stream)
  69. Global.infile = path
  70. print("Infile set: " + Global.infile)
  71. $LoopRegion.size.x = 0
  72. $Playhead.position.x = 0
  73. func _on_play_button_button_down() -> void:
  74. var playhead_position
  75. #check if trim markers are set and set playhead position to correct location
  76. if $LoopRegion.size.x == 0:
  77. playhead_position = 0
  78. else:
  79. playhead_position = $LoopRegion.position.x
  80. $Playhead.position.x = playhead_position
  81. #check if audio is playing, to decide if this is a play or stop button
  82. if audio_player.stream:
  83. if audio_player.playing:
  84. audio_player.stop()
  85. $Timer.stop()
  86. $PlayButton.text = "Play"
  87. else:
  88. $PlayButton.text = "Stop"
  89. if $LoopRegion.size.x == 0: #loop position is not set, play from start of file
  90. audio_player.play()
  91. else:
  92. var length = $AudioStreamPlayer.stream.get_length()
  93. var pixel_to_time = length / 399
  94. audio_player.play(pixel_to_time * $LoopRegion.position.x)
  95. if $LoopRegion.position.x + $LoopRegion.size.x < 399:
  96. $Timer.start(pixel_to_time * $LoopRegion.size.x)
  97. #timer for ending playback at end of loop
  98. func _on_timer_timeout() -> void:
  99. _on_play_button_button_down() #"press" stop button
  100. func _on_audio_finished():
  101. $PlayButton.text = "Play"
  102. # This function will be called when the waveform texture is ready
  103. func _on_texture_ready(image_texture: ImageTexture):
  104. # Set the generated texture to the TextureRect (waveform display node)
  105. waveform_display.texture = image_texture
  106. # Called every frame. 'delta' is the elapsed time since the previous frame.
  107. func _process(delta: float) -> void:
  108. if $AudioStreamPlayer.playing:
  109. var length = $AudioStreamPlayer.stream.get_length()
  110. var total_distance = 399.0
  111. var speed = total_distance / length
  112. $Playhead.position.x += speed * delta
  113. if $Playhead.position.x >= 399:
  114. $Playhead.position.x = 0
  115. if rect_focus == true:
  116. if get_local_mouse_position().x > mouse_pos_x:
  117. $LoopRegion.size.x = clamp(get_local_mouse_position().x - mouse_pos_x, 0, $Panel.size.x - (mouse_pos_x - $Panel.position.x))
  118. else:
  119. $LoopRegion.size.x = clamp(mouse_pos_x - get_local_mouse_position().x, 0, (mouse_pos_x - $Panel.position.x))
  120. $LoopRegion.position.x = clamp(get_local_mouse_position().x, $Panel.position.x, $Panel.position.x + $Panel.size.x)
  121. #func _on_recycle_button_button_down() -> void:
  122. #if outfile_path != "not_loaded":
  123. #recycle_outfile_trigger.emit(outfile_path)
  124. func _on_button_button_down() -> void:
  125. if audio_player.stream:
  126. if audio_player.playing: #if audio is playing allow user to skip around the sound file
  127. $Timer.stop()
  128. var length = $AudioStreamPlayer.stream.get_length()
  129. var pixel_to_time = length / 399
  130. $Playhead.position.x = get_local_mouse_position().x
  131. 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
  132. audio_player.seek(pixel_to_time * get_local_mouse_position().x)
  133. else: #if click position is before the loop position play from there and stop at the end of the loop position
  134. audio_player.seek(pixel_to_time * get_local_mouse_position().x)
  135. if $LoopRegion.position.x + $LoopRegion.size.x < 399:
  136. $Timer.start(pixel_to_time * ($LoopRegion.position.x + $LoopRegion.size.x - get_local_mouse_position().x))
  137. else:
  138. mouse_pos_x = get_local_mouse_position().x
  139. $LoopRegion.position.x = mouse_pos_x
  140. rect_focus = true
  141. func _on_button_button_up() -> void:
  142. rect_focus = false
  143. if get_meta("loadenable") == true:
  144. print("got meta")
  145. if $LoopRegion.size.x > 0:
  146. Global.trim_infile = true
  147. var length = $AudioStreamPlayer.stream.get_length()
  148. var pixel_to_time = length / 399
  149. Global.infile_start = pixel_to_time * $LoopRegion.position.x
  150. Global.infile_stop = Global.infile_start + (pixel_to_time * $LoopRegion.size.x)
  151. print(Global.trim_infile)
  152. print(Global.infile_start)
  153. print(Global.infile_stop)
  154. else:
  155. Global.trim_infile = false
  156. print(Global.trim_infile)