Пример #1
0
int
ad1848_set_channel_gain(struct ad1848_softc *sc, int device,
    struct ad1848_volume *gp)
{
	struct ad1848_mixerinfo *info = &mixer_channel_info[device];
	u_char reg;
	u_int atten;

	sc->gains[device] = *gp;

	atten = ((AUDIO_MAX_GAIN - gp->left) * info->atten_bits) /
	    AUDIO_MAX_GAIN;

	reg = ad_read(sc, info->left_reg) & (info->atten_mask);
	if (device == AD1848_MONITOR_CHANNEL)
		reg |= ((atten & info->atten_bits) << 2);
	else
		reg |= ((atten & info->atten_bits));

	ad_write(sc, info->left_reg, reg);

	if (!info->right_reg)
		return 0;

	atten = ((AUDIO_MAX_GAIN - gp->right) * info->atten_bits) /
	    AUDIO_MAX_GAIN;
	reg = ad_read(sc, info->right_reg);
	reg &= (info->atten_mask);
	ad_write(sc, info->right_reg, (atten & info->atten_bits) | reg);

	return 0;
}
Пример #2
0
/*
 * Close function is called at splaudio().
 */
void
ad1848_close(void *addr)
{
	struct ad1848_softc *sc = addr;
	u_char r;

	ad1848_halt_output(sc);
	ad1848_halt_input(sc);

	sc->sc_pintr = NULL;
	sc->sc_rintr = NULL;

	DPRINTF(("ad1848_close: stop DMA\n"));

	ad_write(sc, SP_LOWER_BASE_COUNT, (u_char)0);
	ad_write(sc, SP_UPPER_BASE_COUNT, (u_char)0);

	/* Disable interrupts */
	DPRINTF(("ad1848_close: disable intrs\n"));
	ad_write(sc, SP_PIN_CONTROL, 
	    ad_read(sc, SP_PIN_CONTROL) & ~INTERRUPT_ENABLE);

	DPRINTF(("ad1848_close: disable capture and playback\n"));
	r = ad_read(sc, SP_INTERFACE_CONFIG);
	r &= ~(CAPTURE_ENABLE | PLAYBACK_ENABLE);
	ad_write(sc, SP_INTERFACE_CONFIG, r);

#ifdef AUDIO_DEBUG
	if (ad1848debug > 2)
		ad1848_dump_regs(sc);
#endif
}
Пример #3
0
int
ad1848_set_rec_port(struct ad1848_softc *sc, int port)
{
	u_char inp, reg;

	DPRINTF(("ad1848_set_rec_port: 0x%x\n", port));

	if (port == MIC_IN_PORT) {
		inp = MIC_INPUT;
	} else if (port == LINE_IN_PORT) {
		inp = LINE_INPUT;
	} else if (port == DAC_IN_PORT) {
		inp = MIXED_DAC_INPUT;
	} else if (sc->mode == 2 && port == AUX1_IN_PORT) {
		inp = AUX_INPUT;
	} else
		return EINVAL;

	reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
	reg &= INPUT_SOURCE_MASK;
	ad_write(sc, SP_LEFT_INPUT_CONTROL, (inp | reg));

	reg = ad_read(sc, SP_RIGHT_INPUT_CONTROL);
	reg &= INPUT_SOURCE_MASK;
	ad_write(sc, SP_RIGHT_INPUT_CONTROL, (inp | reg));

	sc->rec_port = port;

	return 0;
}
Пример #4
0
/*
 * Wait for the user to type a <CR> then capture input audio for approx. 2 sec
 * and compute a measure of its power.
 */
int
main (int32 argc, char *argv[])
{
    char line[1024];
    ad_rec_t *ad;
    int16 buf[1000];
    int32 sps;
    
    sps = DEFAULT_SAMPLES_PER_SEC;

    if ((ad = ad_open_sps (sps)) == NULL)
	E_FATAL("ad_open_sps failed\n");
    
    for (;;) {
	printf ("Hit <CR> to measure A/D power; Q<CR> to quit: ");
	
	fgets (line, sizeof(line), stdin);
	if ((line[0] == 'q') || (line[0] == 'Q'))
	    break;
	
	ad_start_rec (ad);

	adpow(ad);
	
	ad_stop_rec (ad);
	while (ad_read (ad, buf, 1000) >= 0);	/* Flush any buffered, unread data */
    }

    ad_close (ad);
    return 0;
}
Пример #5
0
THREAD_START
process_thread(void *aParam)
{
    ad_rec_t *in_ad = 0;
    int16 samples[BUFSIZE];
    int32 num_samples;

    cond_wait(startEvent);

    if ((in_ad = ad_open_sps((int) cmd_ln_float32("-samprate"))) == NULL) {
        printf("Failed to open audio input device\n");
        exit(1);
    }
    ad_start_rec(in_ad);

    while (cond_wait_timed(&finishEvent, TIMEOUT) == COND_TIMEDOUT) {
        num_samples = ad_read(in_ad, samples, BUFSIZE);
        if (num_samples > 0) {
      /** dump the recorded audio to disk */
            if (fwrite(samples, sizeof(int16), num_samples, dump) <
                num_samples) {
                printf("Error writing audio to dump file.\n");
            }

            ld_process_raw(&decoder, samples, num_samples);
        }
    }

    ad_stop_rec(in_ad);
    ad_close(in_ad);

    ld_end_utt(&decoder);

    return 0;
}
Пример #6
0
/*
 * Compute power in input signal for about 2 sec
 */
