[578] | 1 | #define INCL_DOS
|
---|
| 2 | #define INCL_DOSERRORS
|
---|
| 3 | #include <os2.h>
|
---|
| 4 | #include <stdlib.h>
|
---|
| 5 | #include <stdio.h>
|
---|
| 6 | #include <io.h>
|
---|
| 7 | #include <fcntl.h>
|
---|
| 8 | #include <conio.h>
|
---|
| 9 | #include <math.h>
|
---|
| 10 | #include <string.h>
|
---|
| 11 |
|
---|
| 12 | #include "uniaud.h"
|
---|
| 13 | #include "unidef.h"
|
---|
| 14 | #include "errno.h"
|
---|
| 15 |
|
---|
| 16 | static void set_perc_vol(int card_id, char *name, int vol, int stereo)
|
---|
| 17 | {
|
---|
| 18 | int ctl_id, min, max, new_vol;
|
---|
| 19 | ctl_id = uniaud_get_id_by_name(card_id, name, 0);
|
---|
| 20 | if (ctl_id > 0)
|
---|
| 21 | {
|
---|
| 22 | uniaud_mixer_get_min_max(card_id, ctl_id, &min, &max);
|
---|
| 23 | new_vol = ((float)(max-min)/100.0f)*vol;
|
---|
| 24 | uniaud_mixer_put_value(card_id,ctl_id,new_vol, 0);
|
---|
| 25 | if (stereo) uniaud_mixer_put_value(card_id,ctl_id,new_vol, 1);
|
---|
| 26 | // printf("set id %i to %i\n", ctl_id, new_vol);
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | int main (int argc, char *argv[])
|
---|
| 32 | {
|
---|
| 33 | int ver, err, ret;
|
---|
| 34 | int bytesread, count;
|
---|
| 35 | int card_id = 0;
|
---|
| 36 | uniaud_pcm *pcm = NULL;
|
---|
| 37 | char *Buffer;
|
---|
| 38 | int readed, written, state, written1;
|
---|
| 39 | int vol = 100, mute = 1;
|
---|
| 40 | FILE *input_handle;
|
---|
| 41 | int cards_num = 0; // number of cards
|
---|
| 42 |
|
---|
| 43 | snd_pcm_status_t status;
|
---|
| 44 |
|
---|
| 45 | // (unsigned int)*((unsigned int*)&tt1) = (unsigned int)*((unsigned int*)&tt);
|
---|
| 46 | // printf("%02X%02X%02X%02X\n", tt1[0],tt1[1],tt1[2],tt1[3] );
|
---|
| 47 | // return 0;
|
---|
| 48 |
|
---|
| 49 | printf("PCM test tool for UNIAUD. Version 0.01\n");
|
---|
| 50 | printf("Copyright 2004 by Vlad Stelmahosky aka Vladest\n");
|
---|
| 51 |
|
---|
| 52 | ver = uniaud_get_version();
|
---|
| 53 |
|
---|
| 54 | switch (ver)
|
---|
| 55 | {
|
---|
| 56 | case -2:
|
---|
| 57 | printf("Error: uniaud not detected\n");
|
---|
| 58 | return 1;
|
---|
| 59 | break;
|
---|
| 60 | case -1:
|
---|
| 61 | printf("Error: uniaud error\n");
|
---|
| 62 | return 1;
|
---|
| 63 | break;
|
---|
| 64 | default:
|
---|
| 65 | if (ver < 111)
|
---|
| 66 | {
|
---|
| 67 | printf("Error: unsupported version of uniaud\n");
|
---|
| 68 | return 1;
|
---|
| 69 | } else
|
---|
| 70 | printf("Detected UNIAUD version %1d.%02d.%02d\n", ver/10000, (ver / 100) % 100, ver % 100 );
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | cards_num = uniaud_get_cards();
|
---|
| 74 | if (!cards_num)
|
---|
| 75 | {
|
---|
| 76 | printf("No audio cards detected\n");
|
---|
| 77 | return 1;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | printf("Detected %i audio adapter(s)\n", cards_num);
|
---|
| 81 |
|
---|
| 82 | set_perc_vol(card_id, "PCM Playback Volume", vol, 1);
|
---|
| 83 | set_perc_vol(card_id, "Wave Playback Volume", vol, 1);
|
---|
| 84 | set_perc_vol(card_id, "Wave Surround Playback Volume", vol, 1);
|
---|
| 85 | set_perc_vol(card_id, "Wave Center Playback Volume", vol, 0);
|
---|
| 86 | set_perc_vol(card_id, "Wave LFE Playback Volume", vol, 0);
|
---|
| 87 | set_perc_vol(card_id, "Playback Volume", vol, 1);
|
---|
| 88 | set_perc_vol(card_id, "Surround Playback Volume", vol, 1);
|
---|
| 89 | set_perc_vol(card_id, "Center Playback Volume", vol, 0);
|
---|
| 90 | set_perc_vol(card_id, "LFE Playback Volume", vol, 0);
|
---|
| 91 | uniaud_mixer_put_value_by_name(card_id,"PCM Playback Switch",mute, 1, 0);
|
---|
| 92 | uniaud_mixer_put_value_by_name(card_id,"PCM Playback Switch",mute, 0, 0);
|
---|
| 93 | uniaud_mixer_put_value_by_name(card_id,"Surround Playback Switch",mute, 1, 0);
|
---|
| 94 | uniaud_mixer_put_value_by_name(card_id,"Surround Playback Switch",mute, 0, 0);
|
---|
| 95 | uniaud_mixer_put_value_by_name(card_id,"LFE Playback Switch",mute, 0, 0);
|
---|
| 96 | uniaud_mixer_put_value_by_name(card_id,"Center Playback Switch",mute, 0, 0);
|
---|
| 97 | uniaud_mixer_put_value_by_name(card_id,"PCM Playback Switch",mute, 0, 0);
|
---|
| 98 |
|
---|
| 99 | // for (j=0; j < 2; j++)
|
---|
| 100 | {
|
---|
| 101 | printf("max chans: %i\n",uniaud_get_max_channels(card_id));
|
---|
| 102 | readed = written = 0;
|
---|
| 103 | err = uniaud_pcm_open(card_id, PCM_TYPE_WRITE, 0, 0, 48000, 6,
|
---|
| 104 | SNDRV_PCM_FORMAT_S16_LE, &pcm);
|
---|
| 105 | if (pcm && err == 0)
|
---|
| 106 | {
|
---|
| 107 | printf("opened. pcm = %x\n", pcm);
|
---|
| 108 | err = uniaud_pcm_prepare(pcm);
|
---|
| 109 | printf("prepare err = %i\n", err);
|
---|
| 110 | uniaud_pcm_status(pcm, &status);
|
---|
| 111 | if (argc > 1)
|
---|
| 112 | input_handle = fopen(argv[1], "rb");
|
---|
| 113 | else
|
---|
| 114 | input_handle = fopen("debug6ch.wav", "rb");
|
---|
| 115 | if (!input_handle) printf("Error opening file\n");
|
---|
| 116 | count = pcm->bufsize;
|
---|
| 117 | //count = 528*2;
|
---|
| 118 |
|
---|
| 119 | printf("count = %i\n", count);
|
---|
| 120 | // fseek(input_handle, 0L, SEEK_END);
|
---|
| 121 | // fsize = ftell(input_handle);
|
---|
| 122 | Buffer = malloc(count);
|
---|
| 123 | // fseek(input_handle, 0L, SEEK_SET);
|
---|
| 124 | // memset(Buffer,0, count);
|
---|
| 125 |
|
---|
| 126 | while (1)
|
---|
| 127 | {
|
---|
| 128 | // read file and play into driver
|
---|
| 129 | // if (written >= fsize) break;
|
---|
| 130 | _again:
|
---|
| 131 | bytesread = fread (Buffer, 1, count, input_handle);
|
---|
| 132 | if (bytesread <= 0) break;
|
---|
| 133 |
|
---|
| 134 | written1 = 0;
|
---|
| 135 | if (memcmp(Buffer, "RIFF",4) == 0)
|
---|
| 136 | {
|
---|
| 137 | /* found WAV */
|
---|
| 138 | printf("Found WAV file\n");
|
---|
| 139 | //written1 +=44;
|
---|
| 140 | //bytesread -=44;
|
---|
| 141 | /* TODO!! add parser */
|
---|
| 142 | fseek(input_handle, 0x80, SEEK_SET);
|
---|
| 143 | goto _again;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | // if ((fsize - written) < count)
|
---|
| 147 | // count = fsize - written;
|
---|
| 148 |
|
---|
| 149 | while (written1 < bytesread)
|
---|
| 150 | {
|
---|
| 151 | //printf("cp1\n");fflush(stdout);
|
---|
| 152 | // write samples to driver
|
---|
| 153 | err = uniaud_pcm_write(pcm, Buffer+written1, bytesread-written1);
|
---|
| 154 | //printf("cp2\n");fflush(stdout);
|
---|
| 155 |
|
---|
| 156 | // wait while for next portion 1000 milliseconds
|
---|
| 157 | ret = uniaud_pcm_wait(pcm, 100);
|
---|
| 158 | if (ret < 0)
|
---|
| 159 | {
|
---|
| 160 | //printf("error waiting pcm interrupt %i\n", ret);
|
---|
| 161 | DosSleep(1);
|
---|
| 162 | }
|
---|
| 163 | if (err > 0) // written something
|
---|
| 164 | {
|
---|
| 165 | written+=err;
|
---|
| 166 | written1+=err;
|
---|
| 167 | printf("written all=%i. now = %i\r",written,err);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | if (written1 >= bytesread) break;
|
---|
| 171 | if (err == -EAGAIN) //
|
---|
| 172 | {
|
---|
| 173 | // printf("eagain\n");
|
---|
| 174 | continue;
|
---|
| 175 | }
|
---|
| 176 | if (err < 0) {
|
---|
| 177 | state = uniaud_pcm_state(pcm);
|
---|
| 178 | //printf("state after write: %i, err = %i wr: %i, br: %i\n",state, err, written, bytesread);
|
---|
| 179 | if ( (state != SND_PCM_STATE_PREPARED) &&
|
---|
| 180 | (state != SND_PCM_STATE_RUNNING) &&
|
---|
| 181 | (state != SND_PCM_STATE_DRAINING) ) {
|
---|
| 182 | printf("write:BAD STATE2, state = %d, going to try XRUN\n",state);
|
---|
| 183 | if ((ret = uniaud_pcm_prepare(pcm))<0) {
|
---|
| 184 | // printf("xrun: prepare error: %i", err);
|
---|
| 185 | return 0;
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 | } // while written1
|
---|
| 190 | }
|
---|
| 191 | free(Buffer);
|
---|
| 192 | fclose(input_handle);
|
---|
| 193 | err = uniaud_pcm_close(pcm);
|
---|
| 194 | }
|
---|
| 195 | else
|
---|
| 196 | {
|
---|
| 197 | printf("open err: %i, pcm %x\n", err, pcm);
|
---|
| 198 | if (err == 10005)
|
---|
| 199 | err = uniaud_pcm_close(pcm);
|
---|
| 200 | printf("close err: %i\n", err);
|
---|
| 201 | }
|
---|
| 202 | } //j
|
---|
| 203 | return 0;
|
---|
| 204 | }
|
---|