int main(int argc, char *argv[]) { r2_mf_tx_state_t gen; int16_t amp[1000]; int len; SNDFILE *outhandle; int digit; const char *digits = "0123456789BCDEF"; if ((outhandle = sf_open_telephony_write(OUTPUT_FILE_NAME, 1)) == NULL) { fprintf(stderr, " Cannot open audio file '%s'\n", OUTPUT_FILE_NAME); exit(2); } r2_mf_tx_init(&gen, FALSE); for (digit = 0; digits[digit]; digit++) { r2_mf_tx_put(&gen, digits[digit]); len = r2_mf_tx(&gen, amp, 1000); printf("Generated %d samples of %c\n", len, digits[digit]); if (len > 0) sf_writef_short(outhandle, amp, len); r2_mf_tx_put(&gen, 0); len = r2_mf_tx(&gen, amp, 1000); printf("Generated %d samples\n", len); if (len > 0) sf_writef_short(outhandle, amp, len); } r2_mf_tx_init(&gen, TRUE); for (digit = 0; digits[digit]; digit++) { r2_mf_tx_put(&gen, digits[digit]); len = r2_mf_tx(&gen, amp, 1000); printf("Generated %d samples of %c\n", len, digits[digit]); if (len > 0) sf_writef_short(outhandle, amp, len); r2_mf_tx_put(&gen, 0); len = r2_mf_tx(&gen, amp, 1000); printf("Generated %d samples\n", len); if (len > 0) sf_writef_short(outhandle, amp, len); } if (sf_close_telephony(outhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", OUTPUT_FILE_NAME); exit (2); } return 0; }
int sample_save(channel * channel, const char *filename) { #ifdef HAVE_LIBSNDFILE sf_count_t frameCount; SNDFILE *outputFile; SF_INFO sfinfo; float framesize; /* Hardcoded ;) */ sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16; sfinfo.channels = 2; sfinfo.samplerate = 44100; outputFile = sf_open(filename, SFM_WRITE, &sfinfo); if (outputFile == NULL) return 0; framesize = sizeof(short) * sfinfo.channels; frameCount = channel->parms.d.audio_sample_data.s_length * 2 / framesize; sf_writef_short(outputFile, channel->data, frameCount); sf_close(outputFile); return 0; #endif }
void AudioFileSndfile::write(const void* buffer, unsigned frames) { if (!_handle || (_mode != ModeWrite && _mode != ModeReadWrite)) { RUNTIME_ERROR("Attempt to write file not opened for writing."); } flushChunks(); sf_count_t count; switch (_info.sampleType) { case Sound::Type::Float32: case Sound::Type::Int24E: count = sf_writef_float(_handle.get(), static_cast<const Sound::Float32*>(buffer), frames); break; case Sound::Type::Float64: count = sf_writef_double(_handle.get(), static_cast<const Sound::Float64*>(buffer), frames); break; case Sound::Type::Int8: count = sf_write_raw(_handle.get(), buffer, frames * _info.channels); break; case Sound::Type::Int16: count = sf_writef_short(_handle.get(), static_cast<const Sound::Int16*>(buffer), frames); break; case Sound::Type::Int32: count = sf_writef_int(_handle.get(), static_cast<const Sound::Int32*>(buffer), frames); break; case Sound::Type::Int64: case Sound::Type::Precise: case Sound::Type::Count: RUNTIME_ERROR("Writing for this sample format is not supported"); } }
void main_loop_reverse(void) { LTCFrame f; const long long int end = ceil(duration * samplerate / 1000.0); long long int written = 0; active=1; short *snd = NULL; const short smult = rint(pow(10, volume_dbfs/20.0) * 32767.0); while(active==1 && (duration <= 0 || end >= written)) { int byteCnt; for (byteCnt = 9; byteCnt >= 0; byteCnt--) { int i; ltc_encoder_encode_byte(encoder, byteCnt, -1.0); const int len = ltc_encoder_get_buffer(encoder, enc_buf); if (!snd) snd = malloc(len * sizeof(short)); for (i=0;i<len;i++) { const short val = ( (int)(enc_buf[i] - 128) * smult / 90 ); snd[i] = val; } sf_writef_short(sf, snd, len); written += len; if (end < written) break; } /* end byteCnt - one video frames's worth of LTC */ ltc_encoder_get_frame(encoder, &f); ltc_frame_decrement(&f, ceil(fps_num/fps_den), fps_num/(double)fps_den == 25.0? LTC_TV_625_50 : LTC_TV_525_60, LTC_USE_DATE); ltc_encoder_set_frame(encoder, &f); } free(snd); printf("wrote %lld audio-samples\n", written); }
void main_loop(void) { const long long int end = ceil(duration * samplerate / 1000.0); long long int written = 0; active=1; short *snd = NULL; const short smult = rint(pow(10, volume_dbfs/20.0) * 32767.0); while(active==1 && (duration <= 0 || end >= written)) { int byteCnt; for (byteCnt = 0; byteCnt < 10; byteCnt++) { int i; ltc_encoder_encode_byte(encoder, byteCnt, 1.0); const int len = ltc_encoder_get_buffer(encoder, enc_buf); if (!snd) snd = malloc(len * sizeof(short)); for (i=0;i<len;i++) { const short val = ( (int)(enc_buf[i] - 128) * smult / 90 ); snd[i] = val; } sf_writef_short(sf, snd, len); written += len; if (end < written) break; } /* end byteCnt - one video frames's worth of LTC */ ltc_encoder_inc_timecode(encoder); } free(snd); printf("wrote %lld audio-samples\n", written); }
static void write_file_at_end (int fd, int filetype, int channels, int file_num) { SNDFILE *sndfile ; SF_INFO sfinfo ; int frames, k ; lseek (fd, 0, SEEK_END) ; for (k = 0 ; k < DATA_LENGTH ; k++) data [k] = k ; frames = DATA_LENGTH / channels ; sfinfo.format = filetype ; sfinfo.channels = channels ; sfinfo.samplerate = 44100 ; if ((sndfile = sf_open_fd (fd, SFM_WRITE, &sfinfo, SF_FALSE)) == NULL) { printf ("\n\nLine %d: sf_open_fd failed\n", __LINE__) ; printf ("Embedded file number : %d\n", file_num) ; puts (sf_strerror (sndfile)) ; dump_log_buffer (sndfile) ; exit (1) ; } ; if (sf_writef_short (sndfile, data, frames) != frames) { printf ("\n\nLine %d: short write\n", __LINE__) ; printf ("Embedded file number : %d\n", file_num) ; exit (1) ; } ; sf_close (sndfile) ; } /* write_file_at_end */
int main() { SF_INFO wav_info; SNDFILE *wav; ayemu_player_t player; ayemu_player_sndfmt_t format; short buf[BUFFER_SIZE * 2]; size_t filled_size; format.freq = wav_info.samplerate = AUDIO_FREQ; format.channels = wav_info.channels = AUDIO_CHANS; format.bpc = AUDIO_BITS; wav_info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16; ayemu_player_open_psg( "dancing_queen.psg", &format, &player ); wav = sf_open( "test.wav", SFM_WRITE, &wav_info ); while ( (filled_size = ayemu_player_fill_buffer( &player, buf, BUFFER_SIZE ) ) > 0) { sf_writef_short( wav, buf, filled_size ); } sf_close( wav ); ayemu_player_close( &player ); return 0; }
void DWavVault::AddLogValue(const double value){ //wavfile int16_t valueI = (int16_t)value; sf_writef_short( wav, (const short *)&valueI, 1); m_nrOfRows++; }
static int power_surge_detector_file_test(const char *file) { SNDFILE *inhandle; SNDFILE *outhandle; int inframes; power_surge_detector_state_t *sig; int i; int16_t amp[8000]; int16_t amp_out[2*8000]; int sample; float signal_power; int32_t signal_level; int signal_present; int prev_signal_present; if ((inhandle = sf_open_telephony_read(file, 1)) == NULL) { printf(" Cannot open speech file '%s'\n", file); exit(2); } if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL) { fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME); exit(2); } sig = power_surge_detector_init(NULL, -50.0f, 6.0f); prev_signal_present = FALSE; sample = 0; while ((inframes = sf_readf_short(inhandle, amp, 8000))) { for (i = 0; i < inframes; i++) { signal_level = power_surge_detector(sig, amp[i]); signal_present = (signal_level != 0); if (prev_signal_present != signal_present) { signal_power = power_surge_detector_current_dbm0(sig); if (signal_present) printf("On at %f (%fdBm0)\n", (sample + i)/8000.0, signal_power); else printf("Off at %f (%fdBm0)\n", (sample + i)/8000.0, signal_power); prev_signal_present = signal_present; } amp_out[2*i] = amp[i]; amp_out[2*i + 1] = signal_present*5000; } sf_writef_short(outhandle, amp_out, inframes); sample += inframes; } sf_close(inhandle); sf_close(outhandle); return 0; }
ssize_t LibsndfileSink::write(const void *buffer, size_t count) { if (!mNegotiated) { return (ssize_t) NEGOTIATE; } if (mSndfile == NULL) { return (ssize_t) NO_INIT; } sf_count_t actual = sf_writef_short(mSndfile, (short *) buffer, (sf_count_t) count); mFramesWritten += actual; return actual; }
int main(void){ Lowpass filters[2]; int i; SNDFILE *infile = NULL; SNDFILE *outfile = NULL; SF_INFO sfinfo; sf_count_t readcount; int filetype = SF_FORMAT_WAV | SF_FORMAT_PCM_16; short buffer[BLOCK_SIZE * 2]; // Open a wav file to filter if((infile = sf_open ("music.wav", SFM_READ, &sfinfo)) == NULL) { printf ("Not able to open input file\n"); puts (sf_strerror (NULL)) ; return 1; } // Say info about the input file printf("Frames: %d\n", (int)sfinfo.frames); printf("Samplerate: %d\n", sfinfo.samplerate); printf("Channels: %d\n", sfinfo.channels); sfinfo.format = filetype; // Open output file if((outfile = sf_open ("output.wav", SFM_WRITE, &sfinfo)) == NULL) { printf ("Not able to open input file\n"); puts (sf_strerror (NULL)) ; return 1; } // Read and filter the file while((readcount = sf_readf_short(infile, buffer, BLOCK_SIZE)) > 0){ for(i = 0; i < readcount * 2; i += 2){ buffer[i] = filters[0].value(buffer[i]); buffer[i+1] = filters[1].value(buffer[i+1]); //printf("%d ", buffer[i]); //printf("%d ", buffer[i+1]); } sf_writef_short(outfile, buffer, BLOCK_SIZE); } // Close the files sf_close(infile); sf_close(outfile); return 0; }
void test_writef_short_or_die (SNDFILE *file, int pass, short *test, sf_count_t frames, int line_num) { sf_count_t count ; if ((count = sf_writef_short (file, test, frames)) != frames) { printf ("\n\nLine %d", line_num) ; if (pass > 0) printf (" (pass %d)", pass) ; printf (" : sf_writef_short failed with short writef (%ld => %ld).\n", SF_COUNT_TO_LONG (frames), SF_COUNT_TO_LONG (count)) ; fflush (stdout) ; puts (sf_strerror (file)) ; exit (1) ; } ; return ; } /* test_writef_short */
static inline void put_residue(int16_t tx, int16_t residue) { int outframes; residue_sound[residue_cur++] = tx; residue_sound[residue_cur++] = residue; if (residue_cur >= 8000) { residue_cur >>= 1; outframes = sf_writef_short(resulthandle, residue_sound, residue_cur); if (outframes != residue_cur) { fprintf(stderr, " Error writing residue sound\n"); exit(2); } residue_cur = 0; }
static void sndfile_poll_ready_playing (gpointer data, gint source, GdkInputCondition condition) { sndfile_driver * const d = data; if(!d->firstpoll) { sf_writef_short (d->outfile, d->sndbuf, d->sndbuf_size / 4); d->playtime += (double)(d->sndbuf_size) / 4 / d->p_mixfreq; } d->firstpoll = FALSE; #ifdef WORDS_BIGENDIAN audio_mix(d->sndbuf, d->sndbuf_size / 4, d->p_mixfreq, ST_MIXER_FORMAT_S16_BE | ST_MIXER_FORMAT_STEREO); #else audio_mix(d->sndbuf, d->sndbuf_size / 4, d->p_mixfreq, ST_MIXER_FORMAT_S16_LE | ST_MIXER_FORMAT_STEREO); #endif }
static void complexify_tests(void) { complexify_state_t *s; complexf_t cc; int16_t amp; int i; SNDFILE *outhandle; int outframes; int16_t out[40000]; awgn_state_t noise1; if ((outhandle = sf_open_telephony_write(OUT_FILE_COMPLEXIFY, 2)) == NULL) { fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_COMPLEXIFY); exit(2); } awgn_init_dbm0(&noise1, 1234567, -10.0f); s = complexify_init(); for (i = 0; i < 20000; i++) { amp = awgn(&noise1); cc = complexify(s, amp); out[2*i] = cc.re; out[2*i + 1] = cc.im; } awgn_release(&noise1); complexify_free(s); outframes = sf_writef_short(outhandle, out, 20000); if (outframes != 20000) { fprintf(stderr, " Error writing audio file\n"); exit(2); } if (sf_close_telephony(outhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_COMPLEXIFY); exit(2); } }
void Server::mix(short* buffer, int length) { std::lock_guard<std::mutex> guard(m_mutex); for (int i = 0; i < length; i += 2) { if (m_frame == 0) tick(); if (++m_frame >= m_tune->frames_per_tick) { m_frame = 0; if (m_playing) { if (++m_tick >= m_ticks_per_row.val()) { m_tick = 0; if (++m_row >= get_max_rows(*m_tune, m_line)) { m_row = 0; if (!m_lineloop && (++m_line >= (int) m_tune->table.size() || m_tune->table[m_line] == TableLine())) { if (m_tune->table.size() < 2) m_line = 0; else while (--m_line > 0) { // find empty table line if (m_tune->table[m_line - 1] == TableLine()) break; } } } } } } float frame[2] = { 0, 0 }; for (int i = 0; i < CHANNEL_COUNT; i++) { m_channels[i].add_mix(frame, m_channels[(i + CHANNEL_COUNT - 1) % CHANNEL_COUNT], m_fx); } m_fx.add_mix(frame); buffer[i + 0] = clamp((int) (frame[0] * 8000), -32768, 32767); buffer[i + 1] = clamp((int) (frame[1] * 8000), -32768, 32767); // buffer[i + 0] = map_amp(frame[0]); // buffer[i + 1] = map_amp(frame[1]); } sf_writef_short(m_log, buffer, length / 2); }
static switch_status_t sndfile_file_write(switch_file_handle_t *handle, void *data, size_t *len) { size_t inlen = *len; sndfile_context *context = handle->private_info; if (switch_test_flag(handle, SWITCH_FILE_DATA_RAW)) { *len = (size_t) sf_write_raw(context->handle, data, inlen); } else if (switch_test_flag(handle, SWITCH_FILE_DATA_INT)) { *len = (size_t) sf_writef_int(context->handle, (int *) data, inlen); } else if (switch_test_flag(handle, SWITCH_FILE_DATA_SHORT)) { *len = (size_t) sf_writef_short(context->handle, (short *) data, inlen); } else if (switch_test_flag(handle, SWITCH_FILE_DATA_FLOAT)) { *len = (size_t) sf_writef_float(context->handle, (float *) data, inlen); } else if (switch_test_flag(handle, SWITCH_FILE_DATA_DOUBLE)) { *len = (size_t) sf_writef_double(context->handle, (double *) data, inlen); } else { *len = (size_t) sf_writef_int(context->handle, (int *) data, inlen); } handle->sample_count += *len; return sf_error(context->handle) == SF_ERR_NO_ERROR ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE; }
/* * call-seq: * snd.write(buf) => integer * * Writes the entire contents of the given buffer to the sound and returns the * number of frames written. */ static VALUE ra_sound_write(VALUE self, VALUE buf) { RA_SOUND *snd; Data_Get_Struct(self, RA_SOUND, snd); if(snd->closed) rb_raise(eRubyAudioError, "closed sound"); // Get buffer struct RA_BUFFER *b; Data_Get_Struct(buf, RA_BUFFER, b); // Get info struct SF_INFO *info; Data_Get_Struct(snd->info, SF_INFO, info); // Check buffer channels matches actual channels if(b->channels != info->channels) { rb_raise(eRubyAudioError, "channel count mismatch: %d vs %d", b->channels, info->channels); } // Write data sf_count_t written; switch(b->type) { case RA_BUFFER_TYPE_SHORT: written = sf_writef_short(snd->snd, b->data, b->real_size); break; case RA_BUFFER_TYPE_INT: written = sf_writef_int(snd->snd, b->data, b->real_size); break; case RA_BUFFER_TYPE_FLOAT: written = sf_writef_float(snd->snd, b->data, b->real_size); break; case RA_BUFFER_TYPE_DOUBLE: written = sf_writef_double(snd->snd, b->data, b->real_size); break; } return OFFT2NUM(written); }
static void play_tones(super_tone_tx_state_t *tone, int max_samples) { int16_t amp[8000]; int len; int outframes; int i; int total_length; i = 500; total_length = 0; do { len = super_tone_tx(tone, amp, 160); outframes = sf_writef_short(outhandle, amp, len); if (outframes != len) { fprintf(stderr, " Error writing audio file\n"); exit(2); } total_length += len; } while (len > 0 && --i > 0); printf("Tone length = %d samples (%dms)\n", total_length, total_length/8); }
static ssize_t sa_sndfile_write( simpleaudio *sa, void *buf, size_t nframes ) { // fprintf(stderr, "sf_write: nframes=%ld\n", nframes); SNDFILE *s = (SNDFILE *)sa->backend_handle; int n; switch ( sa->format ) { case SA_SAMPLE_FORMAT_FLOAT: n = sf_writef_float(s, buf, nframes); break; case SA_SAMPLE_FORMAT_S16: n = sf_writef_short(s, buf, nframes); break; default: assert(0); break; } if ( n < 0 ) { fprintf(stderr, "sf_write: "); sf_perror(s); return -1; } return n; }
int main(int argc, char *argv[]) { SNDFILE *inhandle; SNDFILE *refhandle; SNDFILE *outhandle; int frames; double pre_energy; double post_energy; double ref_energy; double diff_energy; int16_t pre_amp[BLOCKS_PER_READ*BLOCK_LEN]; int16_t post_amp[BLOCKS_PER_READ*BLOCK_LEN]; int16_t ref_amp[BLOCKS_PER_READ*BLOCK_LEN]; int16_t log_amp[BLOCKS_PER_READ*BLOCK_LEN*3]; uint8_t lpc10_data[BLOCKS_PER_READ*7]; double xx; lpc10_encode_state_t *lpc10_enc_state; lpc10_decode_state_t *lpc10_dec_state; int i; int block_no; int log_error; int compress; int decompress; const char *in_file_name; int compress_file; int decompress_file; int len; int opt; int enc_len; int dec_len; compress = FALSE; decompress = FALSE; log_error = TRUE; in_file_name = IN_FILE_NAME; while ((opt = getopt(argc, argv, "cdi:l")) != -1) { switch (opt) { case 'c': compress = TRUE; break; case 'd': decompress = TRUE; break; case 'i': in_file_name = optarg; break; case 'l': log_error = FALSE; break; default: //usage(); exit(2); } } compress_file = -1; decompress_file = -1; inhandle = NULL; refhandle = NULL; outhandle = NULL; if (!decompress) { if ((inhandle = sf_open_telephony_read(in_file_name, 1)) == NULL) { fprintf(stderr, " Cannot open audio file '%s'\n", in_file_name); exit(2); } if ((refhandle = sf_open_telephony_read(REF_FILE_NAME, 1)) == NULL) { fprintf(stderr, " Cannot open audio file '%s'\n", REF_FILE_NAME); exit(2); } } else { if ((decompress_file = open(DECOMPRESS_FILE_NAME, O_RDONLY)) < 0) { fprintf(stderr, " Cannot open decompressed data file '%s'\n", DECOMPRESS_FILE_NAME); exit(2); } } if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL) { fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME); exit(2); } if ((lpc10_enc_state = lpc10_encode_init(NULL, TRUE)) == NULL) { fprintf(stderr, " Cannot create encoder\n"); exit(2); } if ((lpc10_dec_state = lpc10_decode_init(NULL, TRUE)) == NULL) { fprintf(stderr, " Cannot create decoder\n"); exit(2); } if (compress) { if ((compress_file = open(COMPRESS_FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { fprintf(stderr, " Cannot create compressed data file '%s'\n", COMPRESS_FILE_NAME); exit(2); } } pre_energy = 0.0; post_energy = 0.0; ref_energy = 0.0; diff_energy = 0.0; if (decompress) { while ((len = read(decompress_file, lpc10_data, BLOCKS_PER_READ*7)) > 0) { lpc10_decode(lpc10_dec_state, post_amp, lpc10_data, len/7); sf_writef_short(outhandle, post_amp, BLOCK_LEN*len/7); } } else { block_no = 0; while ((frames = sf_readf_short(inhandle, pre_amp, BLOCKS_PER_READ*BLOCK_LEN)) == BLOCKS_PER_READ*BLOCK_LEN && (frames = sf_readf_short(refhandle, ref_amp, BLOCKS_PER_READ*BLOCK_LEN)) == BLOCKS_PER_READ*BLOCK_LEN) { enc_len = lpc10_encode(lpc10_enc_state, lpc10_data, pre_amp, BLOCKS_PER_READ*BLOCK_LEN); if (compress) write(compress_file, lpc10_data, enc_len); dec_len = lpc10_decode(lpc10_dec_state, post_amp, lpc10_data, enc_len); for (i = 0; i < dec_len; i++) { pre_energy += (double) pre_amp[i]*(double) pre_amp[i]; post_energy += (double) post_amp[i]*(double) post_amp[i]; ref_energy += (double) ref_amp[i]*(double) ref_amp[i]; /* The reference file has some odd clipping, so eliminate this from the energy measurement. */ if (ref_amp[i] == 32767 || ref_amp[i] == -32768) xx = 0.0; else xx = post_amp[i] - ref_amp[i]; diff_energy += (double) xx*(double) xx; log_amp[i] = xx; } block_no++; if (log_error) sf_writef_short(outhandle, log_amp, dec_len); else sf_writef_short(outhandle, post_amp, dec_len); } if (sf_close_telephony(inhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", in_file_name); exit(2); } if (sf_close_telephony(refhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", REF_FILE_NAME); exit(2); } } if (sf_close_telephony(outhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME); exit(2); } if (compress) close(compress_file); if (decompress) close(decompress_file); lpc10_encode_release(lpc10_enc_state); lpc10_decode_release(lpc10_dec_state); if (!decompress) { printf("Output energy is %f%% of input energy.\n", 100.0*post_energy/pre_energy); printf("Difference energy is %f%% of the total.\n", 100.0*diff_energy/ref_energy); if (fabs(1.0 - post_energy/pre_energy) > 0.05 || fabs(diff_energy/post_energy) > 0.03) { printf("Tests failed.\n"); exit(2); } printf("Tests passed.\n"); } return 0; }
int main(int argc, char *argv[]) { if(argc != 3) { fprintf(stderr, "Usage %s <input file> <output file>\n", argv[0]); return 1; } // Open the input file FILE* fpInput = fopen(argv[1], "rb"); if (!fpInput) { fprintf(stderr, "Could not open %s\n", argv[1]); return 1; } // Validate the input AMR file char header[kFileHeaderSize]; int bytesRead = fread(header, 1, kFileHeaderSize, fpInput); if (bytesRead != kFileHeaderSize || memcmp(header, "#!AMR\n", kFileHeaderSize)) { fprintf(stderr, "Invalid AMR-NB file\n"); return 1; } // Open the output file SF_INFO sfInfo; memset(&sfInfo, 0, sizeof(SF_INFO)); sfInfo.channels = kChannels; sfInfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16; sfInfo.samplerate = kSampleRate; SNDFILE *handle = sf_open(argv[2], SFM_WRITE, &sfInfo); if(!handle){ fprintf(stderr, "Could not create %s\n", argv[2]); return 1; } // Create AMR-NB decoder instance void* amrHandle; int err = GSMInitDecode(&amrHandle, (Word8*)"AMRNBDecoder"); if(err != 0){ fprintf(stderr, "Error creating AMR-NB decoder instance\n"); return 1; } //Allocate input buffer void *inputBuf = malloc(kInputBufferSize); assert(inputBuf != NULL); //Allocate output buffer void *outputBuf = malloc(kOutputBufferSize); assert(outputBuf != NULL); // Decode loop uint32_t retVal = 0; while (1) { // Read mode uint8_t mode; bytesRead = fread(&mode, 1, 1, fpInput); if (bytesRead != 1) break; // Find frame type Frame_Type_3GPP frameType = (Frame_Type_3GPP)((mode >> 3) & 0x0f); if (frameType >= AMR_SID){ fprintf(stderr, "Frame type %d not supported\n",frameType); retVal = 1; break; } // Find frame type int32_t frameSize = kFrameSizes[frameType]; bytesRead = fread(inputBuf, 1, frameSize, fpInput); if (bytesRead != frameSize) break; //Decode frame int32_t decodeStatus; decodeStatus = AMRDecode(amrHandle, frameType, (uint8_t*)inputBuf, (int16_t*)outputBuf, MIME_IETF); if(decodeStatus == -1) { fprintf(stderr, "Decoder encountered error\n"); retVal = 1; break; } //Write output to wav sf_writef_short(handle, (int16_t*)outputBuf, kSamplesPerFrame); } // Close input and output file fclose(fpInput); sf_close(handle); //Free allocated memory free(inputBuf); free(outputBuf); // Close decoder instance GSMDecodeFrameExit(&amrHandle); return retVal; }
int main(int argc, char *argv[]) { SNDFILE *inhandle; SNDFILE *outhandle; int outframes; int opt; int samples; int len2; int len3; int basic_tests; int law; int encode; int decode; int file; const char *in_file; const char *out_file; g711_state_t *enc_state; g711_state_t *dec_state; int16_t indata[BLOCK_LEN]; int16_t outdata[BLOCK_LEN]; uint8_t g711data[BLOCK_LEN]; basic_tests = TRUE; law = G711_ALAW; encode = FALSE; decode = FALSE; in_file = NULL; out_file = NULL; while ((opt = getopt(argc, argv, "ad:e:l:u")) != -1) { switch (opt) { case 'a': law = G711_ALAW; basic_tests = FALSE; break; case 'd': in_file = optarg; basic_tests = FALSE; decode = TRUE; break; case 'e': in_file = optarg; basic_tests = FALSE; encode = TRUE; break; case 'l': out_file = optarg; break; case 'u': law = G711_ULAW; basic_tests = FALSE; break; default: //usage(); exit(2); } } if (basic_tests) { compliance_tests(TRUE); } else { if (!decode && !encode) { decode = encode = TRUE; } if (in_file == NULL) { in_file = (encode) ? IN_FILE_NAME : ENCODED_FILE_NAME; } if (out_file == NULL) { out_file = (decode) ? OUT_FILE_NAME : ENCODED_FILE_NAME; } inhandle = NULL; outhandle = NULL; file = -1; enc_state = NULL; dec_state = NULL; if (encode) { if ((inhandle = sf_open_telephony_read(in_file, 1)) == NULL) { fprintf(stderr, " Cannot open audio file '%s'\n", in_file); exit(2); } enc_state = g711_init(NULL, law); } else { if ((file = open(in_file, O_RDONLY)) < 0) { fprintf(stderr, " Failed to open '%s'\n", in_file); exit(2); } } if (decode) { if ((outhandle = sf_open_telephony_write(out_file, 1)) == NULL) { fprintf(stderr, " Cannot create audio file '%s'\n", out_file); exit(2); } dec_state = g711_init(NULL, law); } else { if ((file = open(out_file, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { fprintf(stderr, " Failed to open '%s'\n", out_file); exit(2); } } for (;;) { if (encode) { samples = sf_readf_short(inhandle, indata, BLOCK_LEN); if (samples <= 0) break; len2 = g711_encode(enc_state, g711data, indata, samples); } else { len2 = read(file, g711data, BLOCK_LEN); if (len2 <= 0) break; } if (decode) { len3 = g711_decode(dec_state, outdata, g711data, len2); outframes = sf_writef_short(outhandle, outdata, len3); if (outframes != len3) { fprintf(stderr, " Error writing audio file\n"); exit(2); } } else { len3 = write(file, g711data, len2); if (len3 <= 0) break; } } if (encode) { if (sf_close_telephony(inhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", IN_FILE_NAME); exit(2); } } else { close(file); } if (decode) { if (sf_close_telephony(outhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME); exit(2); } } else { close(file); } printf("'%s' translated to '%s' using %s.\n", in_file, out_file, (law == G711_ALAW) ? "A-law" : "u-law"); } return 0; }
static void compliance_tests(int log_audio) { SNDFILE *outhandle; power_meter_t power_meter; int outframes; int i; int block; int pre; int post; int post_post; int alaw_failures; int ulaw_failures; float worst_alaw; float worst_ulaw; float tmp; int len; g711_state_t *enc_state; g711_state_t *transcode; g711_state_t *dec_state; outhandle = NULL; if (log_audio) { if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL) { fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME); exit(2); } } printf("Conversion accuracy tests.\n"); alaw_failures = 0; ulaw_failures = 0; worst_alaw = 0.0; worst_ulaw = 0.0; for (block = 0; block < 1; block++) { for (i = 0; i < 65536; i++) { pre = i - 32768; post = alaw_to_linear(linear_to_alaw(pre)); if (abs(pre) > 140) { tmp = (float) abs(post - pre)/(float) abs(pre); if (tmp > 0.10) { printf("A-law: Excessive error at %d (%d)\n", pre, post); alaw_failures++; } if (tmp > worst_alaw) worst_alaw = tmp; } else { /* Small values need different handling for sensible measurement */ if (abs(post - pre) > 15) { printf("A-law: Excessive error at %d (%d)\n", pre, post); alaw_failures++; } } amp[i] = post; } if (log_audio) { outframes = sf_writef_short(outhandle, amp, 65536); if (outframes != 65536) { fprintf(stderr, " Error writing audio file\n"); exit(2); } } for (i = 0; i < 65536; i++) { pre = i - 32768; post = ulaw_to_linear(linear_to_ulaw(pre)); if (abs(pre) > 40) { tmp = (float) abs(post - pre)/(float) abs(pre); if (tmp > 0.10) { printf("u-law: Excessive error at %d (%d)\n", pre, post); ulaw_failures++; } if (tmp > worst_ulaw) worst_ulaw = tmp; } else { /* Small values need different handling for sensible measurement */ if (abs(post - pre) > 4) { printf("u-law: Excessive error at %d (%d)\n", pre, post); ulaw_failures++; } } amp[i] = post; } if (log_audio) { outframes = sf_writef_short(outhandle, amp, 65536); if (outframes != 65536) { fprintf(stderr, " Error writing audio file\n"); exit(2); } } } printf("Worst A-law error (ignoring small values) %f%%\n", worst_alaw*100.0); printf("Worst u-law error (ignoring small values) %f%%\n", worst_ulaw*100.0); if (alaw_failures || ulaw_failures) { printf("%d A-law values with excessive error\n", alaw_failures); printf("%d u-law values with excessive error\n", ulaw_failures); printf("Tests failed\n"); exit(2); } printf("Cyclic conversion repeatability tests.\n"); /* Find what happens to every possible linear value after a round trip. */ for (i = 0; i < 65536; i++) { pre = i - 32768; /* Make a round trip */ post = alaw_to_linear(linear_to_alaw(pre)); /* A second round trip should cause no further change */ post_post = alaw_to_linear(linear_to_alaw(post)); if (post_post != post) { printf("A-law second round trip mismatch - at %d, %d != %d\n", pre, post, post_post); printf("Tests failed\n"); exit(2); } /* Make a round trip */ post = ulaw_to_linear(linear_to_ulaw(pre)); /* A second round trip should cause no further change */ post_post = ulaw_to_linear(linear_to_ulaw(post)); if (post_post != post) { printf("u-law round trip mismatch - at %d, %d != %d\n", pre, post, post_post); printf("Tests failed\n"); exit(2); } } printf("Reference power level tests.\n"); power_meter_init(&power_meter, 7); for (i = 0; i < 8000; i++) { amp[i] = ulaw_to_linear(ulaw_1khz_sine[i & 7]); power_meter_update(&power_meter, amp[i]); } printf("Reference u-law 1kHz tone is %fdBm0\n", power_meter_current_dbm0(&power_meter)); if (log_audio) { outframes = sf_writef_short(outhandle, amp, 8000); if (outframes != 8000) { fprintf(stderr, " Error writing audio file\n"); exit(2); } } if (0.1f < fabs(power_meter_current_dbm0(&power_meter))) { printf("Test failed.\n"); exit(2); } for (i = 0; i < 8000; i++) { amp[i] = alaw_to_linear(alaw_1khz_sine[i & 7]); power_meter_update(&power_meter, amp[i]); } printf("Reference A-law 1kHz tone is %fdBm0\n", power_meter_current_dbm0(&power_meter)); if (log_audio) { outframes = sf_writef_short(outhandle, amp, 8000); if (outframes != 8000) { fprintf(stderr, " Error writing audio file\n"); exit(2); } } if (0.1f < fabs(power_meter_current_dbm0(&power_meter))) { printf("Test failed.\n"); exit(2); } /* Check the transcoding functions. */ printf("Testing transcoding A-law -> u-law -> A-law\n"); for (i = 0; i < 256; i++) { if (alaw_to_ulaw(ulaw_to_alaw(i)) != i) { if (abs(alaw_to_ulaw(ulaw_to_alaw(i)) - i) > 1) { printf("u-law -> A-law -> u-law gave %d -> %d\n", i, alaw_to_ulaw(ulaw_to_alaw(i))); printf("Test failed\n"); exit(2); } } } printf("Testing transcoding u-law -> A-law -> u-law\n"); for (i = 0; i < 256; i++) { if (ulaw_to_alaw(alaw_to_ulaw(i)) != i) { if (abs(alaw_to_ulaw(ulaw_to_alaw(i)) - i) > 1) { printf("A-law -> u-law -> A-law gave %d -> %d\n", i, ulaw_to_alaw(alaw_to_ulaw(i))); printf("Test failed\n"); exit(2); } } } enc_state = g711_init(NULL, G711_ALAW); transcode = g711_init(NULL, G711_ALAW); dec_state = g711_init(NULL, G711_ULAW); len = 65536; for (i = 0; i < len; i++) amp[i] = i - 32768; len = g711_encode(enc_state, alaw_data, amp, len); len = g711_transcode(transcode, ulaw_data, alaw_data, len); len = g711_decode(dec_state, amp, ulaw_data, len); if (len != 65536) { printf("Block coding gave the wrong length - %d instead of %d\n", len, 65536); printf("Test failed\n"); exit(2); } for (i = 0; i < len; i++) { pre = i - 32768; post = amp[i]; if (abs(pre) > 140) { tmp = (float) abs(post - pre)/(float) abs(pre); if (tmp > 0.10) { printf("Block: Excessive error at %d (%d)\n", pre, post); exit(2); } } else { /* Small values need different handling for sensible measurement */ if (abs(post - pre) > 15) { printf("Block: Excessive error at %d (%d)\n", pre, post); exit(2); } } } g711_release(enc_state); g711_release(transcode); g711_release(dec_state); if (log_audio) { if (sf_close_telephony(outhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME); exit(2); } } printf("Tests passed.\n"); }
int main(int argc, char *argv[]) { v17_rx_state_t *rx; v17_tx_state_t *tx; bert_results_t bert_results; int16_t gen_amp[BLOCK_LEN]; int16_t amp[BLOCK_LEN]; SNDFILE *inhandle; SNDFILE *outhandle; int outframes; int samples; int tep; int block_no; int noise_level; int signal_level; int bits_per_test; int line_model_no; int log_audio; int channel_codec; int rbs_pattern; int opt; logging_state_t *logging; channel_codec = MUNGE_CODEC_NONE; rbs_pattern = 0; test_bps = 14400; tep = FALSE; line_model_no = 0; decode_test_file = NULL; use_gui = FALSE; noise_level = -70; signal_level = -13; bits_per_test = 50000; log_audio = FALSE; while ((opt = getopt(argc, argv, "b:B:c:d:glm:n:r:s:t")) != -1) { switch (opt) { case 'b': test_bps = atoi(optarg); if (test_bps != 14400 && test_bps != 12000 && test_bps != 9600 && test_bps != 7200 && test_bps != 4800) { /* 4800 is an extension of V.17, to provide full coverage of the V.32bis modes */ fprintf(stderr, "Invalid bit rate specified\n"); exit(2); } break; case 'B': bits_per_test = atoi(optarg); break; case 'c': channel_codec = atoi(optarg); break; case 'd': decode_test_file = optarg; break; case 'g': #if defined(ENABLE_GUI) use_gui = TRUE; #else fprintf(stderr, "Graphical monitoring not available\n"); exit(2); #endif break; case 'l': log_audio = TRUE; break; case 'm': line_model_no = atoi(optarg); break; case 'n': noise_level = atoi(optarg); break; case 'r': rbs_pattern = atoi(optarg); break; case 's': signal_level = atoi(optarg); break; case 't': tep = TRUE; break; default: //usage(); exit(2); break; } } inhandle = NULL; outhandle = NULL; #if defined(HAVE_FENV_H) fpe_trap_setup(); #endif if (log_audio) { if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL) { fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME); exit(2); } } if (decode_test_file) { /* We will decode the audio from a file. */ tx = NULL; if ((inhandle = sf_open_telephony_read(decode_test_file, 1)) == NULL) { fprintf(stderr, " Cannot open audio file '%s'\n", decode_test_file); exit(2); } } else { /* We will generate V.17 audio, and add some noise to it. */ tx = v17_tx_init(NULL, test_bps, tep, v17getbit, NULL); logging = v17_tx_get_logging_state(tx); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW); span_log_set_tag(logging, "V.17-tx"); v17_tx_power(tx, signal_level); v17_tx_set_modem_status_handler(tx, v17_tx_status, (void *) tx); #if defined(WITH_SPANDSP_INTERNALS) /* Move the carrier off a bit */ tx->carrier_phase_rate = dds_phase_ratef(1792.0f); tx->carrier_phase = 0x40000000; #endif bert_init(&bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20); bert_set_report(&bert, 10000, reporter, NULL); if ((line_model = one_way_line_model_init(line_model_no, (float) noise_level, channel_codec, rbs_pattern)) == NULL) { fprintf(stderr, " Failed to create line model\n"); exit(2); } one_way_line_model_set_dc(line_model, 0.0f); #if defined(ADD_MAINS_INTERFERENCE) one_way_line_model_set_mains_pickup(line_model, 50, -40.0f); #endif } rx = v17_rx_init(NULL, test_bps, v17putbit, NULL); logging = v17_rx_get_logging_state(rx); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW); span_log_set_tag(logging, "V.17-rx"); v17_rx_set_modem_status_handler(rx, v17_rx_status, (void *) rx); v17_rx_set_qam_report_handler(rx, qam_report, (void *) rx); #if defined(ENABLE_GUI) if (use_gui) { qam_monitor = qam_monitor_init(10.0f, NULL); if (!decode_test_file) { start_line_model_monitor(129); line_model_monitor_line_model_update(line_model->near_filter, line_model->near_filter_len); } } #endif memset(&latest_results, 0, sizeof(latest_results)); for (block_no = 0; block_no < 100000000; block_no++) { if (decode_test_file) { samples = sf_readf_short(inhandle, amp, BLOCK_LEN); #if defined(ENABLE_GUI) if (use_gui) qam_monitor_update_audio_level(qam_monitor, amp, samples); #endif if (samples == 0) break; } else { samples = v17_tx(tx, gen_amp, BLOCK_LEN); #if defined(ENABLE_GUI) if (use_gui) qam_monitor_update_audio_level(qam_monitor, gen_amp, samples); #endif if (samples == 0) { printf("Restarting on zero output\n"); /* Push a little silence through, to ensure all the data bits get out of the buffers */ vec_zeroi16(amp, BLOCK_LEN); v17_rx(rx, amp, BLOCK_LEN); /* Note that we might get a few bad bits as the carrier shuts down. */ bert_result(&bert, &bert_results); fprintf(stderr, "Final result %ddBm0/%ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, noise_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs); fprintf(stderr, "Last report %ddBm0/%ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, noise_level, latest_results.total_bits, latest_results.bad_bits, latest_results.resyncs); /* See if bit errors are appearing yet. Also check we are getting enough bits out of the receiver. The last regular report should be error free, though the final report will generally contain bits errors as the carrier was dying. The total number of bits out of the receiver should be at least the number we sent. Also, since BERT sync should have occurred rapidly at the start of transmission, the last report should have occurred at not much less than the total number of bits we sent. */ if (bert_results.total_bits < bits_per_test || latest_results.total_bits < bits_per_test - 100 || latest_results.bad_bits != 0) { break; } memset(&latest_results, 0, sizeof(latest_results)); #if defined(WITH_SPANDSP_INTERNALS) signal_level--; /* Bump the receiver AGC gain by 1dB, to compensate for the above */ rx->agc_scaling_save *= 1.122f; #endif v17_tx_restart(tx, test_bps, tep, TRUE); v17_tx_power(tx, signal_level); v17_rx_restart(rx, test_bps, TRUE); //rx.eq_put_step = rand()%(192*10/3); bert_init(&bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20); bert_set_report(&bert, 10000, reporter, NULL); one_way_line_model_release(line_model); if ((line_model = one_way_line_model_init(line_model_no, (float) noise_level, channel_codec, 0)) == NULL) { fprintf(stderr, " Failed to create line model\n"); exit(2); } } if (log_audio) { outframes = sf_writef_short(outhandle, gen_amp, samples); if (outframes != samples) { fprintf(stderr, " Error writing audio file\n"); exit(2); } } one_way_line_model(line_model, amp, gen_amp, samples); } #if defined(ENABLE_GUI) if (use_gui && !decode_test_file) line_model_monitor_line_spectrum_update(amp, samples); #endif v17_rx(rx, amp, samples); } if (!decode_test_file) { bert_result(&bert, &bert_results); fprintf(stderr, "At completion:\n"); fprintf(stderr, "Final result %ddBm0/%ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, noise_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs); fprintf(stderr, "Last report %ddBm0/%ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, noise_level, latest_results.total_bits, latest_results.bad_bits, latest_results.resyncs); one_way_line_model_release(line_model); if (signal_level > -43) { printf("Tests failed.\n"); exit(2); } printf("Tests passed.\n"); } #if defined(ENABLE_GUI) if (use_gui) qam_wait_to_end(qam_monitor); #endif if (decode_test_file) { if (sf_close_telephony(inhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", decode_test_file); exit(2); } } if (log_audio) { if (sf_close_telephony(outhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME); exit(2); } } return 0; }
static void exchange(faxtester_state_t *s) { int16_t amp[SAMPLES_PER_CHUNK]; int16_t out_amp[2*SAMPLES_PER_CHUNK]; int len; int i; int total_audio_time; logging_state_t *logging; output_tiff_file_name = OUTPUT_TIFF_FILE_NAME; if (log_audio) { if ((out_handle = sf_open_telephony_write(OUTPUT_WAVE_FILE_NAME, 2)) == NULL) { fprintf(stderr, " Cannot create audio file '%s'\n", OUTPUT_WAVE_FILE_NAME); printf("Test failed\n"); exit(2); } /*endif*/ } /*endif*/ total_audio_time = 0; faxtester_set_transmit_on_idle(s, true); s->far_fax = fax_init(NULL, false); s->far_t30 = fax_get_t30_state(s->far_fax); s->far_tag = 'A'; if (s->far_fax) logging = fax_get_logging_state(s->far_fax); else logging = t38_terminal_get_logging_state(s->far_t38); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW); span_log_set_tag(logging, "A"); logging = t30_get_logging_state(s->far_t30); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW); span_log_set_tag(logging, "A"); #if 0 span_log_set_level(&fax.modems.v27ter_rx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW); span_log_set_tag(&fax.modems.v27ter_rx.logging, "A"); span_log_set_level(&fax.modems.v29_rx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW); span_log_set_tag(&fax.modems.v29_rx.logging, "A"); span_log_set_level(&fax.modems.v17_rx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW); span_log_set_tag(&fax.modems.v17_rx.logging, "A"); #endif while (faxtester_next_step(s) == 0) ; /*endwhile*/ for (;;) { len = fax_tx(s->far_fax, amp, SAMPLES_PER_CHUNK); faxtester_rx(s, amp, len); if (log_audio) { for (i = 0; i < len; i++) out_amp[2*i + 0] = amp[i]; /*endfor*/ } /*endif*/ total_audio_time += SAMPLES_PER_CHUNK; logging = t30_get_logging_state(s->far_t30); span_log_bump_samples(logging, len); #if 0 span_log_bump_samples(&fax.modems.v27ter_rx.logging, len); span_log_bump_samples(&fax.modems.v29_rx.logging, len); span_log_bump_samples(&fax.modems.v17_rx.logging, len); #endif logging = fax_get_logging_state(s->far_fax); span_log_bump_samples(logging, len); logging = faxtester_get_logging_state(s); span_log_bump_samples(logging, len); len = faxtester_tx(s, amp, SAMPLES_PER_CHUNK); if (fax_rx(s->far_fax, amp, len)) break; /*endif*/ if (log_audio) { for (i = 0; i < len; i++) out_amp[2*i + 1] = amp[i]; /*endfor*/ if (sf_writef_short(out_handle, out_amp, SAMPLES_PER_CHUNK) != SAMPLES_PER_CHUNK) break; /*endif*/ } /*endif*/ if (s->test_for_call_clear && !s->far_end_cleared_call) { s->call_clear_timer += len; if (!t30_call_active(s->far_t30)) { span_log(faxtester_get_logging_state(s), SPAN_LOG_FLOW, "Far end cleared after %dms (limits %dms to %dms)\n", s->call_clear_timer/8, s->timein_x, s->timeout); if (s->call_clear_timer/8 < s->timein_x || s->call_clear_timer/8 > s->timeout_x) { printf("Test failed\n"); exit(2); } span_log(faxtester_get_logging_state(s), SPAN_LOG_FLOW, "Clear time OK\n"); s->far_end_cleared_call = true; s->test_for_call_clear = false; while (faxtester_next_step(s) == 0) ; /*endwhile*/ } /*endif*/ } /*endif*/ } /*endfor*/ if (log_audio) { if (sf_close_telephony(out_handle)) { fprintf(stderr, " Cannot close audio file '%s'\n", OUTPUT_WAVE_FILE_NAME); printf("Test failed\n"); exit(2); } /*endif*/ } /*endif*/ }
/********************************************************************************************** * make your sequence here * * make a sequence based on the motifs in *ps (in their order of entry) where the probability * * of mot-i at sequence position i is ps[i].hit, and the prob of another * * moitf is (1-ps[i].hit)/(nmots-1) * ***********************************************************************************************/ int make_entseq(Seqstim *ps, int stimclass, char **tmpseq, char *outsfname) { SNDFILE *sfin=NULL, *sfout=NULL; SF_INFO sfin_info, sfout_info; int mot[18], i, newi, j, motnum=0, samp=0, seqlen=0, cindx=0, offset=0; float dohit=0.0; char sfname[1024], seq[1024];//*seq; short *inbuff[MAXMOTS], *obuff; sf_count_t incount[MAXMOTS], outframes=0, fcheck=0; *tmpseq = NULL; cindx = stimclass-1; seqlen = ps[cindx].count; seqlen = 16; srand((unsigned int)time((time_t *)NULL)); do_rand_start = 0; /*choose an offset to reorder the output array */ if(do_rand_start){ offset = (int)((seqlen+0.0)*rand()/(RAND_MAX+1.0)); for (i=0;i<seqlen;i++){ newi=i+offset; if(newi>seqlen-1) /*wrap to start seq when offset runs past the end*/ newi-=seqlen; dohit = (1+(int)(1000.0*rand()/(RAND_MAX+1.0)))/10.0; if(DEBUG==2){fprintf(stderr,"dohit:%g, hit:%g\n", dohit, ps[cindx].hit[newi]);} if(dohit<=ps[cindx].hit[newi]) mot[i] = newi; else{ /* choose any motif other than ps[i]*/ mot[i] = (int) ((seqlen+0.0)*rand()/(RAND_MAX+1.0) ); while(mot[i] == newi){ /*choose antoher that does not match the primarly motif*/ if(DEBUG==2){fprintf(stderr, "found bad motif match mot[%d] can't be %d\n",i, newi);} mot[i] = (int) ((seqlen+0.0)*rand()/(RAND_MAX+1.0)); } } if(i==0) sprintf(seq,"%i", mot[i]); else sprintf(seq,"%s-%i", seq, mot[i]); if(DEBUG==2){fprintf(stderr, "mot %d is set to %d\n",i, mot[i]);} } } else{ highFrequencyMotif = (int) ((seqlen+0.0)*rand()/(RAND_MAX+1.0) ); for (i=0;i<seqlen;i++){ if(DEBUG==12){fprintf(stderr, "HIGH FREQ NUMBER IS %d\n", highFrequencyMotif);} dohit = (int) ((10000+0.0)*rand()/(RAND_MAX+1.0) ); if(dohit<=9000){ mot[i] = highFrequencyMotif;} else{ /* choose any motif other than ps[i]*/ mot[i] = (int) ((seqlen+0.0)*rand()/(RAND_MAX+1.0) ); while(mot[i] == highFrequencyMotif){ /*choose another that does not match the primarly motif*/ if(DEBUG==12){fprintf(stderr, "found motif match mot[%d]=%d\n",i, mot[i]);} mot[i] = (int) ((seqlen+0.0)*rand()/(RAND_MAX+1.0)); } sprintf(seq,"%d", mot[i]); if(DEBUG==12){fprintf(stderr, "SEQSEQSEQSEQ: %s\n", seq);} } if(DEBUG==12){fprintf(stderr, "Here is COUNTER=%d\n",i);} } mot[16] = 16; if(DEBUG==12){fprintf(stderr, "found MOTIF NUMBER[%d]=%d\n",i, mot[i]);} sprintf(seq,"%d", mot[16]); if(stimclass == 1) { mot[17] = highFrequencyMotif; } else { mot[17] = (int) ((seqlen+0.0)*rand()/(RAND_MAX+1.0) ); while(mot[17] == highFrequencyMotif){ /*choose antoher that does not match the primarly motif*/ if(DEBUG==12){fprintf(stderr, "found motif match mot[%d]=%d\n",i, mot[17]);} mot[17] = (int) ((seqlen+0.0)*rand()/(RAND_MAX+1.0)); } sprintf(seq,"%d", mot[17]); if(i==0){ sprintf(seq,"%d", mot[i]); } else{ sprintf(seq,"%s-%i", seq, mot[i]); } if(DEBUG==12){fprintf(stderr, "mot %d is set to %d\n",i, mot[i]);} } } *tmpseq = seq; if(DEBUG==12){fprintf(stderr, "STIMULUS SEQUENCE IS %s\n",*tmpseq);} if(DEBUG==12){ for (i=0; i<18; i++){ motnum = mot[i]; sprintf(sfname,"%s%s", STIMPATH, ps[cindx].exemplar[motnum]); fprintf(stderr, "motif %d is '%s'\n", i, sfname); } } if(do_rand_seq_len) /*choose a number between 2 and seqlen*/ seqlen = 2+(int)((seqlen-2.0)*rand()/(RAND_MAX+1.0)); if(DEBUG==2){fprintf(stderr,"seqlen:%g\n", seqlen);} /*open each motif*/ for (i=0; i<18; i++){ motnum=mot[i]; sprintf(sfname,"%s%s", STIMPATH, ps[cindx].exemplar[motnum]); sfin_info.format=0; if(!(sfin = sf_open(sfname,SFM_READ,&sfin_info))){ fprintf(stderr,"error opening input file %s\n",sfname); return -1; } /*read in the file */ inbuff[i] = (short *) malloc(sizeof(int) * sfin_info.frames); incount[i] = sf_readf_short(sfin, inbuff[i], sfin_info.frames); sf_close(sfin); if(DEBUG==2){fprintf(stderr, "samples in: %lu \n", (long unsigned int)incount[i]);} outframes += incount[i]; if(DEBUG==2){fprintf(stderr, "outframes is: %lu\n", (long unsigned int)outframes);} } obuff = (short *) malloc(sizeof(int)*outframes); for (i=0; i<18; i++){ for (j=0;j<incount[i];j++){ obuff[samp++] = inbuff[i][j]; } free(inbuff[i]); /*free the inbuffs*/ } sfout_info.frames = outframes; /*this works as long as the files have been verified*/ sfout_info.channels = sfin_info.channels; sfout_info.samplerate = sfin_info.samplerate; sfout_info.format = sfin_info.format; if(DEBUG==2){fprintf(stderr,"output file format:%x \tchannels: %d \tsamplerate: %d\n",sfout_info.format, sfout_info.channels, sfout_info.samplerate);} /*write the ouput file*/ sprintf(outsfname,"%smotseq_tmp_box%d.wav", STIMPATH, box_id); if(!(sfout = sf_open(outsfname,SFM_WRITE,&sfout_info))){ fprintf(stderr,"error opening output file '%s'\n",outsfname); return -1; } fcheck=sf_writef_short(sfout, obuff, outframes); if(fcheck!=outframes){ fprintf(stderr,"UH OH!:I could only write %lu out of %lu frames!\n", (long unsigned int)fcheck, (long unsigned int)outframes); return -1; } else if(DEBUG==2){fprintf(stderr,"outframes: %lu \tfcheck: %lu \tduration: %g secs\n", (long unsigned int)outframes,(long unsigned int)fcheck,(double)outframes/sfout_info.samplerate);} sf_close(sfout); free(obuff); return 1; }
static int t30_tests(int log_audio, int test_sending) { int k; int outframes; fax_state_t *fax_state; int16_t t30_amp[SAMPLES_PER_CHUNK]; int16_t t31_amp[SAMPLES_PER_CHUNK]; int16_t silence[SAMPLES_PER_CHUNK]; int16_t out_amp[2*SAMPLES_PER_CHUNK]; int t30_len; int t31_len; SNDFILE *wave_handle; SNDFILE *in_handle; int fast_send; int fast_send_tcf; int fast_blocks; uint8_t fast_buf[1000]; t30_state_t *t30; logging_state_t *logging; int i; wave_handle = NULL; if (log_audio) { if ((wave_handle = sf_open_telephony_write(OUTPUT_WAVE_FILE_NAME, 2)) == NULL) { fprintf(stderr, " Cannot create audio file '%s'\n", OUTPUT_WAVE_FILE_NAME); exit(2); } } memset(silence, 0, sizeof(silence)); in_handle = NULL; if (decode_test_file) { if ((in_handle = sf_open_telephony_read(decode_test_file, 1)) == NULL) { fprintf(stderr, " Cannot create audio file '%s'\n", decode_test_file); exit(2); } } if ((t31_state = t31_init(NULL, at_tx_handler, NULL, modem_call_control, NULL, NULL, NULL)) == NULL) { fprintf(stderr, " Cannot start the T.31 FAX modem\n"); exit(2); } logging = t31_get_logging_state(t31_state); span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME); span_log_set_tag(logging, "T.31"); if (test_sending) { fax_state = fax_init(NULL, FALSE); t30 = fax_get_t30_state(fax_state); t30_set_rx_file(t30, OUTPUT_FILE_NAME, -1); fax_test_seq = fax_send_test_seq; countdown = 0; } else { fax_state = fax_init(NULL, TRUE); t30 = fax_get_t30_state(fax_state); t30_set_tx_file(t30, INPUT_FILE_NAME, -1, -1); fax_test_seq = fax_receive_test_seq; countdown = 250; } t30_set_tx_ident(t30, "11111111"); t30_set_supported_modems(t30, T30_SUPPORT_V27TER | T30_SUPPORT_V29 | T30_SUPPORT_V17); t30_set_phase_b_handler(t30, phase_b_handler, (void *) 'A'); t30_set_phase_d_handler(t30, phase_d_handler, (void *) 'A'); t30_set_phase_e_handler(t30, phase_e_handler, (void *) 'A'); memset(t30_amp, 0, sizeof(t30_amp)); logging = t30_get_logging_state(t30); span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME); span_log_set_tag(logging, "FAX"); logging = fax_get_logging_state(fax_state); span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME); span_log_set_tag(logging, "FAX"); fast_send = FALSE; fast_send_tcf = TRUE; fast_blocks = 0; kick = TRUE; while (!done) { if (kick) { kick = FALSE; if (fax_test_seq[test_seq_ptr].command > (const char *) 2) { if (fax_test_seq[test_seq_ptr].command[0]) { printf("%s\n", fax_test_seq[test_seq_ptr].command); t31_at_rx(t31_state, fax_test_seq[test_seq_ptr].command, fax_test_seq[test_seq_ptr].len_command); } } else { if (fax_test_seq[test_seq_ptr].command == (const char *) 2) { printf("Fast send TCF\n"); fast_send = TRUE; fast_send_tcf = TRUE; fast_blocks = 100; } else { printf("Fast send image\n"); fast_send = TRUE; fast_send_tcf = FALSE; fast_blocks = 100; } } } if (fast_send) { /* Send fast modem data */ if (fast_send_tcf) { memset(fast_buf, 0, 36); } else { if (fast_blocks == 1) { /* Create the end of page condition */ for (i = 0; i < 36; i += 2) { fast_buf[i] = 0x00; fast_buf[i + 1] = 0x80; } } else { /* Create a chunk of white page */ for (i = 0; i < 36; i += 4) { fast_buf[i] = 0x00; fast_buf[i + 1] = 0x80; fast_buf[i + 2] = 0xB2; fast_buf[i + 3] = 0x01; } } } if (fast_blocks == 1) { /* Insert EOLs */ fast_buf[35] = ETX; fast_buf[34] = DLE; fast_buf[31] = fast_buf[28] = fast_buf[25] = fast_buf[22] = fast_buf[19] = fast_buf[16] = 1; } t31_at_rx(t31_state, (char *) fast_buf, 36); if (--fast_blocks == 0) fast_send = FALSE; } t30_len = fax_tx(fax_state, t30_amp, SAMPLES_PER_CHUNK); /* The receive side always expects a full block of samples, but the transmit side may not be sending any when it doesn't need to. We may need to pad with some silence. */ if (t30_len < SAMPLES_PER_CHUNK) { memset(t30_amp + t30_len, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t30_len)); t30_len = SAMPLES_PER_CHUNK; } if (log_audio) { for (k = 0; k < t30_len; k++) out_amp[2*k] = t30_amp[k]; } if (t31_rx(t31_state, t30_amp, t30_len)) break; if (countdown) { if (answered) { countdown = 0; t31_call_event(t31_state, AT_CALL_EVENT_ANSWERED); } else if (--countdown == 0) { t31_call_event(t31_state, AT_CALL_EVENT_ALERTING); countdown = 250; } } t31_len = t31_tx(t31_state, t31_amp, SAMPLES_PER_CHUNK); if (t31_len < SAMPLES_PER_CHUNK) { memset(t31_amp + t31_len, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t31_len)); t31_len = SAMPLES_PER_CHUNK; } if (log_audio) { for (k = 0; k < t31_len; k++) out_amp[2*k + 1] = t31_amp[k]; } if (fax_rx(fax_state, t31_amp, SAMPLES_PER_CHUNK)) break; logging = fax_get_logging_state(fax_state); span_log_bump_samples(logging, SAMPLES_PER_CHUNK); logging = t30_get_logging_state(t30); span_log_bump_samples(logging, SAMPLES_PER_CHUNK); logging = t31_get_logging_state(t31_state); span_log_bump_samples(logging, SAMPLES_PER_CHUNK); if (log_audio) { outframes = sf_writef_short(wave_handle, out_amp, SAMPLES_PER_CHUNK); if (outframes != SAMPLES_PER_CHUNK) break; } } if (decode_test_file) { if (sf_close_telephony(in_handle)) { fprintf(stderr, " Cannot close audio file '%s'\n", decode_test_file); exit(2); } } if (log_audio) { if (sf_close_telephony(wave_handle)) { fprintf(stderr, " Cannot close audio file '%s'\n", OUTPUT_WAVE_FILE_NAME); exit(2); } } return 0; }
int main(int argc, char *argv[]) { SNDFILE *inhandle; SNDFILE *outhandle; int frames; int bytes; int16_t pre_amp[HIST_LEN]; int16_t post_amp[HIST_LEN]; uint8_t gsm0610_data[HIST_LEN]; gsm0610_state_t *gsm0610_enc_state; gsm0610_state_t *gsm0610_dec_state; int opt; int etsitests; int packing; etsitests = TRUE; packing = GSM0610_PACKING_NONE; while ((opt = getopt(argc, argv, "lp:")) != -1) { switch (opt) { case 'l': etsitests = FALSE; break; case 'p': packing = atoi(optarg); break; default: //usage(); exit(2); } } if (etsitests) { etsi_compliance_tests(); } else { if ((inhandle = sf_open_telephony_read(IN_FILE_NAME, 1)) == NULL) { fprintf(stderr, " Cannot open audio file '%s'\n", IN_FILE_NAME); exit(2); } if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL) { fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME); exit(2); } if ((gsm0610_enc_state = gsm0610_init(NULL, packing)) == NULL) { fprintf(stderr, " Cannot create encoder\n"); exit(2); } if ((gsm0610_dec_state = gsm0610_init(NULL, packing)) == NULL) { fprintf(stderr, " Cannot create decoder\n"); exit(2); } while ((frames = sf_readf_short(inhandle, pre_amp, 2*BLOCK_LEN))) { bytes = gsm0610_encode(gsm0610_enc_state, gsm0610_data, pre_amp, frames); gsm0610_decode(gsm0610_dec_state, post_amp, gsm0610_data, bytes); sf_writef_short(outhandle, post_amp, frames); } if (sf_close_telephony(inhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", IN_FILE_NAME); exit(2); } if (sf_close_telephony(outhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME); exit(2); } gsm0610_release(gsm0610_enc_state); gsm0610_release(gsm0610_dec_state); } return 0; }
static void test_both_ways_model(int line_model_no, int speech_test) { both_ways_line_model_state_t *model; int16_t input1[BLOCK_LEN]; int16_t input2[BLOCK_LEN]; int16_t output1[BLOCK_LEN]; int16_t output2[BLOCK_LEN]; int16_t amp[2*BLOCK_LEN]; SNDFILE *inhandle1; SNDFILE *inhandle2; SNDFILE *outhandle; int outframes; int samples; int i; int j; awgn_state_t noise1; awgn_state_t noise2; if ((model = both_ways_line_model_init(line_model_no, -50, -15.0f, -15.0f, line_model_no + 1, -35, -15.0f, -15.0f, channel_codec, rbs_pattern)) == NULL) { fprintf(stderr, " Failed to create line model\n"); exit(2); } awgn_init_dbm0(&noise1, 1234567, -10.0f); awgn_init_dbm0(&noise2, 1234567, -10.0f); if (speech_test) { if ((inhandle1 = sf_open_telephony_read(IN_FILE_NAME1, 1)) == NULL) { fprintf(stderr, " Cannot open audio file '%s'\n", IN_FILE_NAME1); exit(2); } if ((inhandle2 = sf_open_telephony_read(IN_FILE_NAME2, 1)) == NULL) { fprintf(stderr, " Cannot open audio file '%s'\n", IN_FILE_NAME2); exit(2); } } else { inhandle1 = inhandle2 = NULL; } if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME2, 2)) == NULL) { fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME2); exit(2); } for (i = 0; i < 10000; i++) { if (speech_test) { samples = sf_readf_short(inhandle1, input1, BLOCK_LEN); if (samples == 0) break; samples = sf_readf_short(inhandle2, input2, samples); if (samples == 0) break; } else { for (j = 0; j < BLOCK_LEN; j++) { input1[j] = awgn(&noise1); input2[j] = awgn(&noise2); } samples = BLOCK_LEN; } for (j = 0; j < samples; j++) { both_ways_line_model(model, &output1[j], &input1[j], &output2[j], &input2[j], 1); amp[2*j] = output1[j]; amp[2*j + 1] = output2[j]; } outframes = sf_writef_short(outhandle, amp, samples); if (outframes != samples) { fprintf(stderr, " Error writing audio file\n"); exit(2); } } if (speech_test) { if (sf_close_telephony(inhandle1)) { fprintf(stderr, " Cannot close audio file '%s'\n", IN_FILE_NAME1); exit(2); } if (sf_close_telephony(inhandle2)) { fprintf(stderr, " Cannot close audio file '%s'\n", IN_FILE_NAME2); exit(2); } } if (sf_close_telephony(outhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME2); exit(2); } both_ways_line_model_free(model); }