示例#1
0
文件: audio.c 项目: RedTn/niostank
void init_wav(unsigned int *arr[])
{
	printf("Initializing wav file\n");
	int i,j,k;
	int size_of_wav = 0;
	int location = 0;
	unsigned int buffer[100] = {0};
	unsigned int * soundbuf;
	soundbuf = (unsigned int*) malloc(sizeof(char) * size_of_wav);

	audio = alt_up_audio_open_dev(AUDIO_DEV);
	alt_up_audio_reset_audio_core(audio);
	//alt_up_audio_enable_write_interrupt(audio);
	/*
		sound_file = alt_up_sd_card_fopen("WAV2.WAV", false);
		//printf("Soundfile: %d\n", sound_file);
		if (sound_file != -1) {
			printf("File opened.\n");
			while(alt_up_sd_card_read(sound_file) != -1) {
				size_of_wav++;
			}
			printf("Size of wav: %d\n", size_of_wav);

			if(alt_up_sd_card_fclose(sound_file))
				printf("File closed.\n");
			else
				printf("Error closing file.\n");
		}
	 */
	size_of_wav = 875484;
	size_of_sound = (int) size_of_wav/200;
	//printf("Size of sound: %d\n", size_of_sound);
	//printf("Size of wav: %d\n", size_of_wav);
	sound_file = alt_up_sd_card_fopen("wav2.wav", false);

	if (sound_file != -1) {
		//printf("File opened.\n");
		for(i = 0; i<size_of_wav; i++){
			soundbuf[i] = alt_up_sd_card_read(sound_file);
		}
		/*
		 * debugging WAV header
		 *
			printf("ChunkSize: 0x%02x%02x%02x%02x\n", soundbuf[7], soundbuf[6], soundbuf[5], soundbuf[4]);
			printf("SubChunkSize: 0x%02x%02x%02x%02x\n", soundbuf[19], soundbuf[18], soundbuf[17], soundbuf[16]);
			printf("Audio Format: 0x%02x%02x\n", soundbuf[21], soundbuf[20]);
			printf("Channels : 0x%02x%02x\n", soundbuf[23], soundbuf[22]);
			printf("Samplerate: 0x%02x%02x%02x%02x\n", soundbuf[27], soundbuf[26], soundbuf[25], soundbuf[24]);
			printf("Bitrate: 0x%02x%02x%02x%02x\n", soundbuf[31], soundbuf[30], soundbuf[29], soundbuf[28]);
			printf("Blockalign: 0x%02x%02x\n", soundbuf[33], soundbuf[32]);
			printf("BitsPerSample: 0x%02x%02x\n", soundbuf[35], soundbuf[34]);
			printf("Data mark: %c%c%c%c\n", soundbuf[210], soundbuf[211], soundbuf[212], soundbuf[213]);
			printf("Subchunksize2: 0x%02x%02x%02x%02x\n", soundbuf[217], soundbuf[216], soundbuf[215], soundbuf[214]);
			datasize = soundbuf[217] * pow(16,6) + soundbuf[216] * pow(16,4) + soundbuf[215] * pow(16,2) + soundbuf[214];
			printf("Datasize: %d\n", datasize);
			printf("Chunks:");
			for (i = 218; i < 318; i++) {
				printf(" %02x", soundbuf[i]);
			}
			printf("\n");
		 */
		for(i = 0; i<size_of_wav; i++){
			if (soundbuf[i] == 'd') {
				if (soundbuf[i+1] == 'a'){
					if (soundbuf[i+2] == 't'){
						if (soundbuf[i+3] == 'a') {
							location = i;
							break;
						}
					}

				}
			}
		}
		i = location;
		for (k = 0; k<size_of_sound; k++) {
			for (j = 0; j<100; j++){
				buffer[j] = convert(soundbuf[i+1], soundbuf[i]);
				i = i + 2;
			}
			arr[k] = (unsigned int*)malloc(sizeof(buffer));
			memcpy((void*)arr[k], (void*)buffer, sizeof(buffer));
		}

	}
	free(soundbuf);
}
示例#2
0
void audioInit(void)
{
   audio_dev = alt_up_audio_open_dev (AUDIO_0_NAME);
   audiocfg=alt_up_av_config_open_dev(AUDIO_AND_VIDEO_CONFIG_0_NAME);
}
示例#3
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);
}
示例#4
0
int main(void) {

	device_reference = alt_up_sd_card_open_dev("/dev/SD_Card");

	if (device_reference != NULL) {
		while(1) {
			if ((connected == 0) && (alt_up_sd_card_is_Present())) {
			printf("Card connected.\n");
				if (alt_up_sd_card_is_FAT16()) {
					printf("FAT16 file system detected.\n");

					//Print out the list in the sd card
					handler = alt_up_sd_card_find_first("", buffer_name);
					printf("%s \n", buffer_name);

					//Read a file in SD card -needs to speed up(10 sec?) - needs array of int
					sd_fileh = alt_up_sd_card_fopen("TEST.WAV", false);
					if (sd_fileh < 0)
						printf("Problem reading file. Error %i\n", sd_fileh);
					else
					{
						printf("Reading file...\n");
						audio_dev = alt_up_audio_open_dev (AUDIO_NAME);
							if ( audio_dev == NULL){
								return;
							}
							av_config = alt_up_av_config_open_dev(AUDIO_AND_VIDEO_CONFIG_NAME);
							while(!alt_up_av_config_read_ready(av_config));

							alt_up_audio_disable_read_interrupt(audio_dev);
							alt_up_audio_enable_write_interrupt(audio_dev);
						/*
						// Get information of number of channels
						//count = 0;
						char a, b;
						int d;
						short c;
						int index = 0;
						while (buffer_name[index] != "\0"){
							a = alt_up_sd_card_read(sd_fileh);
							b = alt_up_sd_card_read(sd_fileh);
							c = ((unsigned char) a <<8) | (unsigned char) b;

							d = c;
							d = alt_up_audio_write_fifo(device_reference,d,)
							index++;
						}
*/

						/*
						//					printf("NumChannels ");
						byte1 = 0;byte2 = 0;total = 0;
						byte1 = alt_up_sd_card_read(sd_fileh);
						byte2 = alt_up_sd_card_read(sd_fileh);
						total = (byte1 & 0x00ff) | (byte2 << 8);
						NumChannels = total;
						//					printf("%d\n",total);
						//					printf("SampleRate ");
						byte1 = 0;byte2 = 0;byte3 = 0;byte4 = 0;long_total = 0;
						byte1 = alt_up_sd_card_read(sd_fileh);
						byte2 = alt_up_sd_card_read(sd_fileh);
						byte3 = alt_up_sd_card_read(sd_fileh);
						byte4 = alt_up_sd_card_read(sd_fileh);
						long_total = (byte4 << 24) | (byte3 << 16) | (byte2 << 8) | ( byte1 & 0x000000ff);
						sampleRate = long_total;
						//					printf("%ld\n",long_total);
*/
/*
						count = 0;
						while (count < 16) // 44-byte header
						{
							alt_up_sd_card_read(sd_fileh);
							count++;
						}
*/
					}

					//find next file
					while ((handler = alt_up_sd_card_find_next(buffer_name)) != -1)
						printf("%s \n", buffer_name);



				}else{
					printf("Unknown file system.\n");
				}
				connected = 1;
			} else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) {
				printf("Card disconnected.\n");
				connected = 0;
			}
		}
	}
	return 0;
}
示例#5
0
/*
 * Overview: This is the function for reading the bytes of the coin sound for when the bullet is shot out of the cannon.
 *
 */
