Користувальницькькі налаштування

Налаштування сайту


soundcard_oscilloscope

Осциллограф из звуковой платы

Схема

Существующие программы

xoscope

Osqoop использует доступ к звуковой плате через драйвер OSS, поэтому на современных системах нуждается в его эмуляции.

Своя программа

Пробная программа

"soundcard_oscilloscope.c"
/*
  soundcard_oscilloscope.c
 
  v.0.1
 
  17.08.2013 Vladimir Smolyar
 
  GPLv3
 
*/
 
 
 
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#include <math.h>
 
main (int argc, char *argv[])
{
    int i,j;
    int err;
    int buf[128];
    snd_pcm_t *capture_handle;
    snd_pcm_hw_params_t *hw_params;
    unsigned int val,val2;
    int dir;
    int peak_value, average;
    size_t size;
    int number;
 
    err = snd_pcm_open (&capture_handle, argv[1], SND_PCM_STREAM_CAPTURE, 0);
    if (err < 0) 
    {
        fprintf (stderr, "cannot open audio device %s (%s)\n", 
                 argv[1],
                 snd_strerror (err));
        exit (1);
    }
    else printf("Audio device opened.\n");
 
    err = snd_pcm_hw_params_malloc (&hw_params);
    if (err < 0) 
    {
        fprintf(stderr, "cannot allocate hardware parameter structure (%s)\n",
	         snd_strerror(err));
	exit (1);
    }
    else printf("Hardware parameter structure allocated.\n");
 
    err = snd_pcm_hw_params_any(capture_handle, hw_params);
    if (err < 0) 
    {
        fprintf(stderr, "cannot initialize hardware parameter structure (%s)\n",
		 snd_strerror (err));
	exit (1);
    }
    else printf("Hardware parameter structure initialized.\n");
 
 
    err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
    if (err < 0) 
    {
        fprintf (stderr, "cannot set access type (%s)\n",
		 snd_strerror (err));
	exit (1);
    }
    else printf("Access type set.\n");
 
 
    err = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S16_LE);
    if (err < 0) 
    {
        fprintf (stderr, "cannot set sample format (%s)\n",
		 snd_strerror (err));
	exit (1);
    }
    else printf("Sample format set.\n");
 
    val = 44100;
    err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &val, &dir);
    if (err < 0) 
    {
        fprintf (stderr, "cannot set sample rate (%s)\n",
		 snd_strerror (err));
	exit (1);
    }
    else printf("Sample rate set.\n");
 
    err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 1);
    if (err < 0) 
    {
        fprintf (stderr, "cannot set channel count (%s)\n",
		 snd_strerror (err));
	exit (1);
    }
    else printf("Channel count set.\n");
 
 
    err = snd_pcm_hw_params (capture_handle, hw_params);
    if (err < 0) 
    {
        fprintf(stderr, "cannot set parameters (%s)\n",
	        snd_strerror (err));
        exit (1);
    }
    else printf("Parameters set.\n");
 
    snd_pcm_hw_params_free (hw_params);
    err = snd_pcm_prepare (capture_handle);
    if (err < 0) 
    {
        fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
		 snd_strerror (err));
        exit (1);
    }
    else printf("Audio interface prepared for use.\n");
 
    //for (i = 0; i < 10; ++i) 
    peak_value = 0;
    average = 0;
    while (1)
    {
        err = snd_pcm_readi (capture_handle, buf, 128);
	if (err != 128)
	{
            fprintf (stderr, "read from audio interface failed (%s)\n",
	             snd_strerror (err));
            exit (1);
        }
 
	else 
	{    
	    //printf("%d\n",abs(buf[0]));
	    //printf("%d\n",abs(peak_value));
	    //printf(".\n");
 
	    for (i=0; i<128; i++)
	    {
	        average += (abs(buf[i]) - average)/512;
		if (abs(buf[i]) > abs(peak_value))
		{
		    peak_value = buf[i];
	            //printf("Peak: %d\n",peak_value);
		    //write(1, *peak_value, sizeof(peak_value));
 
		}
	    }
	    printf("|");
	    number = round(100 * ((float) average) / 2147483648);
	    for (j=0; j < number; j++) printf("=");
	    printf(">");
	    for (j=number; j < 70; j++) printf("-");
	    //printf("average: %d\n",average);
	    printf("|\n");
        } 
    }
    snd_pcm_close (capture_handle);
    exit (0);
}

Статьи

soundcard_oscilloscope.txt · В останнє змінено: 2015/10/01 01:05 by wombat