Difference between revisions of "SoundData:setSample (日本語)"

m (関数)
m (用例: Translation updated.)
 
Line 24: Line 24:
 
=== 返値 ===
 
=== 返値 ===
 
ありません。
 
ありません。
 +
 +
== 用例: 正弦波による単音の発音 ==
 +
 +
<source lang="lua">
 +
local tau          =  math.pi * 2
 +
local samplerate  =        44100 -- 周波数
 +
local bits        =            16 -- 8 ビットは調停品質のサウンドになります。
 +
local channels    =            2 -- LÖVE はステレオまたはモノラルのみ対応です。
 +
local buffercount  =            2 -- 大容量のバッファは不要
 +
local qsource      = love.audio.newQueueableSource(samplerate, bits, channels, buffercount)
 +
local samplepoints = samplerate/10 -- 一度にバッファで保持する容量
 +
local buffer      = love.sound.newSoundData(samplepoints, samplerate, bits, channels)
 +
local phase        =          0.0
 +
local frequency    =        220.00 -- 周波数
 +
local amplitude    =          0.5 -- 50%
 +
 +
 +
local function synth()
 +
    phase = phase + (tau * frequency / samplerate)
 +
    return math.sin(phase) * amplitude
 +
end
 +
 +
function love.update(dt)
 +
    while qsource:getFreeBufferCount() > 0 do
 +
        for i = 0, samplepoints - 1 do
 +
            buffer:setSample(i, synth())
 +
        end
 +
        qsource:queue(buffer)
 +
        qsource:play()
 +
    end
 +
end
 +
 +
function love.keypressed(key)
 +
    if key == 'k' then
 +
        frequency = frequency * 2^(1/12)
 +
    elseif key == 'j' then
 +
        frequency = frequency * 2^(-1/12)
 +
    end
 +
end
 +
</source>
  
 
== 関連 ==
 
== 関連 ==

Latest revision as of 11:27, 23 July 2023

指定位置にあるサンプルポイントの値を設定します。ステレオ SoundData オブジェクトでは、左側と右側のチャンネルデータは順列形式でインタリーブされます。

関数

概要

SoundData:setSample( i, sample )

引数

number i
サンプルの位置を指定するための整数値 (0 は最初のサンプルへの位置)。
number sample
正規化されたサンプルポイント (範囲は -1.0 ~ 1.0)

返値

ありません。

関数

LÖVE 11.0 から使用可能
この異形は以前のバージョンでは非対応です。

サンプル位置指定用の引数でインタリーブをする方法ではなく、明示的なサンプルインデックスでサンプル値を設定します。

概要

SoundData:setSample( i, channel, sample )

引数

number i
サンプルの位置を指定するための整数値 (0 は最初のサンプルへの位置)。
number channel
指定サンプル内のチャンネル設定用インデックス (起点は 1)。ステレオでは、左チャンネルは 1 であり、右チャンネルは 2 です。}}
number sample
正規化されたサンプルポイント (範囲は -1.0 ~ 1.0)

返値

ありません。

用例: 正弦波による単音の発音

local tau          =   math.pi * 2
local samplerate   =         44100 -- 周波数
local bits         =            16 -- 8 ビットは調停品質のサウンドになります。
local channels     =             2 -- LÖVE はステレオまたはモノラルのみ対応です。
local buffercount  =             2 -- 大容量のバッファは不要
local qsource      = love.audio.newQueueableSource(samplerate, bits, channels, buffercount)
local samplepoints = samplerate/10 -- 一度にバッファで保持する容量
local buffer       = love.sound.newSoundData(samplepoints, samplerate, bits, channels)
local phase        =           0.0
local frequency    =        220.00 -- 周波数
local amplitude    =           0.5 -- 50%


local function synth()
    phase = phase + (tau * frequency / samplerate)
    return math.sin(phase) * amplitude
end

function love.update(dt)
    while qsource:getFreeBufferCount() > 0 do
        for i = 0, samplepoints - 1 do
            buffer:setSample(i, synth())
        end
        qsource:queue(buffer)
        qsource:play()
    end
end

function love.keypressed(key)
    if key == 'k' then
        frequency = frequency * 2^(1/12)
    elseif key == 'j' then
        frequency = frequency * 2^(-1/12)
    end
end

関連


そのほかの言語