<commit_before>//
//  Sampler.cpp
//  AudioKit Core
//
//  Created by <NAME> on 2018-02-21.
//  Copyright © 2018 AudioKit. All rights reserved.
//

#include "Sampler.hpp"
#include <fcntl.h>
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#ifndef WIN32
#include "wavpack_public.h"     // Temporary, will go away once I fix Wavpack itself
#endif

namespace AudioKitCore {
    
    Sampler::Sampler()
    : sampleRateHz(44100.0f)    // sensible guess
    , filterEnable(false)
    , masterVolume(1.0f)
    , pitchOffset(0.0f)
    , vibratoDepth(0.0f)
    , cutoffMultiple(30.0f)
    , resonanceDb(0.0f)
    {
        for (int i=0; i < MAX_POLYPHONY; i++)
        {
            voice[i].ampEG.pParams = &ampEGParams;
            voice[i].filterEG.pParams = &filterEGParams;
        }
    }
    
    Sampler::~Sampler()
    {
    }
    
    int Sampler::init(double sampleRate)
    {
        sampleRateHz = (float)sampleRate;
        ampEGParams.updateSampleRate((float)(sampleRate/CHUNKSIZE));
        filterEGParams.updateSampleRate((float)(sampleRate/CHUNKSIZE));
        vibratoLFO.waveTable.sinusoid();
        vibratoLFO.init(sampleRate/CHUNKSIZE, 5.0f);
        
        return 0;   // no error
    }
    
    void Sampler::deinit()
    {
        for (KeyMappedSampleBuffer* pBuf : sampleBufferList) delete pBuf;
        sampleBufferList.clear();
        for (int i=0; i < MIDI_NOTENUMBERS; i++) keyMap[i].clear();
    }
    
    void Sampler::loadSampleData(AKSampleDataDescriptor& sdd)
    {
        KeyMappedSampleBuffer* pBuf = new KeyMappedSampleBuffer();
        pBuf->min_note = sdd.sd.min_note;
        pBuf->max_note = sdd.sd.max_note;
        pBuf->min_vel = sdd.sd.min_vel;
        pBuf->max_vel = sdd.sd.max_vel;
        sampleBufferList.push_back(pBuf);
        
        pBuf->init(sdd.sampleRateHz, sdd.nChannels, sdd.nSamples);
        float* pData = sdd.pData;
        if (sdd.bInterleaved) for (int i=0; i < sdd.nSamples; i++)
        {
            pBuf->setData(i, *pData++);
            if (sdd.nChannels > 1) pBuf->setData(sdd.nSamples + i, *pData++);
        }
        else for (int i=0; i < sdd.nChannels * sdd.nSamples; i++)
        {
            pBuf->setData(i, *pData++);
        }
        pBuf->noteNumber = sdd.sd.noteNumber;
        pBuf->noteHz = sdd.sd.noteHz;
        
        if (sdd.sd.fStart > 0.0f) pBuf->fStart = sdd.sd.fStart;
        if (sdd.sd.fEnd > 0.0f)   pBuf->fEnd = sdd.sd.fEnd;
        
        pBuf->bLoop = sdd.sd.bLoop;
        if (pBuf->bLoop)
        {
            // fLoopStart, fLoopEnd are usually sample indices, but values 0.0-1.0
            // are interpreted as fractions of the total sample length.
            if (sdd.sd.fLoopStart > 1.0f) pBuf->fLoopStart = sdd.sd.fLoopStart;
            else pBuf->fLoopStart = pBuf->fEnd * sdd.sd.fLoopStart;
            if (sdd.sd.fLoopEnd > 1.0f) pBuf->fLoopEnd = sdd.sd.fLoopEnd;
            else pBuf->fLoopEnd = pBuf->fEnd * sdd.sd.fLoopEnd;
        }
    }
    
#ifndef WIN32   // temporary
    void Sampler::loadCompressedSampleFile(AKSampleFileDescriptor& sfd)
    {
        //printf("loadCompressedSampleFile: %d %.1f Hz %s\n", sfd.sd.noteNumber, sfd.sd.noteHz, sfd.path);
        
        int ifd = _open(sfd.path, O_RDONLY);
        if (ifd < 0)
        {
            printf("Error %d opening %s\n", errno, sfd.path);
            return;
        }
        
        AKSampleDataDescriptor sdd;
        sdd.sd = sfd.sd;
        
        int check = getWvData(ifd, &sdd.sampleRateHz, &sdd.nChannels, &sdd.nSamples);
        _close(ifd);
        if (check != 0)
        {
            printf("getWvData returns %d for %s\n", check, sfd.path);
            return;
        }
        sdd.bInterleaved = (sdd.nChannels > 1);
        
        ifd = _open(sfd.path, O_RDONLY);
        sdd.pData = new float[sdd.nChannels * sdd.nSamples];
        check = getWvSamples(ifd, sdd.pData);
        _close(ifd);
        
        loadSampleData(sdd);
        
        delete[] sdd.pData;
    }
#endif
    
    KeyMappedSampleBuffer* Sampler::lookupSample(unsigned noteNumber, unsigned velocity)
    {
        // common case: only one sample mapped to this note - return it immediately
        if (keyMap[noteNumber].size() == 1) return keyMap[noteNumber].front();
        <commit_msg>AKSampler: reverted temporary function renaming<commit_after>//
//  Sampler.cpp
//  AudioKit Core
//
//  Created by <NAME> on 2018-02-21.
//  Copyright © 2018 AudioKit. All rights reserved.
//

#include "Sampler.hpp"
#include <fcntl.h>
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#ifndef WIN32
#include "wavpack_public.h"     // Temporary, will go away once I fix Wavpack itself
#endif

namespace AudioKitCore {
    
    Sampler::Sampler()
    : sampleRateHz(44100.0f)    // sensible guess
    , filterEnable(false)
    , masterVolume(1.0f)
    , pitchOffset(0.0f)
    , vibratoDepth(0.0f)
    , cutoffMultiple(30.0f)
    , resonanceDb(0.0f)
    {
        for (int i=0; i < MAX_POLYPHONY; i++)
        {
            voice[i].ampEG.pParams = &ampEGParams;
            voice[i].filterEG.pParams = &filterEGParams;
        }
    }
    
    Sampler::~Sampler()
    {
    }
    
    int Sampler::init(double sampleRate)
    {
        sampleRateHz = (float)sampleRate;
        ampEGParams.updateSampleRate((float)(sampleRate/CHUNKSIZE));
        filterEGParams.updateSampleRate((float)(sampleRate/CHUNKSIZE));
        vibratoLFO.waveTable.sinusoid();
        vibratoLFO.init(sampleRate/CHUNKSIZE, 5.0f);
        
        return 0;   // no error
    }
    
    void Sampler::deinit()
    {
        for (KeyMappedSampleBuffer* pBuf : sampleBufferList) delete pBuf;
        sampleBufferList.clear();
        for (int i=0; i < MIDI_NOTENUMBERS; i++) keyMap[i].clear();
    }
    
    void Sampler::loadSampleData(AKSampleDataDescriptor& sdd)
    {
        KeyMappedSampleBuffer* pBuf = new KeyMappedSampleBuffer();
        pBuf->min_note = sdd.sd.min_note;
        pBuf->max_note = sdd.sd.max_note;
        pBuf->min_vel = sdd.sd.min_vel;
        pBuf->max_vel = sdd.sd.max_vel;
        sampleBufferList.push_back(pBuf);
        
        pBuf->init(sdd.sampleRateHz, sdd.nChannels, sdd.nSamples);
        float* pData = sdd.pData;
        if (sdd.bInterleaved) for (int i=0; i < sdd.nSamples; i++)
        {
            pBuf->setData(i, *pData++);
            if (sdd.nChannels > 1) pBuf->setData(sdd.nSamples + i, *pData++);
        }
        else for (int i=0; i < sdd.nChannels * sdd.nSamples; i++)
        {
            pBuf->setData(i, *pData++);
        }
        pBuf->noteNumber = sdd.sd.noteNumber;
        pBuf->noteHz = sdd.sd.noteHz;
        
        if (sdd.sd.fStart > 0.0f) pBuf->fStart = sdd.sd.fStart;
        if (sdd.sd.fEnd > 0.0f)   pBuf->fEnd = sdd.sd.fEnd;
        
        pBuf->bLoop = sdd.sd.bLoop;
        if (pBuf->bLoop)
        {
            // fLoopStart, fLoopEnd are usually sample indices, but values 0.0-1.0
            // are interpreted as fractions of the total sample length.
            if (sdd.sd.fLoopStart > 1.0f) pBuf->fLoopStart = sdd.sd.fLoopStart;
            else pBuf->fLoopStart = pBuf->fEnd * sdd.sd.fLoopStart;
            if (sdd.sd.fLoopEnd > 1.0f) pBuf->fLoopEnd = sdd.sd.fLoopEnd;
            else pBuf->fLoopEnd = pBuf->fEnd * sdd.sd.fLoopEnd;
        }
    }
    
#ifndef WIN32   // temporary
    void Sampler::loadCompressedSampleFile(AKSampleFileDescriptor& sfd)
    {
        //printf("loadCompressedSampleFile: %d %.1f Hz %s\n", sfd.sd.noteNumber, sfd.sd.noteHz, sfd.path);
        
        int ifd = open(sfd.path, O_RDONLY);
        if (ifd < 0)
        {
            printf("Error %d opening %s\n", errno, sfd.path);
            return;
        }
        
        AKSampleDataDescriptor sdd;
        sdd.sd = sfd.sd;
        
        int check = getWvData(ifd, &sdd.sampleRateHz, &sdd.nChannels, &sdd.nSamples);
        close(ifd);
        if (check != 0)
        {
            printf("getWvData returns %d for %s\n", check, sfd.path);
            return;
        }
        sdd.bInterleaved = (sdd.nChannels > 1);
        
        ifd = open(sfd.path, O_RDONLY);
        sdd.pData = new float[sdd.nChannels * sdd.nSamples];
        check = getWvSamples(ifd, sdd.pData);
        close(ifd);
        
        loadSampleData(sdd);
        
        delete[] sdd.pData;
    }
#endif
    
    KeyMappedSampleBuffer* Sampler::lookupSample(unsigned noteNumber, unsigned velocity)
    {
        // common case: only one sample mapped to this note - return it immediately
        if (keyMap[noteNumber].size() == 1) return keyMap[noteNumber].front();
        
{'fraction_non_alphanumeric': 0.13278055429350297, 'fraction_numerical': 0.013630168105406633, 'mean_word_length': 2.0604796663190825, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '816432', 'n_tokens_mistral': 3252, 'n_tokens_neox': 3161, 'n_words': 598}