-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.c
93 lines (76 loc) · 2.2 KB
/
audio.c
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
/*
* Standard audio related includes
*/
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "audio.h"
#include "portaudio.h"
/* Mandatory variables */
PaStream *stream;
void pa_error(PaError err)
{
fprintf(stderr, "An error occured while using the portaudio stream\n");
fprintf(stderr, "Error number: %d\n", err);
fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(err));
// Print more information about the error.
if (err == paUnanticipatedHostError)
{
const PaHostErrorInfo *hostErrorInfo = Pa_GetLastHostErrorInfo();
fprintf(stderr, "Host API error = #%ld, hostApiType = %d\n", hostErrorInfo->errorCode, hostErrorInfo->hostApiType);
fprintf(stderr, "Host API error = %s\n", hostErrorInfo->errorText);
}
Pa_Terminate();
exit(1);
}
void play_buffer(unsigned char *buf, int count)
{
PaError err;
err = Pa_WriteStream(stream, buf, count);
if (err != paNoError)
pa_error(err);
}
/* note, freq is changed! */
void open_audio(int *freq)
{
PaError err;
PaStreamParameters outputParameters;
err = Pa_Initialize();
if (err != paNoError)
pa_error(err);
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
if (outputParameters.device == paNoDevice)
{
fprintf(stderr, "Error: No default output device.\n");
Pa_Terminate();
exit(1);
}
outputParameters.channelCount = 1;
outputParameters.sampleFormat = paUInt8;
outputParameters.suggestedLatency = 0.050; // Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
err = Pa_OpenStream(
&stream,
NULL, /* no input */
&outputParameters,
*freq,
paFramesPerBufferUnspecified,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
NULL, /* no callback, use blocking API */
NULL); /* no callback, so no callback userData */
if (err != paNoError)
pa_error(err);
err = Pa_StartStream(stream);
if (err != paNoError)
pa_error(err);
}
void close_audio()
{
PaError err;
err = Pa_CloseStream(stream);
if (err != paNoError)
pa_error(err);
Pa_Terminate();
}