static void adpow (ad_rec_t *ad)
{
    double en, p;
    int32 i, k, prev, len, s;
    int16 buf[1000];
    
    en = 0.0;
    prev = 0;
    
    for (len = 0; len < 32000; ) {	/* len = #samples gathered */
	if ((k = ad_read (ad, buf, 1000)) < 0)
	    E_FATAL("ad_read returned %d\n", k);
	
	for (i = 0; i < k; i++) {
	    s = buf[i] - prev;		/* Pre-emphasis filter */
	    en += ((double)s)*((double)s);
	    prev = buf[i];
	}
	
	len += k;
    }
    en /= len;
    p = sqrt (en);
    
    if (p < 1.0)
	p = 1.0;
    E_INFO("log(Power) = %.2f dB\n", 10.0 * log10(p));
}
Пример #7
0
DWORD WINAPI
process_thread(LPVOID aParam)
{
  ad_rec_t *in_ad = 0;
  int16 samples[BUFSIZE];
  int32 num_samples;

  WaitForSingleObject(startEvent, INFINITE);

  in_ad = ad_open_sps(cmd_ln_int32 ("-samprate"));
  ad_start_rec(in_ad);

  while (WaitForSingleObject(finishEvent, 0) == WAIT_TIMEOUT) {
    num_samples = ad_read(in_ad, samples, BUFSIZE);
    if (num_samples > 0) {
      if (ld_utt_proc_raw(&decoder, samples, num_samples) < 0) {
	printf(">>>>> ld_utt_proc_raw() returned unexpectedly.\n");
	return -1;
      }
    }
  }

  ad_stop_rec(in_ad);
  ad_close(in_ad);

  if (ld_utt_end(&decoder)) {
    printf(">>>>> ld_utt_end() failed.\n");
    return -1;
  }

  return 0;
}
Пример #8
0
void
ad1848_mute_channel(struct ad1848_softc *sc, int device, int mute)
{
	u_char reg;

	reg = ad_read(sc, mixer_channel_info[device].left_reg);

	if (mute & MUTE_LEFT) {
		if (device == AD1848_MONITOR_CHANNEL) {
			if (sc->open_mode & FREAD)
				ad1848_mute_wave_output(sc, WAVE_UNMUTE1, 0);
			ad_write(sc, mixer_channel_info[device].left_reg,
				 reg & ~DIGITAL_MIX1_ENABLE);
		} else if (device == AD1848_OUT_CHANNEL)
			ad_write(sc, mixer_channel_info[device].left_reg,
				 reg | MONO_OUTPUT_MUTE);
		else
			ad_write(sc, mixer_channel_info[device].left_reg,
				 reg | 0x80);
	} else if (!(sc->mute[device] & MUTE_LEFT)) {
		if (device == AD1848_MONITOR_CHANNEL) {
			ad_write(sc, mixer_channel_info[device].left_reg,
				 reg | DIGITAL_MIX1_ENABLE);
			if (sc->open_mode & FREAD)
				ad1848_mute_wave_output(sc, WAVE_UNMUTE1, 1);
		} else if (device == AD1848_OUT_CHANNEL)
			ad_write(sc, mixer_channel_info[device].left_reg,
				 reg & ~MONO_OUTPUT_MUTE);
		else
			ad_write(sc, mixer_channel_info[device].left_reg,
				 reg & ~0x80);
	}

	if (!mixer_channel_info[device].right_reg)
		return;

	reg = ad_read(sc, mixer_channel_info[device].right_reg);

	if (mute & MUTE_RIGHT) {
		ad_write(sc, mixer_channel_info[device].right_reg, reg | 0x80);
	} else if (!(sc->mute[device] & MUTE_RIGHT)) {
		ad_write(sc, mixer_channel_info[device].right_reg, reg &~0x80);
	}
}
/**
 * You must sucessfully call spInitListener
 * once before using this function.
 *
 * Reads the next block of audio from the microphone
 * up to SPLBUFSIZE number of samples
 * (defined in splistener.h).
 * If an utterance was completed in that block,
 * the transcription is stored in the string words.
 *
 * When calling this function in a realtime loop, delay
 * by some amount of time between calls keeping in mind
 * your recording's sample rate and maximum samples read
 * per call (ex. sleep the thread for 100 milliseconds)
 * so that some audio can be recorded for the next call.
 *
 * @return true if a speech session was completed and
 *         transcribed this block, otherwise false.
 */
static bool spDecode() {
    static bool uttered = false;

    // lock pocketsphinx resources to make sure they 
    // don't get freed by main thread while in use
    std::lock_guard<std::mutex> ps_lock(ps_mtx);
    if(!mic || !ps)
        return false;

    int samples_read = ad_read(mic, buf, SPLBUFSIZE);
    if (samples_read <= 0) {
        spError("failed to read audio :(");
        return false;
    }

    ps_process_raw(ps, buf, samples_read, FALSE, FALSE);
    bool talking = ps_get_in_speech(ps);

    // Just started talking
    if (talking && !uttered) {
        uttered = true;
        return false;
    }

    // Stopped talking, so transcribe what was said
    // and begin the next utterance
    if (!talking && uttered) {
        ps_end_utt(ps);
        const char *trans = ps_get_hyp(ps, NULL);

        if (ps_start_utt(ps) < 0) {
            spError("failed to start utterance :(");
        }
        uttered = false;

        int l = strlen(trans);
        if (trans && l > 0) {
            std::lock_guard<std::mutex> lock(words_mtx);
            if (words && l + 1 > words_buf_size) {
                delete words;
                words = NULL;
            }
            if (!words) {
                words = new char[l + 1];
                words_buf_size = l + 1;
            }

            std::copy(trans, trans + l, words);
            words[l] = '\0';
            
            return true;
        }
    }

    return false;
}
Пример #10
0
void
ad1848_dump_regs(struct ad1848_softc *sc)
{
	int i;
	u_char r;
	
	printf("ad1848 status=%02x", ADREAD(sc, AD1848_STATUS));
	printf(" regs: ");
	for (i = 0; i < 16; i++) {
		r = ad_read(sc, i);
		printf("%02x ", r);
	}
	if (sc->mode == 2) {
		for (i = 16; i < 32; i++) {
			r = ad_read(sc, i);
			printf("%02x ", r);
		}
	}
	printf("\n");
}
int decode()
{
    int ret;
    int16 buf[BUFFER_SIZE];
    
    printf("Listening for input...\n");
    
    if (ad_start_rec(ad) < 0) {
        printf("Error starting recording.\n");
        return 0;
    }
    
	//check if not silent
	while ((ret = cont_ad_read(c_ad, buf, BUFFER_SIZE)) == 0)
        usleep(1000);
	
    if (ps_start_utt(ps, NULL) < 0) {
        printf("Failed to start utterance.\n");
        return 0;
    }
	
	ret = ps_process_raw(ps, buf, BUFFER_SIZE, 0, 0);
    if (ret < 0) {
        printf("Error decoding.\n");
        return 0;
    }
    
    do
    {
        ret = cont_ad_read(c_ad, buf, BUFFER_SIZE);

        if (ret < 0) {
            printf("Failed to record audio.\n");
            return 0;
        } else if(ret > 0) {
            // Valid speech data read.
            ret = ps_process_raw(ps, buf, 4096, 0, 0);
            if (ret < 0) {
                printf("Error decoding.\n");
                return 0;
            }
        } else {
            //no data
            usleep(1000);
        }
    } while(getRun());
    
    ad_stop_rec(ad);
    while (ad_read(ad, buf, BUFFER_SIZE) >= 0);
    cont_ad_reset(c_ad);

    ps_end_utt(ps);
    return 1;
}
Пример #12
0
int
ad1848_set_rec_gain(struct ad1848_softc *sc, struct ad1848_volume *gp)
{
	u_char reg, gain;
	
	DPRINTF(("ad1848_set_rec_gain: %d:%d\n", gp->left, gp->right));

	sc->rec_gain = *gp;

	gain = (gp->left * GAIN_22_5) / AUDIO_MAX_GAIN;
	reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
	reg &= INPUT_GAIN_MASK;
	ad_write(sc, SP_LEFT_INPUT_CONTROL, (gain & 0x0f) | reg);

	gain = (gp->right * GAIN_22_5) / AUDIO_MAX_GAIN;
	reg = ad_read(sc, SP_RIGHT_INPUT_CONTROL);
	reg &= INPUT_GAIN_MASK;
	ad_write(sc, SP_RIGHT_INPUT_CONTROL, (gain & 0x0f) | reg);

	return 0;
}
Пример #13
0
int
ad1848_set_mic_gain(struct ad1848_softc *sc, struct ad1848_volume *gp)
{
	u_char reg;

	DPRINTF(("cs4231_set_mic_gain: %d\n", gp->left));

	if (gp->left > AUDIO_MAX_GAIN / 2) {
		sc->mic_gain_on = 1;
		reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
		ad_write(sc, SP_LEFT_INPUT_CONTROL,
		    reg | INPUT_MIC_GAIN_ENABLE);
	} else {
		sc->mic_gain_on = 0;
		reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
		ad_write(sc, SP_LEFT_INPUT_CONTROL,
		    reg & ~INPUT_MIC_GAIN_ENABLE);
	}

	return 0;
}
Пример #14
0
static void
wait_for_calibration(struct ad1848_softc *sc)
{
	int timeout;

	DPRINTF(("ad1848: Auto calibration started.\n"));
	/*
	 * Wait until the auto calibration process has finished.
	 *
	 * 1) Wait until the chip becomes ready (reads don't return SP_IN_INIT).
	 * 2) Wait until the ACI bit of I11 goes hi and then lo.
	 *   a) With AD1848 alike, ACI goes hi within 5 sample cycles
	 *	  and remains hi for ~384 sample periods.
	 *   b) With CS4231 alike, ACI goes hi immediately and remains
	 *	  hi for at least 168 sample periods.
	 */
	timeout = AD1848_TIMO;
	while (timeout > 0 && ADREAD(sc, AD1848_IADDR) == SP_IN_INIT)
		timeout--;

	if (ADREAD(sc, AD1848_IADDR) == SP_IN_INIT)
		DPRINTF(("ad1848: Auto calibration timed out(1).\n"));

	if (!(sc->sc_flags & AD1848_FLAG_32REGS)) {
		timeout = AD1848_TIMO;
		while (timeout > 0 &&
	 	    !(ad_read(sc, SP_TEST_AND_INIT) & AUTO_CAL_IN_PROG))
			timeout--;

		if (!(ad_read(sc, SP_TEST_AND_INIT) & AUTO_CAL_IN_PROG)) {
			DPRINTF(("ad1848: Auto calibration timed out(2).\n"));
		}
	}

	timeout = AD1848_TIMO;
	while (timeout > 0 && ad_read(sc, SP_TEST_AND_INIT) & AUTO_CAL_IN_PROG)
		timeout--;
	if (ad_read(sc, SP_TEST_AND_INIT) & AUTO_CAL_IN_PROG)
		DPRINTF(("ad1848: Auto calibration timed out(3).\n"));
}
Пример #15
0
/*
 *  This function doesn't set the mute flags but does use them.
 *  The mute flags reflect the mutes that have been applied by the user.
 *  However, the driver occasionally wants to mute devices (e.g. when chaing
 *  sampling rate). These operations should not affect the mute flags.
 */
