rme96xx 24.3 KB
Newer Older
L
Linus Torvalds 已提交
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
Beta release of the rme96xx (driver for RME 96XX cards like the 
"Hammerfall" and the "Hammerfall light") 

Important: The driver module has to be installed on a freshly rebooted system, 
otherwise the driver might not be able to acquire its buffers.

features:

 - OSS programming interface (i.e. runs with standard OSS soundsoftware) 
 - OSS/Multichannel interface (OSS multichannel is done by just aquiring
   more than 2 channels). The driver does not use more than one device 
   ( yet .. this feature may be implemented later ) 
 - more than one RME card supported

The driver uses a specific multichannel interface, which I will document
when the driver gets stable. (take a look at the defines in rme96xx.h,
which adds blocked multichannel formats i.e instead of 
lrlrlrlr --> llllrrrr  etc.

Use the "rmectrl" programm to look at the status of the card .. 
or use xrmectrl, a GUI interface for the ctrl program.

What you can do with the rmectrl program is to set the stereo device for
OSS emulation (e.g. if you use SPDIF out).

You do:

./ctrl offset 24 24

which makes the stereo device use channels 25 and 26.

Guenter Geiger <geiger@epy.co.at>

copy the first part of the attached source code into rmectrl.c
and the  second part into xrmectrl (or get the program from
http://gige.xdv.org/pages/soft/pages/rme)

to compile: gcc -o rmectrl rmectrl.c
------------------------------ snip ------------------------------------

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/soundcard.h>
#include <math.h>
#include <unistd.h>
#include <stdlib.h>
#include "rme96xx.h"

/*
  remctrl.c
  (C) 2000 Guenter Geiger <geiger@debian.org>
  HP20020201 - Heiko Purnhagen <purnhage@tnt.uni-hannover.de>
*/

/* # define DEVICE_NAME "/dev/mixer" */
# define DEVICE_NAME "/dev/mixer1"


void usage(void)
{
     fprintf(stderr,"usage: rmectrl [/dev/mixer<n>] [command [options]]\n\n");
     fprintf(stderr,"where command is one of:\n");
     fprintf(stderr,"  help                       show this help\n");
     fprintf(stderr,"  status                     show status bits\n");
     fprintf(stderr,"  control                    show control bits\n");
     fprintf(stderr,"  mix                        show mixer/offset status\n");
     fprintf(stderr,"  master <n>                 set sync master\n");
     fprintf(stderr,"  pro <n>                    set spdif out pro\n");
     fprintf(stderr,"  emphasis <n>               set spdif out emphasis\n");
     fprintf(stderr,"  dolby <n>                  set spdif out no audio\n");
     fprintf(stderr,"  optout <n>                 set spdif out optical\n");
     fprintf(stderr,"  wordclock <n>              set sync wordclock\n");
     fprintf(stderr,"  spdifin <n>                set spdif in (0=optical,1=coax,2=intern)\n");
     fprintf(stderr,"  syncref <n>                set sync source (0=ADAT1,1=ADAT2,2=ADAT3,3=SPDIF)\n");
     fprintf(stderr,"  adat1cd <n>                set ADAT1 on internal CD\n");
     fprintf(stderr,"  offset <devnr> <in> <out>  set dev (0..3) offset (0..25)\n");
     exit(-1);
}


int main(int argc, char* argv[])
{
     int cards;
     int ret;
     int i;
     double ft;
     int fd, fdwr;
     int param,orig;
     rme_status_t stat;
     rme_ctrl_t ctrl;
     char *device;
     int argidx;

     if (argc < 2)
	  usage();

     if (*argv[1]=='/') {
	  device = argv[1];
	  argidx = 2;
     }
     else {
	  device = DEVICE_NAME;
	  argidx = 1;
     }

     fprintf(stdout,"mixer device %s\n",device);
     if ((fd = open(device,O_RDONLY)) < 0) {
	  fprintf(stdout,"opening device failed\n");
	  exit(-1);
     }

     if ((fdwr = open(device,O_WRONLY)) < 0) {
	  fprintf(stdout,"opening device failed\n");
	  exit(-1);
     }

     if (argc < argidx+1)
	  usage();

     if (!strcmp(argv[argidx],"help"))
        usage();
     if (!strcmp(argv[argidx],"-h"))
        usage();
     if (!strcmp(argv[argidx],"--help"))
        usage();

     if (!strcmp(argv[argidx],"status")) {
	  ioctl(fd,SOUND_MIXER_PRIVATE2,&stat);
	  fprintf(stdout,"stat.irq %d\n",stat.irq);
	  fprintf(stdout,"stat.lockmask %d\n",stat.lockmask);
	  fprintf(stdout,"stat.sr48 %d\n",stat.sr48);
	  fprintf(stdout,"stat.wclock %d\n",stat.wclock);
	  fprintf(stdout,"stat.bufpoint %d\n",stat.bufpoint);
	  fprintf(stdout,"stat.syncmask %d\n",stat.syncmask);
	  fprintf(stdout,"stat.doublespeed %d\n",stat.doublespeed);
	  fprintf(stdout,"stat.tc_busy %d\n",stat.tc_busy);
	  fprintf(stdout,"stat.tc_out %d\n",stat.tc_out);
	  fprintf(stdout,"stat.crystalrate %d (0=64k 3=96k 4=88.2k 5=48k 6=44.1k 7=32k)\n",stat.crystalrate);
	  fprintf(stdout,"stat.spdif_error %d\n",stat.spdif_error);
	  fprintf(stdout,"stat.bufid %d\n",stat.bufid);
	  fprintf(stdout,"stat.tc_valid %d\n",stat.tc_valid);
	  exit (0);
     }

     if (!strcmp(argv[argidx],"control")) {
	  ioctl(fd,SOUND_MIXER_PRIVATE3,&ctrl);
	  fprintf(stdout,"ctrl.start %d\n",ctrl.start);
	  fprintf(stdout,"ctrl.latency %d (0=64 .. 7=8192)\n",ctrl.latency);
	  fprintf(stdout,"ctrl.master %d\n",ctrl.master);
	  fprintf(stdout,"ctrl.ie %d\n",ctrl.ie);
	  fprintf(stdout,"ctrl.sr48 %d\n",ctrl.sr48);
	  fprintf(stdout,"ctrl.spare %d\n",ctrl.spare);
	  fprintf(stdout,"ctrl.doublespeed %d\n",ctrl.doublespeed);
	  fprintf(stdout,"ctrl.pro %d\n",ctrl.pro);
	  fprintf(stdout,"ctrl.emphasis %d\n",ctrl.emphasis);
	  fprintf(stdout,"ctrl.dolby %d\n",ctrl.dolby);
	  fprintf(stdout,"ctrl.opt_out %d\n",ctrl.opt_out);
	  fprintf(stdout,"ctrl.wordclock %d\n",ctrl.wordclock);
	  fprintf(stdout,"ctrl.spdif_in %d (0=optical,1=coax,2=intern)\n",ctrl.spdif_in);
	  fprintf(stdout,"ctrl.sync_ref %d (0=ADAT1,1=ADAT2,2=ADAT3,3=SPDIF)\n",ctrl.sync_ref);
	  fprintf(stdout,"ctrl.spdif_reset %d\n",ctrl.spdif_reset);
	  fprintf(stdout,"ctrl.spdif_select %d\n",ctrl.spdif_select);
	  fprintf(stdout,"ctrl.spdif_clock %d\n",ctrl.spdif_clock);
	  fprintf(stdout,"ctrl.spdif_write %d\n",ctrl.spdif_write);
	  fprintf(stdout,"ctrl.adat1_cd %d\n",ctrl.adat1_cd);
	  exit (0);
     }

     if (!strcmp(argv[argidx],"mix")) {
	  rme_mixer mix;
	  int i;

	  for (i=0; i<4; i++) {
	       mix.devnr = i;
	       ioctl(fd,SOUND_MIXER_PRIVATE1,&mix);
	       if (mix.devnr == i) {
		    fprintf(stdout,"devnr %d\n",mix.devnr);
		    fprintf(stdout,"mix.i_offset %2d (0-25)\n",mix.i_offset);
		    fprintf(stdout,"mix.o_offset %2d (0-25)\n",mix.o_offset);
	       }
	  }
	  exit (0);
     }

/* the control flags */

     if (argc < argidx+2)
	  usage();

     if (!strcmp(argv[argidx],"master")) {
	  int val = atoi(argv[argidx+1]);
	  ioctl(fd,SOUND_MIXER_PRIVATE3,&ctrl);
	  printf("master = %d\n",val);
	  ctrl.master = val;
	  ioctl(fdwr,SOUND_MIXER_PRIVATE3,&ctrl);
	  exit (0);
     }

     if (!strcmp(argv[argidx],"pro")) {
	  int val = atoi(argv[argidx+1]);
	  ioctl(fd,SOUND_MIXER_PRIVATE3,&ctrl);
	  printf("pro = %d\n",val);
	  ctrl.pro = val;
	  ioctl(fdwr,SOUND_MIXER_PRIVATE3,&ctrl);
	  exit (0);
     }

     if (!strcmp(argv[argidx],"emphasis")) {
	  int val = atoi(argv[argidx+1]);
	  ioctl(fd,SOUND_MIXER_PRIVATE3,&ctrl);
	  printf("emphasis = %d\n",val);
	  ctrl.emphasis = val;
	  ioctl(fdwr,SOUND_MIXER_PRIVATE3,&ctrl);
	  exit (0);
     }

     if (!strcmp(argv[argidx],"dolby")) {
	  int val = atoi(argv[argidx+1]);
	  ioctl(fd,SOUND_MIXER_PRIVATE3,&ctrl);
	  printf("dolby = %d\n",val);
	  ctrl.dolby = val;
	  ioctl(fdwr,SOUND_MIXER_PRIVATE3,&ctrl);
	  exit (0);
     }

     if (!strcmp(argv[argidx],"optout")) {
	  int val = atoi(argv[argidx+1]);
	  ioctl(fd,SOUND_MIXER_PRIVATE3,&ctrl);
	  printf("optout = %d\n",val);
	  ctrl.opt_out = val;
	  ioctl(fdwr,SOUND_MIXER_PRIVATE3,&ctrl);
	  exit (0);
     }

     if (!strcmp(argv[argidx],"wordclock")) {
	  int val = atoi(argv[argidx+1]);
	  ioctl(fd,SOUND_MIXER_PRIVATE3,&ctrl);
	  printf("wordclock = %d\n",val);
	  ctrl.wordclock = val;
	  ioctl(fdwr,SOUND_MIXER_PRIVATE3,&ctrl);
	  exit (0);
     }

     if (!strcmp(argv[argidx],"spdifin")) {
	  int val = atoi(argv[argidx+1]);
	  ioctl(fd,SOUND_MIXER_PRIVATE3,&ctrl);
	  printf("spdifin = %d\n",val);
	  ctrl.spdif_in = val;
	  ioctl(fdwr,SOUND_MIXER_PRIVATE3,&ctrl);
	  exit (0);
     }

     if (!strcmp(argv[argidx],"syncref")) {
	  int val = atoi(argv[argidx+1]);
	  ioctl(fd,SOUND_MIXER_PRIVATE3,&ctrl);
	  printf("syncref = %d\n",val);
	  ctrl.sync_ref = val;
	  ioctl(fdwr,SOUND_MIXER_PRIVATE3,&ctrl);
	  exit (0);
     }

     if (!strcmp(argv[argidx],"adat1cd")) {
	  int val = atoi(argv[argidx+1]);
	  ioctl(fd,SOUND_MIXER_PRIVATE3,&ctrl);
	  printf("adat1cd = %d\n",val);
	  ctrl.adat1_cd = val;
	  ioctl(fdwr,SOUND_MIXER_PRIVATE3,&ctrl);
	  exit (0);
     }

/* setting offset */

     if (argc < argidx+4)
	  usage();

     if (!strcmp(argv[argidx],"offset")) {
	  rme_mixer mix;

	  mix.devnr = atoi(argv[argidx+1]);

	  mix.i_offset = atoi(argv[argidx+2]);
	  mix.o_offset = atoi(argv[argidx+3]);
	  ioctl(fdwr,SOUND_MIXER_PRIVATE1,&mix);
	  fprintf(stdout,"devnr %d\n",mix.devnr);
	  fprintf(stdout,"mix.i_offset to %d\n",mix.i_offset);
	  fprintf(stdout,"mix.o_offset to %d\n",mix.o_offset);
	  exit (0);
     }

     usage();
     exit (0); /* to avoid warning */
}


---------------------------- <snip> --------------------------------
#!/usr/bin/wish

# xrmectrl
# (C) 2000 Guenter Geiger <geiger@debian.org>
# HP20020201 - Heiko Purnhagen <purnhage@tnt.uni-hannover.de>

#set defaults "-relief ridged"
set CTRLPROG "./rmectrl"
if {$argc} {
    set CTRLPROG "$CTRLPROG $argv"
}
puts "CTRLPROG $CTRLPROG"

frame .butts
button .butts.exit -text "Exit" -command "exit" -relief ridge
#button .butts.state -text "State" -command "get_all"

pack .butts.exit -side left
pack .butts -side bottom


#
# STATUS
#

frame .status

# Sampling Rate

frame .status.sr
label .status.sr.text -text "Sampling Rate" -justify left
radiobutton .status.sr.441 -selectcolor red -text "44.1 kHz" -width 10 -anchor nw -variable srate -value 44100 -font times
radiobutton .status.sr.480 -selectcolor red -text "48 kHz" -width 10 -anchor nw -variable srate -value 48000 -font times
radiobutton .status.sr.882 -selectcolor red -text "88.2 kHz" -width 10 -anchor nw -variable srate -value 88200 -font times
radiobutton .status.sr.960 -selectcolor red -text "96 kHz" -width 10 -anchor nw  -variable srate -value 96000 -font times

pack .status.sr.text .status.sr.441 .status.sr.480 .status.sr.882 .status.sr.960 -side top -padx 3

# Lock

frame .status.lock
label .status.lock.text -text "Lock" -justify left
checkbutton .status.lock.adat1 -selectcolor red -text "ADAT1" -anchor nw -width 10 -variable adatlock1 -font times
checkbutton .status.lock.adat2 -selectcolor red -text "ADAT2" -anchor nw -width 10 -variable adatlock2 -font times
checkbutton .status.lock.adat3 -selectcolor red -text "ADAT3" -anchor nw -width 10 -variable adatlock3 -font times

pack .status.lock.text .status.lock.adat1 .status.lock.adat2 .status.lock.adat3 -side top -padx 3 

# Sync

frame .status.sync
label .status.sync.text -text "Sync" -justify left
checkbutton .status.sync.adat1 -selectcolor red -text "ADAT1" -anchor nw -width 10 -variable adatsync1 -font times
checkbutton .status.sync.adat2 -selectcolor red -text "ADAT2" -anchor nw -width 10 -variable adatsync2 -font times
checkbutton .status.sync.adat3 -selectcolor red -text "ADAT3" -anchor nw -width 10 -variable adatsync3 -font times

pack .status.sync.text .status.sync.adat1 .status.sync.adat2 .status.sync.adat3 -side top -padx 3 

# Timecode

frame .status.tc
label .status.tc.text -text "Timecode" -justify left
checkbutton .status.tc.busy -selectcolor red -text "busy" -anchor nw -width 10 -variable tcbusy -font times
checkbutton .status.tc.out -selectcolor red -text "out" -anchor nw -width 10 -variable tcout -font times
checkbutton .status.tc.valid -selectcolor red -text "valid" -anchor nw -width 10 -variable tcvalid -font times

pack .status.tc.text .status.tc.busy .status.tc.out .status.tc.valid -side top -padx 3 

# SPDIF In

frame .status.spdif
label .status.spdif.text -text "SPDIF In" -justify left
label .status.spdif.sr -text "--.- kHz" -anchor n -width 10 -font times
checkbutton .status.spdif.error -selectcolor red -text "Input Lock" -anchor nw -width 10 -variable spdiferr -font times

pack .status.spdif.text .status.spdif.sr .status.spdif.error -side top -padx 3 

pack .status.sr .status.lock .status.sync .status.tc .status.spdif -side left -fill x -anchor n -expand 1


#
# CONTROL 
#

proc setprof {} {
    global CTRLPROG
    global spprof
    exec $CTRLPROG pro $spprof
}

proc setemph {} {
    global CTRLPROG
    global spemph
    exec $CTRLPROG emphasis $spemph
}

proc setnoaud {} {
    global CTRLPROG
    global spnoaud
    exec $CTRLPROG dolby $spnoaud
}

proc setoptical {} {
    global CTRLPROG
    global spoptical
    exec $CTRLPROG optout $spoptical
}

proc setspdifin {} {
    global CTRLPROG
    global spdifin
    exec $CTRLPROG spdifin [expr $spdifin - 1]
}

proc setsyncsource {} {
    global CTRLPROG
    global syncsource
    exec $CTRLPROG syncref [expr $syncsource -1]
}


proc setmaster {} {
    global CTRLPROG
    global master
    exec $CTRLPROG master $master
}

proc setwordclock {} {
    global CTRLPROG
    global wordclock
    exec $CTRLPROG wordclock $wordclock
}

proc setadat1cd {} {
    global CTRLPROG
    global adat1cd
    exec $CTRLPROG adat1cd $adat1cd
}


frame .control

# SPDIF In & SPDIF Out


frame .control.spdif

frame .control.spdif.in
label .control.spdif.in.text -text "SPDIF In" -justify left
radiobutton .control.spdif.in.input1 -text "Optical" -anchor nw -width 13 -variable spdifin -value 1 -command setspdifin -selectcolor blue -font times
radiobutton .control.spdif.in.input2 -text "Coaxial" -anchor nw -width 13 -variable spdifin -value 2 -command setspdifin -selectcolor blue -font times
radiobutton .control.spdif.in.input3 -text "Intern " -anchor nw -width 13 -variable spdifin -command setspdifin -value 3 -selectcolor blue -font times

checkbutton .control.spdif.in.adat1cd -text "ADAT1 Intern" -anchor nw -width 13 -variable adat1cd -command setadat1cd -selectcolor blue -font times

pack .control.spdif.in.text .control.spdif.in.input1 .control.spdif.in.input2 .control.spdif.in.input3 .control.spdif.in.adat1cd

label .control.spdif.space

frame .control.spdif.out
label .control.spdif.out.text -text "SPDIF Out" -justify left
checkbutton .control.spdif.out.pro -text "Professional" -anchor nw -width 13 -variable spprof -command setprof -selectcolor blue -font times
checkbutton .control.spdif.out.emphasis -text "Emphasis" -anchor nw -width 13 -variable spemph -command setemph -selectcolor blue -font times
checkbutton .control.spdif.out.dolby -text "NoAudio" -anchor nw -width 13 -variable spnoaud -command setnoaud -selectcolor blue -font times
checkbutton .control.spdif.out.optout -text "Optical Out" -anchor nw -width 13 -variable spoptical -command setoptical -selectcolor blue -font times

pack .control.spdif.out.optout .control.spdif.out.dolby .control.spdif.out.emphasis .control.spdif.out.pro .control.spdif.out.text -side bottom

pack .control.spdif.in .control.spdif.space .control.spdif.out -side top -fill y -padx 3 -expand 1

# Sync Mode & Sync Source

frame .control.sync
frame .control.sync.mode
label .control.sync.mode.text -text "Sync Mode" -justify left
checkbutton .control.sync.mode.master -text "Master" -anchor nw -width 13 -variable master -command setmaster -selectcolor blue -font times
checkbutton .control.sync.mode.wc -text "Wordclock" -anchor nw -width 13 -variable wordclock -command setwordclock -selectcolor blue -font times

pack .control.sync.mode.text .control.sync.mode.master .control.sync.mode.wc

label .control.sync.space

frame .control.sync.src
label .control.sync.src.text -text "Sync Source" -justify left
radiobutton .control.sync.src.input1 -text "ADAT1" -anchor nw -width 13 -variable syncsource -value 1 -command setsyncsource -selectcolor blue -font times
radiobutton .control.sync.src.input2 -text "ADAT2" -anchor nw -width 13 -variable syncsource -value 2 -command setsyncsource -selectcolor blue -font times
radiobutton .control.sync.src.input3 -text "ADAT3" -anchor nw -width 13 -variable syncsource -command setsyncsource -value 3 -selectcolor blue -font times
radiobutton .control.sync.src.input4 -text "SPDIF" -anchor nw -width 13 -variable syncsource -command setsyncsource -value 4 -selectcolor blue -font times

pack .control.sync.src.input4 .control.sync.src.input3 .control.sync.src.input2 .control.sync.src.input1 .control.sync.src.text -side bottom

pack .control.sync.mode .control.sync.space .control.sync.src -side top -fill y -padx 3 -expand 1

label .control.space -text "" -width 10

# Buffer Size

frame .control.buf
label .control.buf.text -text "Buffer Size (Latency)" -justify left
radiobutton .control.buf.b1 -selectcolor red -text "64 (1.5 ms)" -width 13 -anchor nw -variable ssrate -value 1 -font times
radiobutton .control.buf.b2 -selectcolor red -text "128 (3 ms)" -width 13 -anchor nw -variable ssrate -value 2 -font times
radiobutton .control.buf.b3 -selectcolor red -text "256 (6 ms)" -width 13 -anchor nw -variable ssrate -value 3 -font times
radiobutton .control.buf.b4 -selectcolor red -text "512 (12 ms)" -width 13 -anchor nw -variable ssrate -value 4 -font times
radiobutton .control.buf.b5 -selectcolor red -text "1024 (23 ms)" -width 13 -anchor nw -variable ssrate -value 5 -font times
radiobutton .control.buf.b6 -selectcolor red -text "2048 (46 ms)" -width 13 -anchor nw -variable ssrate -value 6 -font times
radiobutton .control.buf.b7 -selectcolor red -text "4096 (93 ms)" -width 13 -anchor nw -variable ssrate -value 7 -font times
radiobutton .control.buf.b8 -selectcolor red -text "8192 (186 ms)" -width 13 -anchor nw -variable ssrate -value 8 -font times

pack .control.buf.text .control.buf.b1 .control.buf.b2 .control.buf.b3 .control.buf.b4 .control.buf.b5 .control.buf.b6 .control.buf.b7 .control.buf.b8 -side top -padx 3 

# Offset

frame .control.offset

frame .control.offset.in
label .control.offset.in.text -text "Offset In" -justify left
label .control.offset.in.off0 -text "dev\#0: -" -anchor nw -width 10 -font times
label .control.offset.in.off1 -text "dev\#1: -" -anchor nw -width 10 -font times
label .control.offset.in.off2 -text "dev\#2: -" -anchor nw -width 10 -font times
label .control.offset.in.off3 -text "dev\#3: -" -anchor nw -width 10 -font times

pack .control.offset.in.text .control.offset.in.off0 .control.offset.in.off1 .control.offset.in.off2 .control.offset.in.off3

label .control.offset.space

frame .control.offset.out
label .control.offset.out.text -text "Offset Out" -justify left
label .control.offset.out.off0 -text "dev\#0: -" -anchor nw -width 10 -font times
label .control.offset.out.off1 -text "dev\#1: -" -anchor nw -width 10 -font times
label .control.offset.out.off2 -text "dev\#2: -" -anchor nw -width 10 -font times
label .control.offset.out.off3 -text "dev\#3: -" -anchor nw -width 10 -font times

pack .control.offset.out.off3 .control.offset.out.off2 .control.offset.out.off1 .control.offset.out.off0 .control.offset.out.text -side bottom

pack .control.offset.in .control.offset.space .control.offset.out -side top -fill y -padx 3 -expand 1


pack .control.spdif .control.sync .control.space .control.buf .control.offset -side left -fill both -anchor n -expand 1


label .statustext -text Status -justify center -relief ridge
label .controltext -text Control -justify center -relief ridge

label .statusspace
label .controlspace

pack .statustext .status .statusspace .controltext .control .controlspace -side top -anchor nw -fill both -expand 1


proc get_bit {output sstr} {
    set idx1 [string last [concat $sstr 1] $output]
    set idx1 [expr $idx1 != -1]
    return $idx1
}

proc get_val {output sstr} {
    set val [string wordend $output [string last $sstr $output]] 
    set val [string range $output $val [expr $val+1]]
    return $val
}

proc get_val2 {output sstr} {
    set val [string wordend $output [string first $sstr $output]] 
    set val [string range $output $val [expr $val+2]]
    return $val
}

proc get_control {} {
    global spprof
    global spemph
    global spnoaud
    global spoptical
    global spdifin
    global ssrate
    global master
    global wordclock
    global syncsource
    global CTRLPROG

    set f [open "| $CTRLPROG control" r+]
    set ooo [read $f 1000]
    close $f
#    puts $ooo

    set spprof [ get_bit $ooo "pro"]
    set spemph [ get_bit $ooo "emphasis"]
    set spnoaud [ get_bit $ooo "dolby"]
    set spoptical [ get_bit $ooo "opt_out"]
    set spdifin [ expr [ get_val $ooo "spdif_in"] + 1]
    set ssrate [ expr [ get_val $ooo "latency"] + 1]
    set master [ expr [ get_val $ooo "master"]]
    set wordclock [ expr [ get_val $ooo "wordclock"]]
    set syncsource [ expr [ get_val $ooo "sync_ref"] + 1]
}

proc get_status {} {
    global srate
    global ctrlcom

    global adatlock1
    global adatlock2
    global adatlock3

    global adatsync1
    global adatsync2
    global adatsync3

    global tcbusy
    global tcout
    global tcvalid

    global spdiferr
    global crystal
    global .status.spdif.text
    global CTRLPROG


    set f [open "| $CTRLPROG status" r+]
    set ooo [read $f 1000]
    close $f
#    puts $ooo

# samplerate

    set idx1 [string last "sr48 1" $ooo]
    set idx2 [string last "doublespeed 1" $ooo]
    if {$idx1 >= 0} {
	set fact1 48000
    } else {
	set fact1 44100
    }

    if {$idx2 >= 0} {
	set fact2 2
    } else {
	set fact2 1
    }
    set srate [expr $fact1 * $fact2]
#   ADAT lock

    set val [get_val $ooo lockmask]
    set adatlock1 0
    set adatlock2 0
    set adatlock3 0
    if {[expr $val & 1]} {
       set adatlock3 1
    }
    if {[expr $val & 2]} {
       set adatlock2 1
    }
    if {[expr $val & 4]} {
       set adatlock1 1
    }

#  ADAT sync
    set val [get_val $ooo syncmask]
    set adatsync1 0
    set adatsync2 0
    set adatsync3 0

    if {[expr $val & 1]} {
       set adatsync3 1
    }
    if {[expr $val & 2]} {
       set adatsync2 1
    }
    if {[expr $val & 4]} {
       set adatsync1 1
    }

# TC busy

    set tcbusy [get_bit $ooo "busy"]
    set tcout [get_bit $ooo "out"]
    set tcvalid [get_bit $ooo "valid"]
    set spdiferr [expr [get_bit $ooo "spdif_error"] == 0]

#  000=64kHz, 100=88.2kHz, 011=96kHz
#  111=32kHz, 110=44.1kHz, 101=48kHz

    set val [get_val $ooo crystalrate]

    set crystal "--.- kHz"
    if {$val == 0} {
        set crystal "64 kHz"
    }
    if {$val == 4} {
        set crystal "88.2 kHz"
    }
    if {$val == 3} {
        set crystal "96 kHz"
    }
    if {$val == 7} {
        set crystal "32 kHz"
    }
    if {$val == 6} {
        set crystal "44.1 kHz"
    }
    if {$val == 5} {
        set crystal "48 kHz"
    }
    .status.spdif.sr configure -text $crystal
}

proc get_offset {} {
    global inoffset
    global outoffset
    global CTRLPROG

    set f [open "| $CTRLPROG mix" r+]
    set ooo [read $f 1000]
    close $f
#    puts $ooo

    if { [string match "*devnr*" $ooo] } {
	set ooo [string range $ooo [string wordend $ooo [string first devnr $ooo]] end]
	set val [get_val2 $ooo i_offset]
	.control.offset.in.off0 configure -text "dev\#0: $val"
	set val [get_val2 $ooo o_offset]
	.control.offset.out.off0 configure -text "dev\#0: $val"
    } else {
	.control.offset.in.off0 configure -text "dev\#0: -"
	.control.offset.out.off0 configure -text "dev\#0: -"
    }
    if { [string match "*devnr*" $ooo] } {
	set ooo [string range $ooo [string wordend $ooo [string first devnr $ooo]] end]
	set val [get_val2 $ooo i_offset]
	.control.offset.in.off1 configure -text "dev\#1: $val"
	set val [get_val2 $ooo o_offset]
	.control.offset.out.off1 configure -text "dev\#1: $val"
    } else {
	.control.offset.in.off1 configure -text "dev\#1: -"
	.control.offset.out.off1 configure -text "dev\#1: -"
    }
    if { [string match "*devnr*" $ooo] } {
	set ooo [string range $ooo [string wordend $ooo [string first devnr $ooo]] end]
	set val [get_val2 $ooo i_offset]
	.control.offset.in.off2 configure -text "dev\#2: $val"
	set val [get_val2 $ooo o_offset]
	.control.offset.out.off2 configure -text "dev\#2: $val"
    } else {
	.control.offset.in.off2 configure -text "dev\#2: -"
	.control.offset.out.off2 configure -text "dev\#2: -"
    }
    if { [string match "*devnr*" $ooo] } {
	set ooo [string range $ooo [string wordend $ooo [string first devnr $ooo]] end]
	set val [get_val2 $ooo i_offset]
	.control.offset.in.off3 configure -text "dev\#3: $val"
	set val [get_val2 $ooo o_offset]
	.control.offset.out.off3 configure -text "dev\#3: $val"
    } else {
	.control.offset.in.off3 configure -text "dev\#3: -"
	.control.offset.out.off3 configure -text "dev\#3: -"
    }
}


proc get_all {} {
get_status
get_control
get_offset
}

# main
while {1} {
  after 200
  get_all
  update
}