int readCoin(unsigned int *buffer, char* filename, short int handle)
{

	short int data;
	unsigned char upper;
	unsigned char lower;
	int size;

	alt_up_audio_dev *audio=alt_up_audio_open_dev("/dev/audio");

	//printf("Now loading: %s\n", filename);

	handle = alt_up_sd_card_fopen(filename, false);
	if (handle < 0)
	{
		//printf("Failed to open the file");
		return;
	}

	int i;

	//printf("first for loop\n");
	for (i = 0; i < 46; i++)
	{
		alt_up_sd_card_read(handle);
	}

	size = 0;
	while (1)
	{
		////printf("inside the while loop\n");

		data = alt_up_sd_card_read(handle);
		////printf("data is 0x%x", data);

		if (data == -1)
		{
			break;
		}
		else
		{
			lower = data;
		}

		data = alt_up_sd_card_read(handle);
		if (data == -1)
		{
			break;
		}
		else
		{
			upper = data;
		}

		buffer[size] = ((upper << 8) | lower);
		////printf("buffer is 0x%x\n", buffer[size]);
		size++;
		if (size + 1 > COIN_SIZE)
		{//7720 for cha ching sound
			break;
		}
		//play_audio(audio, buffer, handle);
	}

	/*while(1)//do this to loop
    {
        play_audio(audio, buffer, handle);
    }*/

	////printf("Song size: %d\n", size);
	alt_up_sd_card_fclose(handle);

	return size;
}