- 001
- 002
- 003
- 004
- 005
- 006
- 007
- 008
- 009
- 010
- 011
- 012
- 013
- 014
- 015
- 016
- 017
- 018
- 019
- 020
- 021
- 022
- 023
- 024
- 025
- 026
- 027
- 028
- 029
- 030
- 031
- 032
- 033
- 034
- 035
- 036
- 037
- 038
- 039
- 040
- 041
- 042
- 043
- 044
- 045
- 046
- 047
- 048
- 049
- 050
- 051
- 052
- 053
- 054
- 055
- 056
- 057
- 058
- 059
- 060
- 061
- 062
- 063
- 064
- 065
- 066
- 067
- 068
- 069
- 070
- 071
- 072
- 073
- 074
- 075
- 076
- 077
- 078
- 079
- 080
- 081
- 082
- 083
- 084
- 085
- 086
- 087
- 088
- 089
- 090
- 091
- 092
- 093
- 094
- 095
- 096
- 097
- 098
- 099
- 100
void riffoser_track_writeriff(struct riffoser_track * track,char * filename,riffoser_samplerate_t samplerate,riffoser_bitspersample_t bitspersample) {
FILE * fp;
unsigned long i1,i2,i3,i4,i5,i6,fpi;
unsigned char c1,bytespersample;
unsigned char * skipbuf;
float fret,val;
long ival;
unsigned char nomorewaves;
riffoser_channel_t chan;
bytespersample=bitspersample/8;
fp=fopen(filename,"wb");
i1=track->channels*samplerate*bytespersample*track->length;
i2=4+24+8+i1+(i1%2>0?1:0);
riffoser_writestr("RIFF");
riffoser_writeint(4,4+24+8+i1+(i1%2>0?1:0));
riffoser_writestr("WAVEfmt ");
riffoser_writeint(4,16);
riffoser_writeint(2,1);
riffoser_writeint(2,track->channels);
riffoser_writeint(4,samplerate);
riffoser_writeint(4,bytespersample*track->channels*samplerate);
riffoser_writeint(2,bytespersample*track->channels);
riffoser_writeint(2,bytespersample*8);
riffoser_writestr("data");
riffoser_writeint(4,i1);
if (i1%2>0)
riffoser_writeint(1,0);
i1=0;
i3=track->waves_count;
i6=0;
nomorewaves=0;
while (1) {
chan=i1%track->channels;
if (i3==track->waves_count&&!nomorewaves) {
i5=i1;
0&&printf("%lu: finding nearest wave...\n",i1);
for (i2=0;i2<track->waves_count;i2++){
if ((track->wavestates[i2]->state==RIFFOSER_WAVESTATE_IDLE)&&(((i3==track->waves_count))||(RIFFOSER_RENDER___FROM(track->wavestates[i2])<=i5))&&(RIFFOSER_RENDER___FROM(track->wavestates[i2])>=i6)) {
0&&printf("%lu: found wave %lu with from %f\n",i1,i2,RIFFOSER_RENDER___FROM(track->wavestates[i2]));
i3=i2;
i5=RIFFOSER_RENDER___FROM(track->wavestates[i2]);
}
else {
0&&printf("%lu: skipping wave %lu\n",i1,i2);
}
}
if (i3!=track->waves_count)
0&&printf("next wave will be %lu at %f\n",i3,RIFFOSER_RENDER___FROM(track->wavestates[i3]));
else nomorewaves=1;
}
c1=0;
val=0;
for (i2=0;i2<track->waves_count;i2++){
if (track->wavestates[i2]->state==RIFFOSER_WAVESTATE_RENDERING) {
if (RIFFOSER_RENDER___TO(track->wavestates[i2])<i1) {
track->wavestates[i2]->state=RIFFOSER_WAVESTATE_FINISHED;
0&&printf("%lu: wave %lu finished\n",i1,i2);
} else {
if (track->wavestates[i2]->channel==chan) {
// printf("correct channel\n");
track->wavestates[i2]->samplenum++;
if (track->wavestates[i2]->samplenum>RIFFOSER_RENDER___WSC(track->waves[i2]))
track->wavestates[i2]->samplenum-=RIFFOSER_RENDER___WSC(track->waves[i2]);
0&&printf("%lu: rendering wave %lu (%f-%f), samplenum is %f (%f%%)\n",i1,i2,RIFFOSER_RENDER___FROM(track->wavestates[i2]),RIFFOSER_RENDER___TO(track->wavestates[i2]),track->wavestates[i2]->samplenum,RIFFOSER_RENDER___WPP(track->waves[i2],track->wavestates[i2]->samplenum));
RIFFOSER_WAVE_FUNC(track->waves[i2],RIFFOSER_RENDER___WPP(track->waves[i2],track->wavestates[i2]->samplenum));
fret=fret*track->waves[i2]->amplitude/200;
// replace for now
val=fret;
}
else {
// printf("wrong channel ( %u / %u )\n",track->wavestates[i2]->channel,chan);
}
c1=1;
}
}
}
if (bytespersample==1) {
ival=round(val*2.56)-128;
if (ival>127)
ival=127;
}
else if (bytespersample==2) {
ival=round(val*655.36);
if (ival>65535)
ival=65535;
}
else if (bytespersample==4) {
ival=round(val*42949672.96);
if (ival>4294967295)
ival=4294967295;
}
riffoser_writeint(bytespersample,ival);
if (c1==0) {
if (i3!=track->waves_count) {
0&&printf("%lu: warping to position %lu\n",i1,i5);
i4=(i5-i1)*bytespersample;
skipbuf=malloc(i4);
memset(skipbuf,0,i4);
riffoser_writebuf(i4,skipbuf);
Полная версия - https://github.com/anon4ik/riffoser/blob/master/libriffoser.c