comparison libao2/ao_alsa9.c @ 7657:dda97c5190d7

fixed ao_data.bps - patch by Tobias Diedrich <td@sim.uni-hannover.de>
author arpi
date Mon, 07 Oct 2002 19:21:35 +0000
parents a24f91c247cd
children b9da278e4c92
comparison
equal deleted inserted replaced
7656:52009e5ef4fc 7657:dda97c5190d7
57 * which seems to be good avarge for most situations 57 * which seems to be good avarge for most situations
58 * so buffersize is 16384 frames by default */ 58 * so buffersize is 16384 frames by default */
59 static int alsa_fragcount = 16; 59 static int alsa_fragcount = 16;
60 static int chunk_size = 1024; //is alsa_fragsize / 4 60 static int chunk_size = 1024; //is alsa_fragsize / 4
61 61
62 static size_t bits_per_sample, bits_per_frame; 62 static size_t bits_per_sample, bytes_per_sample, bits_per_frame;
63 static size_t chunk_bytes; 63 static size_t chunk_bytes;
64 64
65 int ao_mmap = 0; 65 int ao_mmap = 0;
66 int ao_noblock = 0; 66 int ao_noblock = 0;
67 int first = 1; 67 int first = 1;
220 printf("alsa-init: no soundcards found: %s\n", snd_strerror(err)); 220 printf("alsa-init: no soundcards found: %s\n", snd_strerror(err));
221 return(0); 221 return(0);
222 } 222 }
223 223
224 ao_data.samplerate = rate_hz; 224 ao_data.samplerate = rate_hz;
225 ao_data.bps = channels; /* really this is bytes per frame so bad varname */ 225 ao_data.bps = channels * rate_hz;
226 ao_data.format = format; 226 ao_data.format = format;
227 ao_data.channels = channels; 227 ao_data.channels = channels;
228 ao_data.outburst = OUTBURST; 228 ao_data.outburst = OUTBURST;
229 //ao_data.buffersize = MAX_OUTBURST; // was 16384 229 //ao_data.buffersize = MAX_OUTBURST; // was 16384
230 230
281 audio_out_format_name(format)); 281 audio_out_format_name(format));
282 return(0); 282 return(0);
283 default: 283 default:
284 break; 284 break;
285 } 285 }
286 286 bytes_per_sample = ao_data.bps / ao_data.samplerate;
287
287 if (ao_subdevice) { 288 if (ao_subdevice) {
288 //start parsing ao_subdevice, ugly and not thread safe! 289 //start parsing ao_subdevice, ugly and not thread safe!
289 //maybe there's a better way? 290 //maybe there's a better way?
290 int i2 = 1; 291 int i2 = 1;
291 int i3 = 0; 292 int i3 = 0;
670 printf("alsa-init: pcm prepare error: %s\n", snd_strerror(err)); 671 printf("alsa-init: pcm prepare error: %s\n", snd_strerror(err));
671 return(0); 672 return(0);
672 } 673 }
673 674
674 printf("alsa9: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n", 675 printf("alsa9: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n",
675 ao_data.samplerate, ao_data.channels, ao_data.bps, ao_data.buffersize, 676 ao_data.samplerate, ao_data.channels, bytes_per_sample, ao_data.buffersize,
676 snd_pcm_format_description(alsa_format)); 677 snd_pcm_format_description(alsa_format));
677 678
678 } // end switch alsa_handler (spdif) 679 } // end switch alsa_handler (spdif)
679 return(1); 680 return(1);
680 } // end init 681 } // end init
845 */ 846 */
846 847
847 static int play_normal(void* data, int len) 848 static int play_normal(void* data, int len)
848 { 849 {
849 850
850 //ao_data.bps is always 4 for 2 chn S16_LE 851 //bytes_per_sample is always 4 for 2 chn S16_LE
851 int num_frames = len / ao_data.bps; 852 int num_frames = len / bytes_per_sample;
852 signed short *output_samples=data; 853 signed short *output_samples=data;
853 snd_pcm_sframes_t res = 0; 854 snd_pcm_sframes_t res = 0;
854 855
855 //printf("alsa-play: frames=%i, len=%i",num_frames,len); 856 //printf("alsa-play: frames=%i, len=%i",num_frames,len);
856 857
930 #endif 931 #endif
931 932
932 outbuffer = alloca(ao_data.buffersize); 933 outbuffer = alloca(ao_data.buffersize);
933 934
934 //don't trust get_space() ;) 935 //don't trust get_space() ;)
935 frames_available = snd_pcm_avail_update(alsa_handler) * ao_data.bps; 936 frames_available = snd_pcm_avail_update(alsa_handler) * bytes_per_sample;
936 if (frames_available < 0) 937 if (frames_available < 0)
937 xrun("play"); 938 xrun("play");
938 939
939 if (frames_available < 4) { 940 if (frames_available < 4) {
940 if (first) { 941 if (first) {
946 first = 1; 947 first = 1;
947 } 948 }
948 } 949 }
949 950
950 /* len is simply the available bufferspace got by get_space() 951 /* len is simply the available bufferspace got by get_space()
951 * but real avail_buffer in frames is ab/ao_data.bps */ 952 * but real avail_buffer in frames is ab/bytes_per_sample */
952 size = len / ao_data.bps; 953 size = len / bytes_per_sample;
953 954
954 //if (verbose) 955 //if (verbose)
955 //printf("len: %i size %i, f_avail %i, bps %i ...\n", len, size, frames_available, ao_data.bps); 956 //printf("len: %i size %i, f_avail %i, bps %i ...\n", len, size, frames_available, bytes_per_sample);
956 957
957 frames_transmit = size; 958 frames_transmit = size;
958 959
959 /* prepare areas and set sw-pointers 960 /* prepare areas and set sw-pointers
960 * frames_transmit returns the real available buffer-size 961 * frames_transmit returns the real available buffer-size
964 /* this is specific to interleaved streams (or non-interleaved 965 /* this is specific to interleaved streams (or non-interleaved
965 * streams with only one channel) */ 966 * streams with only one channel) */
966 outbuffer = ((char *) area->addr + (area->first + area->step * offset) / 8); //8 967 outbuffer = ((char *) area->addr + (area->first + area->step * offset) / 8); //8
967 968
968 //write data 969 //write data
969 memcpy(outbuffer, data, (frames_transmit * ao_data.bps)); 970 memcpy(outbuffer, data, (frames_transmit * bytes_per_sample));
970 971
971 commitres = snd_pcm_mmap_commit(alsa_handler, offset, frames_transmit); 972 commitres = snd_pcm_mmap_commit(alsa_handler, offset, frames_transmit);
972 973
973 if (commitres < 0 || commitres != frames_transmit) { 974 if (commitres < 0 || commitres != frames_transmit) {
974 if (snd_pcm_state(alsa_handler) == SND_PCM_STATE_XRUN || 975 if (snd_pcm_state(alsa_handler) == SND_PCM_STATE_XRUN ||
986 /* return 0; */ 987 /* return 0; */
987 /* } */ 988 /* } */
988 989
989 990
990 //calculate written frames! 991 //calculate written frames!
991 result = commitres * ao_data.bps; 992 result = commitres * bytes_per_sample;
992 993
993 994
994 /* if (verbose) { */ 995 /* if (verbose) { */
995 /* if (len == result) */ 996 /* if (len == result) */
996 /* printf("result: %i, frames written: %i ...\n", result, frames_transmit); */ 997 /* printf("result: %i, frames written: %i ...\n", result, frames_transmit); */
1036 str_status = "open"; 1037 str_status = "open";
1037 case SND_PCM_STATE_PREPARED: 1038 case SND_PCM_STATE_PREPARED:
1038 if (str_status != "open") { 1039 if (str_status != "open") {
1039 str_status = "prepared"; 1040 str_status = "prepared";
1040 first = 1; 1041 first = 1;
1041 ret = snd_pcm_status_get_avail(status) * ao_data.bps; 1042 ret = snd_pcm_status_get_avail(status) * bytes_per_sample;
1042 if (ret == 0) //ugly workaround for hang in mmap-mode 1043 if (ret == 0) //ugly workaround for hang in mmap-mode
1043 ret = 10; 1044 ret = 10;
1044 break; 1045 break;
1045 } 1046 }
1046 case SND_PCM_STATE_RUNNING: 1047 case SND_PCM_STATE_RUNNING:
1047 ret = snd_pcm_status_get_avail(status) * ao_data.bps; 1048 ret = snd_pcm_status_get_avail(status) * bytes_per_sample;
1048 //avail_frames = snd_pcm_avail_update(alsa_handler) * ao_data.bps; 1049 //avail_frames = snd_pcm_avail_update(alsa_handler) * bytes_per_sample;
1049 if (str_status != "open" && str_status != "prepared") 1050 if (str_status != "open" && str_status != "prepared")
1050 str_status = "running"; 1051 str_status = "running";
1051 break; 1052 break;
1052 case SND_PCM_STATE_PAUSED: 1053 case SND_PCM_STATE_PAUSED:
1053 if (verbose) printf("alsa-space: paused"); 1054 if (verbose) printf("alsa-space: paused");
1060 first = 1; 1061 first = 1;
1061 ret = 0; 1062 ret = 0;
1062 break; 1063 break;
1063 default: 1064 default:
1064 str_status = "undefined"; 1065 str_status = "undefined";
1065 ret = snd_pcm_status_get_avail(status) * ao_data.bps; 1066 ret = snd_pcm_status_get_avail(status) * bytes_per_sample;
1066 if (ret <= 0) { 1067 if (ret <= 0) {
1067 xrun("space"); 1068 xrun("space");
1068 } 1069 }
1069 } 1070 }
1070 1071