From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.4 required=3.0 tests=AWL,BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 536E71F5B1 for ; Fri, 31 Jul 2020 09:39:47 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1k1RVx-0006fu-6c; Fri, 31 Jul 2020 09:39:45 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k1RVv-0006fi-LD for sox-devel@lists.sourceforge.net; Fri, 31 Jul 2020 09:39:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=s8I3dhDtGegvTCwWni8nFSl9uNf7RPZHL9t1g+ZS/Io=; b=M6K8nW11EXf65yvnUAHRS2TETc 7rTeYmzXcNTM1SM7//1b2zHJGUH5Dlkv6M9YEf+Kn6JxXiY2QErBI4fOCsSYS/72cmGl1hsSlfBtK xI8lalg0x6sdtH3pFfZXwzjLDJA6/59pJ1tlEtlzRPcKHS53Pb+8bBcn325fUNyLjx9k=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=s8I3dhDtGegvTCwWni8nFSl9uNf7RPZHL9t1g+ZS/Io=; b=WI0mUs6n5Fv+dg0YpygJkgpkF5 4me4dsANYXD5yaL/rTA8O0pbcDWU3XWBc+/N7uFlgD9JzZ3MwT9Tg5LyNSk/CY0kkzOC1gQ7qYxNV 2GUBCPBXkt4LmxllQHo8HboDX1ynOtRI5Yg+Uzyj/fEUagBLP0TXVy/OlGUMlt+yLMc8=; Received: from dcvr.yhbt.net ([64.71.152.64]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) id 1k1RVt-00FjHR-TL for sox-devel@lists.sourceforge.net; Fri, 31 Jul 2020 09:39:43 +0000 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 9B1A81F8F2; Fri, 31 Jul 2020 09:38:03 +0000 (UTC) From: Eric Wong To: =?UTF-8?q?M=C3=A5ns=20Rullg=C3=A5rd?= Date: Fri, 31 Jul 2020 09:38:01 +0000 Message-Id: <20200731093801.23548-10-normalperson@yhbt.net> In-Reply-To: <20200731093801.23548-1-normalperson@yhbt.net> References: <20200731093801.23548-1-normalperson@yhbt.net> MIME-Version: 1.0 X-Headers-End: 1k1RVt-00FjHR-TL Subject: [PATCH RESEND 9/9] Added average power spectrum for stat -freq -a X-BeenThere: sox-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: sox-devel@lists.sourceforge.net Cc: Pander , sox-devel@lists.sourceforge.net Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: sox-devel-bounces@lists.sourceforge.net From: Pander references: https://sourceforge.net/p/sox/mailman/message/33186778/ https://sourceforge.net/p/sox/mailman/message/33175940/ https://sourceforge.net/p/sox/mailman/message/33625255/ Edited-by: Eric Wong [ew: removed extraneous whitespace changes, squashed re_average initialization fix from Pander, removed unnecessary check before free(3) call (ref: <5498A2F9.2010500@free.fr>), converted fft_average to bool] --- sox.1 | 6 ++++++ src/stat.c | 27 ++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/sox.1 b/sox.1 index 04ab09b1..d64762af 100644 --- a/sox.1 +++ b/sox.1 @@ -3596,6 +3596,12 @@ audio in SoX's internal buffer. This is mainly used to help track down endian problems that sometimes occur in cross-platform versions of SoX. .SP +The +.B \-a +option +will output the average input's power spectrum instead of the default +behavior in which the power spectrum is given for each 4096 point DFT. +.SP See also the .B stats effect. diff --git a/src/stat.c b/src/stat.c index cdea27cc..0ca6c232 100644 --- a/src/stat.c +++ b/src/stat.c @@ -34,6 +34,7 @@ typedef struct { float *re_out; unsigned long fft_size; unsigned long fft_offset; + sox_bool fft_average; } priv_t; @@ -69,6 +70,8 @@ static int sox_stat_getopts(sox_effect_t * effp, int argc, char **argv) stat->fft = 1; else if (!(strcmp(*argv, "-d"))) stat->volume = 2; + else if (!(strcmp(*argv, "-a"))) + stat->fft_average = sox_true; else { lsx_fail("Summary effect: unknown option"); return SOX_EOF; @@ -100,6 +103,7 @@ static int sox_stat_start(sox_effect_t * effp) stat->bin[i] = 0; stat->fft_size = 4096; + stat->fft_average = sox_false; stat->re_in = stat->re_out = NULL; if (stat->fft) { @@ -134,6 +138,16 @@ static int sox_stat_flow(sox_effect_t * effp, const sox_sample_t *ibuf, sox_samp priv_t * stat = (priv_t *) effp->priv; int done, x, len = min(*isamp, *osamp); short count = 0; + float *re_average = NULL; + unsigned samples = 0; + float ffa = 0.0; + unsigned i; + + if (stat->fft_average) { + samples = (stat->fft_size / 2); + ffa = effp->in_signal.rate / samples; + re_average = lsx_malloc(sizeof(float) * (int)samples); + } if (len) { if (stat->read == 0) /* 1st sample */ @@ -146,10 +160,20 @@ static int sox_stat_flow(sox_effect_t * effp, const sox_sample_t *ibuf, sox_samp if (stat->fft_offset >= stat->fft_size) { stat->fft_offset = 0; + if (stat->fft_average) { + lsx_power_spectrum_f((int)samples, stat->re_in, stat->re_out); + for (i = 0; i < samples / 2; i++) /* FIXME: should be <= samples / 2 */ + re_average[i] += stat->re_out[i]; + } else { print_power_spectrum((unsigned) stat->fft_size, effp->in_signal.rate, stat->re_in, stat->re_out); + } } } + if (stat->fft_average) { + for (i = 0; i < samples / 2; i++) /* FIXME: should be <= samples / 2 */ + fprintf(stderr, " %f %f\n", ffa * i, re_average[i] / len); + } } for (done = 0; done < len; done++) { @@ -192,6 +216,7 @@ static int sox_stat_flow(sox_effect_t * effp, const sox_sample_t *ibuf, sox_samp stat->read += len; } + free(re_average); *isamp = *osamp = len; /* Process all samples */ @@ -320,7 +345,7 @@ static int sox_stat_stop(sox_effect_t * effp) static sox_effect_handler_t sox_stat_effect = { "stat", - "[ -s N ] [ -rms ] [-freq] [ -v ] [ -d ]", + "[ -s N ] [ -rms ] [-freq] [ -v ] [ -d ] [ -a ]", SOX_EFF_MCHAN | SOX_EFF_MODIFY, sox_stat_getopts, sox_stat_start, _______________________________________________ SoX-devel mailing list SoX-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sox-devel