]> code.delx.au - pulseaudio/blobdiff - src/modules/module-virtual-surround-sink.c
Remove pa_bool_t and replace it with bool.
[pulseaudio] / src / modules / module-virtual-surround-sink.c
index fc8f898f546b3ce1ac506a540150f2191b131bdb..356ca22e04a47ceddbad93dfecf642f5d2c92c90 100644 (file)
@@ -48,7 +48,7 @@
 PA_MODULE_AUTHOR("Niels Ole Salscheider");
 PA_MODULE_DESCRIPTION(_("Virtual surround sink"));
 PA_MODULE_VERSION(PACKAGE_VERSION);
-PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_LOAD_ONCE(false);
 PA_MODULE_USAGE(
         _("sink_name=<name for the sink> "
           "sink_properties=<properties for the sink> "
@@ -68,14 +68,14 @@ struct userdata {
     pa_module *module;
 
     /* FIXME: Uncomment this and take "autoloaded" as a modarg if this is a filter */
-    /* pa_bool_t autoloaded; */
+    /* bool autoloaded; */
 
     pa_sink *sink;
     pa_sink_input *sink_input;
 
     pa_memblockq *memblockq;
 
-    pa_bool_t auto_desc;
+    bool auto_desc;
     unsigned channels;
     unsigned hrir_channels;
 
@@ -165,7 +165,7 @@ static void sink_request_rewind_cb(pa_sink *s) {
     /* Just hand this one over to the master sink */
     pa_sink_input_request_rewind(u->sink_input,
                                  s->thread_info.rewind_nbytes +
-                                 pa_memblockq_get_length(u->memblockq), TRUE, FALSE, FALSE);
+                                 pa_memblockq_get_length(u->memblockq), true, false, false);
 }
 
 /* Called from I/O thread context */
@@ -196,7 +196,7 @@ static void sink_set_volume_cb(pa_sink *s) {
         !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
         return;
 
-    pa_sink_input_set_volume(u->sink_input, &s->real_volume, s->save_volume, TRUE);
+    pa_sink_input_set_volume(u->sink_input, &s->real_volume, s->save_volume, true);
 }
 
 /* Called from main context */
@@ -252,8 +252,8 @@ static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk
 
     pa_memblockq_drop(u->memblockq, n * u->sink_fs);
 
-    src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
-    dst = (float*) pa_memblock_acquire(chunk->memblock);
+    src = pa_memblock_acquire_chunk(&tchunk);
+    dst = pa_memblock_acquire(chunk->memblock);
 
     for (l = 0; l < n; l++) {
         memcpy(((char*) u->input_buffer) + u->input_buffer_offset * u->sink_fs, ((char *) src) + l * u->sink_fs, u->sink_fs);
@@ -303,7 +303,7 @@ static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
         u->sink->thread_info.rewind_nbytes = 0;
 
         if (amount > 0) {
-            pa_memblockq_seek(u->memblockq, - (int64_t) amount, PA_SEEK_RELATIVE, TRUE);
+            pa_memblockq_seek(u->memblockq, - (int64_t) amount, PA_SEEK_RELATIVE, true);
 
             /* Reset the input buffer */
             memset(u->input_buffer, 0, u->hrir_samples * u->sink_fs);
@@ -382,11 +382,11 @@ static void sink_input_attach_cb(pa_sink_input *i) {
 
     pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
 
-    pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
+    pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i) * u->sink_fs / u->fs);
 
     /* FIXME: Too small max_rewind:
      * https://bugs.freedesktop.org/show_bug.cgi?id=53709 */
-    pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
+    pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i) * u->sink_fs / u->fs);
 
     pa_sink_attach_within_thread(u->sink);
 }
@@ -410,7 +410,7 @@ static void sink_input_kill_cb(pa_sink_input *i) {
     pa_sink_unref(u->sink);
     u->sink = NULL;
 
-    pa_module_unload_request(u->module, TRUE);
+    pa_module_unload_request(u->module, true);
 }
 
 /* Called from IO thread context */
@@ -425,20 +425,10 @@ static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t s
     if (PA_SINK_INPUT_IS_LINKED(state) &&
         i->thread_info.state == PA_SINK_INPUT_INIT) {
         pa_log_debug("Requesting rewind due to state change.");
-        pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
+        pa_sink_input_request_rewind(i, 0, false, true, true);
     }
 }
 
-/* Called from main context */
-static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
-    struct userdata *u;
-
-    pa_sink_input_assert_ref(i);
-    pa_assert_se(u = i->userdata);
-
-    return u->sink != dest;
-}
-
 /* Called from main context */
 static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
     struct userdata *u;
@@ -537,8 +527,8 @@ int pa__init(pa_module*m) {
     pa_sink *master=NULL;
     pa_sink_input_new_data sink_input_data;
     pa_sink_new_data sink_data;
-    pa_bool_t use_volume_sharing = TRUE;
-    pa_bool_t force_flat_volume = FALSE;
+    bool use_volume_sharing = true;
+    bool force_flat_volume = false;
     pa_memchunk silence;
 
     const char *hrir_file;
@@ -550,10 +540,13 @@ int pa__init(pa_module*m) {
     pa_channel_map hrir_map;
 
     pa_sample_spec hrir_temp_ss;
-    pa_memchunk hrir_temp_chunk;
+    pa_memchunk hrir_temp_chunk, hrir_temp_chunk_resampled;
     pa_resampler *resampler;
 
+    size_t hrir_copied_length, hrir_total_length;
+
     hrir_temp_chunk.memblock = NULL;
+    hrir_temp_chunk_resampled.memblock = NULL;
 
     pa_assert(m);
 
@@ -668,7 +661,7 @@ int pa__init(pa_module*m) {
     pa_sink_set_set_mute_callback(u->sink, sink_set_mute_cb);
     if (!use_volume_sharing) {
         pa_sink_set_set_volume_callback(u->sink, sink_set_volume_cb);
-        pa_sink_enable_decibel_volume(u->sink, TRUE);
+        pa_sink_enable_decibel_volume(u->sink, true);
     }
     /* Normally this flag would be enabled automatically be we can force it. */
     if (force_flat_volume)
@@ -681,7 +674,7 @@ int pa__init(pa_module*m) {
     pa_sink_input_new_data_init(&sink_input_data);
     sink_input_data.driver = __FILE__;
     sink_input_data.module = m;
-    pa_sink_input_new_data_set_sink(&sink_input_data, master, FALSE);
+    pa_sink_input_new_data_set_sink(&sink_input_data, master, false);
     sink_input_data.origin_sink = u->sink;
     pa_proplist_setf(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Virtual Surround Sink Stream from %s", pa_proplist_gets(u->sink->proplist, PA_PROP_DEVICE_DESCRIPTION));
     pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
@@ -704,7 +697,6 @@ int pa__init(pa_module*m) {
     u->sink_input->attach = sink_input_attach_cb;
     u->sink_input->detach = sink_input_detach_cb;
     u->sink_input->state_change = sink_input_state_change_cb;
-    u->sink_input->may_move_to = sink_input_may_move_to_cb;
     u->sink_input->moving = sink_input_moving_cb;
     u->sink_input->volume_changed = use_volume_sharing ? NULL : sink_input_volume_changed_cb;
     u->sink_input->mute_changed = sink_input_mute_changed_cb;
@@ -719,17 +711,47 @@ int pa__init(pa_module*m) {
     /* resample hrir */
     resampler = pa_resampler_new(u->sink->core->mempool, &hrir_temp_ss, &hrir_map, &hrir_ss, &hrir_map,
                                  PA_RESAMPLER_SRC_SINC_BEST_QUALITY, PA_RESAMPLER_NO_REMAP);
-    pa_resampler_run(resampler, &hrir_temp_chunk, &hrir_temp_chunk);
-    pa_resampler_free(resampler);
 
-    u->hrir_samples =  hrir_temp_chunk.length / pa_frame_size(&hrir_ss);
+    u->hrir_samples = hrir_temp_chunk.length / pa_frame_size(&hrir_temp_ss) * hrir_ss.rate / hrir_temp_ss.rate;
+    if (u->hrir_samples > 64) {
+        u->hrir_samples = 64;
+        pa_log("The (resampled) hrir contains more than 64 samples. Only the first 64 samples will be used to limit processor usage.");
+    }
+
+    hrir_total_length = u->hrir_samples * pa_frame_size(&hrir_ss);
     u->hrir_channels = hrir_ss.channels;
 
-    /* copy hrir data */
-    hrir_data = (float *) pa_memblock_acquire(hrir_temp_chunk.memblock);
-    u->hrir_data = (float *) pa_xmalloc(hrir_temp_chunk.length);
-    memcpy(u->hrir_data, hrir_data, hrir_temp_chunk.length);
-    pa_memblock_release(hrir_temp_chunk.memblock);
+    u->hrir_data = (float *) pa_xmalloc(hrir_total_length);
+    hrir_copied_length = 0;
+
+    /* add silence to the hrir until we get enough samples out of the resampler */
+    while (hrir_copied_length < hrir_total_length) {
+        pa_resampler_run(resampler, &hrir_temp_chunk, &hrir_temp_chunk_resampled);
+        if (hrir_temp_chunk.memblock != hrir_temp_chunk_resampled.memblock) {
+            /* Silence input block */
+            pa_silence_memblock(hrir_temp_chunk.memblock, &hrir_temp_ss);
+        }
+
+        if (hrir_temp_chunk_resampled.memblock) {
+            /* Copy hrir data */
+            hrir_data = (float *) pa_memblock_acquire(hrir_temp_chunk_resampled.memblock);
+
+            if (hrir_total_length - hrir_copied_length >= hrir_temp_chunk_resampled.length) {
+                memcpy(u->hrir_data + hrir_copied_length, hrir_data, hrir_temp_chunk_resampled.length);
+                hrir_copied_length += hrir_temp_chunk_resampled.length;
+            } else {
+                memcpy(u->hrir_data + hrir_copied_length, hrir_data, hrir_total_length - hrir_copied_length);
+                hrir_copied_length = hrir_total_length;
+            }
+
+            pa_memblock_release(hrir_temp_chunk_resampled.memblock);
+            pa_memblock_unref(hrir_temp_chunk_resampled.memblock);
+            hrir_temp_chunk_resampled.memblock = NULL;
+        }
+    }
+
+    pa_resampler_free(resampler);
+
     pa_memblock_unref(hrir_temp_chunk.memblock);
     hrir_temp_chunk.memblock = NULL;
 
@@ -797,6 +819,9 @@ fail:
     if (hrir_temp_chunk.memblock)
         pa_memblock_unref(hrir_temp_chunk.memblock);
 
+    if (hrir_temp_chunk_resampled.memblock)
+        pa_memblock_unref(hrir_temp_chunk_resampled.memblock);
+
     if (ma)
         pa_modargs_free(ma);