cont_adseg.c
/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* ====================================================================
* Copyright (c) 1999-2001 Carnegie Mellon University. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* This work was supported in part by funding from the Defense Advanced
* Research Projects Agency and the National Science Foundation of the
* United States of America, and the CMU Sphinx Speech Consortium.
*
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
* NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ====================================================================
*
*/
/*
* cont_adseg.c -- Continuously listen and segment input speech into utterances.
*
* HISTORY
*
* 27-Jun-96 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
* Created.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <sphinxbase/prim_type.h>
#include <sphinxbase/ad.h>
#include <sphinxbase/cont_ad.h>
#include <sphinxbase/err.h>
/*
* Segment raw A/D input data into utterances whenever silence region of given
* duration is encountered.
* Utterances are written to files named 0001.raw, 0002.raw, 0003.raw, etc.
*/
int
main(int32 argc, char **argv)
{
ad_rec_t *ad;
cont_ad_t *cont;
int32 k, uttno, ts, uttlen, sps, endsilsamples;
float endsil;
int16 buf[4096];
FILE *fp;
char file[1024];
if ((argc != 3) ||
(sscanf(argv[1], "%d", &sps) != 1) ||
(sscanf(argv[2], "%f", &endsil) != 1) || (endsil <= 0.0)) {
E_FATAL("Usage: %s <sampling-rate> <utt-end-sil(sec)>\n", argv[0]);
}
/* Convert desired min. inter-utterance silence duration to #samples */
endsilsamples = (int32) (endsil * sps);
/* Open raw A/D device */
if ((ad = ad_open_sps(sps)) == NULL)
E_FATAL("ad_open_sps(%d) failed\n", sps);
/* Associate new continuous listening module with opened raw A/D device */
if ((cont = cont_ad_init(ad, ad_read)) == NULL)
E_FATAL("cont_ad_init failed\n");
/* Calibrate continuous listening for background noise/silence level */
printf("Calibrating ...");
fflush(stdout);
ad_start_rec(ad);
if (cont_ad_calib(cont) < 0)
printf(" failed\n");
else
printf(" done\n");
/* Forever listen for utterances */
printf("You may speak now\n");
fflush(stdout);
uttno = 0;
for (;;) {
/* Wait for beginning of next utterance; for non-silence data */
while ((k = cont_ad_read(cont, buf, 4096)) == 0);
if (k < 0)
E_FATAL("cont_ad_read failed\n");
/* Non-silence data received; open and write to new logging file */
uttno++;
sprintf(file, "%04d.raw", uttno);
if ((fp = fopen(file, "wb")) == NULL)
E_FATAL_SYSTEM("Failed to open '%s' for reading", file);
fwrite(buf, sizeof(int16), k, fp);
uttlen = k;
printf("Utterance %04d, logging to %s\n", uttno, file);
/* Note current timestamp */
ts = cont->read_ts;
/* Read utterance data until a gap of at least 1 sec observed */
for (;;) {
if ((k = cont_ad_read(cont, buf, 4096)) < 0)
E_FATAL("cont_ad_read failed\n");
if (k == 0) {
/*
* No speech data available; check current timestamp. End of
* utterance if no non-silence data been read for at least 1 sec.
*/
if ((cont->read_ts - ts) > endsilsamples)
break;
}
else {
/* Note timestamp at the end of most recently read speech data */
ts = cont->read_ts;
uttlen += k;
fwrite(buf, sizeof(int16), k, fp);
}
}
fclose(fp);
printf("\tUtterance %04d = %d samples (%.1fsec)\n\n",
uttno, uttlen, (double) uttlen / (double) sps);
}
ad_stop_rec(ad);
cont_ad_close(cont);
ad_close(ad);
return 0;
}
ad_rec_t
Definition: ad.h:255
cont_ad_read
SPHINXBASE_EXPORT int32 cont_ad_read(cont_ad_t *r, int16 *buf, int32 max)
Read raw audio data into the silence filter.
Definition: cont_ad_base.c:863
cont_ad.h
Continuous A/D listening and silence filtering module.
prim_type.h
Basic type definitions used in Sphinx.
E_FATAL_SYSTEM
#define E_FATAL_SYSTEM
Print error text; Call perror(""); exit(errno);.
Definition: err.h:132
cont_ad_close
SPHINXBASE_EXPORT int32 cont_ad_close(cont_ad_t *cont)
Close the continuous listening object.
Definition: cont_ad_base.c:1264
cont_ad_init
SPHINXBASE_EXPORT cont_ad_t * cont_ad_init(ad_rec_t *ad, int32(*adfunc)(ad_rec_t *ad, int16 *buf, int32 max))
Initialize a continuous listening/silence filtering object.
cont_ad_calib
SPHINXBASE_EXPORT int32 cont_ad_calib(cont_ad_t *cont)
Calibrate the silence filter.
Definition: cont_ad_base.c:1022
err.h
Implementation of logging routines.
ad.h
generic live audio interface for recording and playback
cont_ad_t
Continuous listening module or object Continuous listening module or object.
Definition: cont_ad.h:151
E_FATAL
#define E_FATAL
Exit with non-zero status after error message.
Definition: err.h:127
cont_ad_t::read_ts
int32 read_ts
Absolute timestamp (total no.
Definition: cont_ad.h:167
ad_open_sps
SPHINXBASE_EXPORT ad_rec_t * ad_open_sps(int32 samples_per_sec)
Open the default audio device with a given sampling rate.
Definition: ad_alsa.c:290