2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 // SwitchingSampRate.cpp : Defines the entry point for the console
18 #include "signal_processing_library.h"
20 #define MAX_FILE_NAME 500
21 #define MAX_NUM_CLIENTS 2
28 int main(int argc, char* argv[])
30 char fileNameWB[MAX_FILE_NAME];
31 char fileNameSWB[MAX_FILE_NAME];
33 char outFileName[MAX_NUM_CLIENTS][MAX_FILE_NAME];
35 FILE* inFile[MAX_NUM_CLIENTS];
36 FILE* outFile[MAX_NUM_CLIENTS];
38 ISACStruct* codecInstance[MAX_NUM_CLIENTS];
39 int32_t resamplerState[MAX_NUM_CLIENTS][8];
41 int encoderSampRate[MAX_NUM_CLIENTS];
49 strcpy(outFileName[0], "switchSampRate_out1.pcm");
50 strcpy(outFileName[1], "switchSampRate_out2.pcm");
54 unsigned int lenEncodedInBytes[MAX_NUM_CLIENTS];
55 unsigned int lenAudioIn10ms[MAX_NUM_CLIENTS];
56 unsigned int lenEncodedInBytesTmp[MAX_NUM_CLIENTS];
57 unsigned int lenAudioIn10msTmp[MAX_NUM_CLIENTS];
58 BottleNeckModel* packetData[MAX_NUM_CLIENTS];
60 char versionNumber[100];
61 short samplesIn10ms[MAX_NUM_CLIENTS];
62 int bottleneck[MAX_NUM_CLIENTS];
65 printf("____________________________________________\n\n");
66 WebRtcIsac_version(versionNumber);
67 printf(" iSAC-swb version %s\n", versionNumber);
68 printf("____________________________________________\n");
72 fileNameSWB[0] = '\0';
75 strcpy(myFlag, "-wb");
76 // READ THE WIDEBAND AND SUPER-WIDEBAND FILE NAMES
77 if(readParamString(argc, argv, myFlag, fileNameWB, MAX_FILE_NAME) <= 0)
79 printf("No wideband file is specified");
82 strcpy(myFlag, "-swb");
83 if(readParamString(argc, argv, myFlag, fileNameSWB, MAX_FILE_NAME) <= 0)
85 printf("No super-wideband file is specified");
88 // THE FIRST CLIENT STARTS IN WIDEBAND
89 encoderSampRate[0] = 16000;
90 OPEN_FILE_RB(inFile[0], fileNameWB);
92 // THE SECOND CLIENT STARTS IN SUPER-WIDEBAND
93 encoderSampRate[1] = 32000;
94 OPEN_FILE_RB(inFile[1], fileNameSWB);
97 short codingMode = readSwitch(argc, argv, myFlag);
99 for(clientCntr = 0; clientCntr < NUM_CLIENTS; clientCntr++)
101 codecInstance[clientCntr] = NULL;
104 printf("Client %d\n", clientCntr + 1);
105 printf("---------\n");
106 printf("Starting %s",
107 (encoderSampRate[clientCntr] == 16000)
108 ? "wideband":"super-wideband");
110 // Open output File Name
111 OPEN_FILE_WB(outFile[clientCntr], outFileName[clientCntr]);
112 printf("Output File...................... %s\n", outFileName[clientCntr]);
114 samplesIn10ms[clientCntr] = encoderSampRate[clientCntr] * 10;
118 bottleneck[clientCntr] = (clientCntr)? bnSWB:bnWB;
122 bottleneck[clientCntr] = (clientCntr)? minBn:maxBn;
125 printf("Bottleneck....................... %0.3f kbits/sec \n",
126 bottleneck[clientCntr] / 1000.0);
129 printf("Encoding Mode.................... %s\n",
130 (codingMode == 1)? "Channel-Independent (Instantaneous)":"Adaptive");
132 lenEncodedInBytes[clientCntr] = 0;
133 lenAudioIn10ms[clientCntr] = 0;
134 lenEncodedInBytesTmp[clientCntr] = 0;
135 lenAudioIn10msTmp[clientCntr] = 0;
137 packetData[clientCntr] = (BottleNeckModel*)new(BottleNeckModel);
138 if(packetData[clientCntr] == NULL)
140 printf("Could not allocate memory for packetData \n");
143 memset(packetData[clientCntr], 0, sizeof(BottleNeckModel));
144 memset(resamplerState[clientCntr], 0, sizeof(int32_t) * 8);
147 for(clientCntr = 0; clientCntr < NUM_CLIENTS; clientCntr++)
150 if(WebRtcIsac_Create(&codecInstance[clientCntr]))
152 printf("Could not creat client %d\n", clientCntr + 1);
156 WebRtcIsac_SetEncSampRate(codecInstance[clientCntr], encoderSampRate[clientCntr]);
158 WebRtcIsac_SetDecSampRate(codecInstance[clientCntr],
159 encoderSampRate[clientCntr + (1 - ((clientCntr & 1)<<1))]);
161 // Initialize Encoder
162 if(WebRtcIsac_EncoderInit(codecInstance[clientCntr],
165 printf("Could not initialize client, %d\n", clientCntr + 1);
169 // Initialize Decoder
170 if(WebRtcIsac_DecoderInit(codecInstance[clientCntr]) < 0)
172 printf("Could not initialize decoder of client %d\n",
177 // setup Rate if in Instantaneous mode
180 // ONLY Clients who are not in Adaptive mode
181 if(WebRtcIsac_Control(codecInstance[clientCntr],
182 bottleneck[clientCntr], 30) < 0)
184 printf("Could not setup bottleneck and frame-size for client %d\n",
193 short numSamplesRead;
194 short lenDecodedAudio;
199 short num10ms[MAX_NUM_CLIENTS];
200 memset(num10ms, 0, sizeof(short)*MAX_NUM_CLIENTS);
201 FILE* arrivalTimeFile1 = fopen("arrivalTime1.dat", "wb");
202 FILE* arrivalTimeFile2 = fopen("arrivalTime2.dat", "wb");
203 short numPrint[MAX_NUM_CLIENTS];
204 memset(numPrint, 0, sizeof(short) * MAX_NUM_CLIENTS);
207 short silence10ms[10 * 32];
208 memset(silence10ms, 0, 320 * sizeof(short));
209 short audioBuff10ms[10 * 32];
210 short audioBuff60ms[60 * 32];
211 short resampledAudio60ms[60 * 32];
213 unsigned short bitStream[600+600];
216 short numSampFreqChanged = 0;
217 while(numSampFreqChanged < 10)
219 for(clientCntr = 0; clientCntr < NUM_CLIENTS; clientCntr++)
221 // Encoding/decoding for this pair of clients, if there is
222 // audio for any of them
223 //if(audioLeft[clientCntr] || audioLeft[clientCntr + 1])
225 //for(pairCntr = 0; pairCntr < 2; pairCntr++)
227 senderIdx = clientCntr; // + pairCntr;
228 receiverIdx = 1 - clientCntr;// + (1 - pairCntr);
230 //if(num10ms[senderIdx] > 6)
232 // printf("Too many frames read for client %d",
237 numSamplesRead = (short)fread(audioBuff10ms, sizeof(short),
238 samplesIn10ms[senderIdx], inFile[senderIdx]);
239 if(numSamplesRead != samplesIn10ms[senderIdx])
241 // file finished switch encoder sampling frequency.
242 printf("Changing Encoder Sampling frequency in client %d to ", senderIdx+1);
243 fclose(inFile[senderIdx]);
244 numSampFreqChanged++;
245 if(encoderSampRate[senderIdx] == 16000)
247 printf("super-wideband.\n");
248 OPEN_FILE_RB(inFile[senderIdx], fileNameSWB);
249 encoderSampRate[senderIdx] = 32000;
253 printf("wideband.\n");
254 OPEN_FILE_RB(inFile[senderIdx], fileNameWB);
255 encoderSampRate[senderIdx] = 16000;
257 WebRtcIsac_SetEncSampRate(codecInstance[senderIdx], encoderSampRate[senderIdx]);
258 WebRtcIsac_SetDecSampRate(codecInstance[receiverIdx], encoderSampRate[senderIdx]);
260 samplesIn10ms[clientCntr] = encoderSampRate[clientCntr] * 10;
262 numSamplesRead = (short)fread(audioBuff10ms, sizeof(short),
263 samplesIn10ms[senderIdx], inFile[senderIdx]);
264 if(numSamplesRead != samplesIn10ms[senderIdx])
266 printf(" File %s for client %d has not enough audio\n",
267 (encoderSampRate[senderIdx]==16000)? "wideband":"super-wideband",
272 num10ms[senderIdx]++;
275 //if(num10ms[senderIdx] > 6)
277 // printf("Client %d has got more than 60 ms audio and encoded no packet.\n",
285 streamLen = WebRtcIsac_Encode(codecInstance[senderIdx],
286 audioBuff10ms, (short*)bitStream);
289 if(( WebRtcIsac_ReadFrameLen(codecInstance[receiverIdx],
290 (short *) bitStream, &ggg))<0)
297 printf(" Encoder error in client %d \n", senderIdx + 1);
304 // Packet generated; model sending through a channel, do bandwidth
305 // estimation at the receiver and decode.
306 lenEncodedInBytes[senderIdx] += streamLen;
307 lenAudioIn10ms[senderIdx] += (unsigned int)num10ms[senderIdx];
308 lenEncodedInBytesTmp[senderIdx] += streamLen;
309 lenAudioIn10msTmp[senderIdx] += (unsigned int)num10ms[senderIdx];
311 // Print after ~5 sec.
312 if(lenAudioIn10msTmp[senderIdx] >= 100)
314 numPrint[senderIdx]++;
315 printf(" %d, %6.3f => %6.3f ", senderIdx+1,
316 bottleneck[senderIdx] / 1000.0,
317 lenEncodedInBytesTmp[senderIdx] * 0.8 /
318 lenAudioIn10msTmp[senderIdx]);
323 WebRtcIsac_GetUplinkBw(codecInstance[senderIdx], &bn);
326 //int16_t rateIndexLB;
327 //int16_t rateIndexUB;
328 //WebRtcIsac_GetDownLinkBwIndex(codecInstance[receiverIdx],
329 // &rateIndexLB, &rateIndexUB);
330 //printf(" (%2d, %2d) ", rateIndexLB, rateIndexUB);
333 lenEncodedInBytesTmp[senderIdx] = 0;
334 lenAudioIn10msTmp[senderIdx] = 0;
335 //if(senderIdx == (NUM_CLIENTS - 1))
337 printf(" %0.1f \n", lenAudioIn10ms[senderIdx] * 10. /1000);
340 // After ~20 sec change the bottleneck.
341 // if((numPrint[senderIdx] == 4) && (codingMode == 0))
343 // numPrint[senderIdx] = 0;
344 // if(codingMode == 0)
346 // int newBottleneck = bottleneck[senderIdx] +
347 // (bottleneckChange[senderIdx] * 1000);
349 // if(bottleneckChange[senderIdx] > 0)
351 // if(newBottleneck >maxBn)
353 // bottleneckChange[senderIdx] = -1;
354 // newBottleneck = bottleneck[senderIdx] +
355 // (bottleneckChange[senderIdx] * 1000);
356 // if(newBottleneck > minBn)
358 // bottleneck[senderIdx] = newBottleneck;
363 // bottleneck[senderIdx] = newBottleneck;
368 // if(newBottleneck < minBn)
370 // bottleneckChange[senderIdx] = 1;
371 // newBottleneck = bottleneck[senderIdx] +
372 // (bottleneckChange[senderIdx] * 1000);
373 // if(newBottleneck < maxBn)
375 // bottleneck[senderIdx] = newBottleneck;
380 // bottleneck[senderIdx] = newBottleneck;
387 // model a channel of given bottleneck, to get the receive timestamp
388 get_arrival_time(num10ms[senderIdx] * samplesIn10ms[senderIdx],
389 streamLen, bottleneck[senderIdx], packetData[senderIdx],
390 encoderSampRate[senderIdx]*1000, encoderSampRate[senderIdx]*1000);
392 // Write the arrival time.
395 if (fwrite(&(packetData[senderIdx]->arrival_time),
396 sizeof(unsigned int),
397 1, arrivalTimeFile1) != 1) {
403 if (fwrite(&(packetData[senderIdx]->arrival_time),
404 sizeof(unsigned int),
405 1, arrivalTimeFile2) != 1) {
411 if(WebRtcIsac_UpdateBwEstimate(codecInstance[receiverIdx],
412 bitStream, streamLen, packetData[senderIdx]->rtp_number,
413 packetData[senderIdx]->sample_count,
414 packetData[senderIdx]->arrival_time) < 0)
416 printf(" BWE Error at client %d \n", receiverIdx + 1);
421 lenDecodedAudio = WebRtcIsac_Decode(
422 codecInstance[receiverIdx], bitStream, streamLen,
423 audioBuff60ms, speechType);
424 if(lenDecodedAudio < 0)
426 printf(" Decoder error in client %d \n", receiverIdx + 1);
431 if(encoderSampRate[senderIdx] == 16000)
433 WebRtcSpl_UpsampleBy2(audioBuff60ms, lenDecodedAudio, resampledAudio60ms,
434 resamplerState[receiverIdx]);
435 if (fwrite(resampledAudio60ms, sizeof(short), lenDecodedAudio << 1,
436 outFile[receiverIdx]) !=
437 static_cast<size_t>(lenDecodedAudio << 1)) {
443 if (fwrite(audioBuff60ms, sizeof(short), lenDecodedAudio,
444 outFile[receiverIdx]) !=
445 static_cast<size_t>(lenDecodedAudio)) {
449 num10ms[senderIdx] = 0;