Exemplo n.º 1
0
	void SourceMusic::Idle(void) {
		if(_sample == NULL)
			return;
		
		// printf("idling\n");
		while( _isPlaying && 
					 (_read == _decoded || 
					 (_read - _decoded + _buffersize) % _buffersize >
					 _sample_buffersize ) )	{
			// if(_read == _decoded)	printf("_read == _decoded == %d\n", _read);
			// fill the buffer
			int count = Sound_Decode(_sample);
			// printf("adding %d bytes to buffer\n", count);
			if(count <= _buffersize - _decoded) {
				memcpy(_buffer + _decoded, _sample->buffer, count);
			} else {
				// wrapping around end of buffer (usually doesn't happen when 
				// _buffersize is a multiple of _sample_buffersize)
				// printf("wrapping around end of buffer\n");
				memcpy(_buffer + _decoded, _sample->buffer, _buffersize - _decoded);
				memcpy(_buffer, (Uint8*) _sample->buffer + _buffersize - _decoded,
							 count - (_buffersize - _decoded));
			}
			_decoded = (_decoded + count) % _buffersize;

			// check for end of sample, loop
			if((_sample->flags & SOUND_SAMPLEFLAG_ERROR) || 
			   (_sample->flags & SOUND_SAMPLEFLAG_EOF)) {
				// some error has occured, maybe end of sample reached
#ifndef macintosh
				SDL_SemWait(_sem);
#else
                SDL_LockAudio();
#endif
				// todo: let playback finish, because there's still data
				// in the buffer that has to be mixed
				CleanUp();
				// fprintf(stderr, "end of sample reached!\n");
				if(_loop) {
					// fprintf(stderr, "looping music\n");
					if(_loop != 255) 
						_loop--;
					CreateSample();
				} else {
					_isPlaying = 0;
					// todo: notify sound system (maybe load another song?)
				}
#ifndef macintosh
				SDL_SemPost(_sem);
#else
                SDL_UnlockAudio();
#endif
			}
		} // buffer has been filled
	}
Exemplo n.º 2
0
//+----------------------------------------------------------------------------+
//|bool Initialize(AudioEngine* engine, const rString &path, SoundType type)
//\----------------------------------------------------------------------------+
bool SoundSDL::Initialize(AudioEngine* engine, const rString &path, SoundType type)
{
	AudioEngineSDL* audioEngine = reinterpret_cast<AudioEngineSDL*>(engine);
	const SDL_AudioSpec* sysSpec = audioEngine->GetSystemSpec();
	m_AudioEngine = audioEngine;
	m_Type = type;
	m_Name = path;
	m_ChunkSize = sysSpec->size;

	m_AudioCVT = pNew(SDL_AudioCVT);
	m_BufferData = tNew(AudioBuffer);
	m_AudioFile = nullptr;

	///Check which file format the requested audiofile are
	///Only wav and ogg are supported

	///Is wave file
	if(path.find(".wav", 0) != std::string::npos)
	{ 
		m_AudioFile = pNew(WavFileReader);
	}
	///Is ogg file
	else if(path.find(".ogg", 0) != std::string::npos)
	{ 
		m_AudioFile = pNew(OggFileReader);
	}
	///Not supported format
	else
	{
		fprintf(stderr, "SoundSDL. Trying to load unsupported file format %s \n", path.c_str());
		return false;
	}

	m_AudioFile->Initialize();

	///Read as sample, read everything into buffer
	if(type == SoundType::SAMPLE)
	{
		CreateSample(path, m_BufferData, sysSpec);
	}
	///Read as stream, prepare for buffer streaming
	else if(type == SoundType::STREAM)
	{
		CreateStream(path, m_BufferData, sysSpec);
	}
	else
	{
		///This should not happend..
		fprintf(stderr, "SoundSDL. Unexpected error loading %s while setting sound type \n", path.c_str());
		return false;
	}

	return true;
}
Exemplo n.º 3
0
  void SourceMusic::Load(char *filename) {
		int n = strlen(filename);
		_filename = (char*) malloc(n + 1);
		memcpy(_filename, filename, n + 1);
    CreateSample();
  }
