]> git.sesse.net Git - pitch/blobdiff - pitch.cpp
Port from OSS to ALSA, as OSS is pretty much dying these days.
[pitch] / pitch.cpp
index 12cf6d8cb8ddfdcb2d092d494255c2e60a67edc5..421c2f24cea126da2dc786f4a918624dd459ac41 100644 (file)
--- a/pitch.cpp
+++ b/pitch.cpp
@@ -2,43 +2,22 @@
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <fcntl.h>
-#include <complex>
-#include <cassert>
-#include <algorithm>
-#include <fftw3.h>
-#include <sys/ioctl.h>
-#include <linux/soundcard.h>
 
 
+#include "config.h"
+#include "notes.h"
+#include "linux_audio.h"
 #include "pitchdetector.h"
 
 #include "pitchdetector.h"
 
-#define SAMPLE_RATE     22050
-#define FFT_LENGTH      4096     /* in samples */
-#define PAD_FACTOR      2        /* 1/pf of the FFT samples are real samples, the rest are padding */
-#define OVERLAP         4        /* 1/ol samples will be replaced in the buffer every frame. Should be
-                                 * a multiple of 2 for the Hamming window (see
-                                 * http://www-ccrma.stanford.edu/~jos/parshl/Choice_Hop_Size.html).
-                                 */
-
-#define EQUAL_TEMPERAMENT     0
-#define WELL_TEMPERED_GUITAR  1
-
-#define TUNING WELL_TEMPERED_GUITAR
-
-int get_dsp_fd();
-void read_chunk(int fd, short *in, unsigned num_samples);
 void print_spectrogram(double freq, double amp);
 void print_spectrogram(double freq, double amp);
-void write_sine(int dsp_fd, double freq, unsigned num_samples);
 
 int main()
 {
        PitchDetector pd(SAMPLE_RATE, FFT_LENGTH, PAD_FACTOR, OVERLAP);
 
 int main()
 {
        PitchDetector pd(SAMPLE_RATE, FFT_LENGTH, PAD_FACTOR, OVERLAP);
-       
-       int fd = get_dsp_fd();
+       snd_pcm_t *pcm = get_dsp_handle(SAMPLE_RATE);
        for ( ;; ) {
                short buf[FFT_LENGTH / PAD_FACTOR / OVERLAP];
 
        for ( ;; ) {
                short buf[FFT_LENGTH / PAD_FACTOR / OVERLAP];
 
-               read_chunk(fd, buf, FFT_LENGTH / PAD_FACTOR / OVERLAP);
+               read_chunk(pcm, buf, FFT_LENGTH / PAD_FACTOR / OVERLAP);
                std::pair<double, double> peak = pd.detect_pitch(buf);
 
                if (peak.first < 50.0 || peak.second - log10(FFT_LENGTH) < 0.0) {
                std::pair<double, double> peak = pd.detect_pitch(buf);
 
                if (peak.first < 50.0 || peak.second - log10(FFT_LENGTH) < 0.0) {
@@ -53,81 +32,10 @@ int main()
        }
 }
 
        }
 }
 
-int get_dsp_fd()
-{
-       int fd = open("/dev/dsp", O_RDWR);
-       if (fd == -1) {
-               perror("/dev/dsp");
-               exit(1);
-       }
-       
-       ioctl(3, SNDCTL_DSP_RESET, 0);
-       
-       int fmt = AFMT_S16_LE;   // FIXME
-       ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
-
-       int chan = 1;
-       ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &chan);
-       
-       int rate = SAMPLE_RATE;
-       ioctl(fd, SOUND_PCM_WRITE_RATE, &rate);
-
-       int max_fragments = 2;
-       int frag_shift = ffs(FFT_LENGTH / OVERLAP) - 1;
-       int fragments = (max_fragments << 16) | frag_shift;
-        ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fragments);
-       
-       ioctl(3, SNDCTL_DSP_SYNC, 0);
-       
-       return fd;
-}
-
-#if 1
-void read_chunk(int fd, short *in, unsigned num_samples)
-{
-       int ret;
-
-       ret = read(fd, in, num_samples * sizeof(short));
-       if (ret == 0) {
-               printf("EOF\n");
-               exit(0);
-       }
-
-       if (ret != int(num_samples * sizeof(short))) {
-               // blah
-               perror("read");
-               exit(1);
-       }
-}
-#else
-// make a pure 440hz sine for testing
-void read_chunk(int fd, short *in, unsigned num_samples)
-{
-       static double theta = 0.0;
-       for (unsigned i = 0; i < num_samples; ++i) {
-               in[i] = 32768.0 * cos(theta);
-               theta += 2.0 * M_PI * 440.0 / double(SAMPLE_RATE);
-       }
-}
-#endif
-
-void write_sine(int dsp_fd, double freq, unsigned num_samples) 
-{
-       static double theta = 0.0;
-       short buf[num_samples];
-       
-       for (unsigned i = 0; i < num_samples; ++i) {
-               buf[i] = short(cos(theta) * 16384.0);
-               theta += 2.0 * M_PI * freq / double(SAMPLE_RATE);
-       }
-
-       write(dsp_fd, buf, num_samples * sizeof(short));
-}
-
 std::string freq_to_tonename(double freq)
 {
        std::string notenames[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
 std::string freq_to_tonename(double freq)
 {
        std::string notenames[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
-       double half_notes_away = 12.0 * log2(freq / 440.0) - 3.0;
+       double half_notes_away = 12.0 * log2(freq / BASE_PITCH) - 3.0;
        int hnai = int(floor(half_notes_away + 0.5));
        int octave = (hnai + 48) / 12;
 
        int hnai = int(floor(half_notes_away + 0.5));
        int octave = (hnai + 48) / 12;
 
@@ -140,7 +48,7 @@ std::string freq_to_tonename(double freq)
 void print_spectrogram(double freq, double amp)
 {
        std::string notenames[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
 void print_spectrogram(double freq, double amp)
 {
        std::string notenames[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
-       double half_notes_away = 12.0 * log2(freq / 440.0) - 3.0;
+       double half_notes_away = 12.0 * log2(freq / BASE_PITCH) - 3.0;
        int hnai = int(floor(half_notes_away + 0.5));
        int octave = (hnai + 48) / 12;
 
        int hnai = int(floor(half_notes_away + 0.5));
        int octave = (hnai + 48) / 12;
 
@@ -169,19 +77,6 @@ void print_spectrogram(double freq, double amp)
 
 }
 #else
 
 }
 #else
-struct note {
-       char notename[16];
-       double freq;
-};
-static note notes[] = {
-       { "E-3", 110.0 * (3.0/4.0) },
-       { "A-3", 110.0 },
-       { "D-4", 110.0 * (4.0/3.0) },
-       { "G-4", 110.0 * (4.0/3.0)*(4.0/3.0) },
-       { "B-4", 440.0 * (3.0/4.0)*(3.0/4.0) },
-       { "E-5", 440.0 * (3.0/4.0) }
-};
-
 void print_spectrogram(double freq, double amp)
 {
        double best_away = 999999999.9;
 void print_spectrogram(double freq, double amp)
 {
        double best_away = 999999999.9;