Esempio n. 1
0
    def get_more_samples(self, samples) -> int:

        if self.currentSample >= self.wantedSamples:
            return 0

        samples = []
        sample = Sample()

        # generate random tuple for image samples and shift them
        while True:
            u = get_radical_invers(self.currentSample, 3)
            v = get_radical_invers(self.currentSample, 2)
            lerp_delta = float(max(self.bucket_extend.get_width(), self.bucket_extend.get_height()))
            image_xy = (maths.tools.get_lerp(self.bucket_extend.start_x, self.bucket_extend.start_x + lerp_delta, u),
                        maths.tools.get_lerp(self.bucket_extend.start_y, self.bucket_extend.start_y + lerp_delta, v))
            self.currentSample += 1
            if image_xy[0] < self.bucket_extend.end_x and image_xy[1] < self.bucket_extend.end_y:
                sample.image_xy = image_xy
                break

        sample.lens_uv = (get_radical_invers(self.currentSample, 5), get_radical_invers(self.currentSample, 7))
        sample.time = maths.tools.get_lerp(self.shutterOpen, self.shutterClose, get_radical_invers(self.currentSample, 11))

        samples.append(sample)

        return samples
Esempio n. 2
0
    def load_session(file_path) -> Session:
        kick = Sample(name='kick',
                      path='../../audio/kick.wav',
                      spectral_position=SpectralPositions.low)
        perc = Sample(name='perc',
                      path='../../audio/perc.wav',
                      spectral_position=SpectralPositions.mid)
        hat = Sample(name='hat',
                     path='../../audio/hat.wav',
                     spectral_position=SpectralPositions.high)

        session = Session()
        session.add_sample(kick)
        session.add_sample(perc)
        session.add_sample(hat)

        time_signature = TimeSignature(4, 4, 4)
        session.change_time_signature(time_signature)
        session.change_tempo(120)

        for i in range(16):
            if i % 2 == 0:
                session.add_event(Event(sample=hat, time_stamp=i))
            if i % 8 == 4:
                session.add_event(Event(sample=perc, time_stamp=i))
            if i % 4 == 0:
                session.add_event(Event(sample=kick, time_stamp=i))

        return session
Esempio n. 3
0
    def test_sample_removing_does_remove_events(self):
        s1 = Sample(name='kick', path='kick.wav', spectral_position=SpectralPositions.low)
        s2 = Sample(name='snare', path='snare.wav', spectral_position=SpectralPositions.mid)
        self.session.add_sample(s1)
        self.session.add_sample(s2)
        self.session.add_event(Event(sample=s1, time_stamp=0))
        self.session.add_event(Event(sample=s1, time_stamp=4))
        self.session.add_event(Event(sample=s2, time_stamp=3))
        self.assertEqual(self.listener.num_events, 3)
        self.assertEqual(self.listener.num_samples, 2)

        # now when s1 gets removed, the two events that use it should also get removed...
        self.session.remove_sample(s1)
        self.assertEqual(self.listener.num_samples, 1)
        self.assertEqual(self.listener.num_events, 1)
Esempio n. 4
0
 def test_event_removing(self):
     s = Sample(name='kick', path='kick.wav', spectral_position=SpectralPositions.low)
     self.session.add_sample(s)
     e = Event(sample=s, time_stamp=0)
     self.session.add_event(e)
     self.assertEqual(self.listener.num_events, 1)
     self.session.remove_event(e)
     self.assertEqual(self.listener.num_events, 0)
Esempio n. 5
0
    def sample_from_spectrogram(spectrogram):
        """
        Restores a sample from the input spectrogram

        Args:
            spectrogram (Spectrogram): the input spectrogram

        Returns:
            Sample: the restored sample
        """

        overlap = int(spectrogram.overlap)
        wave = []
        for i, slice in enumerate(spectrogram.fft_slices):
            # Restore spectrum info in the same format as it came out of numpy
            amp_array = slice.amp_spectrum / 2
            amp_array[0] *= 2

            sample_count = 2 * len(amp_array)
            amp_array *= sample_count

            # Append 0-axis symmetrical image of amp spectrum (frequency aliases)
            amp_array = np.concatenate((amp_array, list(reversed(amp_array))))
            # Remove duplicate 0-frequency
            amp_array = amp_array[:-1]

            # Restore phase spectrum
            phase_array = slice.phase_spectrum
            # Get 0-axis symmetrical image of phase spectrum
            reversed_phase_array = list(reversed(phase_array))
            # Remove duplicate 0-phase
            reversed_phase_array = np.array(reversed_phase_array[:-1])
            # Multiply by -1 to get symmetry around 0
            reversed_phase_array = reversed_phase_array * (-1)
            # Append to phase spectrum
            phase_array = np.concatenate((phase_array, reversed_phase_array))

            complex_array = 1j * phase_array
            complex_array += amp_array
            wave_slice = ifft(complex_array)
            list_slice = deepcopy(np.abs(wave_slice).tolist())

            if not wave:
                wave = list_slice
            elif len(wave) <= overlap:
                wave.extend(list_slice)
            else:
                wave = wave[:-overlap]
                wave.extend(list_slice)

        reference_level = spectrogram.fft_slices[0].reference_level
        sample_width = ((int(reference_level).bit_length() - 1) // 8) + 1

        wave = list(map(lambda x: x - reference_level, wave))

        return Sample(wave, spectrogram.metadata['sampling_frequency'],
                      sample_width)
Esempio n. 6
0
    def normalise(sample):
        """
        Returns a normalised copy of the input sample.
        Args:
            sample (Sample): the sample to copy and normalise

        Returns:
            (Sample): a normalised copy of the input sample
        """
        wave_min = min(sample.wave)
        wave_max = max(sample.wave)
        max_value = 1 << (sample.bit_depth) - 1

        max_amp = max(abs(wave_min), abs(wave_max))
        factor = max_value / max_amp

        new_wave = list(map(lambda x: x * factor, sample.wave))
        return Sample(new_wave, sample.sample_rate, sample.sample_width)
Esempio n. 7
0
    def test_adding_event(self):
        s = Sample(name='kick', path='kick.wav', spectral_position=SpectralPositions.low)
        e = Event(sample=s, time_stamp=0)
        self.session.add_event(e)
        # event can't be added, because the sample it uses is not in the session yet...
        self.assertEqual(self.listener.num_events, 0)

        self.session.add_sample(s)
        self.session.add_event(e)
        self.assertEqual(self.listener.num_events, 1)

        # adding the same event twice, should not result in two identical events, but just a single event
        self.session.add_event(e)
        self.assertEqual(self.listener.num_events, 1)

        # adding a different event should actually add
        e2 = Event(sample=s, time_stamp=1)
        self.session.add_event(e2)
        self.assertEqual(self.listener.num_events, 2)
    def RequestSamples(self, sampler: Sampler, sample: Sample, scene: Scene):

        if self.strategy == LightStrategy.SAMPLE_ALL_UNIFORM:
            # Allocate and request samples for sampling all lights
            nLights = len(scene.lights)
            self.lightSampleOffsets = [LightSampleOffsets] * nLights
            self.bsdfSampleOffsets = [BSDFSampleOffsets] * nLights
            for i in range(nLights):
                light = scene.lights[i]
                nSamples = light.samples_count
                # if (sampler)
                #                    nSamples = sampler->RoundSize(nSamples);
                self.lightSampleOffsets[i] = LightSampleOffsets(nSamples, sample)
                self.bsdfSampleOffsets[i] = BSDFSampleOffsets(nSamples, sample)
            self.lightNumOffset = -1
        else:
            # Allocate and request samples for sampling one light
            self.lightSampleOffsets = [LightSampleOffsets] * 1
            self.lightSampleOffsets[0] = LightSampleOffsets(1, sample)
            self.lightNumOffset = sample.add_1d(1)
            self.bsdfSampleOffsets = [BSDFSampleOffsets] * 1
            self.bsdfSampleOffsets[0] = BSDFSampleOffsets(1, sample)
Esempio n. 9
0
def convert_dictionary_to_sample(dictionary: dict) -> Sample:
    return Sample(dictionary['name'], dictionary['path'],
                  dictionary['spectral_position'])
Esempio n. 10
0
 def RequestSamples(self, sampler: Sampler, sample: Sample, scene: Scene):
     for i in range(self.maxDepth):
         self.lightSampleOffsets[i] = LightSampleOffsets(1, sample)
         self.lightNumOffset[i] = sample.add_1d(1)
         self.bsdfSampleOffsets[i] = BSDFSampleOffsets(1, sample)
         self.pathSampleOffsets[i] = BSDFSampleOffsets(1, sample)
Esempio n. 11
0
 def __init__(self, count: int, sample: Sample):
     self.nSamples = count
     self.componentOffset = sample.add_1d(self.nSamples)
     self.dirOffset = sample.add_2d(self.nSamples)
Esempio n. 12
0
 def get_sample(self):
     if not hasattr(self, 'wave'):
         self.read()
     return Sample(deepcopy(self.wave), self.sample_rate, self.sample_width)
Esempio n. 13
0
 def test_sample_removing(self):
     # first test the removing of a sample on an empty session
     s = Sample(name='kick', path='kick.wav', spectral_position=SpectralPositions.low)
     self.session.add_sample(s)
     self.session.remove_sample(s)
     self.assertEqual(self.listener.num_samples, 0)
Esempio n. 14
0
 def test_double_different_sample_adding(self):
     self.session.add_sample(Sample(name='kick', path='kick.wav', spectral_position=SpectralPositions.low))
     self.session.add_sample(Sample(name='snare', path='snare.wav', spectral_position=SpectralPositions.mid))
     self.assertEqual(self.listener.num_samples, 2)