Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <alsa/asoundlib.h>
- #include <cmath>
- #include <random>
- constexpr int rate = 44100;
- int main()
- {
- int err;
- snd_pcm_t *handle;
- snd_pcm_sframes_t frames;
- if ((err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_ASYNC)) < 0) {
- printf("Playback open error: %s\n", snd_strerror(err));
- exit(EXIT_FAILURE);
- }
- if ((err = snd_pcm_set_params(handle,
- SND_PCM_FORMAT_FLOAT,
- SND_PCM_ACCESS_RW_INTERLEAVED,
- 1,
- rate,
- 0,
- 50000)) < 0) {
- printf("Playback open error: %s\n", snd_strerror(err));
- exit(EXIT_FAILURE);
- }
- std::random_device rd;
- std::mt19937 gen(rd());
- std::uniform_real_distribution<float> distrib(-1.0f, 1.0f);
- constexpr int buf_len = rate * 3;
- float* in = new float[buf_len];
- float* out = new float[buf_len]();
- constexpr int M = 200;
- float fc = 2100.0f / rate;
- double h[M + 1]{0.0f};
- for (int i = 0; i < buf_len; i++)
- {
- in[i] = distrib(gen);
- }
- for (int i = 0; i <= M; i++)
- {
- if ((i - M/2) == 0)
- {
- h[i] = 2*M_PI*fc;
- }
- else
- {
- h[i] = std::sin(2.0*M_PI*fc*(i - M/2)) / (i - M/2);
- }
- h[i] *= 0.42 - 0.5 * std::cos((2.0*M_PI*i)/M) + 0.08 * std::cos((4.0*M_PI*i)/M);
- }
- double sum = 0;
- for (int i = 0; i <= M; i++)
- {
- sum += h[i];
- }
- for (int i = 0; i <= M; i++)
- {
- h[i] /= sum;
- }
- //change to high pass
- for (int i = 0; i <= M; i++)
- {
- h[i] *= -1.0;
- }
- h[M/2] += 1.0f;
- // Convolve
- for (int i = M; i < buf_len; i++)
- {
- for (int j = 0; j <= M; j++)
- {
- out[i] += in[i - j] * h[j];
- }
- }
- frames = snd_pcm_writei(handle, out, buf_len);
- if (frames < 0)
- {
- frames = snd_pcm_recover(handle, frames, 0);
- }
- snd_pcm_drain(handle);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement