Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples for converting sample/bitrate? #1

Open
faxinadu opened this issue Jun 11, 2021 · 2 comments
Open

Examples for converting sample/bitrate? #1

faxinadu opened this issue Jun 11, 2021 · 2 comments

Comments

@faxinadu
Copy link

Hey,

I would really like to see an example of converting a file's bit depth and sample rate, I have been trying to get this to work but can not, would it be possible to see some simple Lua code to demonstrate?

@MasterVitronic
Copy link
Owner

MasterVitronic commented Jun 17, 2021

I'm not sure if this is what you want, to be more precise, how would you do it from the command line with sox, here an example to modify the samplerate:

local libsox	= require("libsox")
local sox		= libsox:new()
local effect		= nil
local sample_rate 	= 22050/2 		-- New sample rate
local sound_input	= '/tmp/coco.mp3' 	-- Input sound
local sound_output	= '/tmp/coco2.mp3' 	-- Output sound


--All libSoX applications must start by initialising the SoX library
if not sox:init() then
	error("Failed to initialize SoX")
end

--Open audiofile
local input = sox:open_read(sound_input)
if not input then
	error("Failed to open input handler")
end
local in_signal = sox:signal(input)

--Open soundcard
--Change "alsa" in this line to use an alternative audio device driver:
local output = sox:open_write('default', {
	rate=sample_rate,channels=2,precision=16
}, 'alsa')

--Write output file
--local output = sox:open_write(sound_output,{
	--rate=sample_rate,channels=2,precision=16
--}, 'mp3')

if not output then
	error("Failed to open output handler")
end
local out_signal = sox:signal(output)

--Create an effects chain; some effects need to know about the input
--or output file encoding so we provide that information here
local chain = sox:create_effects_chain(input, output)

local interm_signal = input  -- NB: deep copy

effect = sox:create_effect(sox:find_effect("input"))
	assert(sox:effect_options(effect, 1, input))
	assert(sox:add_effect(chain, effect, input, input))
effect = nil

effect = sox:create_effect(sox:find_effect("rate"))
	assert(sox:effect_options(effect, 1, sample_rate))
	assert(sox:add_effect(chain, effect, interm_signal, input))
effect = nil

effect = sox:create_effect(sox:find_effect("output"))
	assert(sox:effect_options(effect, 1, output))
	assert(sox:add_effect(chain, effect, input, input))
effect = nil

--Flow samples through the effects processing chain until EOF is reached
sox:flow_effects(chain)

--All done; tidy up:
sox:delete_effects_chain(chain)
sox:close(input)
sox:close(output)
sox:quit()

@faxinadu
Copy link
Author

faxinadu commented Jun 18, 2021

Hey, thanks for your reply.

If I follow along your example, I get incorrect results.

I have built my function like this:

function CtSox.sox_convert_samplerate(input_file,output_file,encoder,sample_rate,verbose_mode)
    if verbose_mode == nil then verbose_mode = true end
    if verbose_mode then print("----------SoX Audio Process----------") end

    if sample_rate == nil then sample_rate = 44100 end
    if encoder == nil then encoder = "wav" end
    if output_file == nil then output_file = filesystem.replaceExtension(input_file,encoder) end

    local input = sox:open_read(input_file)
    local in_signal = sox:signal(input)
    
    local output = sox:open_write(output_file,{rate=sample_rate,channels=2,precision=16},encoder)
    
    local out_signal = sox:signal(output)
    
    local chain = sox:create_effects_chain(input, output)
    local effect

    local interm_signal = input  -- NB: deep copy
    
    effect = sox:create_effect(sox:find_effect("input"))
    sox:effect_options(effect,1,input)
    sox:add_effect(chain,effect,input, input)
    
    effect = sox:create_effect(sox:find_effect("rate"))
    sox:effect_options(effect,1,sample_rate)
    sox:add_effect(chain,effect,interm_signal,input)
    
    effect = sox:create_effect(sox:find_effect("output"))
    sox:effect_options(effect,1,output)
    sox:add_effect(chain,effect,input,input)
    
    sox:flow_effects(chain)
    
    sox:delete_effects_chain(chain)
    sox:close(input)
    sox:close(output)
end

I call it like this:

local process_sample_rates = {
    "48000",
    "44100",
    "22050"
}

if run_sr_tests then
    for i = 1,#process_sample_rates, 1 do
        local output_file = directory_path .. "/" .. filesystem.stem(file) .. "_sr_test_" .. process_sample_rates[i] .. ".wav"
        ctSox.sox_convert_samplerate(file,output_file,"wav",process_sample_rates[i],verbose_mode)
    end
end

The resulting files I get are wrong, as shown in their infos:

image

Any ideas what is wrong here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants