Example #1
0
void flowcontrol_deliver_from_uart(border_packet_t *packet, int len)
{
    if (packet->type == BORDER_PACKET_ACK_TYPE) {
        if (in_window(packet->seq_num, slwin_stat.last_ack + 1, slwin_stat.last_frame)) {
            if (synack_seqnum == packet->seq_num) {
                synack_seqnum = -1;
                sem_post(&connection_established);
            }

            do {
                struct send_slot *slot;
                slot = &(slwin_stat.send_win[++slwin_stat.last_ack % BORDER_SWS]);
                vtimer_remove(&slot->timeout);
                memset(&slot->frame, 0, BORDER_BUFFER_SIZE);
                sem_post(&slwin_stat.send_win_not_full);
            }
            while (slwin_stat.last_ack != packet->seq_num);
        }
    }
    else {
        struct recv_slot *slot;

        slot = &(slwin_stat.recv_win[packet->seq_num % BORDER_RWS]);

        if (!in_window(packet->seq_num,
                       slwin_stat.next_exp,
                       slwin_stat.next_exp + BORDER_RWS - 1)) {
            return;
        }

        memcpy(slot->frame, (uint8_t *)packet, len);
        slot->received = 1;

        if (packet->seq_num == slwin_stat.next_exp) {
            while (slot->received) {
                demultiplex((border_packet_t *)slot->frame);
                memset(&slot->frame, 0, BORDER_BUFFER_SIZE);
                slot->received = 0;
                slot = &slwin_stat.recv_win[++(slwin_stat.next_exp) % BORDER_RWS];
            }
        }

        send_ack(slwin_stat.next_exp - 1);
    }
}
Example #2
0
/**
 * 指定したバイト数だけpcmデータを読み込みます。
 * エラーが発生するか、終端に達するまでsizeで指定したバイト数だけ読み込めます。
 */
bool PitchShiftWaveReader::read(void *dst, std::size_t size, std::size_t *actualSize)
{
	if(!isOpen()){
		return false;
	}
	
	if(size == 0){
		if(actualSize){
			*actualSize = 0;
		}
		return true;
	}

	// 出力に必要な入力サンプル数を求める。
	const std::size_t reqOutSamples = size / format_.blockAlign; //floor
	const WaveSize reqSrcSamples0 = resampler_ ? resampler_->calcRequiredInputSize(reqOutSamples) : reqOutSamples;
	if(reqSrcSamples0 > std::numeric_limits<std::size_t>::max() / format_.blockAlign){
		return false;
	}
	const std::size_t reqSrcSamples = static_cast<std::size_t>(reqSrcSamples0);
	const std::size_t reqSrcBytes = reqSrcSamples * format_.blockAlign;

	// read source bytes.
	std::size_t actualSizeSrc = 0;
	HeapArray<uint8_t> byteBuffer(reqSrcBytes);
	if(!source_->read(byteBuffer.get(), reqSrcBytes, &actualSizeSrc)){
		return false;
	}

	// pitch shift.
	const std::size_t actualSrcSamples = actualSizeSrc / format_.blockAlign; //floor

	if(shiftRatio_ != 1.0f){
		HeapArray<float> floatBuffer(actualSrcSamples);
		for(unsigned int ch = 0; ch < format_.channels; ++ch){
			demultiplex(floatBuffer.get(), byteBuffer.get(), ch, actualSrcSamples, format_.blockAlign, format_.bytesPerSample);

			shifters_[ch]->smbPitchShift(
				shiftRatio_,
				actualSrcSamples,
				fftOverlapFactor_,
				static_cast<float>(format_.samplesPerSec),
				floatBuffer.get(),
				floatBuffer.get());

			multiplex(byteBuffer.get(), floatBuffer.get(), ch, actualSrcSamples, format_.blockAlign, format_.bytesPerSample);
		}
	}

	// resample.
	if(resampler_){
		assert(actualSrcSamples == reqSrcSamples);
		std::size_t actualOutSamples = reqOutSamples;
		if(actualSrcSamples != reqSrcSamples){
			const std::size_t maxOutSamples = static_cast<std::size_t>(std::min(resampler_->calcMaxOutputSize(actualSrcSamples), static_cast<WaveSize>(reqOutSamples)));
			if(actualOutSamples > maxOutSamples){
				actualOutSamples = maxOutSamples;
			}
		}

		switch(format_.blockAlign){
		case 1:
			resampler_->resample(
				reinterpret_cast<uint8_t *>(dst),
				reinterpret_cast<uint8_t *>(dst) + actualOutSamples,
				byteBuffer.get(),
				byteBuffer.get() + actualSrcSamples);
			break;
		case 2:
			resampler_->resample(
				reinterpret_cast<uint16_t *>(dst),
				reinterpret_cast<uint16_t *>(dst) + actualOutSamples,
				reinterpret_cast<const uint16_t *>(byteBuffer.get()),
				reinterpret_cast<const uint16_t *>(byteBuffer.get()) + actualSrcSamples);
			break;
		case 4:
			resampler_->resample(
				reinterpret_cast<uint32_t *>(dst),
				reinterpret_cast<uint32_t *>(dst) + actualOutSamples,
				reinterpret_cast<const uint32_t *>(byteBuffer.get()),
				reinterpret_cast<const uint32_t *>(byteBuffer.get()) + actualSrcSamples);
			break;
		}
		if(actualSize){
			*actualSize = actualOutSamples * format_.blockAlign;
		}
		outputPos_ += actualOutSamples;
	}
	else{
		std::memcpy(dst, byteBuffer.get(), actualSrcSamples * format_.blockAlign);
		if(actualSize){
			*actualSize = actualSrcSamples * format_.blockAlign;
		}
		outputPos_ += actualSrcSamples;
	}

	return true;
}