/* * Overview: This plays the boom sound when the tanks are hit * */ void play_doh(alt_up_audio_dev *audio, unsigned int *buffer, short int handle) { int lbytes; int rbytes; int lindex=22; int rindex=22; alt_up_audio_dev* aud=audio; int i=0; while(i<DOH_LENGTH) //200 for cha ching { if (alt_up_audio_write_fifo_space(audio, ALT_UP_AUDIO_LEFT) > 22) { lbytes = alt_up_audio_write_fifo(aud, &buffer[lindex], 96, ALT_UP_AUDIO_LEFT); rbytes = alt_up_audio_write_fifo(aud, &buffer[rindex], 96, ALT_UP_AUDIO_RIGHT); lindex += lbytes; rindex += rbytes; i++; } } //printf("all good things come to an end\n"); alt_up_sd_card_fclose(handle); }
/* Checks if the write FIFO for the left channel has at least BUF_THRESHOLD space available. * If it doesn't, then just returns 0. If it does, then data from buf is written into the * FIFO, up to a maximum of len words. */ unsigned int alt_up_audio_play_l(alt_up_audio_dev *audio, unsigned int *buf, int len) { unsigned int space = alt_up_audio_write_fifo_space (audio, ALT_UP_AUDIO_LEFT); if (space <= BUF_THRESHOLD) return 0; else return (alt_up_audio_write_fifo(audio, buf, len, ALT_UP_AUDIO_LEFT)); }
/* The audio task is responsible for continuously playing music * off the SD card. */ void audio_task(void *pdata) { int err = 0; err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_ACTIVE_CTRL, 0x00); err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_LEFT_LINE_IN, 0x97); err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_RIGHT_LINE_IN, 0x97); err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_LEFT_HEADPHONE_OUT, 0x79); err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_RIGHT_HEADPHONE_OUT, 0x79); err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_ANALOG_AUDIO_PATH_CTRL, 0x12); err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_DIGITAL_AUDIO_PATH_CTRL, 0x05); err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_POWER_DOWN_CTRL, 0x07); err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_AUDIO_DIGITAL_INTERFACE, 0x42); err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_SAMPLING_CTRL, 0x22); err += alt_up_av_config_write_audio_cfg_register(av_dev, AUDIO_REG_ACTIVE_CTRL, 0x01); err += audio_set_headphone_volume(av_dev, DEFAULT_VOLUME); if(err < 0) printf("Audio Configuration Failed\n"); alt_up_audio_reset_audio_core(audio_dev); alt_up_audio_disable_read_interrupt(audio_dev); alt_up_audio_disable_write_interrupt(audio_dev); char fileName[10] = {"class.wav\0"}; if (file_fopen(&readFile, &efsl.myFs, fileName, 'r') != 0) printf("Error:\tCould not open file\n"); int readSize = 0; euint32 currentSize = 44; euint8 buf[AUDIO_BUF_SIZE]; int i; /* The task is suspended so that it can be played by another task. */ OSTaskSuspend(AUDIO_TASK_PRIORITY); while(1) { if (currentSize < readFile.FileSize) { int fifospace = alt_up_audio_write_fifo_space(audio_dev, ALT_UP_AUDIO_LEFT); if (fifospace > WORD_COUNT) { readSize = file_fread(&readFile, currentSize, AUDIO_BUF_SIZE, buf); currentSize += readSize; i = 0; while(i < AUDIO_BUF_SIZE) { IOWR_ALT_UP_AUDIO_LEFTDATA(audio_dev->base, (buf[i+1]<<8)|buf[i]); IOWR_ALT_UP_AUDIO_RIGHTDATA(audio_dev->base, (buf[i+3]<<8)|buf[i+2]); i+=4; } } } else { currentSize = 44; } } file_fclose(&readFile); }
// Requires: // Effects: Plays an audio .wav file, blocking the CPU until the song has completed. int playBlockingMusic(char* audioFile) { int i = 0; int currentSample = 0; unsigned int *sample; file_handle fileHandle = open_file(audioFile, false); if (fileHandle < 0) { printf("Reading file failed \n"); return AUDIO_ERROR; } int fileLength = findWavSize(fileHandle); // Allocate a buffer that is the same byte size as the file unsigned int *buf = (unsigned int*) malloc(fileLength * 2); int bufSize = fileLength/2; for (i = 0; i < bufSize; i++) { // Extract data and store in the buf. unsigned char firstByte = read_file(fileHandle); unsigned char secondByte = read_file(fileHandle); unsigned short val = (secondByte << 8) | firstByte; buf[i] = val; } // Close the file on the sd card. alt_up_sd_card_fclose(fileHandle); while (currentSample < bufSize) { unsigned int space = alt_up_audio_write_fifo_space(audio, ALT_UP_AUDIO_RIGHT); if (space > bufSize - currentSample) { // Don't need to fully fill the rest of the buffer. space = bufSize - currentSample; } if (space > 0) { sample = &(buf[currentSample]); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_RIGHT); currentSample += space; } } free(buf); return 0; }
void swapInSound(int* buf, int len) { if (bufSizeSwap == 0) { musicSwapBuffer = interruptMusicBuffer; bufSizeSwap = interruptBufSize; swapSample = interruptSample; swapLoop = musicLoop; swapDone = musicDone; interruptMusicBuffer = buf; interruptBufSize = len; interruptSample = 0; musicLoop = 0; musicDone = 0; unsigned int space = alt_up_audio_write_fifo_space(audio, ALT_UP_AUDIO_RIGHT); unsigned int* sample; if (space > interruptBufSize - interruptSample) { // Don't need to fully fill the rest of the buffer. space = interruptBufSize - interruptSample; } if (space > 0) { sample = &(interruptMusicBuffer[interruptSample]); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_RIGHT); interruptSample += space; } if (interruptSample >= interruptBufSize) { if (musicLoop) { interruptSample = 0; alt_up_audio_enable_write_interrupt(audio); } } else { printf("Enabling interrupt."); // Enable the write interrupt alt_up_audio_enable_write_interrupt(audio); } } }
int loadMusic(char* audioFile, unsigned short loop, float volumeFactor) { int i = 0; unsigned int *sample; alt_up_audio_disable_write_interrupt(audio); printf("Opening file\n"); file_handle fileHandle = open_file(audioFile, false); if (fileHandle < 0) { printf("Reading file failed \n"); return AUDIO_ERROR; } int fileLength = findWavSize(fileHandle); // Discard header-- we are making an assumption about // how the data is stored to make it easier to // add sound to the music. for (i = 0; i < 32; i++) read_file(fileHandle); // Allocate the main music buffer to be the size of the file. if (interruptMusicBuffer != 0) free(interruptMusicBuffer); musicLoop = loop; musicDone = 0; interruptMusicBuffer = (int*) malloc((fileLength-32) * 2); interruptBufSize = (fileLength-32)/2; interruptSample = 0; if (volumeFactor <= 0) volumeFactor = 1; for (i = 0; i < interruptBufSize; i++) { // Extract data and store in the buf. char firstByte = read_file(fileHandle); char secondByte = read_file(fileHandle); short val = ( (unsigned char) secondByte << 8) | (unsigned char) firstByte; val = val * volumeFactor; interruptMusicBuffer[i] = val; } alt_up_sd_card_fclose(fileHandle); unsigned int space = alt_up_audio_write_fifo_space(audio, ALT_UP_AUDIO_RIGHT); if (space > interruptBufSize - interruptSample) { // Don't need to fully fill the rest of the buffer. space = interruptBufSize - interruptSample; } if (space > 0) { sample = &(interruptMusicBuffer[interruptSample]); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_RIGHT); interruptSample += space; } if (interruptSample >= interruptBufSize) { if (musicLoop) { interruptSample = 0; alt_up_audio_enable_write_interrupt(audio); } } else { // Enable the write interrupt alt_up_audio_enable_write_interrupt(audio); } return 0; }
static void playMusicISR (void* context, alt_u32 id) #endif { /* Interrupt for writing*/ unsigned int space = alt_up_audio_write_fifo_space(audio, ALT_UP_AUDIO_RIGHT); unsigned int* sample; int i; if (space > interruptBufSize - interruptSample) { // Don't need to fully fill the rest of the buffer. space = interruptBufSize - interruptSample; } if (space > 0) { if (addSound) { /* Add a sound in-- must add word by word. */ for (i = 0; i < space; i++) { int currentWord = interruptMusicBuffer[interruptSample++]; if (soundBufferSample < soundBufSize) { currentWord += soundBuffer[soundBufferSample++]; } else { removeSound(); } alt_up_audio_write_fifo(audio, ¤tWord, 1, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio, ¤tWord, 1, ALT_UP_AUDIO_RIGHT); } } else { sample = &(interruptMusicBuffer[interruptSample]); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_RIGHT); interruptSample += space; } } if (interruptSample >= interruptBufSize) { if (musicLoop) { interruptSample = 0; } else { musicDone = 1; alt_up_audio_disable_write_interrupt(audio); if (bufSizeSwap != 0) { swapOutSound(); } } } }
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); }