void 
ad1848_mute_channel(struct ad1848_softc *sc, int device, int mute)
{
	u_char reg;

	reg = ad_read(sc, mixer_channel_info[device].left_reg);

	if (mute & MUTE_LEFT) {
		if (device == AD1848_MONITOR_CHANNEL) {
			ad_write(sc, mixer_channel_info[device].left_reg,
			    reg & 0xFE);
		} else {
			ad_write(sc, mixer_channel_info[device].left_reg,
			    reg | 0x80);
		}
	} else if (!(sc->mute[device] & MUTE_LEFT)) {
		if (device == AD1848_MONITOR_CHANNEL) {
			ad_write(sc, mixer_channel_info[device].left_reg,
			    reg | 0x01);
		} else {
			ad_write(sc, mixer_channel_info[device].left_reg,
			    reg & ~0x80);
		}
	}

	if (!mixer_channel_info[device].right_reg) {
		return;
  	}

	reg = ad_read(sc, mixer_channel_info[device].right_reg);

	if (mute & MUTE_RIGHT) {
		ad_write(sc, mixer_channel_info[device].right_reg, reg | 0x80);
	} else if (!(sc->mute[device] & MUTE_RIGHT)) {
		ad_write(sc, mixer_channel_info[device].right_reg, reg & ~0x80);
	}
}
Пример #16
0
/*
 * Continuous recognition from mic
 */
