Subversion Repositories svnkaklik

Rev

Rev 518 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 518 Rev 526
Line 1... Line 1...
1
//ALSA Audio playback testing program 
1
//ALSA Audio playback testing program 
2
//
2
//
3
 
3
 
4
	#include <stdio.h>
4
	#include <stdio.h>
5
	#include <stdlib.h>
5
	#include <stdlib.h>
-
 
6
 
6
	#include <alsa/asoundlib.h>
7
	#include <alsa/asoundlib.h>
7
 
8
 
8
	int exact_rate=44100;
9
	int exact_rate=44100;
-
 
10
 
-
 
11
 
-
 
12
int linear_chirp(int *pole, int delka_pole, int spozdeni){  // vygeneruje linearni chirp a vzorky ulozi do pole
-
 
13
 
-
 
14
static const double pi = 3.141592653589793238462643383279502884197; // Archimedes constant pi
-
 
15
static const float f0 = 0.01;
-
 
16
static const float k = 0.0001;
-
 
17
 
-
 
18
int t;
-
 
19
float ble;
-
 
20
  if((spozdeni+delka_pulsu) < delka_pole)
-
 
21
    for(t=0;t < delka_pulsu;t++) pole[spozdeni+t] = round ( 100*sin(2*pi*t*(f0+(k/2)*t)) );
-
 
22
  else return 0;
-
 
23
 
-
 
24
}
-
 
25
 
-
 
26
static void async_callback(snd_async_handler_t *ahandler)
-
 
27
{
-
 
28
	snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
-
 
29
	struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
-
 
30
	signed short *samples = data->samples;
-
 
31
	snd_pcm_channel_area_t *areas = data->areas;
-
 
32
	snd_pcm_sframes_t avail;
-
 
33
	int err;
-
 
34
	
-
 
35
	avail = snd_pcm_avail_update(handle);
-
 
36
	while (avail >= period_size) {
-
 
37
		generate_sine(areas, 0, period_size, &data->phase);
-
 
38
 
-
 
39
		err = snd_pcm_writei(handle, samples, period_size);
-
 
40
		if (err < 0) {
-
 
41
			printf("Write error: %s\n", snd_strerror(err));
-
 
42
			exit(EXIT_FAILURE);
-
 
43
		}
-
 
44
		if (err != period_size) {
-
 
45
			printf("Write error: written %i expected %li\n", err, period_size);
-
 
46
			exit(EXIT_FAILURE);
-
 
47
		}
-
 
48
		avail = snd_pcm_avail_update(handle);
-
 
49
	}
-
 
50
}
-
 
51
 
-
 
52
static int async_loop(snd_pcm_t *handle,
-
 
53
		      signed short *samples,
-
 
54
		      snd_pcm_channel_area_t *areas)
-
 
55
{
-
 
56
	struct async_private_data data;
-
 
57
	snd_async_handler_t *ahandler;
-
 
58
	int err, count;
-
 
59
	unsigned int i;
-
 
60
 
-
 
61
	data.samples = samples;
-
 
62
	data.areas = areas;
-
 
63
	data.phase = 0;
-
 
64
	err = snd_async_add_pcm_handler(&ahandler, handle, async_callback, &data);
-
 
65
	if (err < 0) {
-
 
66
		printf("Unable to register async handler\n");
-
 
67
		exit(EXIT_FAILURE);
-
 
68
	}
-
 
69
	for (count = 0; count < 2; count++) {
-
 
70
		generate_sine(areas, 0, period_size, &data.phase);
-
 
71
		err = snd_pcm_writei(handle, samples, period_size);
-
 
72
		if (err < 0) {
-
 
73
			printf("Initial write error: %s\n", snd_strerror(err));
-
 
74
			exit(EXIT_FAILURE);
-
 
75
		}
-
 
76
		if (err != period_size) {
-
 
77
			printf("Initial write error: written %i expected %li\n", err, period_size);
-
 
78
			exit(EXIT_FAILURE);
-
 
79
		}
-
 
80
	}
-
 
81
	if (snd_pcm_state(handle) == SND_PCM_STATE_PREPARED) {
-
 
82
		err = snd_pcm_start(handle);
-
 
83
		if (err < 0) {
-
 
84
			printf("Start error: %s\n", snd_strerror(err));
-
 
85
			exit(EXIT_FAILURE);
-
 
86
		}
-
 
87
	}
-
 
88
 
-
 
89
	/* because all other work is done in the signal handler,
-
 
90
	   suspend the process */
-
 
91
	for(i=0; i<=10;i++) {
-
 
92
		sleep(1);
-
 
93
	}
-
 
94
}
9
	      
95
	      
10
	main (int argc, char *argv[])
96
	main (int argc, char *argv[])
11
	{
97
	{
12
		int i;
98
		int i;
13
		int err;
99
		int err;
14
		short buf[128];
100
		short buf[128];
15
		snd_pcm_t *playback_handle;
101
		snd_pcm_t *playback_handle;
16
		snd_pcm_hw_params_t *hw_params;
102
		snd_pcm_hw_params_t *hw_params;
-
 
103
		snd_pcm_channel_area_t *areas;
17
	
104
	
18
		if ((err = snd_pcm_open (&playback_handle, argv[1], SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
105
		if ((err = snd_pcm_open (&playback_handle, argv[1], SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
19
			fprintf (stderr, "cannot open audio device %s (%s)\n", 
106
			fprintf (stderr, "cannot open audio device %s (%s)\n", 
20
				 argv[1],
107
				 argv[1],
21
				 snd_strerror (err));
108
				 snd_strerror (err));
22
			exit (1);
109
			exit (1);
23
		}
110
		}
24
		   
111
		   
25
		if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
112
		if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
26
			fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
113
			fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
27
				 snd_strerror (err));
114
				 snd_strerror (err));
28
			exit (1);
115
			exit (1);
29
		}
116
		}
30
				 
117
				 
Line 62... Line 149...
62
			fprintf (stderr, "cannot set parameters (%s)\n",
149
			fprintf (stderr, "cannot set parameters (%s)\n",
63
				 snd_strerror (err));
150
				 snd_strerror (err));
64
			exit (1);
151
			exit (1);
65
		}
152
		}
66
	
153
	
67
		snd_pcm_hw_params_free (hw_params);
-
 
68
	
-
 
69
		if ((err = snd_pcm_prepare (playback_handle)) < 0) {
154
		if ((err = snd_pcm_prepare (playback_handle)) < 0) {
70
			fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
155
			fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
71
				 snd_strerror (err));
156
				 snd_strerror (err));
72
			exit (1);
157
			exit (1);
73
		}
158
		}
74
	
159
	
-
 
160
	async_loop(playback_handle, samples, areas);
-
 
161
 
75
		for (i = 0; i < 100; ++i) {
162
/*		for (i = 0; i < 100; ++i) {
76
			if ((err = snd_pcm_writei (playback_handle, buf, 128)) != 128) {
163
			if ((err = snd_pcm_writei (playback_handle, buf, 128)) != 128) {
77
				fprintf (stderr, "write to audio interface failed (%s)\n",
164
				fprintf (stderr, "write to audio interface failed (%s)\n",
78
					 snd_strerror (err));
165
					 snd_strerror (err));
79
				exit (1);
166
				exit (1);
80
			}
167
			}
81
		}
168
		}*/
82
	
169
	
83
		snd_pcm_close (playback_handle);
170
		snd_pcm_close (playback_handle);
84
		exit (0);
171
		exit (0);
85
	}
172
	}
86
 
173