shithub: opus

Download patch

ref: 1f45081548ee2ca12982cdc607e3347091278af1
parent: 77d02dbd2f6e93cedd83e0ebab15fb2b5fedeab1
author: Jean-Marc Valin <jmvalin@jmvalin.ca>
date: Thu Mar 28 06:54:33 EDT 2019

Implement -feature option

--- a/dnn/include/lpcnet.h
+++ b/dnn/include/lpcnet.h
@@ -125,6 +125,13 @@
   */
 LPCNET_EXPORT int lpcnet_encode(LPCNetEncState *st, const short *pcm, unsigned char *buf);
 
+/** Compute features on LPCNET_PACKET_SAMPLES speech samples (currently 640) and output features for 4 10-ms frames at once.
+  * @param [in] st <tt>LPCNetDecState*</tt>: Encoder state
+  * @param [in] pcm <tt>short **</tt>: Input speech to be analyzed
+  * @param [out] features <tt>float[4][NB_TOTAL_FEATURES]</tt>: Four feature vectors
+  * @retval 0 Success
+  */
+LPCNET_EXPORT int lpcnet_compute_features(LPCNetEncState *st, const short *pcm, float features[4][NB_TOTAL_FEATURES]);
 
 
 /** Gets the size of an <code>LPCNetState</code> structure.
--- a/dnn/lpcnet_demo.c
+++ b/dnn/lpcnet_demo.c
@@ -94,7 +94,17 @@
         }
         lpcnet_decoder_destroy(net);
     } else if (mode == MODE_FEATURES) {
-        fprintf(stderr, "-features not yet implemented\n");
+        LPCNetEncState *net;
+        net = lpcnet_encoder_create();
+        while (1) {
+            float features[4][NB_TOTAL_FEATURES];
+            short pcm[LPCNET_PACKET_SAMPLES];
+            fread(pcm, sizeof(pcm[0]), LPCNET_PACKET_SAMPLES, fin);
+            if (feof(fin)) break;
+            lpcnet_compute_features(net, pcm, features);
+            fwrite(features, sizeof(float), 4*NB_TOTAL_FEATURES, fout);
+        }
+        lpcnet_encoder_destroy(net);
     } else if (mode == MODE_SYNTHESIS) {
         LPCNetState *net;
         net = lpcnet_create();
--- a/dnn/lpcnet_enc.c
+++ b/dnn/lpcnet_enc.c
@@ -733,3 +733,19 @@
   process_superframe(st, buf, NULL, 1, 1);
   return 0;
 }
+
+LPCNET_EXPORT int lpcnet_compute_features(LPCNetEncState *st, const short *pcm, float features[4][NB_TOTAL_FEATURES]) {
+  int i, k;
+  for (k=0;k<4;k++) {
+    float x[FRAME_SIZE];
+    for (i=0;i<FRAME_SIZE;i++) x[i] = pcm[k*FRAME_SIZE + i];
+    preemphasis(x, &st->mem_preemph, x, PREEMPHASIS, FRAME_SIZE);
+    st->pcount = k;
+    compute_frame_features(st, x);
+  }
+  process_superframe(st, NULL, NULL, 0, 0);
+  for (k=0;k<4;k++) {
+    RNN_COPY(&features[k][0], &st->features[k][0], NB_TOTAL_FEATURES);
+  }
+  return 0;
+}
--