<commit_before>--Chiptune Thread

local chIn = ...

require("love.audio")
require("love.sound")
require("love.timer")

local QSource = require("Peripherals.Audio.QueueableSource")

local qs = QSource:new()

local rate = 44100
local buffer_size = rate/4

local wave, freq, amp

local waves = {}

--Sin
waves[0] = function(samples)
  local r = 0
  
  return function()
    r = r + math.pi/(samples/2)
    
    return math.sin(r)*amp
  end
end

--Square
waves[1] = function(samples)
  local c = 0
  local flag = false
  
  return function()
    c = (c+1)%(samples/2)
    if c == 0 then flag = not flag end
    
    return (flag and amp or -amp)
  end
end

--Sawtooth
waves[2] = function(samples)
  local c = 0
  local inc = 2/samples
  
  return function()
    c = (c+inc)%2 -1
    return c
  end
end


local function pullParams()
  return chIn:pop()
end

while true do
  for params in pullParams do
    if type(params) == "string" and params == "stop" then
      return
    else
      local ofreq = freq
      wave, freq, amp = unpack(params)
      
      if ofreq then
        --error(ofreq.." -> "..freq)
      end
      --error(tostring(wave).."_"..tostring(freq).."_"..tostring(amp))
      
      --amp = amp or 1
      
      qs:clear()
      
      qs = QSource:new()
      
      chIn:clear()
    end
  end
  
  --Generate audio.
  if wave then
    qs:step()
    
    if qs:getFreeBufferCount() > 0 then
      local sd = love.sound.newSoundData(buffer_size, rate, 16, 1)
      local gen = waves[wave](rate/freq)
      for i=0,buffer_size-1 do
        sd:setSample(i,gen())
      end
      
      qs:queue(sd)
      qs:play()
    end
  end
end<commit_msg>Added noise waveform<commit_after>--Chiptune Thread

local chIn = ...

require("love.audio")
require("love.sound")
require("love.timer")

local QSource = require("Peripherals.Audio.QueueableSource")

local qs = QSource:new()

local rate = 44100
local buffer_size = rate/4

local wave, freq, amp

local waves = {}

--Sin
waves[0] = function(samples)
  local r = 0
  
  return function()
    r = r + math.pi/(samples/2)
    
    return math.sin(r)*amp
  end
end

--Square
waves[1] = function(samples)
  local c = 0
  local flag = false
  
  return function()
    c = (c+1)%(samples/2)
    if c == 0 then flag = not flag end
    
    return (flag and amp or -amp)
  end
end

--Sawtooth
waves[2] = function(samples)
  local c = 0
  local inc = 2/samples
  
  return function()
    c = (c+inc)%2 -1
    return c
  end
end

waves[3] = function(samples)
  local c = samples/2
  local v = math.random()
  local flag = flag
  
  return function()
    --[[c = c -1
    if c == 0 then
      c = samples/2
      flag = not flag
      v = math.random()*2
      if flag then v = -v end
    end
    
    return v*amp
    ]]
  
    return math.random()*2-1
  end
end


local function pullParams()
  return chIn:pop()
end

while true do
  for params in pullParams do
    if type(params) == "string" and params == "stop" then
      return
    else
      local ofreq = freq
      wave, freq, amp = unpack(params)
      
      if ofreq then
        --error(ofreq.." -> "..freq)
      end
      --error(tostring(wave).."_"..tostring(freq).."_"..tostring(amp))
      
      --amp = amp or 1
      
      qs:clear()
      
      qs = QSource:new()
      
      chIn:clear()
    end
  end
  
  --Generate audio.
  if wave then
    qs:step()
    
    if qs:getFreeBufferCount() > 0 then
      local sd = love.sound.newSoundData(buffer_size, rate, 16, 1)
      local gen = waves[wave](rate/freq)
      for i=0,buffer_size-1 do
        sd:setSample(i,gen())
      end
      
      qs:queue(sd)
      qs:play()
    end
  end
end
{'fraction_non_alphanumeric': 0.1279037988521454, 'fraction_numerical': 0.01639792292976223, 'mean_word_length': 2.0912162162162162, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2626145', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1308, 'n_words': 367}