====== Осциллограф из звуковой платы ====== ====== Схема ====== {{::soundcard_oscilloscope_probe-1.jpg?300|}} ====== Существующие программы ====== [[http://xoscope.sourceforge.net/ | xoscope]] [[https://github.com/xobs/osqoop | Osqoop]] использует доступ к звуковой плате через драйвер OSS, поэтому на современных системах нуждается в [[http://alsa.opensrc.org/Aoss | его эмуляции]]. ====== Своя программа ====== [[http://www.linuxjournal.com/article/6735?page=0,2 | Introduction to Sound Programming with ALSA]] [[http://users.suse.com/~mana/alsa090_howto.html#sect03 | ALSA Programming HOWTO: PCM capture]] [[http://equalarea.com/paul/alsa-audio.html | A Tutorial on Using the ALSA Audio API]] [[http://www.alsa-project.org/main/index.php/Main_Page | Advanced Linux Sound Architecture (ALSA) project homepage]] ===== Пробная программа ===== /* soundcard_oscilloscope.c v.0.1 17.08.2013 Vladimir Smolyar GPLv3 */ #include #include #include #include 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); } ====== Статьи ====== [[http://www.energeticforum.com/renewable-energy/3901-el-cheapo-oscilloscope-using-pc-soundcard.html | El-Cheapo Oscilloscope using PC + soundcard]] [[http://www.instructables.com/id/Use-Your-Laptop-as-Oscilloscope/?ALLSTEPS | Use Your Laptop as Oscilloscope]] [[http://www.opencircuits.com/Oscilloscope]]