Exemplo n.º 4
0
static void PacketReceived (PACKET_Instance_t *inst, PACKET_Packet_t *packet, PACKET_Error_t err)
{
	if (err == PACKET_ERROR_NONE)
	{
		// we really only care about packets for us...
		if (packet->m_id == 0x01)
		{
			switch (packet->m_cmd)
			{
				case PACKET_CMD_SET_ID:
				{
					//printf ("got SET_ID command\n");
					break;
				}
				
				case PACKET_CMD_START_ASYNC:
				{
					//printf ("got START_ASYNC command\n");
					turnOnAsyncCapture();
					break;
				}
				
				case PACKET_CMD_STOP_ASYNC:
				{
					//printf ("got STOP_ASYNC command\n");
					sendingSamples = 0;
					break;
				}
				
				case PACKET_CMD_TURN_OFF_BATTERY:
				{
					//printf ("got TURN_OFF_BATTERY command\n");
					Battery_EnableLow();
					break;
				}
				
				case PACKET_CMD_TURN_ON_BATTERY:
				{
					//printf ("got TURN_ON_BATTERY command\n");
					Battery_EnableHigh();
					break;
				}
				
				case PACKET_CMD_SEND_SAMPLE:
				{
					//printf ("got PACKET_CMD_SEND_SAMPLE command\n");
					//turnOnFlag();
					currentSample = 0;
					CreateSample(SAMPLE_NORMAL);
					SendSamples(1, PACKET_CMD_SAMPLE);
					break;
				}
				
				case PACKET_CMD_SET_CALIBRATION:
				{
					//printf ("got PACKET_CMD_SET_CALIBRATION command\n");
					calibrationFloor = *(float *)&packet->m_param[0];
					calibrationScale = *(float *)&packet->m_param[4];
					eeprom_write_float((float*)(CALIBRATION_EEPROM_BASE + CALIBRATION_FLOOR_LOCATION), calibrationFloor);
					eeprom_write_float((float*)(CALIBRATION_EEPROM_BASE + CALIBRATION_SCALE_LOCATION), calibrationScale);
					eeprom_write_byte((uint8_t*)(CALIBRATION_EEPROM_BASE + CALIBRATION_SIGNATURE_LOCATION), CALIBRATION_SIGNATURE);
					//printf("Calibration parameters saved to EEPROM\n");
					//char output[16];
					//dtostrf(calibrationFloor, 7, 4, output);
					//printf("FLOOR: %s\n", output);
					//dtostrf(calibrationScale, 7, 4, output);
					//printf("SCALE: %s\n", output);
					break;
				}
				
				case PACKET_CMD_GET_CALIBRATION:
				{
					if (debugMode) {
						printf ("got PACKET_CMD_GET_CALIBRATION command\n");
					}
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
					uint8_t checksum = 0x01;
					uint8_t floatSize = sizeof(calibrationFloor);
					uint8_t packetLength = (floatSize * 2) + 2;
					RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
					checksum += packetLength;
					RingBuffer_Insert(&Send_USB_Buffer, PACKET_CMD_CALIBRATION); // command
					checksum += PACKET_CMD_CALIBRATION;
					for (int index=0; index < floatSize; index++) {
						uint8_t value = ((uint8_t *)&calibrationFloor)[index];
						RingBuffer_Insert(&Send_USB_Buffer, value);
						checksum += value;
					}
					for (int index=0; index < floatSize; index++) {
						uint8_t value = ((uint8_t *)&calibrationScale)[index];
						RingBuffer_Insert(&Send_USB_Buffer, value);
						checksum += value;
					}
					RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
					break;
				}
				
				case PACKET_CMD_GET_COMPENSATION:
				{
					if (debugMode) {
						printf ("got PACKET_CMD_GET_COMPENSATION command\n");
					}
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
					uint8_t checksum = 0x01;
					uint8_t floatSize = sizeof(calibrationFloor);
					uint8_t packetLength = 1 + floatSize + 2;
					RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
					checksum += packetLength;
					RingBuffer_Insert(&Send_USB_Buffer, PACKET_CMD_COMPENSATION); // command
					checksum += PACKET_CMD_COMPENSATION;
					RingBuffer_Insert(&Send_USB_Buffer, compensation);
					checksum += compensation;
					for (int index=0; index < floatSize; index++) {
						uint8_t value = ((uint8_t *)&baselineVoltage)[index];
						RingBuffer_Insert(&Send_USB_Buffer, value);
						checksum += value;
					}
					RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
					break;
				}
				
				case PACKET_CMD_GET_RAW_SAMPLE:
				{
					//printf ("got PACKET_CMD_GET_RAW_SAMPLE command\n");
					currentSample = 0;
					CreateSample(SAMPLE_RAW);
					SendSamples(1, PACKET_CMD_GET_RAW_SAMPLE);
					break;
				}
				
				case PACKET_CMD_GET_VERSION:
				{
					//printf ("got PACKET_CMD_GET_VERSION command\n");
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
					uint8_t checksum = 0x01;
					uint8_t packetLength = 1 + 2;
					RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
					checksum += packetLength;
					RingBuffer_Insert(&Send_USB_Buffer, PACKET_CMD_VERSION); // command
					checksum += PACKET_CMD_VERSION;
					RingBuffer_Insert(&Send_USB_Buffer, AMMETER_VERSION);
					checksum += AMMETER_VERSION;
					RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
					break;
				}
				
				case PACKET_CMD_GET_SERIAL:
				{
					if (debugMode) {
						printf ("got PACKET_CMD_GET_SERIAL command\n");
					}
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
					uint8_t checksum = 0x01;
					uint8_t packetLength = 2 + 2;
					RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
					checksum += packetLength;
					RingBuffer_Insert(&Send_USB_Buffer, PACKET_CMD_SERIAL); // command
					checksum += PACKET_CMD_SERIAL;
					RingBuffer_Insert(&Send_USB_Buffer, serialNumber & 0xFF);
					checksum += serialNumber & 0xFF;
					RingBuffer_Insert(&Send_USB_Buffer, serialNumber >> 8);
					checksum += serialNumber >> 8;
					RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
					break;
				}

				case PACKET_CMD_SET_SERIAL:
				{
					//printf ("got PACKET_CMD_SET_SERIAL command\n");
					serialNumber = *(uint16_t *)&packet->m_param[0];
					eeprom_write_word((float*)(CALIBRATION_EEPROM_BASE + SERIAL_NUMBER_LOCATION), serialNumber);
					//printf("Serial number saved to EEPROM\n");
					//printf("Serial Number: %d\n", serialNumber);
					break;
				}

				case PACKET_CMD_DUMP_DEBUG_INFO:
				{
					dumpDebugInfo();
					break;
				}

				case PACKET_CMD_TURN_ON_COMPENSATION:
				{
					float value = *(float *)&packet->m_param[0];
					if ((value >= 3.7) && (value <= 4.2)) {
						compensation = 1;
						eeprom_write_byte((float*)(CALIBRATION_EEPROM_BASE + COMPENSATION_FLAG_LOCATION), compensation);
						baselineVoltage = value;
						eeprom_write_float((float*)(CALIBRATION_EEPROM_BASE + BASELINE_VOLTAGE_LOCATION), baselineVoltage);
					}
					break;
				}
				
				case PACKET_CMD_TURN_OFF_COMPENSATION:
				{
					compensation = 0;
					eeprom_write_byte((float*)(CALIBRATION_EEPROM_BASE + COMPENSATION_FLAG_LOCATION), compensation);
					break;
				}
				
				case PACKET_CMD_START_LOGGING:
				{
					loggingFlag = 1;
					if (debugMode) {
						debugMode = 0;
					} else {
						InitUART ();
						fdevopen (UART1_PutCharStdio, UART1_GetCharStdio);
					}
					eeprom_write_byte((float*)(CALIBRATION_EEPROM_BASE + LOGGING_FLAG_LOCATION), loggingFlag);
					turnOnAsyncCapture();
					break;
				}
				
				case PACKET_CMD_STOP_LOGGING:
				{
					sendingSamples = 0;
					loggingFlag = 0;
					eeprom_write_byte((float*)(CALIBRATION_EEPROM_BASE + LOGGING_FLAG_LOCATION), loggingFlag);
					break;
				}
				
				case PACKET_CMD_CHECK_AUX_BATTERY:
				{
					loggingFlag = 0;
					if (debugMode) {
						debugMode = 0;
					} else {
						InitUART ();
						fdevopen (UART1_PutCharStdio, UART1_GetCharStdio);
					}
					printf("I2C Test\n");
                    float voltage = readAuxBatteryVoltage();
                    char output[16];
                    dtostrf(voltage, 4, 2, output);
                    printf("Battery Voltage: %s volts\n", output);
					break;
				}

                case PACKET_CMD_TURN_OFF_AUX_USB:
                {
                    //printf ("got TURN_OFF_BATTERY command\n");
                    AuxUSB_Disable();
                    break;
                }

                case PACKET_CMD_TURN_ON_AUX_USB:
                {
                    //printf ("got TURN_ON_BATTERY command\n");
                    AuxUSB_Enable();
                    break;
                }

                case PACKET_CMD_GET_TEMPERATURE:
                {
                    if (debugMode) {
                        printf ("got PACKET_CMD_GET_TEMPERATURE command\n");
                    }
                    float temperature = ReadTemperatureProbe();
                    RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
                    RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
                    RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
                    uint8_t checksum = 0x01;
                    uint8_t floatSize = sizeof(temperature);
                    uint8_t packetLength = floatSize + 2;
                    RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
                    checksum += packetLength;
                    RingBuffer_Insert(&Send_USB_Buffer, PACKET_CMD_TEMPERATURE); // command
                    checksum += PACKET_CMD_TEMPERATURE;
                    for (int index=0; index < floatSize; index++) {
                        uint8_t value = ((uint8_t *)&temperature)[index];
                        RingBuffer_Insert(&Send_USB_Buffer, value);
                        checksum += value;
                    }
                    RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
                    break;
                }
                
                case PACKET_CMD_SOFT_RESET:
                {
                    if (debugMode) {
                        printf ("got PACKET_CMD_SOFT_RESET command\n");
                    }
                    wdt_enable(WDTO_60MS);
                    while(1) {};
                    
                    break;
                }

                default:
				{
					// there are other commands that we don't care about....
					//printf ("ID:0x%02x Cmd: 0x%02x *** Unknown ***\n", packet->m_id, packet->m_cmd);
					break;
				}
			}
		}
		else {
			//printf ("Got packet for ID: %3d\n", packet->m_id);
		}
	}