sox-devel@lists.sourceforge.net unofficial mirror
 help / color / mirror / code / Atom feed
From: Ulrich Klauer <ulrich@chirlu.de>
To: sox-devel@lists.sourceforge.net
Subject: Re: Half length output with libsox for stereo -> mono
Date: Wed, 10 Apr 2013 17:47:51 +0200	[thread overview]
Message-ID: <20130410174751.Horde.Np1as3bg2m6mEzrCjDQ57g1@webmail.df.eu> (raw)
In-Reply-To: <BDC1841AE4B16746AB058F709D669BB0458AB650@AMSPRD0311MB400.eurprd03.prod.outlook.com>

[-- Attachment #1: Type: text/plain, Size: 1214 bytes --]

Stephen Paterson wrote:

> I can't get it to work with libsox. I've made a tiny modification to  
> example3.c that should do the same but no matter what I try I always  
> end up with a file that is exactly half the length of the one I get  
> from sox (or some crazy audio).

example3 is buggy. It doesn't take into account that sox_add_effect()  
modifies the "in" signalinfo, which is why it isn't a good idea to  
pass it a signalinfo that is still in use elsewhere, or to call  
sox_add_effect() with identical in and out signalinfos.

What is happening here is that in->signal.length, after the  
sox_open_read(), contains some value like 882000, for a 10-second  
stereo 44100 Hz input file. After adding "rate", the value will have  
changed to 160000, and after "channels", to 80000. Because it is the  
same memory location all the time, the input format handler (from  
sox_open_read()) will see this value as well and think that its file  
is only 80000 samples long. It will therefore stop reading after 80000  
samples (/2 channels, /44100 Hz = 0.907 seconds), and that is all that  
is processed by the effects chain.

See the attached diff for how to use a copy of the signalinfo instead.

Ulrich

[-- Attachment #2: diff.txt --]
[-- Type: text/plain, Size: 1913 bytes --]

--- main-pre.c	2013-04-10 17:12:23.000000000 +0200
+++ main.c	2013-04-10 17:20:10.000000000 +0200
@@ -4,6 +4,7 @@
 int main(int argc, char * argv[])
 {
 	static sox_format_t * in, * out;
+	sox_signalinfo_t c;
 	sox_effects_chain_t * chain;
 	sox_effect_t * e;
 	char * args[10];
@@ -13,13 +14,14 @@
 
 	assert(sox_init() == SOX_SUCCESS);
 	assert(in = sox_open_read(argv[1], NULL, NULL, NULL));
+	c = in->signal; /* NB: deep copy */
 	assert(out= sox_open_write("converted.wav", &in->signal, NULL, "wav", NULL, NULL));
 
 	chain = sox_create_effects_chain(&in->encoding, &out->encoding);
 
 	e = sox_create_effect(sox_find_effect("input"));
 	args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
-	assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
+	assert(sox_add_effect(chain, e, &c, &in->signal) == SOX_SUCCESS);
 	free(e);
 
 	out->signal.precision = 16;
@@ -29,20 +31,20 @@
 	if (in->signal.rate != out->signal.rate) {
 		e = sox_create_effect(sox_find_effect("rate"));
 		assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
-		assert(sox_add_effect(chain, e, &in->signal, &out->signal) == SOX_SUCCESS);
+		assert(sox_add_effect(chain, e, &c, &out->signal) == SOX_SUCCESS);
 		free(e);
 	}
 
 	if (in->signal.channels != out->signal.channels) {
 		e = sox_create_effect(sox_find_effect("channels"));
 		assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
-		assert(sox_add_effect(chain, e, &in->signal, &out->signal) == SOX_SUCCESS);
+		assert(sox_add_effect(chain, e, &c, &out->signal) == SOX_SUCCESS);
 		free(e);
 	}
 
 	e = sox_create_effect(sox_find_effect("output"));
 	args[0] = (char *)out, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
-	assert(sox_add_effect(chain, e, &in->signal, &out->signal) == SOX_SUCCESS);
+	assert(sox_add_effect(chain, e, &c, &out->signal) == SOX_SUCCESS);
 	free(e);
 
 	sox_flow_effects(chain, NULL, NULL);

[-- Attachment #3: Type: text/plain, Size: 416 bytes --]

------------------------------------------------------------------------------
Precog is a next-generation analytics platform capable of advanced
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!
http://www2.precog.com/precogplatform/slashdotnewsletter

[-- Attachment #4: Type: text/plain, Size: 158 bytes --]

_______________________________________________
SoX-devel mailing list
SoX-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sox-devel

  parent reply	other threads:[~2013-04-10 15:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-10 12:04 Half length output with libsox for stereo -> mono Stephen.Paterson
2013-04-10 13:36 ` Stephen.Paterson
2013-04-10 15:47 ` Ulrich Klauer [this message]
2013-04-11 10:15   ` Stephen.Paterson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.sourceforge.net/lists/listinfo/sox-devel

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130410174751.Horde.Np1as3bg2m6mEzrCjDQ57g1@webmail.df.eu \
    --to=sox-devel@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/sox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).