int
recognize_from_mic()
{
	ad_rec_t *ad;
    int16 adbuf[2048];
    const char *fname;
	const char* seg;
    int32 k;
	char str[1000]="";
    uint8 utt_started, in_speech;
	
    if ((ad = ad_open_dev(cmd_ln_str_r(config, "-adcdev"),16000)) == NULL)
		perror("Failed to open audio device\n");
	if (ad_start_rec(ad) < 0)
		perror("Failed to start recording\n");
    
    ps_start_utt(ps);
    utt_started = FALSE;
	ps_seg_t *psegt;
    while (!finished) {
		if ((k = ad_read(ad, adbuf, 2048)) < 0)
			perror("Failed to read audio\n");
        ps_process_raw(ps, adbuf, k, FALSE, FALSE);
        in_speech = ps_get_in_speech(ps);
        if (in_speech && !utt_started) {
            utt_started = TRUE;
        } 
        if (!in_speech && utt_started) {
            ps_end_utt(ps);
			psegt = ps_seg_iter(ps, NULL);
			while (psegt!=NULL){
				seg = ps_seg_word(psegt);
				strncpy_s( str, seg, strlen(seg));
				listenCallback(str);
				printf("%s\n", seg);
				int prob = ps_seg_prob(psegt,NULL,NULL,NULL);
				printf("%d\n", prob);
				psegt = ps_seg_next(psegt);
			}
            ps_start_utt(ps);
            utt_started = FALSE;
        }
		Sleep(100);
    }
	
    ps_end_utt(ps);
    fclose(rawfd);
	return 0;
}
Пример #17
0
static int
read_audio_adev(int16 * buf, int len)
{
    int k;

    if (!ad) {
        E_FATAL("Failed to read audio from mic\n");
        return -1;
    }
    while ((k = ad_read(ad, buf, len)) == 0)
        /* wait until something is read */
        sleep_msec(50);
    
    return k;
}
Пример #18
0
/* This function is called periodically by status_run(). It updates the device_state_t
 * data, which is accessed by the READ command in object device
 */
void device_state_update() {
	struct timeval now;

	if (!first_time) {
		first_time = 1;
		ad_init(0);
		ad_init(1);
	}

	gettimeofday(&now, NULL);
	device_state.cur_time = now.tv_sec;
	device_state.ps_voltage[0] = 5000;
	device_state.ps_current[0] = 500;

	device_state.ps_current[1] = 500;
	device_state.ps_voltage[1] = ad_read(0);

	device_state.ps_current[2] = 500;
	device_state.ps_voltage[2] = ad_read(1);

	device_state.error_code = 0;
	device_state.memory_free = free_memory();
	device_state.state_code = 0;
}
Пример #19
0
/*
 * Main utterance processing loop:
 *     for (;;) {
 *        start utterance and wait for speech to process
 *        decoding till end-of-utterance silence will be detected
 *        print utterance result;
 *     }
 */
static void
recognize_from_microphone()
{
    ad_rec_t *ad;
    int16 adbuf[2048];
    uint8 utt_started, in_speech;
    int32 k;
    char const *hyp;

    if ((ad = ad_open_dev(cmd_ln_str_r(config, "-adcdev"),
                          (int) cmd_ln_float32_r(config,
                                                 "-samprate"))) == NULL)
        E_FATAL("Failed to open audio device\n");
    if (ad_start_rec(ad) < 0)
        E_FATAL("Failed to start recording\n");

    if (ps_start_utt(ps) < 0)
        E_FATAL("Failed to start utterance\n");
    utt_started = FALSE;
    E_INFO("Ready....\n");

    for (;;) {
        if ((k = ad_read(ad, adbuf, 2048)) < 0)
            E_FATAL("Failed to read audio\n");
        ps_process_raw(ps, adbuf, k, FALSE, FALSE);
        in_speech = ps_get_in_speech(ps);
        if (in_speech && !utt_started) {
            utt_started = TRUE;
            E_INFO("Listening...\n");
        }
        if (!in_speech && utt_started) {
            /* speech -> silence transition, time to start new utterance  */
            ps_end_utt(ps);
            hyp = ps_get_hyp(ps, NULL );
            if (hyp != NULL) {
                printf("%s\n", hyp);
                fflush(stdout);
            }

            if (ps_start_utt(ps) < 0)
                E_FATAL("Failed to start utterance\n");
            utt_started = FALSE;
            E_INFO("Ready....\n");
        }
        sleep_msec(100);
    }
    ad_close(ad);
}
Пример #20
0
static void
recognize_from_microphone()
{
    ad_rec_t *ad;
    int16 adbuf[2048];
    uint8 utt_started, in_speech;
    int32 k;
    char const *hyp;

    if ((ad = ad_open_dev(AUDIO_DEVICE_NAME,
                          (int) SAMPLE_RATE )) == NULL) {
        E_FATAL("Failed to open audio device\n");
	}
    if (ad_start_rec(ad) < 0) {
        E_FATAL("Failed to start recording\n");
    }
    if (ps_start_utt(ps) < 0) {
        E_FATAL("Failed to start utterance\n");
    }
    utt_started = FALSE;
    printf("READY....\n");

    for (;;) {
        if ((k = ad_read(ad, adbuf, 2048)) < 0)
            E_FATAL("Failed to read audio\n");
        ps_process_raw(ps, adbuf, k, FALSE, FALSE);
        in_speech = ps_get_in_speech(ps);
        if (in_speech && !utt_started) {
            utt_started = TRUE;
            printf("Listening...\n");
        }
        if (!in_speech && utt_started) {
            /* speech -> silence transition, time to start new utterance  */
            ps_end_utt(ps);
            hyp = ps_get_hyp(ps, NULL );
            if (hyp != NULL)
                printf("%s\n", hyp);

            if (ps_start_utt(ps) < 0)
                E_FATAL("Failed to start utterance\n");
            utt_started = FALSE;
            printf("READY....\n");
        }
        sleep_msec(100);
    }
    ad_close(ad);
}
Пример #21
0
/*
 * Record A/D data for approximately specified number of seconds into specified file.
 */
