void SoundWrapper::setFile(char * file) { strcpy(this->file, file); get_wav_header(); readWav(); }
static int format_match(const char *filename) { char buf[40]; if (get_wav_header(filename, buf) != SR_OK) return FALSE; if (strncmp(buf, "RIFF", 4)) return FALSE; if (strncmp(buf + 8, "WAVE", 4)) return FALSE; if (strncmp(buf + 12, "fmt ", 4)) return FALSE; if (GUINT16_FROM_LE(*(uint16_t *)(buf + 20)) != 1) /* Not PCM. */ return FALSE; if (strncmp(buf + 36, "data", 4)) return FALSE; return TRUE; }
static int init(struct sr_input *in, const char *filename) { struct sr_probe *probe; struct context *ctx; char buf[40], probename[8]; int i; if (get_wav_header(filename, buf) != SR_OK) return SR_ERR; if (!(ctx = g_try_malloc0(sizeof(struct context)))) return SR_ERR_MALLOC; /* Create a virtual device. */ in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL); in->sdi->priv = ctx; ctx->samplerate = GUINT32_FROM_LE(*(uint32_t *)(buf + 24)); ctx->samplesize = GUINT16_FROM_LE(*(uint16_t *)(buf + 34)) / 8; if (ctx->samplesize != 1 && ctx->samplesize != 2 && ctx->samplesize != 4) { sr_err("only 8, 16 or 32 bits per sample supported."); return SR_ERR; } if ((ctx->num_channels = GUINT16_FROM_LE(*(uint16_t *)(buf + 22))) > 20) { sr_err("%d channels seems crazy.", ctx->num_channels); return SR_ERR; } for (i = 0; i < ctx->num_channels; i++) { snprintf(probename, 8, "CH%d", i + 1); if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, probename))) return SR_ERR; in->sdi->probes = g_slist_append(in->sdi->probes, probe); } return SR_OK; }
/* wav input */ static int wav_read_header(AVFormatContext *s, AVFormatParameters *ap) { int size; unsigned int tag; ByteIOContext *pb = s->pb; AVStream *st; WAVContext *wav = s->priv_data; /* check RIFF header */ tag = get_le32(pb); if (tag != MKTAG('R', 'I', 'F', 'F')) return -1; get_le32(pb); /* file size */ tag = get_le32(pb); if (tag != MKTAG('W', 'A', 'V', 'E')) return -1; /* parse fmt header */ size = find_tag(pb, MKTAG('f', 'm', 't', ' ')); if (size < 0) return -1; st = av_new_stream(s, 0); if (!st) return AVERROR(ENOMEM); get_wav_header(pb, st->codec, size); st->need_parsing = AVSTREAM_PARSE_FULL; av_set_pts_info(st, 64, 1, st->codec->sample_rate); size = find_tag(pb, MKTAG('d', 'a', 't', 'a')); if (size < 0) return -1; wav->data_end= url_ftell(pb) + size; return 0; }
euint8 open_wav(SDfile fileinfo) { euint8 buff[600]; euint32 buff2[512]; euint32 right[512]; euint32 left[512]; euint8 leftovers[512]; euint8 leftoverCount=0; euint32 count = 0; euint32 i,zz; euint32 tempright; euint32 temp[5]; euint32 overflow_amount = 0; euint32 LoopC = 0; euint32 stuff_to_write = 0; euint32 ints_to_send = 0; euint32 sample_rate = 0; euint32 bitdepth = 0; euint32 channels = 0; euint32 formatcode = 0; euint32 blocksize = 0; euint32 header_size = 0; euint32 next_address = 0; euint32 clust_address = 0; euint32 FAT_address = 0; euint32 filesize = 0; euint32 end_offset = 0; euint32 bytes_read = 0; euint8 end_of_file = 0; alt_up_audio_dev* audio_dev = alt_up_audio_open_dev(AUDIO_NAME); alt_up_audio_reset_audio_core(audio_dev); filesize = fileinfo.file_size; FAT_address = (euint32)fileinfo.startL | ((euint32)fileinfo.startH)<<16; clust_address = (FAT_address-2)*Part_Boot.sec_per_clust + clust_start; sd_readSector(clust_address,buff); //read in wav header info bytes_read += 512; //address = next_sect_address(address); header_size = get_wav_header(buff,&sample_rate,&bitdepth,&channels,&formatcode,&blocksize); //psuedo code for reading in wav details, returns wav info if(formatcode != 1) { UART_write("Error: incorrect wav format type, please use PCM\n\r"); return(1); } if(channels != 2) { UART_write("Error, number of channels must be 2\n\r"); return(1); } if((bitdepth != 32)&&(bitdepth != 16)&&(bitdepth != 24)&&(bitdepth != 8)) { UART_write("Error, bitdepth is incorrect\n\r"); return(1); } if(sample_rate != 8000) { UART_write("Error, sample rate is incorrect\n\r"); return(1); } switch(sample_rate) { case 8000: AUDIO_SetSampleRate(RATE_ADC8K_DAC8K_USB); break; case 32000: AUDIO_SetSampleRate(RATE_ADC32K_DAC32K_USB); break; case 44100: AUDIO_SetSampleRate(RATE_ADC44K_DAC44K_USB); break; case 48000: AUDIO_SetSampleRate(RATE_ADC48K_DAC48K_USB); break; case 96000: AUDIO_SetSampleRate(RATE_ADC96K_DAC96K_USB); break; default: printf("Non-standard sampling rate\n"); return -1; } count = count + header_size; while(end_of_file == 0) //start reading bytes from offset position, if there are bytes to be read, continue { //convert char buffer into 32 bit buffer while(LoopC < Part_Boot.sec_per_clust) { if(bitdepth == 32) { for(i=0;i<128;i++) { *((unsigned char*)&buff2[i]+0) = buff[count+0]; *((unsigned char*)&buff2[i]+1) = buff[count+1]; *((unsigned char*)&buff2[i]+2) = buff[count+2]; *((unsigned char*)&buff2[i]+3) = buff[count+3]; count += 4; if(count > 512) { ints_to_send = i+1; break; } if(i==127) ints_to_send = i+1; } count = 0; } if(bitdepth == 24) { for(i=0;i<170;i++) // PROBLEM HERE PROBS SINCE 512/3 ISNT WHOLE NUMBER { *((unsigned char*)&buff2[i]+0) = 0; *((unsigned char*)&buff2[i]+1) = buff[count+0]; *((unsigned char*)&buff2[i]+2) = buff[count+1]; *((unsigned char*)&buff2[i]+3) = buff[count+2]; count += 3; if(count >= 512) { count = (count-512)+3; ints_to_send = i+1; break; } } } if(bitdepth == 16) { for(i=0;i<256;i++) { *((unsigned char*)&buff2[i]+0) = 0; *((unsigned char*)&buff2[i]+1) = 0; *((unsigned char*)&buff2[i]+2) = buff[count+0]; *((unsigned char*)&buff2[i]+3) = buff[count+1]; count += 2; if(count > 512) { ints_to_send = i+1; break; } if(i==255) ints_to_send = i+1; } count = 0; } if(bitdepth == 8) { for(i=0;i<512;i++) { *((unsigned char*)&buff2[i]+0) = 0; *((unsigned char*)&buff2[i]+1) = 0; *((unsigned char*)&buff2[i]+2) = 0; *((unsigned char*)&buff2[i]+3) = buff[count+0]; count++; if(count > 512) { ints_to_send = i+1; break; } if(i==511) ints_to_send = i+1; } count = 0; } // pass to left and right channels for(i=0;i<256;i++) { left[i] = buff2[i*2]; right[i] = buff2[i*2+1]; } if(end_offset != 0) { stuff_to_write = end_offset/(1024/ints_to_send); end_of_file = 1; end_offset = 0; } else { stuff_to_write = ints_to_send/2; } //is there space in the fifo? while(alt_up_audio_write_fifo_space(audio_dev,0) < stuff_to_write); //send to audio channels alt_up_audio_write_fifo(audio_dev,right,stuff_to_write,ALT_UP_AUDIO_RIGHT); alt_up_audio_write_fifo(audio_dev,left,stuff_to_write,ALT_UP_AUDIO_LEFT); clust_address++; sd_readSector(clust_address,buff); bytes_read += 512; LoopC++; if(bytes_read >= filesize) { end_of_file = 1; //end_offset = 512 - (bytes_read-filesize); } } LoopC = 0; clust_address = next_sect_address(FAT_address,&next_address); FAT_address = next_address; if(bytes_read >= filesize) { end_of_file = 1; //end_offset = 512 - (bytes_read-filesize); } } return(0); }
/* All and mighty connection handler. */ static void* rsd_thread(void *thread_data) { connection_t conn; void *data = NULL; wav_header_t w; wav_header_t w_orig; int resample = 0; int rc, written; void *buffer = NULL; #ifdef HAVE_SAMPLERATE SRC_STATE *resample_state = NULL; #else resampler_t *resample_state = NULL; #endif float *resample_buffer = NULL; resample_cb_state_t cb_data; connection_t *temp_conn = thread_data; conn.socket = temp_conn->socket; conn.ctl_socket = temp_conn->ctl_socket; conn.serv_ptr = 0; conn.rate_ratio = 1.0; conn.identity[0] = '\0'; free(temp_conn); if ( debug ) log_printf("Connection accepted, awaiting WAV header data ...\n"); /* Firstly, get the wave header with stream settings. */ rc = get_wav_header(conn, &w); if ( rc == -1 ) { close(conn.socket); close(conn.ctl_socket); log_printf("Couldn't read WAV header... Disconnecting.\n"); pthread_exit(NULL); } memcpy(&w_orig, &w, sizeof(wav_header_t)); if ( resample_freq > 0 && resample_freq != (int)w.sampleRate ) { w.sampleRate = resample_freq; w.bitsPerSample = w_orig.bitsPerSample == 32 ? 32 : 16; if (w_orig.bitsPerSample == 32) w.rsd_format = (is_little_endian()) ? RSD_S32_LE : RSD_S32_BE; else w.rsd_format = (is_little_endian()) ? RSD_S16_LE : RSD_S16_BE; resample = 1; conn.rate_ratio = (float)w.sampleRate * w.bitsPerSample / ((float)w_orig.sampleRate * w_orig.bitsPerSample); } if ( debug ) { log_printf("Successfully got WAV header ...\n"); pheader(&w_orig); if ( resample ) { log_printf("Resamples to:\n"); pheader(&w); } } if ( debug ) log_printf("Initializing %s ...\n", backend->backend); /* Sets up backend */ if ( backend->init(&data) < 0 ) { log_printf("Failed to initialize %s ...\n", backend->backend); goto rsd_exit; } /* Opens device with settings. */ if ( backend->open(data, &w) < 0 ) { log_printf("Failed to open audio driver ...\n"); goto rsd_exit; } backend_info_t backend_info; memset(&backend_info, 0, sizeof(backend_info)); backend->get_backend_info(data, &backend_info); if ( backend_info.chunk_size == 0 ) { log_printf("Couldn't get backend info ...\n"); goto rsd_exit; } if ( backend_info.resample ) { resample = 1; w.sampleRate = w.sampleRate * backend_info.ratio; conn.rate_ratio = backend_info.ratio; w.bitsPerSample = w_orig.bitsPerSample == 32 ? 32 : 16; if (w_orig.bitsPerSample == 32) w.rsd_format = (is_little_endian()) ? RSD_S32_LE : RSD_S32_BE; else w.rsd_format = (is_little_endian()) ? RSD_S16_LE : RSD_S16_BE; } size_t size = backend_info.chunk_size; size_t read_size = size; size_t buffer_size = (read_size > size) ? read_size : size; buffer = malloc(buffer_size); if ( buffer == NULL ) { log_printf("Could not allocate memory for buffer."); goto rsd_exit; } if ( resample ) { resample_buffer = malloc(BYTES_TO_SAMPLES(buffer_size, w.rsd_format) * sizeof(float)); if ( resample_buffer == NULL ) { log_printf("Could not allocate memory for buffer."); goto rsd_exit; } cb_data.format = w_orig.rsd_format; cb_data.data = data; cb_data.conn = &conn; cb_data.framesize = w_orig.numChannels * rsnd_format_to_bytes(w_orig.rsd_format); #ifdef HAVE_SAMPLERATE int err; resample_state = src_callback_new(resample_callback, src_converter, w.numChannels, &err, &cb_data); #else resample_state = resampler_new(resample_callback, (float)w.sampleRate/w_orig.sampleRate, w.numChannels, &cb_data); #endif if ( resample_state == NULL ) { log_printf("Could not initialize resampler."); goto rsd_exit; } } #define MAX_TCP_BUFSIZ (1 << 14) // We only bother with setting buffer size if we're doing TCP. if ( rsd_conn_type == RSD_CONN_TCP ) { int flag = 1; int bufsiz = backend_info.chunk_size * 32; if (bufsiz > MAX_TCP_BUFSIZ) bufsiz = MAX_TCP_BUFSIZ; setsockopt(conn.socket, SOL_SOCKET, SO_RCVBUF, CONST_CAST &bufsiz, sizeof(int)); if ( conn.ctl_socket ) { setsockopt(conn.ctl_socket, SOL_SOCKET, SO_RCVBUF, CONST_CAST &bufsiz, sizeof(int)); setsockopt(conn.ctl_socket, SOL_SOCKET, SO_SNDBUF, CONST_CAST &bufsiz, sizeof(int)); setsockopt(conn.ctl_socket, IPPROTO_TCP, TCP_NODELAY, CONST_CAST &flag, sizeof(int)); } setsockopt(conn.socket, IPPROTO_TCP, TCP_NODELAY, CONST_CAST &flag, sizeof(int)); } /* Now we can send backend info to client. */ if ( send_backend_info(conn, &backend_info) < 0 ) { log_printf("Failed to send backend info ...\n"); goto rsd_exit; } if ( debug ) log_printf("Initializing of %s successful ...\n", backend->backend); if ( debug ) { if ( resample ) { log_printf("Resampling active. %d Hz --> %d Hz ", (int)w_orig.sampleRate, (int)w.sampleRate); #ifdef HAVE_SAMPLERATE log_printf("(libsamplerate)\n"); #else log_printf("(internal quadratic resampler)\n"); #endif } } /* Recieve data, write to sound card. Rinse, repeat :') */ for(;;) { if ( strlen(conn.identity) > 0 && verbose ) { log_printf(" :: %s\n", conn.identity); conn.identity[0] = '\0'; } if ( resample ) { #ifdef HAVE_SAMPLERATE rc = src_callback_read(resample_state, (double)w.sampleRate/(double)w_orig.sampleRate, BYTES_TO_SAMPLES(size, w.rsd_format)/w.numChannels, resample_buffer); if (rsnd_format_to_bytes(w.rsd_format) == 4) src_float_to_int_array(resample_buffer, buffer, BYTES_TO_SAMPLES(size, w.rsd_format)); else src_float_to_short_array(resample_buffer, buffer, BYTES_TO_SAMPLES(size, w.rsd_format)); #else rc = resampler_cb_read(resample_state, BYTES_TO_SAMPLES(size, w.rsd_format)/w.numChannels, resample_buffer); if (rsnd_format_to_bytes(w.rsd_format) == 4) resampler_float_to_s32(buffer, resample_buffer, BYTES_TO_SAMPLES(size, w.rsd_format)); else resampler_float_to_s16(buffer, resample_buffer, BYTES_TO_SAMPLES(size, w.rsd_format)); #endif } else rc = receive_data(data, &conn, buffer, read_size); if ( rc <= 0 ) { if ( debug ) log_printf("Client closed connection.\n"); goto rsd_exit; } for ( written = 0; written < (int)size; ) { rc = backend->write(data, (const char*)buffer + written, size - written); if ( rc == 0 ) goto rsd_exit; written += rc; } } /* Cleanup */ rsd_exit: if ( debug ) log_printf("Closed connection.\n\n"); #ifdef _WIN32 #undef close #endif backend->close(data); #ifdef _WIN32 #define close(x) closesocket(x) #endif free(buffer); close(conn.socket); if (conn.ctl_socket) close(conn.ctl_socket); if (resample_state) { #ifdef HAVE_SAMPLERATE src_delete(resample_state); #else resampler_free(resample_state); #endif } free(resample_buffer); pthread_exit(NULL); }