revert between 56095 -> 55830 in arch
[AROS.git] / workbench / devs / AHI / Drivers / Filesave / filesave-recslave.c
blobc2fbb6ef2222086915a0b8ab279f11f03eb90419
2 #include <config.h>
4 #include <exec/memory.h>
5 #include <exec/execbase.h>
6 #include <datatypes/soundclass.h>
7 #include <devices/ahi.h>
8 #include <libraries/ahi_sub.h>
9 #include <libraries/asl.h>
11 #include <proto/datatypes.h>
12 #include <proto/dos.h>
13 #include <proto/exec.h>
15 #include "DriverData.h"
16 #include "library.h"
18 #define dd ((struct FilesaveData*) AudioCtrl->ahiac_DriverData)
21 /******************************************************************************
22 ** The record slave process ***************************************************
23 ******************************************************************************/
25 #undef SysBase
27 static void RecSlave( struct ExecBase* SysBase );
29 #if defined( __AROS__ )
31 #include <aros/asmcall.h>
33 AROS_UFH3(void, RecSlaveEntry,
34 AROS_UFHA(STRPTR, argPtr, A0),
35 AROS_UFHA(ULONG, argSize, D0),
36 AROS_UFHA(struct ExecBase *, SysBase, A6))
38 AROS_USERFUNC_INIT
39 RecSlave( SysBase );
40 AROS_USERFUNC_EXIT
43 #else
45 void RecSlaveEntry(void)
47 struct ExecBase* SysBase = *((struct ExecBase**) 4);
49 RecSlave( SysBase );
51 #endif
53 static void RecSlave( struct ExecBase* SysBase )
55 struct AHIAudioCtrlDrv* AudioCtrl;
56 struct DriverBase* AHIsubBase;
57 struct FilesaveBase* FilesaveBase;
59 ULONG signals;
60 BPTR lock = 0,cd=0,file = 0;
61 Object *o = NULL;
62 BYTE *samples = NULL;
63 ULONG length = 0;
64 ULONG count = 0,offs = 0,i;
66 struct AHIRecordMessage RecordMessage =
68 AHIST_S16S,
69 NULL,
70 RECBUFFERSIZE
73 /* Note that in OS4, we cannot call FindTask(NULL) here, since IExec
74 * is inside AHIsubBase! */
75 AudioCtrl = (struct AHIAudioCtrlDrv*) FindTask(NULL)->tc_UserData;
76 AHIsubBase = (struct DriverBase*) dd->fs_AHIsubBase;
77 FilesaveBase = (struct FilesaveBase*) AHIsubBase;
79 RecordMessage.ahirm_Buffer = dd->fs_RecBuffer;
81 if(!(lock = Lock(dd->fs_RecFileReq->fr_Drawer,ACCESS_READ)))
82 goto quit;
83 cd = CurrentDir(lock);
85 if(DataTypesBase)
87 struct TagItem newtags[] =
89 { DTA_GroupID, GID_SOUND },
90 { TAG_DONE, 0 }
93 struct TagItem attrtags[] =
95 { SDTA_Sample, (IPTR)&samples },
96 { SDTA_SampleLength, (IPTR)&length },
97 { TAG_DONE, 0 }
100 if (!(o = NewDTObjectA (dd->fs_RecFileReq->fr_File, newtags)))
101 goto quit;
103 GetDTAttrsA(o, attrtags );
105 else // datatypes.library not open. Open the selected file as raw 8 bit signed instead.
107 if(!(file = Open(dd->fs_RecFileReq->fr_File,MODE_OLDFILE)))
108 goto quit;
109 Seek(file,0,OFFSET_END);
110 length = Seek(file,0,OFFSET_BEGINNING);
111 if(!(samples = AllocVec(length,MEMF_ANY)))
112 goto quit;
113 if(length != (ULONG) Read(file,samples,length))
114 goto quit;
117 if(!samples || !length )
118 goto quit;
120 if((dd->fs_RecSlaveSignal = AllocSignal(-1)) == -1)
121 goto quit;
123 // Everything set up. Tell Master we're alive and healthy.
124 Signal((struct Task *)dd->fs_MasterTask,1L<<dd->fs_RecMasterSignal);
126 for(;;)
128 signals = SetSignal(0L,0L);
129 if(signals & (SIGBREAKF_CTRL_C | 1L<<dd->fs_RecSlaveSignal))
130 break;
132 for(;;)
134 if(count+RECBUFFERSIZE-offs < length)
136 // End of sample will not be reached; just fill to the end of dd->fs_RecBuffer.
137 for(i = RECBUFFERSIZE-offs;i>0;i--)
139 dd->fs_RecBuffer[(offs)<<1] =
140 dd->fs_RecBuffer[((offs)<<1)+1] =
141 samples[count++]<<8;
142 offs++;
144 offs = 0;
145 break;
147 else
149 // End of sample will be reached. Fill part of buffer, and iterate (== don't break).
150 for(i = length-count;i>0;i--)
152 dd->fs_RecBuffer[(offs)<<1] =
153 dd->fs_RecBuffer[((offs)<<1)+1] =
154 samples[count++]<<8;
155 offs++;
157 count = 0;
162 CallHookPkt(AudioCtrl->ahiac_SamplerFunc,AudioCtrl,&RecordMessage);
163 Delay(50*RECBUFFERSIZE/AudioCtrl->ahiac_MixFreq);
166 quit:
167 // Get rid of object
168 if(DataTypesBase)
170 if(o)
171 DisposeDTObject (o);
173 else // datatypes.library not open.
175 if(samples)
176 FreeVec(samples);
177 if(file)
178 Close(file);
180 CurrentDir(cd);
181 if(lock)
182 UnLock(lock);
184 Forbid();
185 dd->fs_RecSlaveTask = NULL;
186 FreeSignal(dd->fs_RecSlaveSignal);
187 dd->fs_RecSlaveSignal = -1;
188 // Tell the Master we're dying
189 Signal((struct Task *)dd->fs_MasterTask,1L<<dd->fs_RecMasterSignal);
190 // Multitaking will resume when we are dead.