int
main (int32 argc, char *argv[])
{
    char line[1024];
    ad_rec_t *ad;
    int16 buf[1000];
    int32 len, k, sps;
    FILE *fp;
    
    if ((argc != 4) ||
	(sscanf (argv[1], "%d", &sps) != 1) ||
	(sscanf (argv[2], "%d", &len) != 1)) {
	E_FATAL("Usage: %s <sampling-rate> <#sec-to-record> <output-file>\n", argv[0]);
    }
    if ((fp = fopen (argv[3], "wb")) == NULL)
	E_FATAL("fopen(%s,wb) failed\n", argv[3]);
    
    len *= sps;		/* Convert to min. #samples to record */
    
    if ((ad = ad_open_sps (sps)) == NULL)
	E_FATAL("ad_open_sps(%d) failed\n", sps);
    
    printf ("Hit <CR> to start recording\n");
    fgets (line, sizeof(line), stdin);
    
    ad_start_rec (ad);
    
    /* Record desired no. of samples */
    while (len > 0) {
	/* Read A/D */
	if ((k = ad_read (ad, buf, 1000)) < 0)
	    E_FATAL("ad_read returned %d\n", k);

	/* Write data read, if any, to file (ad_read may return 0 samples) */
	if (k > 0) {
	    fwrite (buf, sizeof(int16), k, fp);
	    fflush (fp);
	    len -= k;
	}
    }
    
    ad_stop_rec (ad);
    ad_close (ad);

    fclose (fp);
    return 0;
}
Пример #22
0
/*
 * Halt a DMA in progress.
 */
int
ad1848_halt_output(void *addr)
{
    struct ad1848_softc *sc = addr;
    u_char reg;

    DPRINTF(("ad1848: ad1848_halt_output\n"));
    mtx_enter(&audio_lock);
    reg = ad_read(sc, SP_INTERFACE_CONFIG);
    ad_write(sc, SP_INTERFACE_CONFIG, (reg & ~PLAYBACK_ENABLE));

    if (sc->sc_playrun == 1) {
        isa_dmaabort(sc->sc_isa, sc->sc_drq);
        sc->sc_playrun = 0;
    }
    mtx_leave(&audio_lock);
    return 0;
}
Пример #23
0
int
ad1848_halt_input(void *addr)
{
	struct ad1848_softc *sc = addr;
	u_char reg;

	DPRINTF(("ad1848: ad1848_halt_input\n"));

	reg = ad_read(sc, SP_INTERFACE_CONFIG);
	ad_write(sc, SP_INTERFACE_CONFIG, (reg & ~CAPTURE_ENABLE));

	if (sc->sc_recrun == 1) {
		isa_dmaabort(sc->sc_isa, sc->sc_recdrq);
		sc->sc_recrun = 0;
	}

	return 0;
}
uint32 FSpeechRecognitionWorker::Run() {

	char const *hyp;
	// attempt to open the default recording device
	if ((ad = ad_open_dev(cmd_ln_str_r(config, "-adcdev"),
		(int)cmd_ln_float32_r(config,
		"-samprate"))) == NULL) {
		ClientMessage(FString(TEXT("Failed to open audio device")));
		return 1;
	}
	if (ad_start_rec(ad) < 0) {
		ClientMessage(FString(TEXT("Failed to start recording")));
		return 2;
	}
	if (ps_start_utt(ps) < 0) {
		ClientMessage(FString(TEXT("Failed to start utterance")));
		return 3;
	}

	while (StopTaskCounter.GetValue() == 0) {
		if ((k = ad_read(ad, adbuf, 1024)) < 0)
			ClientMessage(FString(TEXT("Failed to read audio")));
		ps_process_raw(ps, adbuf, k, 0, 0);
		in_speech = ps_get_in_speech(ps);
		if (in_speech && !utt_started) {
			utt_started = 1;
		}
		if (!in_speech && utt_started) {
			/* speech -> silence transition, time to start new utterance  */
			ps_end_utt(ps);
			hyp = ps_get_hyp(ps, NULL);
			if (hyp != NULL)
				Manager->WordSpoken_method(FString(hyp));

			if (ps_start_utt(ps) < 0)
				ClientMessage(FString(TEXT("Failed to start")));
			utt_started = 0;
		}
	}

	ad_close(ad);
	return 0;
}
int processCommands()
{
    int32 samples;
    int16 audioBuf[BUFFER_SIZE];
    char const *uttid;
    char const *hyp;
    
    while(run)
    {
        printf("Waiting for utterance...\n");
        samples = waitForNextUtterance();
        if(samples < 0)
            return -1;
        
        if(ps_start_utt(psDecoder, NULL) < 0) {
            fprintf(stderr, "Failed to start next utterance\n");
            return -1;
        }
        ps_process_raw(psDecoder, audioBuf, samples, FALSE, FALSE);
        
        printf("Recording...\n");
        fflush(stdout);
        record();
        
        ad_stop_rec(audioDevice);
        while(ad_read(audioDevice, audioBuf, BUFFER_SIZE) >= 0);
        cont_ad_reset(continousAudoDevice);
        ps_end_utt(psDecoder);
        
        hyp = ps_get_hyp(psDecoder, NULL, &uttid);
        printf("Heard: %s\n", hyp);
        
        if (ad_start_rec(audioDevice) < 0) {
            fprintf(stderr, "Failed to start audio device.\n");
            return -1;
        }
    }
    
    return 0;
}
Пример #26
0
int
ad1848_open(void *addr, int flags)
{
	struct ad1848_softc *sc = addr;

	DPRINTF(("ad1848_open: sc=%p\n", sc));

	sc->sc_pintr = sc->sc_parg = NULL;
	sc->sc_rintr = sc->sc_rarg = NULL;

	/* Enable interrupts */
	DPRINTF(("ad1848_open: enable intrs\n"));
	ad_write(sc, SP_PIN_CONTROL,
	    INTERRUPT_ENABLE | ad_read(sc, SP_PIN_CONTROL));

#ifdef AUDIO_DEBUG
	if (ad1848debug > 2)
		ad1848_dump_regs(sc);
#endif

	return 0;
}
Пример #27
0
void
ad1848_reset(struct ad1848_softc *sc)
{
	u_char r;

	DPRINTF(("ad1848_reset\n"));

	/* Clear the PEN and CEN bits */
	r = ad_read(sc, SP_INTERFACE_CONFIG);
	r &= ~(CAPTURE_ENABLE | PLAYBACK_ENABLE);
	ad_write(sc, SP_INTERFACE_CONFIG, r);

	/* Clear interrupt status */
	if (sc->mode == 2)
	ad_write(sc, CS_IRQ_STATUS, 0);
	ADWRITE(sc, AD1848_STATUS, 0);

#ifdef AUDIO_DEBUG
	if (ad1848debug > 2)
		ad1848_dump_regs(sc);
#endif
}
Пример #28
0
/*
 *  side-effects: allocates buffer
 */
