File size: 2,681 Bytes
806d4b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

Bool record_stop,record_done;

U0 (*fp_old_copy_buf)(SND_IN_CONTAINER *buf,I64 buf_num)=NULL;

#define RECORD_BUF_SIZE (SND_BUF_LEN*0x100)

SND_IN_CONTAINER *record_buf[2];
I64 record_buf_iptr,record_ibuf_num,record_obuf_num;

U0 RecordCopyBuf(SND_IN_CONTAINER *buf,I64)
{
  I64 j;
  SND_IN_CONTAINER *_s=record_buf[record_ibuf_num&1];
  j=0;
  while (j<SND_BUF_LEN) {
    _s[record_buf_iptr++]=buf[j++];
    j++; //Record in mono
  }
  if (record_buf_iptr>=RECORD_BUF_SIZE) {
    record_buf_iptr=0;
    record_ibuf_num++;
  }
}

U0 DrawIt(CTask *task,CDC *dc)
{
  SND_IN_CONTAINER *buf=hda.istr0_buf[0];
  I64 i,x1,y1,x2=0,y2=0,
        cy=task->pix_height>>1;
  dc->color=BLUE;
  for (i=0;i<SND_BUF_LEN;i++) {
    x1=i*task->pix_width/SND_BUF_LEN;
    y1=cy-buf[i]*cy/I16_MAX;
    GrLine(dc,x2,y2,x1,y1);
    x2=x1;
    y2=y1;
  }
  dc->color=RED;
}

U0 RecordTaskEndCB()
{
  fp_snd_copy_buf=fp_old_copy_buf;
  HDStop(TRUE,FALSE);
  Free(record_buf[0]);
  Free(record_buf[1]);
  record_done=TRUE;
  Exit;
}

U0 RecordTask(U8 *filename)
{
  Bool old_io_snd;
  U8 *filename=StrNew(filename),*filename2;
  TaskInitExt; //needed for disk access
  record_buf[0]=CAlloc(RECORD_BUF_SIZE*sizeof(SND_IN_CONTAINER));
  record_buf[1]=CAlloc(RECORD_BUF_SIZE*sizeof(SND_IN_CONTAINER));
  FileExtRem(filename);
  record_buf_iptr=0;
  record_ibuf_num=0;
  record_obuf_num=0;
  record_done=FALSE;
  Fs->task_end_cb=&RecordTaskEndCB;
  fp_old_copy_buf=fp_snd_copy_buf;
  fp_snd_copy_buf=&RecordCopyBuf;
  HDRun(TRUE,FALSE);
  old_io_snd=IOSnd(OFF);
  while (!record_stop) {
    if (record_ibuf_num>record_obuf_num) {
      filename2=MPrint("%s%04d.SNZ",filename,record_obuf_num);
      SndFileWrite(filename2,record_buf[record_obuf_num&1],RECORD_BUF_SIZE);
      record_obuf_num++;
      Free(filename2);
    }
    Sleep(1);
  }
  filename2=MPrint("%s%04d.SNZ",filename,record_obuf_num);
  SndFileWrite(filename2,record_buf[record_obuf_num&1],record_buf_iptr);
  record_obuf_num++;
  Free(filename2);
  Free(filename);
  IOSnd(old_io_snd);
  record_done=TRUE;
}

U0 RecordDemo(U8 *filename="~/Record.SNZ")
{
  if (snd_dev!=SD_HD_AUDIO) {
    "Only works for HD Audio.\n";
    return;
  }
  record_done=FALSE;
  record_stop=FALSE;
  Spawn(&RecordTask,filename,"Record .SNZ",,Fs);
  SettingsPush; //See SettingsPush
  DocCursor;
  DocClear;
  Fs->draw_it=&DrawIt;
  try
          GetChar;
  catch //Catch <CTRL-ALT-C>, but pass it on to next higher hndlr.
    record_stop=TRUE;
  record_stop=TRUE;
  while (record_done)
    Yield;
  SettingsPop;
  while (TRUE) {
    "\n\nPlay ";
    if (YorN)
      SndFilePlaySeq(filename,record_obuf_num);
    else
      break;
  }
  NewLine(2);
}

RecordDemo;