Discussion:
[SoX-devel] Half length output with libsox for stereo -> mono
Stephen.Paterson
2013-04-10 12:04:28 UTC
Permalink
Hi all,

I've found a couple of threads relating to this problem but I can't for the life of me get it to work.

The following command line works fine:

sox original.wav -c 1 -r 8000 converted.wav

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).
If I leave the output as stereo I get the full length audio but sadly stereo is no use to me.
The 2 threads I've found are [Sox-devel] 'Effects going crazy' - 2012-05-07 and [SoX] 'Channels/remix does not function as documented' - 2011-11-12
Nothing suggested in either seems to work for me. Could someone take a look at the attached code (it's very simple) to see if they can see what I'm doing wrong please? It's driving me crazy! I'm at the point of giving up and just spawning the sox executable from my code but that would make me cry.

The following is the format of my test input file but it needs to work for any wav file:
RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, stereo 24000 Hz

Desired output will always be in the following format regardless of the input format:
RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 8000 Hz

Apologies if this is more appropriate for sox-users. If so, just let me know and I'll post there.

Cheers


Stephen Paterson
Developer | SYNETY
www.synety.com<http://www.synety.com/>

ddi: 01134 166029
main: 0330 335 0000

SMARTER COMMUNICATIONS
Confidentiality: This e-mail transmission, including any attachments, is intended only for the named recipient(s) and may contain information that is privileged, confidential and/or exempt from disclosure under applicable law. If you have received this transmission in error, or are not the named recipient(s), please notify the sender immediately by return e-mail and permanently delete this transmission, including any attachments.

Security: This e-mail and any attachments are believed to be free from any virus but it is the responsibility of the recipient to ensure this is so. E-mail is not a 100% secure communications medium. We recommend you observe this when e-mailing us.
Stephen.Paterson
2013-04-10 13:36:16 UTC
Permalink
OK, after some more trial and error I have the correct data, however, it is not writing the wav header correctly. Now I don't really care about that as when I play it back I can specify the format in which it will be played and everything sounds fine.
So I'm not really concerned but I would still be interested to know how to do it all properly. At the moment I can either get the correct header info or the correct data but not both.

Cheers

Stephen Paterson
Developer | SYNETY
www.synety.com<http://www.synety.com/>

ddi: 01134 166029
main: 0330 335 0000

SMARTER COMMUNICATIONS
Confidentiality: This e-mail transmission, including any attachments, is intended only for the named recipient(s) and may contain information that is privileged, confidential and/or exempt from disclosure under applicable law. If you have received this transmission in error, or are not the named recipient(s), please notify the sender immediately by return e-mail and permanently delete this transmission, including any attachments.

Security: This e-mail and any attachments are believed to be free from any virus but it is the responsibility of the recipient to ensure this is so. E-mail is not a 100% secure communications medium. We recommend you observe this when e-mailing us.

From: Stephen.Paterson [mailto:***@synety.com]
Sent: 10 April 2013 13:04
To: sox-***@lists.sourceforge.net
Subject: [SoX-devel] Half length output with libsox for stereo -> mono

Hi all,

I've found a couple of threads relating to this problem but I can't for the life of me get it to work.

The following command line works fine:

sox original.wav -c 1 -r 8000 converted.wav

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).
If I leave the output as stereo I get the full length audio but sadly stereo is no use to me.
The 2 threads I've found are [Sox-devel] 'Effects going crazy' - 2012-05-07 and [SoX] 'Channels/remix does not function as documented' - 2011-11-12
Nothing suggested in either seems to work for me. Could someone take a look at the attached code (it's very simple) to see if they can see what I'm doing wrong please? It's driving me crazy! I'm at the point of giving up and just spawning the sox executable from my code but that would make me cry.

The following is the format of my test input file but it needs to work for any wav file:
RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, stereo 24000 Hz

Desired output will always be in the following format regardless of the input format:
RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 8000 Hz

Apologies if this is more appropriate for sox-users. If so, just let me know and I'll post there.

Cheers


Stephen Paterson
Developer | SYNETY
www.synety.com<http://www.synety.com/>

ddi: 01134 166029
main: 0330 335 0000

SMARTER COMMUNICATIONS
Confidentiality: This e-mail transmission, including any attachments, is intended only for the named recipient(s) and may contain information that is privileged, confidential and/or exempt from disclosure under applicable law. If you have received this transmission in error, or are not the named recipient(s), please notify the sender immediately by return e-mail and permanently delete this transmission, including any attachments.

Security: This e-mail and any attachments are believed to be free from any virus but it is the responsibility of the recipient to ensure this is so. E-mail is not a 100% secure communications medium. We recommend you observe this when e-mailing us.
Ulrich Klauer
2013-04-10 15:47:51 UTC
Permalink
Post by Stephen.Paterson
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
Stephen.Paterson
2013-04-11 10:15:59 UTC
Permalink
Hi Ulrich,

Thanks for that. All working now.

Cheers

Stephen Paterson
Developer   |  SYNETY
www.synety.com

ddi:  01134 166029
main: 0330 335 0000
 
SMARTER COMMUNICATIONS
Confidentiality: This e-mail transmission, including any attachments, is intended only for the named recipient(s) and may contain information that is privileged, confidential and/or exempt from disclosure under applicable law. If you have received this transmission in error, or are not the named recipient(s), please notify the sender immediately by return e-mail and permanently delete this transmission, including any attachments.

Security: This e-mail and any attachments are believed to be free from any virus but it is the responsibility of the recipient to ensure this is so. E-mail is not a 100% secure communications medium. We recommend you observe this when e-mailing us.


-----Original Message-----
From: Ulrich Klauer [mailto:***@chirlu.de]
Sent: 10 April 2013 16:48
To: sox-***@lists.sourceforge.net
Subject: Re: [SoX-devel] Half length output with libsox for stereo -> mono
Post by Stephen.Paterson
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 sig

Loading...