ssize_t ad_read_mono_dbl(void *sf, struct adinfo *nfo, double* d, size_t len){
	unsigned int c,f;
	unsigned int chn = nfo->channels;
	if (len<1) return 0;

	static float *buf = NULL;
	static size_t bufsiz = 0;
	if (!buf || bufsiz != len*chn) {
		bufsiz=len*chn;
		buf = (float*) realloc((void*)buf, bufsiz * sizeof(float));
	}

	len = ad_read(sf, buf, bufsiz);

	for (f=0;f< (len/chn);f++) {
		double val=0.0;
		for (c=0;c<chn;c++) {
			val+=buf[f*chn + c];
		}
		d[f]= val/chn;
	}
	return len/chn;
}
Пример #29
0
double ad_maxsignal(const char *fn) {
	struct adinfo nfo;
	void * sf = ad_open(fn, &nfo);
	if (!sf) return 0.0;

	int     read_len = 1024 * nfo.channels;
	float* sf_data = malloc(sizeof(float) * read_len);

	int readcount;
	float max_val = 0.0;
	do {
		readcount = ad_read(sf, sf_data, read_len);
		int k;
		for (k = 0; k < readcount; k++){
			const float temp = fabs (sf_data [k]);
			max_val = temp > max_val ? temp : max_val;
		};
	} while (readcount > 0);

	ad_close(sf);
	free(sf_data);
	ad_free_nfo(&nfo);
	return max_val;
}
Пример #30
0
/*
 * Main utterance processing loop:
 *     for (;;) {
 * 	   wait for start of next utterance;
 * 	   decode utterance until silence of at least 1 sec observed;
 * 	   print utterance result;
 *     }
 */
