-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
118 lines (89 loc) · 2.42 KB
/
main.cpp
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
113
114
115
116
117
118
//============================================================================
// Name : AudioBase.cpp
// Author :
// Version :
// Copyright : copyright alright
// Description : Playing sound with alsa library
//============================================================================
#include <iostream>
#include <regex>
#include "WavPlayer.h"
#include <functional>
/**
* \ Wav player paramters
*/
Audio::AudioParam wavParam {
SND_PCM_STREAM_PLAYBACK, SND_PCM_ACCESS_RW_INTERLEAVED, {}, {}, 44
};
Audio::AudioParam recoredParam {
SND_PCM_STREAM_CAPTURE, SND_PCM_ACCESS_RW_INTERLEAVED, SND_PCM_FORMAT_S16_LE, Audio::Channels::stereo, 44100
};
bool compareExtension(std::string& filename, std::string extension)
{
std::string file_ext = filename.substr(filename.find_last_of(".") + 1);
if(std::regex_match(file_ext, std::regex{extension, std::regex_constants::icase}))
return true;
else
return false;
}
bool isWavExtension(std::string& filename)
{
return compareExtension(filename, "wav");
}
/*
* replace function pointers
*/
using namespace std::placeholders;
class A
{
std::function<void(char*, size_t)> f;
public:
int writeI(char* buf, size_t len)
{
std::cout <<"write i" << std::endl;
}
int writeNI(char* buf, size_t len)
{
std::cout <<"write ni" << std::endl;
}
void checkStuff()
{
f = [=](char* buf, size_t len) {
return this->writeNI(buf, len); };
f = [=](char* buf, size_t len) {
return this->writeI(buf, len); };
}
void write(char * buf, size_t len)
{
f(buf, len);
}
};
int main(int argc, char* argv[])
{
std::cout << "ALSA Aplication Test: " << SND_LIB_VERSION_STR << std::endl;
// get file
if(argc < 2)
{
std::cout << "No file arguments, please call like: ./player /dir/.../song.wav" << std::endl;
return -1;
}
else if(argc > 2)
{
std::cout << "To many arguments" << std::endl;
return -1;
}
auto* filenamep = argv[1];
std::string filename{filenamep};
std::unique_ptr<Audio::Player> player;
if(isWavExtension(filename))
player = std::make_unique<Audio::WavPlayer>(Audio::WavPlayer{"default", wavParam});
else
player.reset();
std::cout << "start playing" << std::endl;
player.get()->play(filenamep);
std::cout << "stop playing" << std::endl;
std::cout << "start playing" << std::endl;
player.get()->play(filenamep);
std::cout << "stop playing" << std::endl;
exit(0);
}