[Bf-blender-cvs] [6ee633575fc] temp-sample-sound-node: initial sound sampling

Jacques Lucke noreply at git.blender.org
Sun Nov 14 14:29:40 CET 2021


Commit: 6ee633575fcc8e8dfad297d0423e420177db6624
Author: Jacques Lucke
Date:   Sun Nov 14 12:57:00 2021 +0100
Branches: temp-sample-sound-node
https://developer.blender.org/rB6ee633575fcc8e8dfad297d0423e420177db6624

initial sound sampling

===================================================================

M	source/blender/blenkernel/intern/sound.c
M	source/blender/makesdna/DNA_sound_types.h
M	source/blender/nodes/CMakeLists.txt
M	source/blender/nodes/geometry/nodes/node_geo_sample_sound.cc

===================================================================

diff --git a/source/blender/blenkernel/intern/sound.c b/source/blender/blenkernel/intern/sound.c
index f523c5e02bd..1cd1f024241 100644
--- a/source/blender/blenkernel/intern/sound.c
+++ b/source/blender/blenkernel/intern/sound.c
@@ -86,6 +86,8 @@ static void sound_copy_data(Main *UNUSED(bmain),
   sound_dst->playback_handle = NULL;
   sound_dst->spinlock = MEM_mallocN(sizeof(SpinLock), "sound_spinlock");
   BLI_spin_init(sound_dst->spinlock);
+  sound_dst->tot_samples = 0;
+  sound_dst->samples = NULL;
 
   /* Just to be sure, should not have any value actually after reading time. */
   sound_dst->ipo = NULL;
@@ -117,6 +119,9 @@ static void sound_free_data(ID *id)
     MEM_freeN(sound->spinlock);
     sound->spinlock = NULL;
   }
+  if (sound->samples) {
+    AUD_Sound_freeData(sound->samples);
+  }
 }
 
 static void sound_foreach_cache(ID *id,
@@ -162,6 +167,8 @@ static void sound_blend_read_data(BlendDataReader *reader, ID *id)
   sound->tags = 0;
   sound->handle = NULL;
   sound->playback_handle = NULL;
+  sound->samples = NULL;
+  sound->tot_samples = 0;
 
   /* versioning stuff, if there was a cache, then we enable caching: */
   if (sound->cache) {
diff --git a/source/blender/makesdna/DNA_sound_types.h b/source/blender/makesdna/DNA_sound_types.h
index e6394f0a56a..387322e9dea 100644
--- a/source/blender/makesdna/DNA_sound_types.h
+++ b/source/blender/makesdna/DNA_sound_types.h
@@ -93,6 +93,11 @@ typedef struct bSound {
   void *spinlock;
   /* XXX unused currently (SOUND_TYPE_LIMITER) */
   /* float start, end; */
+
+  float *samples;
+  int tot_samples;
+  char _pad2[4];
+  void *_pad3;
 } bSound;
 
 /* XXX unused currently */
diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt
index 782299bd707..2e7a4704c5a 100644
--- a/source/blender/nodes/CMakeLists.txt
+++ b/source/blender/nodes/CMakeLists.txt
@@ -553,4 +553,12 @@ if(WITH_OPENVDB)
   add_definitions(-DWITH_OPENVDB ${OPENVDB_DEFINITIONS})
 endif()
 
+if(WITH_AUDASPACE)
+  add_definitions(-DWITH_AUDASPACE)
+
+  list(APPEND INC_SYS
+    ${AUDASPACE_C_INCLUDE_DIRS}
+  )
+endif()
+
 blender_add_lib(bf_nodes "${SRC}" "${INC}" "${INC_SYS}" "${LIB}")
diff --git a/source/blender/nodes/geometry/nodes/node_geo_sample_sound.cc b/source/blender/nodes/geometry/nodes/node_geo_sample_sound.cc
index 64326107a15..2de55e4769b 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_sample_sound.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_sample_sound.cc
@@ -17,6 +17,14 @@
 #include "UI_interface.h"
 #include "UI_resources.h"
 
+#include "DNA_sound_types.h"
+
+#include "AUD_Sound.h"
+
+#include "BKE_sound.h"
+
+#include "DEG_depsgraph_query.h"
+
 #include "node_geometry_util.hh"
 
 namespace blender::nodes {
@@ -36,6 +44,37 @@ static void geo_node_sample_sound_layout(uiLayout *layout, bContext *UNUSED(C),
 
 static void geo_node_sample_sound_exec(GeoNodeExecParams params)
 {
+  bSound *sound_id = (bSound *)params.node().id;
+  Scene *scene = DEG_get_input_scene(params.depsgraph());
+  const float fps = FPS;
+  if (sound_id != nullptr) {
+    AUD_Sound *sound = (AUD_Sound *)sound_id->handle;
+    if (sound != NULL) {
+      if (sound_id->samples == NULL) {
+        AUD_Specs specs;
+        sound_id->samples = AUD_Sound_data(sound, &sound_id->tot_samples, &specs);
+      }
+      AUD_Specs specs = AUD_Sound_getSpecs(sound);
+      const float *samples = sound_id->samples;
+      const int tot_samples = sound_id->tot_samples;
+      const float sample_rate = specs.rate;
+
+      const float frame = params.get_input<float>("Frame");
+      const float time = (frame - 1) / fps;
+      const int end_sample = std::max(
+          0, std::min<int>(time * sample_rate * specs.channels, tot_samples));
+      const int sample_duration = 1000;
+      const int start_sample = std::max(0,
+                                        std::min<int>(end_sample - sample_duration, tot_samples));
+      float sum = 0.0f;
+      for (int i = start_sample; i < end_sample; i++) {
+        sum += fabsf(samples[i]);
+      }
+      const float average = sum / (end_sample - start_sample);
+      params.set_output("Volume", average);
+      return;
+    }
+  }
   params.set_output("Volume", 0.0f);
 }



More information about the Bf-blender-cvs mailing list