static void
recognize_from_microphone()
{
    ad_rec_t *ad;
    int16 adbuf[4096];
    int32 k, ts, rem;
    char const *hyp;
    char const *uttid;
    cont_ad_t *cont;
    char word[256];
	char c1[256], c2[256];

	int tracking = 0;
	int halted = 0;
	int LEFT = 0;
	int RIGHT = 1;
	int MOVE_CENT = 100; //1 meter
	int numwords;

	setlinebuf(stdout);

    if ((ad = ad_open_dev(cmd_ln_str_r(config, "-adcdev"),
                          (int)cmd_ln_float32_r(config, "-samprate"))) == NULL)
        E_FATAL("Failed to open audio device\n");

    /* Initialize continuous listening module */
    if ((cont = cont_ad_init(ad, ad_read)) == NULL)
        E_FATAL("Failed to initialize voice activity detection\n");
    if (ad_start_rec(ad) < 0)
        E_FATAL("Failed to start recording\n");
    if (cont_ad_calib(cont) < 0)
        E_FATAL("Failed to calibrate voice activity detection\n");


	printf("LEDON BLUE\n");
    for (;;) {
        /* Indicate listening for next utterance */
        fprintf(stderr, "READY....\n");
        fflush(stderr);

        /* Wait data for next utterance */
        while ((k = cont_ad_read(cont, adbuf, 4096)) == 0)
            sleep_msec(100);

        if (k < 0)
            E_FATAL("Failed to read audio\n");

        /*
         * Non-zero amount of data received; start recognition of new utterance.
         * NULL argument to uttproc_begin_utt => automatic generation of utterance-id.
         */
        if (ps_start_utt(ps, NULL) < 0)
            E_FATAL("Failed to start utterance\n");
        ps_process_raw(ps, adbuf, k, FALSE, FALSE);
        fprintf(stderr, "Listening...\n");

        /* Note timestamp for this first block of data */
        ts = cont->read_ts;

        /* Decode utterance until end (marked by a "long" silence, >1sec) */
        for (;;) {
            /* Read non-silence audio data, if any, from continuous listening module */
            if ((k = cont_ad_read(cont, adbuf, 4096)) < 0)
                E_FATAL("Failed to read audio\n");
            if (k == 0) {
                /*
                 * No speech data available; check current timestamp with most recent
                 * speech to see if more than 1 sec elapsed.  If so, end of utterance.
                 */
                if ((cont->read_ts - ts) > DEFAULT_SAMPLES_PER_SEC)
                    break;
            }
            else {
                /* New speech data received; note current timestamp */
                ts = cont->read_ts;
            }

            /*
             * Decode whatever data was read above.
             */
            rem = ps_process_raw(ps, adbuf, k, FALSE, FALSE);

            /* If no work to be done, sleep a bit */
            if ((rem == 0) && (k == 0))
                sleep_msec(20);
        }

        /*
         * Utterance ended; flush any accumulated, unprocessed A/D data and stop
         * listening until current utterance completely decoded
         */
        ad_stop_rec(ad);
        while (ad_read(ad, adbuf, 4096) >= 0);
        cont_ad_reset(cont);

        fprintf(stderr, "Stopped listening, please wait...\n");
        fflush(stdout);
        /* Finish decoding, obtain and print result */
        ps_end_utt(ps);
        hyp = ps_get_hyp(ps, NULL, &uttid);
        fprintf(stderr, "%s: %s\n", uttid, hyp);

        /* Exit if the first word spoken was GOODBYE */
        if (hyp) {
			numwords = sscanf(hyp, "%s %s %s", word, c1, c2);
			if(strcmp(word, "GUGGUG") == 0) {
				if(strcmp(c1, "HALT") == 0) {
					printf("LEDOFF BLUE\n");
					halted = 1;
				} else if(strcmp(c1, "RESUME") == 0) {
					printf("LEDON BLUE\n");
					halted = 0;
				}
				if(strcmp(c1, "BEGIN") == 0 || strcmp(c1, "START") == 0) {
					if(strcmp(c2, "TRACKING") == 0 && !tracking) {
						printf("START TRACKING\n");
						tracking = 1;
						halted = 0;
					}
				} else if(strcmp(c1, "STOP") == 0) {
					if(strcmp(c2, "TRACKING") == 0 && tracking) {
						printf("STOP TRACKING\n");
						tracking = 0;
					}
				}
				if(!tracking && !halted && numwords == 3) {
					if(strcmp(c1, "TURN") == 0) {
						if(strcmp(c2, "AROUND") == 0) {
							printf("TURN %d 180\n", LEFT);
						} else if(strcmp(c2, "LEFT") == 0) {
							printf("TURN %d 90\n", LEFT);
						} else if(strcmp(c2, "RIGHT") == 0) {
							printf("TURN %d 90\n", RIGHT);
						}
					} else if(strcmp(c1, "MOVE") == 0) {
						if(strcmp(c2, "FORWARD") == 0) {
							printf("MOVE 0 %d\n", MOVE_CENT);
						} else if(strcmp(c2, "BACKWARD") == 0) {
							printf("MOVE 1 %d\n", MOVE_CENT);
						}
					}
				}
			}
        }

        /* Resume A/D recording for next utterance */
        if (ad_start_rec(ad) < 0)
            E_FATAL("Failed to start recording\n");
    }

    cont_ad_close(cont);
    ad_close(ad);
}