summaryrefslogtreecommitdiffhomepage
path: root/alsa.cpp
blob: c5455b47bcd3073d8701e3a73810f7a1d4531a71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <libreichwein/file.h>

#include <alsa/asoundlib.h>

#include <cstdint>
#include <cmath>
#include <iostream>
#include <limits>

static const char *device = "default";            /* playback device */
const snd_pcm_sframes_t nframes = 8*1024;
int16_t buffer[nframes];              /* some random data */
const double f_tone = 440.0;
const unsigned int f_sample = 48000;
const double pi = std::acos(-1);

class Sin
{

public:

double generate(double phase)
{
    int i;

    for (i = 0; i < nframes; i++)
        buffer[i] = std::sin(i * 2 * pi * f_tone / f_sample + phase) * std::numeric_limits<int16_t>::max();

    double dummy;
    return std::modf((i * 2 * pi * f_tone / f_sample + phase) / (2 * pi), &dummy) * 2 * pi;
}

};

class M3
{
private:
        std::vector<uint16_t> m_data;
public:
        M3()
        {
                std::string data_s = Reichwein::File::getFile("media/Bmw_M3_f80_StartUp.s16le");
                m_data.resize(data_s.size() / 2);
                memcpy(m_data.data(), data_s.data(), data_s.size());
        }

double generate(double phase)
{
    int i;
    size_t j = phase;

    for (i = 0; i < nframes; i++) {
        if (j >= m_data.size())
                j = 0;
        buffer[i] = m_data[j];
        j++;
    }

    return j;
}

};

int main(void)
{
    int err;
    unsigned int i;
    snd_pcm_t *handle;
    snd_pcm_sframes_t frames;
 
    // non-blocking
    if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
        printf("Playback open error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }
    if ((err = snd_pcm_set_params(handle,
                      SND_PCM_FORMAT_S16_LE,
                      SND_PCM_ACCESS_RW_INTERLEAVED,
                      1,
                      f_sample,
                      1,
                      500000)) < 0) {   /* 0.5sec */
        printf("Playback open error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }
 
    double phase = 0;
    //Sin generator;
    M3 generator;
    for (i = 0; i < 40; i++) {
        phase = generator.generate(phase);

        frames = snd_pcm_writei(handle, buffer, nframes);
        if (frames < 0) {
            std::cout << "Recovering." << std::endl;
            frames = snd_pcm_recover(handle, frames, 0);
        }
        if (frames < 0) {
            printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
            break;
        }
        if (frames > 0 && frames < nframes)
            printf("Short write (expected %li, wrote %li)\n", nframes, frames);
    }
 
    /* pass the remaining samples, otherwise they're dropped in close */
    err = snd_pcm_drain(handle);
    if (err < 0)
        printf("snd_pcm_drain failed: %s\n", snd_strerror(err));
    snd_pcm_close(handle);
    return 0;
}