Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <alsa/asoundlib.h>
- #include <math.h>
- #include "main.h"
- double fclip(double value, double start, double end) {
- return 0.5 * ( fabs(value-start) + (start+end) - fabs(value-end) );
- }
- void play(struct user_data_s* data) {
- static char* device = "default";
- /* snd_output_t* output = NULL; */
- int err;
- snd_pcm_t* handle;
- snd_pcm_sframes_t frames;
- short buffer[data->audio_info_save->frames];
- for(unsigned long int i = 0; i < data->audio_info_save->frames; i++ ) {
- buffer[i] = (short)fclip( 32767.0 * data->mono_channel[i], -32767, +32768 );
- }
- if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
- printf("Playback open error: %s\n", snd_strerror(err));
- exit(1);
- }
- if ((err = snd_pcm_set_params(handle, SND_PCM_FORMAT_S16,
- SND_PCM_ACCESS_RW_INTERLEAVED, 1,
- data->audio_info_save->samplerate,
- 1, 500000)) < 0) {
- printf("Playback open error: %s\n", snd_strerror(err));
- exit(1);
- }
- for (int i = 0; i < 1; i++) {
- frames = snd_pcm_writei(handle, buffer, data->audio_info_save->frames);
- if (frames < 0) {
- frames = snd_pcm_recover(handle, frames, 0);
- }
- if (frames < 0) {
- printf("snd_pcm_writei failed: %s\n", snd_strerror(err));
- break;
- }
- if (frames > 0 && frames < (long)data->audio_info_save->frames) {
- printf("Short write (expected %li, wrote %li)\n",
- (long)data->audio_info_save->frames, frames);
- }
- }
- snd_pcm_close(handle);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement